Issues with microphone recordings: AudioStreamError: Cannot stop a stream that has not been started

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Windows 10 Pro
PsychoPy version (e.g. 1.84.x): 2021.2.3
Standard Standalone? (y/n) If not then what?: YES
What are you trying to achieve?: I am trying to record voice responses using the microphone component and the ptb audio library.

What did you try to make it work?:
I have tried using different sampling rates, audio libaries (e.g., ptb, pyo, system, etc.), to debug the microphone component in the script of my experiment, but I do not manage to make it work as when the mic component is called (e.g., mic.stop() ) the experiment crashes and shows the: “Cannot stop a stream that has not been started”).

My experiment was built in v 1.83.04 and it works just fine in that version, recording audio and everything. Can the issues be due to retro-compatibility in the newest version? I am working in a neuroimaging experimental PC that has not access to the internet (thus, not access to older versions) for security reasons.

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

Running: C:\Users\experimental user\Desktop\TAOT\exp_1dB\taot_exp_builtin_lab_bk_lastrun.py

3527.9022 INFO Loaded monitor calibration from [‘2020_01_29 00:44’]
3527.9426 INFO C:\Program Files\PsychoPy\pythonw.exe -m psychopy.scripts.psyexpCompile C:\Users\experimental user\Desktop\TAOT\exp_1dB\taot_exp_builtin_lab_bk.psyexp -o C:\Users\experimental user\Desktop\TAOT\exp_1dB\taot_exp_builtin_lab_bk_lastrun.py -v 1.90.0
pygame 1.9.6
Hello from the pygame community. Contribute - pygame wiki
Traceback (most recent call last):
File “C:\Users\experimental user\Desktop\TAOT\exp_1dB\taot_exp_builtin_lab_bk_lastrun.py”, line 496, in
mic_1.stop()
File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\sound\microphone.py”, line 803, in stop
“Cannot stop a stream that has not been started.”)
psychopy.sound.exceptions.AudioStreamError: Cannot stop a stream that has not been started.

Experiment ended.

Hi There,

Please could you show us the settings of your mic component?

Thanks,
Becca

Hi Becca,

I managed to solve this by changing the point in which audio recordings are stored.

I realied that mic.stop() was happening twice in my script (I don’t know how because I didn’t build any custom script, just used builder components), so the second time psychopy was trying to close a microphone component there was nothing to close and it would crash.

So I commented the second mic close and moved the audio file creation to right before mic.stop(), and now it works just fine.

Thank you for your help!

jose

So pleased you fixed it and thanks for sharing your solution!

1 Like

Hi Jose,

I’m having the same problem getting my recordings to save. Do you mind sharing your code? I’ve commented out the second mic.stop() like you did and that solved the error message, but I’m still having issues storing the recordings. Thanks!

Jessie

It is possible to stop a recording that is in progress by using a code component. Every frame, check for a condition (such as key ‘q’, or a mouse click), and call the .stop() method of the microphone component. The recording will end at that point and be saved. For example, if mic is the name of your microphone component, then in the code component, do this on Each frame :

if event.getKeys([‘q’]):
mic.stop()

Hi jessieq,

I moved the highlighted chunk of code below (i.e., the one used to store the files of the recordings) from the last mic.stop() instance in the script to right before the mic.stop() that I wanted to use as actual microphone stop and record.

if mic_1.status == NOT_STARTED and sound_1.status==FINISHED:
parallel.setData(0)
# keep track of start time/frame for later
mic_1.frameNStart = frameN # exact frame index
mic_1.tStart = t # local t and not account for scr refresh
mic_1.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(mic_1, ‘tStartRefresh’) # time at next scr refresh
# start recording with mic_1
mic_1.start()
mic_1.status = STARTED
if mic_1.status == STARTED:
# update recorded clip for mic_1
mic_1.poll()
if mic_1.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > mic_1.tStartRefresh + mic_time-frameTolerance:
# keep track of stop time/frame for later
mic_1.tStop = t # not accounting for scr refresh
mic_1.frameNStop = frameN # exact frame index
win.timeOnFlip(mic_1, ‘tStopRefresh’) # time at next scr refresh
# save mic_1 recordings
for tag in mic_1.clips:
for i, clip in enumerate(mic_1.clips[tag]):
clip.save(os.path.join(mic_1RecFolder, ‘recording_mic_1_%s_%s.wav’ % (tag, i)))
# stop recording with mic_1
mic_1.stop()
mic_1.status = FINISHED

I hope this helps!

Best wishes,
jose

1 Like

Hi all!
Does anyone know why/when this happens? I have 2 related and, therefore, more or less similar experiments (both builder built) and, in one I am getting the above errot message and in the other I do not. The main difference between the experiments is whether or not an additional acoustic stimulus appears (after the mic is already recording). Most confusing: once the experiment run fine without commenting out the second mic.stop() and moving up the other part, sometimes it crashed in block 4/4, and sometimes it did not even get through the first block.
Best,
Friederike

Addendum: When I am commenting out the second mic.stop() only the error message disappears. The folder where the .wav files should be appear remains empty.

Yet another addendum: When I am adding a sound stimulus to the simple experiment that is actually recording sound, it does not record any longer.

Have you tried moving the code chunk I suggest in my comment of the 10th of January (2022)?

Yes. Unfortunately the folder remains empty :confused:

Update: What worked for me now was copying (instead moving) this piece of code to where you did Jose - meaning it’s actually twice now in the script.

1 Like

I came across the same error. In my case, the routine, during which sound was recorded, was ended with a key stroke. There was no end time for the microphone component and it seems this was the problem.

I found a solution that allowed me to stay inside the builder GUI: I entered a very long end time for the microphone component, which was longer than the time the module could run. I guess this solved the issue because the mic component never had the chance to call mic.stop() so when the second mic.stop() is called (whereever this comes from), it still has a stream to stop and does not complain.

In my case, I set the mic component to a duration of 31 sec and added a code component with

if t >= 30:
continueRoutine = False
in the “Each frame” tab.

The audio files seem to be only saved if the entire task runs to the end. Not sure if this is related of not ever reaching the mic component’s end but that’s the best I could get to.

Hello Jose,

First of all thanks, because regarding this issue your comment seems to be the most helpful on the internet!
I have built a picture naming task in PsychoPy v2022.2.4 in which participants will have to name the images by speaking and typing. The experiment is not blocked by modality, namely participants will be seeing both speaking and typing trials in the same block.
I have tried what you mention in the comment above and although the experiment is running properly and I am not getting the AudioStreamError anymore (thanks :D), I am getting the following error and I have no clue where to start looking:

PsychPortAudio-WARNING: ‘Start’ method on audiodevice 0 called, although playback on device not yet completely stopped.
Will forcefully restart with possible audible artifacts or timing glitches.
Check your playback timing or use the ‘Stop’ function properly!

Any advice?
Thank you in advance!
Best,
Katerina

Hi Katerina,

thanks for your reply.

(Un)fortunately, I did not encounter the same issue in this case. I don’t know if it could have something to do with which playback device you are forcing your experiment to use, as well as its latency/priority. You could try changing those parameters of your audio stimuli (latency, device, etc.) as well as those specific to PTB (Psychtoolbox-3 - PsychPortAudio(‘Start’))

Sorry for the unspecified help, and good luck!

Thanks for your reply, Jose!
In the end, I did what Jens also describes above, with increasing the duration of the trial and adding a code component that stops the microphone from recording before the trial ends.