Microphone recording not ending properly

That sucks. Several months ago after coming to the conclusion that pyo sucks, I had done a little digging and came up with a program myself to use for playing and recording audio.

The recording program is modified slightly from a post on github by slorian, so he deserves any credit there, I wrote the player code myself.

If you’re coding your experiment yourself you could consider using this, though it comes ‘without warranty’ :slight_smile: It could also be added in a code component with some careful surgery. It was my first foray into audio programming, so I’m sure there are ways it could be improved. If you want to use it, let me know if anything I’m about to say doesn’t make sense to you.

Some notes and caveats: It only does wav files. My experiment doesn’t care about reaction times, so latency issues haven’t been a big deal for me, I always start recording before the window flip in the hope that I don’t lose the beginning of the enunciation due to slow startup, and haven’t had a problem yet, though my machine is pretty fast.

Keep in mind that since you won’t know how long you want to record for, you want to use the recorder in ‘non-blocking’ mode, meaning the recorder will start a separate thread. A consequence of this is that you will need to do something to pause your main experiment script to wait for recording to finish, otherwise it will keep marching on.

To quickly see if it works, open a Python shell in the same folder as where you have the file, (make sure a file named __init__.py exists that folder as well), and enter:

import simpleAudio 
outFile = 'testFile.wav'
recorder = simpleAudio.Recorder()
rf = recorder.open(outFile, 'wb')
rf.start_recording() # say some stuff
rf.stop_recording()

So to give you an example, you could do something like this. Put this file (simpleAudio.py) in the same folder as your experiment. Now editing your experiment script:

Toward top of experiment file

import simpleAudio

Somewhere toward the beginning of the experiment

recorder = simpleAudio.Recorder()
recordClock = core.Clock()

Now in the recording routine

outFile = 'outFile.wav'

with recorder.open(outFile, 'wb') as rf:
    recordClock.reset()
    rf.start_recording()

    # imaginary icon indicating recording
    recIcon.draw()
    win.flip()

    keepRecording = True
    # if you put too much stuff in this loop 
    # you might get distortion.
    while keepRecording:
        keys = event.getKeys()
        if 'escape' in keys:
            core.quit()
        if 'return' in keys:
            # make sure at least a second
            # has passed
            recT = recordClock.getTime()
            if recT > 1:
                rf.stop_recording()
                keepRecording = False
            

Let me know if you need more info or are having trouble getting it going.

simpleAudio.py (5.2 KB)

3 Likes