Cannot have microphones in several routines

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