sound.microphone.Microphone() mistaking microphone input as a keyboard

Psychopy version: 2025.1.1

Standalone: No

OS: Windows 11 Version 23H2 for x64 systems.

I have been working on this for a few days now and can’t seem to get psychopy to take my microphone as a microphone, psychopy thinks it’s a keyboard. I have a script from 2023 that I am adapting to upgrade for the 2025 version on a new system, and it gives me an issue when I initialize and attempt to record like so:

mic = sound.microphone.Microphone(device=None, channels=2, sampleRateHz=44100)

later on in my script I want to record audio with the microphone at a precise time, so within some conditional logic I call:

mic.start()

resulting in the error:

File “C:\Users\****\anaconda3\envs\psychopy\lib\site-packages\psychopy\sound\microphone.py”, line 185, in start
return self.device.start(
TypeError: KeyboardDevice.start() got an unexpected keyword argument 'when'

I have been troubleshooting this for a few days now. I tried using sounddevice to discover the correct index for the microphone in the inputs and the name of my microphone as well (these do appear in python as an input with the correct number of channels). The return from manually putting in the correct index or string with the name of the microphone both return that an input device does not exist there. I also tried inputting indices 0-15 in it and got the same result every time.

I tried adding the microphone using psychopy.hardware and did not get that to work.
I have gone through my windows settings to ensure the microphone is selected as the default and that psychopy is allowed to access it.

I have ensured directaudio is accessible to psychopy.

I am using PTB as the audio library.

I have tried the above using standalone psychopy 2024.2.5 including the compatibility version.

I have tried fresh reinstallations of psychopy.

After poking around in psychopy.hardware a lot more I found that if I initialize the device in the hardware first and then call the psychopy.sound then I can get it to work specifying the hardware device. The sound module was not handling it automatically and microphone.MicrophoneDevice is not listed in the hardware documentation. psychopy.hardware - hardware interfaces — PsychoPy v2025.2.0

deviceManager.addDevice(
deviceClass= ‘psychopy.hardware.microphone.MicrophoneDevice’,
deviceName= ‘mic’,
index=None,
exclusive=False,
)

mic = sound.microphone.Microphone(device=’mic’, channels=2, sampleRateHz=48000)

Now the microphone works appropriately with the sound.microphone.Microphone methods such as start(), stop(), etc.

1 Like

Ah, I see the problem… the logic in sound.Microphone is essentially:

  • Check if there’s a device with this name
  • If not, and device is None, get the first MicrophoneDevice we can find
  • If still nothing, make a MicrophoneDevice

Problem is, if device is None and there’s a Keyboard device called None (which there is by default) then the first step gets that device. I’ll put in a fix to the next release - it should be a case of both checking whether device is None before everything else and only getting named devices of the correct class.

1 Like