Microphone recording in 2022.1.2 for an undefined amount of time

I found a solution using sounddevice rather than psychopy

Similar to this example

switching to sounddevice i.e. import sounddevice as sd rather than psychopy.sound.microphone is what can work for now.

Below example is tested on psychopy 2022.1.2 on windows 10. with a logitech HD pro webcam C920 as the Microphone and playing the sound using VLC media player.

The below code (adapted to a psychopy “experiment” from this example) opens a sound device and streams to it for an undefined amount of time (until 4 trials are completed),
The experiment is just clicking n 4 times.
I did have to go into windows settings to change microphone to the correct one. (and keeping device = None)

#!/usr/bin/env python3
import os
import queue
import sys

import sounddevice as sd
import soundfile as sf
from psychopy import core, event, visual


win = visual.Window([1200,400], monitor="testMonitor", units="deg")
textStim = visual.TextStim(win,height=1,text="Start text")
textStim.draw()
win.flip()
core.wait(0.5)

trials = 4
q = queue.Queue()

channels = 1

print("list of sound devices found")
print(sd.query_devices())
print("setting devices to none should work.. ")
print("tested in windows:  Make sure your microphone is connected to your PC.Select Start > Settings > System > "
      "Sound.In Sound settings, go to Input and in the Choose your input device list, select the microphone or recording device you want to use...."
      " make sure you can see that it is recording some sound (windows 10)")

device = None
filename='python_sound_device_audio_file.wav'
if os.path.exists(filename):os.remove(filename)

list_devices = False
samplerate = 44100
subtype = None

def callback(indata, frames, time, status):
    """This is called (from a separate thread) for each audio block."""
    if status:
        print(status, file=sys.stderr)
    q.put(indata.copy())

file =  sf.SoundFile(filename, mode='x', samplerate=samplerate,
                  channels=channels, subtype=subtype)

with sd.InputStream(samplerate=samplerate, device=device,
                        channels=channels, callback=callback):
    print(device)

    for trial in range(trials):

        textStim.setText("Trial %s\npress n for next trial"% str(trial))

        nextTrial = False
        while not nextTrial:
            file.write(q.get())
            textStim.draw()
            win.flip()
            response = event.getKeys(["n","escape"])
            if response:
                if response[0] == "n":
                    nextTrial = True
                elif response[0] == "escape":
                    core.quit()
                    print("escape was hit - stopping trials")
                    print("wav file saved in %s" % os.getcwd() + "/" + filename)

print("wav file saved in %s" % os.getcwd() + "/" + filename)
print("VLC media player worked for me for playing the recording... Groove Music did not... :/ ")

textStim.setText("wav file saved in %s" % os.getcwd() + "\n\n and is named:\n%s"% filename )
textStim.draw()
win.flip()
core.wait(3)
core.quit()