No sound from microphone

OS : MacOS Mojave
PsychoPy version : 2021 2.3
What are you trying to achieve?: In my experiment, I am hoping to collect verbal responses from participants. I am able to create .wav files, but the files are empty. Using other software on my computer (e.g. inquisit) I am able to get my microphone to work and have sound come through, however, using psychopy there does not seem to be a connection with my computer microphone

What did you try to make it work?:
I implemented the code below. This code opens the window and creates an empty .wav file. Specifically, I also downloaded psychtoolbox and changed my hardware preferences for audiolib as well. I also tried running several builder experiments using the microphone component, and also received blank .wav files.

import psychopy 
from psychopy import visual, core, event, data, gui
from psychopy import visual, core, event, data, gui
from psychopy import prefs
psychopy.prefs.hardware['audioLib'] = ['PTB']
from psychopy import sound

resolution = [1440, 900]
win = visual.Window(resolution, monitor="testMonitor", fullscr=True, screen=0, units='pix')


recordingDevicesList = sound.Microphone.getDevices()

recordingDevice = recordingDevicesList[0]  # assume not empty
inputLatencyLow, inputLatencyHigh = recordingDevice.inputLatency

deviceName = recordingDevice.deviceName

recordingDevicesList = sound.Microphone.getDevices()
recordingDevice = recordingDevicesList[0]

mic = sound.Microphone(streamBufferSecs=10.0, device=recordingDevice, channels=1)

mic.start()  # start recording

mic.poll()
win.flip()  # calling the window flip function

core.wait(10.0)  # wait 10 seconds
mic.stop()  # stop recording

audioClip = mic.getRecording()

print(audioClip.duration)  # should be ~10 seconds
audioClip.save('test.wav')  # save the recorded audio as a 'wav' file

What specifically went wrong when you tried that?:
I receive no errors when I run this code, however, the .wav files are still empty. Any help would be appreciated as to why psychopy is not communicating with my microphone!

As an update, I realized that psychopy is able to find my microphone, but is only recording with the macbook microphone which has only input channels and 0 output channels.

AudioDeviceInfo(deviceIndex=0, deviceName=MacBook Pro Microphone, hostAPIName=Core Audio, outputChannels=0, outputLatency=(0.01, 0.1), inputChannels=1, inputLatency=(0.04852607709750567, 0.05868480725623583), defaultSampleRate=44100, audioLib=‘ptb’)

I am able to detect devices on my computer which have output channels, however, I still can’t figure out a way to playback the audio I recorded (outside of the experiment, in itunes for example):

{‘External Headphones’: {‘DeviceIndex’: 1.0, ‘HostAudioAPIId’: 5.0, ‘HostAudioAPIName’: ‘Core Audio’, ‘DeviceName’: ‘External Headphones’, ‘NrInputChannels’: 0.0, ‘NrOutputChannels’: 2.0, ‘LowInputLatency’: 0.01, ‘HighInputLatency’: 0.1, ‘LowOutputLatency’: 0.0040625, ‘HighOutputLatency’: 0.013395833333333333, ‘DefaultSampleRate’: 48000.0, ‘id’: 1}, ‘MacBook Pro Speakers’: {‘DeviceIndex’: 3.0, ‘HostAudioAPIId’: 5.0, ‘HostAudioAPIName’: ‘Core Audio’, ‘DeviceName’: ‘MacBook Pro Speakers’, ‘NrInputChannels’: 0.0, ‘NrOutputChannels’: 2.0, ‘LowInputLatency’: 0.01, ‘HighInputLatency’: 0.1, ‘LowOutputLatency’: 0.01018140589569161, ‘HighOutputLatency’: 0.020340136054421767, ‘DefaultSampleRate’: 44100.0, ‘id’: 3}}

Any idea how to record using my microphone but also be able to be listened to after the fact? Thanks!

1 Like

Hi There,

Just to check you have already tried using the microphone components in builder but opted to use coder microphone because of issues selecting the desired microphone is that correct? if not, does the mircophone demo in builder work for you? Demos > feature demos > microphone

Thanks,
Becca

Yes that is correct. I tried the builder demo as well and was still getting empty .wav files afterwards. Ultimately I’m hoping to use the coder for my whole experiment to have more control, but would be fine if I could get the microphone component to work in any way (builder or coder) as it is a necessary component to my experiment. Thanks!

I’m having a similar issue. My builder demos yield empty .wav files and changes in audio libraries, durations, latencies, etc. don’t seem to be making a difference. I am not writing my own code, just using the builder right now, and the error that comes up is that my audio device (which is set to Built-in Output) is unsupported.

