-
Notifications
You must be signed in to change notification settings - Fork 73
Break audio up into chunks before sending to google #51
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,10 @@ const SCREEN_PLAYING = ScreenOutConfig.ScreenMode.PLAYING; | |
| const SCREEN_OFF = ScreenOutConfig.ScreenMode.OFF; | ||
| const DEFAULT_SAMPLE_RATE_IN = 16000; | ||
| const DEFAULT_SAMPLE_RATE_OUT = 24000; | ||
| // The max number of bytes of audio that can be sent in a single chunk. | ||
| // Not documented anywhere by google, but sending more than this amount | ||
| // throws an INVALID_REQUEST error stating that audio_in is too long. | ||
| const MAX_FRAME_LENGTH = 31 * 1024; | ||
|
|
||
| let conversationState; | ||
| let volumePercent = 100; | ||
|
|
@@ -203,8 +207,16 @@ function Conversation(assistant, config) { | |
| // if audio tries to come in when we are sending text, bail out | ||
| if (sendingText) return; | ||
|
|
||
| const request = AssistRequest.create({ audioIn: data }); | ||
| conversation.write(request); | ||
| // Take the audio data and split it up into bite-sized chunks that google can digest | ||
| // and then send it along. Under ordinary circumstances, we should be capturing | ||
| // audio data fast enough to send in a single request so that the assistant can be | ||
| // highly responsive. | ||
| let chunks = []; | ||
| while(chunks.length < data.length / MAX_FRAME_LENGTH) { | ||
| chunks.push(data.slice(chunks.length * MAX_FRAME_LENGTH, Math.min(data.length, (chunks.length + 1) * MAX_FRAME_LENGTH))); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I've never done reviewing in github, so forgive my noobness) Can you break your Math.min into a const and give it a name explaining what it's doing? |
||
| } | ||
| chunks.forEach((chunk) => | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use the curly braces to wrap the for each, or put it all on 1 line? |
||
| conversation.write(AssistRequest.create({ audioIn: chunk }))); | ||
| }; | ||
|
|
||
| // end the conversation | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this just figured out by process of elimination? Love me the prime number :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly, yes. I'm very unfamiliar with the gRPC stuff so if you know a better way, let me know. It's susceptible to changes on their end which is obviously bad.