Single audio recording across all trials within a loop

OS (e.g. Win10): Win11
PsychoPy version (e.g. 2024.2.4 Py 3.8): 2025.1.1
Standard Standalone Installation? (y/n) If not then what?: Y
Do you want it to also run online? (y/n) N
What are you trying to achieve?:

I am trying to use the microphone component during the course of a Serial Reaction Task and a Sustained Attention to Response Task. Each of these tasks have their own loops that produce infinite trials and finish based on a countdown timer set to 15 minutes. These two tasks are counterbalanced using an outer loop with a condition file. I want to have two 15-minute audio recordings collected during these tasks.

What did you try to make it work?:

I have tried starting the microphone inside the counterbalancing loop but outside the trial loop, then end the microphone with the countdown timer inside the trial routine. But because I use the microphone component in the builder, I have to specify its duration or leave it as blank, so it never proceeds to the trials. I tried to start the microphone with a custom code at the same spot too but kept failing at it for days. I was just wondering if there is any way around this so that I can use the Builder instead of coding all microphone related events.

A solution which works locally is to create a mic component in the instructions routine and then control it with code in the trials loop.

Begin Routine

if trials.thisN == 0:
   print('Starting mic')
   mic.start()

Each Frame

mic.poll()

End Routine

if trials.thisN == 9:
    mic.stop()
    tag = data.utils.getDateStr()
    micClip = mic.bank(
        tag=tag, transcribe='None',
        config=None
    )
    thisExp.addData(
        'mic.clip', mic.recordingFolder / mic.getClipFilename(tag)
    )

mic-code.psyexp (20.1 KB)

I can’t get this approach to work online, but I think creating the microphone component in code would work.

1 Like

Thank you so much, this was incredibly helpful!! I was able to adjust this code to end it based on my timer too. I really appreciate it!