openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

More intuitive Microphone helper

Open davidgilbertson opened this issue 1 year ago • 1 comments

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • [x] This is a feature request for the Python library

Describe the feature or improvement you're requesting

I'm looking at the Microphone class, and it seems to favour the use case where you want to record a specific amount of audio (where you know that time in advance).

I would have thought the most common use case is where a USER decides when to start and stop recording, and I can't work out how to use this class in that case.

I can wrap it to turn it from async to sync, something like this:

class MicrophoneSync:
    def __init__(self):
        self.do_rec = threading.Event()
        self.loop = asyncio.new_event_loop()
        self.loop_thread = threading.Thread(target=self.loop.run_forever, daemon=True)
        self.loop_thread.start()
        self.future = None
        self.mic = Microphone(should_record=self.should_record)

    def should_record(self):
        return self.do_rec.is_set()

    def start(self):
        self.do_rec.set()
        self.future = asyncio.run_coroutine_threadsafe(self.mic.record(), self.loop)

    def stop(self):
        self.do_rec.clear()
        return self.future.result()[1]


mic = MicrophoneSync()

mic.start()
time.sleep(2)
wav_bytes = mic.stop()

But that's almost as complex as just creating a synchronous one from scratch:

class MicrophoneSync:
    def __init__(self, sample_rate=24_000):
        self.frames = []
        self.stream = InputStream(
            samplerate=sample_rate,
            channels=1,  # mono
            dtype="int16",  # 16-bit
            callback=self._callback,
        )

    def _callback(self, indata, frames, time, status):
        self.frames.append(indata.copy())

    def start(self):
        self.frames = []
        self.stream.start()

    def stop(self):
        self.stream.stop()

        wav_bytes = io.BytesIO()

        with wave.open(wav_bytes, "wb") as wave_file:
            wave_file.setframerate(self.stream.samplerate)
            wave_file.setnchannels(self.stream.channels)
            wave_file.setsampwidth(self.stream.samplesize)
            wave_file.writeframes(np.concatenate(self.frames, axis=0).tobytes())

        return wav_bytes


mic = MicrophoneSync()

mic.start()
time.sleep(2)
wav_bytes = mic.stop()

So I have two questions:

  1. Am I missing something, is there a simple way to call the Microphone class such that I can start and stop it in response to a user interaction?
  2. Is it worth adding a sync version (either of the above) to the package?

Additional context

No response

davidgilbertson avatar Apr 16 '25 05:04 davidgilbertson

Hey David!

You can pass a should_record callable into the Microphone that gets called regularly to determine if the recording is over. I have used that before to build push-to-talk use cases. It's not synchronous but I found it to be more versatile this way.

I don't have my laptop at hand to provide an example at the moment but hope that helps.

dkundel-openai avatar Apr 16 '25 08:04 dkundel-openai