Cannot have microphones in several routines

Hi everybody,

I’m working on WIN11 with the standalone 2021.2.3 psychopy version.
I’m actually trying to build an experiment in which I would like (among other things) to record participants’ voices during 210 sec before and after they take some medicines.
So my script is made of several routines including :

  • A routine in which voice is recorded while the participant is reading a text on the screen (3:30 minutes)
  • A routine in which I ask him/her to take the pils
  • Another routine in which I would like to record again the participant’s voice while reading another text (3:30 minutes)

Here are my problems:

  1. When the microphone is enabled in the first routine only, the experiment works fine but I get a warning saying Audio recording buffer filled! This means that no samples are saved beyond 125.0 seconds. Specify a larger recording buffer next time to avoid data loss. and my *.wav files are indeed no longer than 125 sec (I need 210).

  2. As soon as I enable the microphone component of the second routine, the experiment fail to run and I got error messages saying:

PTB-ERROR: Failed to open audio device 5. PortAudio reports this error: Invalid device 
PTB-ERROR: Desired audio parameters likely unsupported by audio device.
PTB-ERROR: This could be, e.g., due to an unsupported combination of audio sample rate, audio channel count/allocation, or audio sample format.
Error in function Open:     Usage error
Failed to open PortAudio audio device due to unsupported combination of audio parameters.
PsychPortAudio:Open: Usage:

pahandle = PsychPortAudio('Open' [, deviceid][, mode][, reqlatencyclass][, freq][, channels][, buffersize][, suggestedLatency][, selectchannels][, specialFlags=0]);
Traceback (most recent call last):
  File "C:\Users\ambre\Documents\CoCo_Projets\PronutriVoice_AliceGuyon\SCRIPTS_pronutri\Pronutri_2022_lastrun.py", line 850, in <module>
    sampleRateHz=48000, maxRecordingSize=48000.0
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\sound\microphone.py", line 483, in __init__
    channels=self._channels)
  File "C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py", line 101, in __init__
    flags)
Exception: Failed to open PortAudio audio device due to unsupported combination of audio parameters.
Exception ignored in: <bound method Stream.__del__ of <psychtoolbox.audio.Stream object at 0x0000014D74D159B0>>
Traceback (most recent call last):
  File "C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py", line 236, in __del__
    self.close()
  File "C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py", line 146, in close
    raise err
  File "C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py", line 139, in close
    PsychPortAudio('Close', self.handle)
AttributeError: 'Stream' object has no attribute 'handle'
##### Experiment ended. #####

The microphone components of the two routines have exactly the same parameters, namely:

  • duration of 210 sec
  • device setted at default
  • no transcription required
  • nothing to save except the audio file
  • sample rate + max recording size setted at 48kHz (because I also have some videos with this sample rate)
  • I disabled the “Force stereo” option

The required audio library is PTB but I’ve the same problem no matter the audio library I use (in addition with other few warnings and error messages when I don’t require PTB).

Does anyone know how to fix these two problems?
Any help would be greatly appreciated!!!

Cheers :beers: !

1 Like

Hey, I got the exact same issue. Have you solved the problem?

And for buffer error, simply increase the Max Recording size option of the microphone component.

@DrSmith I have encountered the exact same problem as well. Have you figured out a solution for using microphone for multiple routines?

Tagging @Becca and @TParsons because I have seen you both helping issues relating to the microphone recording function. .

Any help is greatly appreciated! Thank you!

I couldn’t, unfortunately. But I strongly suspect it may be due to version of the Psychopy I am using, which is the most recent one, so I am planning to try out previous versions as well. Can you also try some of the previous versions? It would be great if we keep in touch to see if a particular previous version works.

Hi @DrSmith & @HungShao
The problem regarding the recording time was sold when running the experiment on WIN10.

However, I couldn’t have two microphone components in the same experiment, even if not in the same routine. So I splitted the experiment in two parts/scripts as it was not a big deal in this particular experiment.
I know that previous versions of Psychopy allow to have multiple microphone components in the same script but they start to be quite old and much less user-friendly than the last versions.

Thanks @Ambre_Denis-Noel for the into. @DrSmith, I have tried running the exp using different previous versions but none of them worked on my end.

Version 3.2.4 should work

Hay please can I check if you have tried a newer release (i.e. a 2022 release?) thanks!
Becca

ofc
I don’t think I tried with any of the 2022 releases. Do you know if it works now?

Ambre

Hi @Becca, @Ambre_Denis-Noel, and @DrSmith,

After hours of troubleshooting, I have come up with a working solution. I currently use 2022.2.2 version of PsychoPy and it has been working nicely. Basically, you can only include the microphone component directly from the GUI once. After that, you will need to insert the microphone component via code chunks. Please see below.

Begin Experiment:

mic_q1 = sound.microphone.Microphone(
    device=8, channels=1, 
    sampleRateHz=48000, maxRecordingSize=24000.0) ## I named the mic as mic_q1, you can change the name

Each frame

Be sure to change the variable name from mic_q1 to whichever you come up.


if mic_q1.status == NOT_STARTED and t >= 0.4-frameTolerance:
    # keep track of start time/frame for later
    mic_q1.frameNStart = frameN  # exact frame index
    mic_q1.tStart = t  # local t and not account for scr refresh
    mic_q1.tStartRefresh = tThisFlipGlobal  # on global time
    win.timeOnFlip(mic_q1, 'tStartRefresh')  # time at next scr refresh
    # add timestamp to datafile
    thisExp.addData('mic_q1.started', t)
    # start recording with mic
    mic_q1.start()
    mic_q1.status = STARTED
if mic_q1.status == STARTED:
    # update recorded clip for mic
    mic_q1.poll()
if mic_q1.status == STARTED:
    # is it time to stop? (based on global clock, using actual start)
    if tThisFlipGlobal > mic_q1.tStartRefresh + 4.55-frameTolerance:
        # keep track of stop time/frame for later
        mic_q1.tStop = t  # not accounting for scr refresh
        mic_q1.frameNStop = frameN  # exact frame index
        # add timestamp to datafile
        thisExp.addData('mic_q1.stopped', t)
        # stop recording with mic
        mic_q1.stop()
        mic_q1.status = FINISHED

End routine:

# tell mic to keep hold of current recording in mic.clips and transcript (if applicable) in mic.scripts
# this will also update mic.lastClip and mic.lastScript
mic_q1.stop()
tag_q1 = data.utils.getDateStr()
micClip_q1 = mic_q1.bank(
    tag = tag_q1, transcribe='None',
    config=None
)   

q1_trials.addData('mic_q1.clip', os.path.join(micRecFolder, u'practice_mic_q1_%s_%s.wav' % (expInfo['participant'] ,q1_trials.thisN)))

for tag_q1 in mic_q1.clips:
    for i, clip in enumerate(mic_q1.clips[tag_q1]):
        clipFilename = u'practice_mic_q1_%s_%s.wav' % (expInfo['participant'], q1_trials.thisN)
        # if there's more than 1 clip with this tag, append a counter for all beyond the first
        if i > 0:
            clipFilename += '_%s' % i
        clip.save(os.path.join(micRecFolder, clipFilename))

Hope this helps

3 Likes

Hi. Much thanks for sharing your solution. I don’t have much knowledge regarding how to use the coder, so where exactly I should add that chunk of code, and how many times?(I need to have 4 microphones in my experiment). Also should I add the first one grom GUI, then the remaining 3 parts manually, using the code of the first one but changing the names(since other properties of the mics are same).

Thanks again for your help, hope this works for me as well.