OK - just wondering if this could be a mac permission error - can you view what applications have access to your mic?

Thanks,
Becca

PS. in general I would say that builder + code components allows the same flexibility as coder without the risk of bugs from typos - I use that combo as it’s faster and then we can code the fun bits in code comps!

Ah! When I just checked I had a version of psychopy getting access from my mic, but I just realized it was an older version (that no longer worked). I just deleted that version off of my computer, and now there is no psychopy getting access to my mic- which must be the issue!

However, after spending a good amount of time trying, I cannot find any way to get PsychoPy access to my microphone in the Security and Privacy settings. I uninstalled and reinstalled psychopy a few times. Is this an issue with the new release of psychopy and mac specifically? Please let me know if you think there is a way of fixing this or if it is best to install and older version of psychopy. Thanks again!

Hi all,

I am also using a Mac and cannot get it to allow psychopy access to the microphone. My peer tried opening psychopy through terminal (as opposed to just clicking on the psychopy icon to start) and his Mac automatically prompted to give psychopy microphone permissions. I can’t get that to work though.

@akoch, did your colleague just open the PsychoPy app via the Terminal (i.e., with open -a /Applications/PsychoPy.app)? If not, what was the exact command? If the same command worked for them but not for you, maybe you are on a different versions of macOS?

When I tried opening the PsychoPy app with open -a and running a test experiment with a mic component, I didn’t get a macOS permission prompt. This is on Big Sur (v11.6.1) with PsychoPy v2021.2.3. Just as a side note: On my machine, I don’t get empty sound files. Instead, the experiment terminates with a “psychopy.sound.exceptions.AudioStreamError: Cannot stop a stream that has not been started” error. (Edit: That is apparently a different problem; when I use “Demos > feature demos > microphone” instead of my own experiment, I don’t get that error and empty wav files are being saved at the end of the experiment, just like the other posters in this thread have described.)

If anyone has an idea, how to get microphone support working again, I would be very glad. I hadn’t used the mic component in a few years, but now I am working on an experiment for which it is essential.


UPDATE:
I couldn’t let this go and it then occurred to me that “opening psychopy through terminal” could also mean executing the PsychoPy binary inside the application bundle (i.e., using /Applications/PsychoPy.app/Contents/MacOS/PsychoPy as the Terminal command).

When I did this and started an experiment, I first got a macOS permission dialog for keyboard input monitoring and then one for microphone access. Both of these were for the Terminal app itself, though, and not for the PsychoPy app. After granting these permissions and restarting Terminal, the mic component worked and non-empty sound files were being saved.

Granting Terminal these permissions does not strike me as a great idea from a security perspective. Plus, I don’t know if running the PsychoPy app this way will cause any weirdness or problems down the road, because everything is rerouted through Terminal now. However, it does allow me to work on the experiment with verbal responses now, which is very helpful, and it does confirm that it is really a permissions issue that is preventing the mic component from properly working with current versions of macOS.

Hopefully the issue can get fixed soon, so we won’t have to rely on the potentially problematic workaround described above.

4 Likes

Thank you so much for your insight! I opened PsychoPy inside the application bundle as a Terminal command and I got the audio to work exactly as you said. I appreciate you ‘not letting this go’ and going deeper into it! You’re right, this doesn’t seem like the best fix, but at least it is something until a better solution comes along.

Completely agree! This worked for me too. Thank you as well for looking into this further!

That’s very helpful! Thank you very much!

It worked! Thanks

I would like to add a condition to this solution.
I have an M1 running MacOS Monterey and I tried executing the PsychoPy binary inside the Terminal application with the following command:

/Applications/PsychoPy.app/Contents/MacOS/PsychoPy

I was not asked for any permissions and my experiment still produced empty .wav files.
I decided to go back to my Security & Privacy settings and found that the Terminal application could be given permission to access the microphone. So I checked the box next to Terminal and tried again using the above command. I was still never asked about any permissions BUT my wav files have sound now!
So if anyone is still having trouble with this issue and bjoern’s fix doesn’t work for your Mac, try this!

Hello all,
hopefully someone can help me with my issue,
I’m trying to record audio from a microphone, but unfortunately, the files are empty. I have made several attempts to solve this issue, such as specifying the duration as 8 seconds when the length of the video is 8, but the problem seems to occur when I connect to the sound card (Scarlett 2i2). If I use the Mac without any sound card, it plays normally through headphones and records audio from the laptop.

I have a similar experiment that uses an audio component instead of a video component, and it works normally. What can be the issue, and how can I solve it?