Minimize delay between two sounds played using winsound.PlaySound - python

winsound.PlaySound('1.wav', winsound.SND_FILENAME)
time.sleep(0.15)
winsound.PlaySound('1.wav', winsound.SND_FILENAME)
1.wav is a sound file of length 01s
There is delay of more then a second between the two winsound.PlaySound calls, even if time.sleep is commented out. But if the parameter for time.sleep is increase for more then 1s then my code runs as it should.
I need to bring the delay down to a 0.15s.
Thanks in advance.

The winsound module seems to be unloved. It hasn't adapted to the Python 3 distinction between bytes and strings (http://bugs.python.org/issue11620), so it can't play a .wav file that is stored in memory.
You should probably move to a different audio module such as pyaudio https://people.csail.mit.edu/hubert/pyaudio/
Since the file is a short one. You should read the whole file into memory. If there is still a gap, you can join the file to itself with 0.15 s of silence in the middle and then play (that single file). Audio modules wave, pydub, audioop or audiolab can do the joining. How to join two wav files using python?

Related

Is there a way to change/add audio files while playing in pydub?

I am currently using the PyDub module in python to create a music/data streaming system.
For this to work I have to append "chunks" of songs onto a queue of "chunks" that are being played by the user
Is there a way to manipulate an audio file while it is being played? The PyDub .play() function does not move to the next line until playing the audio file has finished.
I have tried playing 1 chunk, then playing the next, each individually with a new .play() function, however this becomes quite choppy as it takes time to run the .play() function.
Is there a way to append more chunks onto the current queue while it is being played?
Any help? Thanks
#test using smaller queue
testQueue = chunks[0]
pydub.playback.play(testQueue)
testQueue = testQueue + chunks[1]

Moviepy write_videofile works the second time but not the first?

I'm concatenating a list of video objects together then writing them with write_videofile, weirdly enough the first time I write the file, it plays fine for the first halfish then the first few frames of each clip in the file afterwards plays before freezing. But here's the odd part, If I write the exact same video object right after the first video writes, it writes just fine and plays perfectly.
Here's my code
from moviepy.editor import VideoFileClip, concatenate_videoclips
clipslist = []
clips = ['https://clips-media-assets2.twitch.tv/AT-cm%7C787619651.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787628097.mp4', 'https://clips-media-assets2.twitch.tv/2222789345-offset-20860.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787624765.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787539697.mp4', 'https://clips-media-assets2.twitch.tv/39235981488-offset-3348.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788412970.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787682495.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787962593.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787627256.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787573008.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788543065.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787593688.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788079881.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788707738.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788021727.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787595029.mp4', 'https://clips-media-assets2.twitch.tv/39233367648-offset-9536.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788517651.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C788087743.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787497542.mp4', 'https://clips-media-assets2.twitch.tv/39233367648-offset-9154.mp4', 'https://clips-media-assets2.twitch.tv/7109626012888880881-offset-4818.mp4', 'https://clips-media-assets2.twitch.tv/72389234-offset-760.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787774924.mp4', 'https://clips-media-assets2.twitch.tv/AT-cm%7C787565708.mp4']
for clip in clips:
dlclip = VideoFileClip(clip, target_resolution=(1080, 1920)) # Download clip
clipslist.append(dlclip)
videofile = concatenate_videoclips(clipslist)
videofile.write_videofile("final1.mp4") # Broken after the first halfish
videofile.write_videofile("final2.mp4") # Works entirely fine.
videofile.close
Any ideas? Any suggestions appreciated.
Sometimes when the video is small enough it seems to write the first time just fine too.
It seems there is no set point where it breaks, each time I write it for the first time it typically breaks at a different spot.
I've tried waiting for the thread to exit and sleeping after the concatenation and that doesn't seem to fix the issue.
If you cannot consistently replicate the issue, it's most likely not an issue with your code.
Try opening the produced clip with a different program, such as VLC.
I came across with the same problem when writting multiple videos at the same time with write_videofile, it seems like the later tasks will cause the wrong outputs of the previous write_videofile tasks by hanging their writting processes, although the processes will continue after the later tasks finish, the result videos of previous tasks break at the hanging spots, haven't found a solution

Python play audio in sync with cv2 video

I am very new to the cv2 library, and since I recently discovered it is unable to play audio I wanted to know if there is another library that will allow me to play the audio in sync with the video itself (preferably without needing an audio file, just using the video's audio)
Turns out there is a very simple method that I will found after researching later, unfortunately, it requires a separate audio file that matches the video:
First, import simpleaudio as sa (install it with pip if necessary) and then add this piece of code to your cv2 video player. Add it before the while loop but after the line of code that defines the video file:
wave_obj = sa.WaveObject.from_wave_file("AudioFile.wav")
wave_obj.play()
Then you must manually adjust the waitKey until the audio matches the video, otherwise it might be too fast or too slow. Generally a value close to 25 must be used, and if the audio abruptly ends, try adding one to the value until it matches

push-to-listen-into-the-past with pyaudio

Essentially, what I want to achieve is upon triggering, the previous two seconds and further audio should be recorded, until silence.
For this, of course, the microphone has to listen all the time and record to a circular buffer. Upon triggering, current audio shall be recorded until there is a long enough span of silence. In the end, both streams should be combined to a single audio file/stream.
Unfortunately, I can't seem to wrap my head around how to achieve this with pyaudio.
I would be great if you had some advice for me.

python pygame how to fade in the volume?

song = "choy_san_dou.ogg"
pygame.init()
pygame.display.set_mode((200,100))
pygame.mixer.init()
pygame.mixer.music.load(song)
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play(0)
I am unable to find a way to fade in the volume when the song is loaded, means to have the volume of the song slowly increase until a max volume instead of blasting it off full volume from the very beginning. How to do it?
Another problem is this, I must declare
pygame.display.set_mode((200,100))
or the sound won't play. The program is actually running because after time of the sound has elapsed, the subsequent steps in the program continues. Why is it this way? I am testing it on windows pc, with the intention of transferring it to a raspberry pi later.
The pygame.mixer module has a Sound class (doc: http://www.pygame.org/docs/ref/mixer.html) that takes a file name as input and converts the file contents to a Sound object. Sound objects have a method play() where you can specify an amount of time where the Sound is faded in (you likely want to make it much shorter than the actual length of the Sound). I suggest you read the documentation to get the gist of how pygame.mixer works. If you want to alter the Sound object, such as adding fade-in or fade-out, there's the pygame.sndarray module that can convert a Sound object to a numpy array and vice versa, though you'll need to use numpy or scipy to do stuff with that array (I recommend the free Anaconda package).
The difference between the music and the mixer module is that the music module STREAMS data to playback, while the mixer module handles data saved to main memory.

Categories