Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Speech api: Fill audio buffer in a separate thread. #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2016
Merged

Conversation

jerjou
Copy link
Contributor

@jerjou jerjou commented Sep 15, 2016

This is to avoid timing issues where the request thread doesn't poll the
generator fast enough to consume all the incoming audio data from the input
device. In that case, the audio device buffer overflows, leading to lost data
and exceptions and other nastiness.

Address #515

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Sep 15, 2016
@theacodes
Copy link
Contributor

This seems to add a lot of complexity. Is this an edge case or a common case?

@jerjou
Copy link
Contributor Author

jerjou commented Sep 15, 2016

Mm... so I think the way I was doing it before was actually incorrect. Here's my current understanding: before, there were two threads - one handling sending requests, and one handling receiving responses. However, there are actually three asynchronous things happening - requests, responses, and input from the microphone. Two threads works as long as your request thread consumes audio faster than realtime, because input devices have a buffer somewhere. But if your request thread stalls - because of network latency, or resource constraints, or what have you - then the audio buffer can overflow.

So I think this is actually the Right Way to do it. If you have any ideas on how to express it more simply, I'm all ears :-)

@jerjou
Copy link
Contributor Author

jerjou commented Sep 15, 2016

Actually, I have an idea... gimme a sec.

@theacodes
Copy link
Contributor

Okay - let me know.

@puneith
Copy link
Contributor

puneith commented Sep 15, 2016

@jonparrott I am afraid this may not be edge case. I think @jerjou is heading in the right direction, but I am thinking that with the same logic this code should be simplified by using futures.

@jerjou
Copy link
Contributor Author

jerjou commented Sep 15, 2016

Okay - modified to make the three threads more explicit.
@puneith - can you be more specific? How can we use futures to simplify things?

@puneith
Copy link
Contributor

puneith commented Sep 15, 2016

@theacodes
Copy link
Contributor

This looks good to me. We can switch to concurrent in another PR if you two agree to do so.

@theacodes
Copy link
Contributor

@dpebot merge when travis passes

@dpebot
Copy link
Collaborator

dpebot commented Sep 15, 2016

Okay! I'll merge when all statuses are green.

@dpebot dpebot added the automerge Merge the pull request once unit tests and other checks pass. label Sep 15, 2016
@jerjou
Copy link
Contributor Author

jerjou commented Sep 15, 2016

@puneith - can you be more specific? How can we use Executors to simplify things?

I think a major refactor is outside the scope of this PR. If you'd like to refactor to use Executors, please submit a separate PR, which will also help the specificity.

@puneith
Copy link
Contributor

puneith commented Sep 15, 2016

Sure, you can merge this and I will do that as a separate PR.

@puneith
Copy link
Contributor

puneith commented Sep 17, 2016

@jerjou Other than travis this is merge eligible?

@jerjou jerjou removed the automerge Merge the pull request once unit tests and other checks pass. label Sep 17, 2016
@jerjou jerjou force-pushed the threaded-speech branch 4 times, most recently from 35e045d to 0051ea9 Compare September 17, 2016 03:39
@jerjou
Copy link
Contributor Author

jerjou commented Sep 17, 2016

Okay, yeah. Now it's ready.
I had an idea when I was on the way home on how to make things better / more correct :-)

@jerjou
Copy link
Contributor Author

jerjou commented Sep 17, 2016

FYI the changes I made were somewhat non-trivial, so I'd appreciate another look.

@@ -126,25 +174,34 @@ def listen_print_loop(recognize_stream):

# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if any(re.search(r'\b(exit|quit)\b', alt.transcript)
if any(re.search(r'\b(exit|quit)\b', alt.transcript, re.I)
for result in resp.results
for alt in result.alternatives):
print('Exiting..')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The program doesn't exit even when you say exit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should - are you seeing that it doesn't? It should break out of the listen-print loop and return from the function, which should exit all the context handlers in the calling function. What behavior are you seeing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prints "Exiting" but hangs there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - probably one of the threads isn't exiting correctly -_-;
Just to make sure - you're sync'ed to the latest changes, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to explicitly cancel the request stream. See if that helps?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok that works!

This is to avoid timing issues where the request thread doesn't poll the
generator fast enough to consume all the incoming audio data from the input
device. In that case, the audio device buffer overflows, leading to lost data
and exceptions and other nastiness.

Address #515
@jerjou jerjou merged commit 5fca324 into master Sep 19, 2016
@jerjou jerjou deleted the threaded-speech branch September 19, 2016 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants