从微型音箱和扬声器捕捉声音

2024-06-16 08:49:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我们正在使用amazon transcribe将speach转换为文本,但我们需要从麦克风和扬声器捕获声音。你认为这可以通过sounddevice来实现,还是我们应该使用其他工具

amazon-transcribe-streaming-sdk

  • 话筒功能
async def mic_stream():
    # This function wraps the raw input stream from the microphone forwarding
    # the blocks to an asyncio.Queue.
    loop = asyncio.get_event_loop()
    input_queue = asyncio.Queue()

    def callback(indata, outdata, frame_count, time_info, status):
        '''
        if status:
            print(status)
        '''
        #indata[:] = outdata
        loop.call_soon_threadsafe(input_queue.put_nowait, (bytes(indata), status))

    # Be sure to use the correct parameters for the audio stream that matches
    # the audio formats described for the source language you'll be using:
    # https://docs.aws.amazon.com/transcribe/latest/dg/streaming.html
    stream = sounddevice.RawStream(
        #device=3,
        channels=1,
        samplerate=16000,
        callback=callback,
        blocksize=1024 * 2,
        dtype="int16",
    )
    # Initiate the audio stream and asynchronously yield the audio chunks
    # as they become available.
    with stream:
        while True:
            indata, status = await input_queue.get()
            yield indata, status


Tags: theloopasyncioamazoninputstreamqueuedef