Joshua Lochner commited on
Commit
0c54572
·
1 Parent(s): f7f9224

Handle stereo audio correctly

Browse files
Files changed (1) hide show
  1. src/hooks/useTranscriber.ts +18 -1
src/hooks/useTranscriber.ts CHANGED
@@ -147,8 +147,25 @@ export function useTranscriber(): Transcriber {
147
  if (audioData) {
148
  setTranscript(undefined);
149
  setIsBusy(true);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  webWorker.postMessage({
151
- audio: audioData.getChannelData(0),
152
  model,
153
  multilingual,
154
  quantized,
 
147
  if (audioData) {
148
  setTranscript(undefined);
149
  setIsBusy(true);
150
+
151
+ let audio;
152
+ if (audioData.numberOfChannels === 2) {
153
+ const SCALING_FACTOR = Math.sqrt(2);
154
+
155
+ let left = audioData.getChannelData(0);
156
+ let right = audioData.getChannelData(1);
157
+
158
+ audio = new Float32Array(left.length);
159
+ for (let i = 0; i < audioData.length; ++i) {
160
+ audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2;
161
+ }
162
+ } else {
163
+ // If the audio is not stereo, we can just use the first channel:
164
+ audio = audioData.getChannelData(0);
165
+ }
166
+
167
  webWorker.postMessage({
168
+ audio,
169
  model,
170
  multilingual,
171
  quantized,