@@ -12,6 +12,7 @@ const eventStreamMarshaller = new marshaller.EventStreamMarshaller(util_utf8_nod
12
12
let languageCode ;
13
13
let region ;
14
14
let sampleRate ;
15
+ let inputSampleRate ;
15
16
let transcription = "" ;
16
17
let socket ;
17
18
let micStream ;
@@ -51,6 +52,11 @@ $('#start-button').click(function () {
51
52
let streamAudioToWebSocket = function ( userMediaStream ) {
52
53
//let's get the mic input from the browser, via the microphone-stream module
53
54
micStream = new mic ( ) ;
55
+
56
+ micStream . on ( "format" , function ( data ) {
57
+ inputSampleRate = data . sampleRate ;
58
+ } ) ;
59
+
54
60
micStream . setStream ( userMediaStream ) ;
55
61
56
62
// Pre-signed URLs are a way to authenticate a request (or WebSocket connection, in this case)
@@ -61,6 +67,8 @@ let streamAudioToWebSocket = function (userMediaStream) {
61
67
socket = new WebSocket ( url ) ;
62
68
socket . binaryType = "arraybuffer" ;
63
69
70
+ let sampleRate = 0 ;
71
+
64
72
// when we get audio data from the mic, send it to the WebSocket if possible
65
73
socket . onopen = function ( ) {
66
74
micStream . on ( 'data' , function ( rawAudioChunk ) {
@@ -185,7 +193,7 @@ function convertAudioToBinaryMessage(audioChunk) {
185
193
return ;
186
194
187
195
// downsample and convert the raw audio bytes to PCM
188
- let downsampledBuffer = audioUtils . downsampleBuffer ( raw , sampleRate ) ;
196
+ let downsampledBuffer = audioUtils . downsampleBuffer ( raw , inputSampleRate , sampleRate ) ;
189
197
let pcmEncodedBuffer = audioUtils . pcmEncode ( downsampledBuffer ) ;
190
198
191
199
// add the right JSON headers and structure to the message
0 commit comments