Experiment crashing due to microphone code?

Hello,

I’m having a really frustrating bug in my code causing untraceable crashes and would greatly appreciate any help. The experiment is made using Builder, but this specific question is related to custom code in the code component.

Short about my experiment… My goal is to record the participant during an entire 60 s routine. This is done twice in each loop, and the loop has 12 iterations. Therefore I have used AdvAudioCapture, so that I can save the files with custom filenames.

Microphone code - pretty much based on docs:

##### Init. routine
### Before exp:
from psychopy import microphone

### Begin experiment:
# Init. microphone
microphone.switchOn()

# Set directory for recordings to be saved
recordingDir = f"Data/Sound recordings/participant_{expInfo['participant']}/"

# Init. mic object with mono sound.
mic_channel = 0 
mic = microphone.AdvAudioCapture(name='mic', saveDir=recordingDir, stereo=False, chnl=mic_channel)

##### Recording routines (both use the same code)
### Begin routine:

# Start sound recording
mic.reset()

mic_filename = recordingDir + f'participant_{participant_id}_problem{problem_id}_{stimuli}_wordset{wordset}.wav'
mic.record(60, filename=mic_filename, block=False)

### End routine:
mic.stop() # Failsafe

My preferences is set to pyo and it is in fact recording and saving the segments as expected - but crashes from time to time during the first iteration (but after both recordings) without giving a stack trace or log.
Logging with DEBUG level seems to uncover an error in switchOn(). Below are two logs - one for a working run and one where it suddenly crashed.

From working run:

(…)
21.6623 INFO Success: Primary Sound Capture Driver
21.6623 INFO Using sound-input driver: Primary Sound Capture Driver (ID=6)
23.0879 EXP Set sound=C
23.0880 DEBUG pyo sound server started
23.0888 EXP C:\Program Files\PsychoPy3\lib\site-packages\psychopy\microphone: switch on (48000hz) took 1.787s
23.0895 EXP frequency of recording onset marker: 19000.0
23.0897 EXP Set mic.marker_tone sound=19000.0
(log continues…)

From crashing run

(…)
15.7781 INFO Success: Primary Sound Capture Driver
15.7781 INFO Using sound-input driver: Primary Sound Capture Driver (ID=6)
17.2608 EXP Set sound=C
17.208 DEBUG pyo sound server started
(log stops here even though exp. continued for a while - like it is blocked?)

As C:\Program Files\PsychoPy3\lib\site-packages\psychopy\microphone: switch on (48000hz) took 1.787s is never logged in the crashing run, it seems like switchOn() is never fully executed. This is supposed to be logged on the end of the function call.

Hopefully I’ve explained the problem well enough, but please tell me if I need to elaborate further.
Thanks in advance :slight_smile:

For those interested…

I ended up scrapping the microphone module (which uses pyo) and using recording functionality from sounddevice. The WAV file was generated using a module from scipy.

My code is now the following:

### Init. routine
## Before exp
import sounddevice as sd    
from scipy.io.wavfile import write as wav_write

## Begin routine
# Make and set directory for recordings to be saved
recordingDir = _thisDir + fr"\Data\Sound recordings\participant_{expInfo['participant']}"
os.mkdir(recordingDir)
    
# Set recording params
fs = 48000
sd.default.samplerate = fs
sd.default.channels = 1 # Mono sound
rec_duration = 65 

### Recording routine
## Begin routine
mic_recording = sd.rec(int(rec_duration * fs))

## End routine
mic_filename = recordingDir + fr'\participant_{participant_id}_problem{problem_id}_{stimuli}_wordset{wordset}.wav'
wav_write(mic_filename, fs, mic_recording)

Seems to be working like a charm, with simple syntax/code snippets similar to the microphone module.

1 Like