Speech recognition streaming delay before start to recording on python
<p>I have a delay while trying to stream audio to speech recognition service.</p>

<p>I have two functions that handles this task, 
the first one that using alsaaudio and "yield" to return the data to the calling function.
and the second function that using requests that I passing to it the url the headers and the recording function.</p>

<p>the problem that I have a delay from the moment that I call to the requests function and until it starts to record the audio about half a second</p>

<p>here is a pseudo code:</p>

<pre><code>def listen():
    stream = audiostream()
    while user_speak:
        yield stream.read(chunksize)

def stream_speech():
    response = requests.post(url, data=listen(), headers, stream=true)

    if response.status_code == 200:
        print(response)
</code></pre>

<p>I think the problem is in the delay when the requests is opening the connection to the server and only then calls to the listen function.</p>

<p>so I thought maybe to open a thread that will record the chunks into an queue and the main thread will call the requests function with the queue. 
but I don't know how to implement this, maybe someone here can help me out.</p>

<p>Well, there are many lower-level APIs, including async ones like <a href="https://pymotw.com/2/asyncore/" rel="nofollow">asyncore</a> which allows you to interact without using threads at all.</p>

<p>I would simply increase the buffer size in alsaaudio with <a href="https://larsimmisch.github.io/pyalsaaudio/libalsaaudio.html#alsaaudio.PCM.setperiodsize" rel="nofollow">setperiodsize</a> to something like 0.5 seconds and use bigger buffer. Then you can start recording first and then wait for the connection while alsa will prepare the first buffer.</p>

python linux multithreading speech-recognition