transcribe.py
def read_audio(ws, timeout):
    """Read audio and sent it to the websocket port.

    This uses pyaudio to read from a device in chunks and send these
    over the websocket wire.

    """
    global RATE
    p = pyaudio.PyAudio()
    # NOTE(sdague): if you don't seem to be getting anything off of
    # this you might need to specify:
    #
    #    input_device_index=N,
    #
    # Where N is an int. You'll need to do a dump of your input
    # devices to figure out which one you want.
    RATE = int(p.get_default_input_device_info()['defaultSampleRate'])
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    print("* recording")
    rec = timeout or RECORD_SECONDS

    for i in range(0, int(RATE / CHUNK * rec)):
        data = stream.read(CHUNK)
        # print("Sending packet... %d" % i)
        # NOTE(sdague): we're sending raw binary in the stream, we
        # need to indicate that otherwise the stream service
        # interprets this as text control messages.
        ws.send(data, ABNF.OPCODE_BINARY)

    # Disconnect the audio stream
    stream.stop_stream()
    stream.close()
    print("* done recording")

    # In order to get a final response from STT we send a stop, this
    # will force a final=True return message.
    data = {"action": "stop"}
    ws.send(json.dumps(data).encode('utf8'))
    # ... which we need to wait for before we shutdown the websocket
    time.sleep(1)
    ws.close()

    # ... and kill the audio device
    p.terminate()
Read audio and sent it to the websocket port.

    This uses pyaudio to read from a device in chunks and send these
    over the websocket wire.

    
Read audio and sent it to the websocket port.
This uses pyaudio to read from a device in chunks and send these
over the websocket wire.
Read audio and sent it to the websocket port . This uses pyaudio to read from a device in chunks and send these over the websocket wire .
def read_audio(ws, timeout):
    global RATE
    p = pyaudio.PyAudio()
        input_device_index=N,
     Where N is an int. You'll need to do a dump of your input
     devices to figure out which one you want.
    RATE = int(p.get_default_input_device_info()['defaultSampleRate'])
    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)
    print("* recording")
    rec = timeout or RECORD_SECONDS
    for i in range(0, int(RATE / CHUNK * rec)):
        data = stream.read(CHUNK)
         print("Sending packet... %d" % i)
         NOTE(sdague): we're sending raw binary in the stream, we
         need to indicate that otherwise the stream service
         interprets this as text control messages.
        ws.send(data, ABNF.OPCODE_BINARY)
     Disconnect the audio stream
    stream.stop_stream()
    stream.close()
    print("* done recording")
     In order to get a final response from STT we send a stop, this
     will force a final=True return message.
    data = {"action": "stop"}
    ws.send(json.dumps(data).encode('utf8'))
     ... which we need to wait for before we shutdown the websocket
    time.sleep(1)
    ws.close()
     ... and kill the audio device
    p.terminate()
def read_audio ( ws , timeout ) : global RATE p = pyaudio . PyAudio ( ) RATE = int ( p . get_default_input_device_info ( ) [ 'defaultSampleRate' ] ) stream = p . open ( format = FORMAT , channels = CHANNELS , rate = RATE , input = True , frames_per_buffer = CHUNK ) print ( "* recording" ) rec = timeout or RECORD_SECONDS for i in range ( 0 , int ( RATE / CHUNK * rec ) ) : data = stream . read ( CHUNK ) ws . send ( data , ABNF . OPCODE_BINARY ) stream . stop_stream ( ) stream . close ( ) print ( "* done recording" ) data = { "action" : "stop" } ws . send ( json . dumps ( data ) . encode ( 'utf8' ) ) time . sleep ( 1 ) ws . close ( ) p . terminate ( )
Read audio and sent it to the websocket port.
Read audio and sent it to the websocket port .
"""Read audio and sent it to the websocket port.

    This uses pyaudio to read from a device in chunks and send these
    over the websocket wire.

    """ # NOTE(sdague): if you don't seem to be getting anything off of # this you might need to specify: # #    input_device_index=N, # # Where N is an int. You'll need to do a dump of your input # devices to figure out which one you want. # print("Sending packet... %d" % i) # NOTE(sdague): we're sending raw binary in the stream, we # need to indicate that otherwise the stream service # interprets this as text control messages. # Disconnect the audio stream # In order to get a final response from STT we send a stop, this # will force a final=True return message. # ... which we need to wait for before we shutdown the websocket # ... and kill the audio device
ws
timeout