csukuangfj commited on
Commit
c1197c0
·
1 Parent(s): 5198ea0

update model

Browse files
Files changed (2) hide show
  1. app-asr.js +39 -26
  2. sherpa-onnx-wasm-main-asr.wasm +1 -1
app-asr.js CHANGED
@@ -15,7 +15,7 @@ let resultList = [];
15
  clearBtn.onclick = function() {
16
  resultList = [];
17
  textArea.value = getDisplayResult();
18
- textArea.scrollTop = textArea.scrollHeight; // auto scroll
19
  };
20
 
21
  function getDisplayResult() {
@@ -48,9 +48,21 @@ Module.locateFile = function(path, scriptDirectory = '') {
48
  Module.setStatus = function(status) {
49
  console.log(`status ${status}`);
50
  const statusElement = document.getElementById('status');
51
- if (status == "Running...") {
52
  status = 'Model downloaded. Initializing recongizer...'
53
  }
 
 
 
 
 
 
 
 
 
 
 
 
54
  statusElement.textContent = status;
55
  if (status === '') {
56
  statusElement.style.display = 'none';
@@ -80,11 +92,11 @@ let audioCtx;
80
  let mediaStream;
81
 
82
  let expectedSampleRate = 16000;
83
- let recordSampleRate; // the sampleRate of the microphone
84
- let recorder = null; // the microphone
85
- let leftchannel = []; // TODO: Use a single channel
86
 
87
- let recordingLength = 0; // number of samples so far
88
 
89
  let recognizer = null;
90
  let recognizer_stream = null;
@@ -93,11 +105,11 @@ if (navigator.mediaDevices.getUserMedia) {
93
  console.log('getUserMedia supported.');
94
 
95
  // see https://w3c.github.io/mediacapture-main/#dom-mediadevices-getusermedia
96
- const constraints = {audio : true};
97
 
98
  let onSuccess = function(stream) {
99
  if (!audioCtx) {
100
- audioCtx = new AudioContext({sampleRate : 16000});
101
  }
102
  console.log(audioCtx);
103
  recordSampleRate = audioCtx.sampleRate;
@@ -160,7 +172,7 @@ if (navigator.mediaDevices.getUserMedia) {
160
  }
161
 
162
  textArea.value = getDisplayResult();
163
- textArea.scrollTop = textArea.scrollHeight; // auto scroll
164
 
165
  let buf = new Int16Array(samples.length);
166
  for (var i = 0; i < samples.length; ++i) {
@@ -247,8 +259,9 @@ if (navigator.mediaDevices.getUserMedia) {
247
  };
248
  };
249
 
250
- let onError = function(
251
- err) { console.log('The following error occured: ' + err); };
 
252
 
253
  navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);
254
  } else {
@@ -281,22 +294,22 @@ function toWav(samples) {
281
 
282
  // http://soundfile.sapp.org/doc/WaveFormat/
283
  // F F I R
284
- view.setUint32(0, 0x46464952, true); // chunkID
285
- view.setUint32(4, 36 + samples.length * 2, true); // chunkSize
286
  // E V A W
287
- view.setUint32(8, 0x45564157, true); // format
288
- //
289
  // t m f
290
- view.setUint32(12, 0x20746d66, true); // subchunk1ID
291
- view.setUint32(16, 16, true); // subchunk1Size, 16 for PCM
292
- view.setUint32(20, 1, true); // audioFormat, 1 for PCM
293
- view.setUint16(22, 1, true); // numChannels: 1 channel
294
- view.setUint32(24, expectedSampleRate, true); // sampleRate
295
- view.setUint32(28, expectedSampleRate * 2, true); // byteRate
296
- view.setUint16(32, 2, true); // blockAlign
297
- view.setUint16(34, 16, true); // bitsPerSample
298
- view.setUint32(36, 0x61746164, true); // Subchunk2ID
299
- view.setUint32(40, samples.length * 2, true); // subchunk2Size
300
 
301
  let offset = 44;
302
  for (let i = 0; i < samples.length; ++i) {
@@ -304,7 +317,7 @@ function toWav(samples) {
304
  offset += 2;
305
  }
306
 
307
- return new Blob([ view ], {type : 'audio/wav'});
308
  }
309
 
310
  // this function is copied from
 
15
  clearBtn.onclick = function() {
16
  resultList = [];
17
  textArea.value = getDisplayResult();
18
+ textArea.scrollTop = textArea.scrollHeight; // auto scroll
19
  };
20
 
21
  function getDisplayResult() {
 
48
  Module.setStatus = function(status) {
49
  console.log(`status ${status}`);
50
  const statusElement = document.getElementById('status');
51
+ if (status == 'Running...') {
52
  status = 'Model downloaded. Initializing recongizer...'
53
  }
54
+
55
+ const downloadMatch = status.match(/Downloading data... \((\d+)\/(\d+)\)/);
56
+ if (downloadMatch) {
57
+ const downloaded = BigInt(downloadMatch[1]);
58
+ const total = BigInt(downloadMatch[2]);
59
+ const percent =
60
+ total === 0 ? 0.00 : Number((downloaded * 10000n) / total) / 100;
61
+ status = `Downloading data... ${percent.toFixed(2)}% (${downloadMatch[1]}/${
62
+ downloadMatch[2]})`;
63
+ console.log(`here ${status}`)
64
+ }
65
+
66
  statusElement.textContent = status;
67
  if (status === '') {
68
  statusElement.style.display = 'none';
 
92
  let mediaStream;
93
 
94
  let expectedSampleRate = 16000;
95
+ let recordSampleRate; // the sampleRate of the microphone
96
+ let recorder = null; // the microphone
97
+ let leftchannel = []; // TODO: Use a single channel
98
 
99
+ let recordingLength = 0; // number of samples so far
100
 
101
  let recognizer = null;
102
  let recognizer_stream = null;
 
105
  console.log('getUserMedia supported.');
106
 
107
  // see https://w3c.github.io/mediacapture-main/#dom-mediadevices-getusermedia
108
+ const constraints = {audio: true};
109
 
110
  let onSuccess = function(stream) {
111
  if (!audioCtx) {
112
+ audioCtx = new AudioContext({sampleRate: 16000});
113
  }
114
  console.log(audioCtx);
115
  recordSampleRate = audioCtx.sampleRate;
 
172
  }
173
 
174
  textArea.value = getDisplayResult();
175
+ textArea.scrollTop = textArea.scrollHeight; // auto scroll
176
 
177
  let buf = new Int16Array(samples.length);
178
  for (var i = 0; i < samples.length; ++i) {
 
259
  };
260
  };
261
 
262
+ let onError = function(err) {
263
+ console.log('The following error occured: ' + err);
264
+ };
265
 
266
  navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);
267
  } else {
 
294
 
295
  // http://soundfile.sapp.org/doc/WaveFormat/
296
  // F F I R
297
+ view.setUint32(0, 0x46464952, true); // chunkID
298
+ view.setUint32(4, 36 + samples.length * 2, true); // chunkSize
299
  // E V A W
300
+ view.setUint32(8, 0x45564157, true); // format
301
+ //
302
  // t m f
303
+ view.setUint32(12, 0x20746d66, true); // subchunk1ID
304
+ view.setUint32(16, 16, true); // subchunk1Size, 16 for PCM
305
+ view.setUint32(20, 1, true); // audioFormat, 1 for PCM
306
+ view.setUint16(22, 1, true); // numChannels: 1 channel
307
+ view.setUint32(24, expectedSampleRate, true); // sampleRate
308
+ view.setUint32(28, expectedSampleRate * 2, true); // byteRate
309
+ view.setUint16(32, 2, true); // blockAlign
310
+ view.setUint16(34, 16, true); // bitsPerSample
311
+ view.setUint32(36, 0x61746164, true); // Subchunk2ID
312
+ view.setUint32(40, samples.length * 2, true); // subchunk2Size
313
 
314
  let offset = 44;
315
  for (let i = 0; i < samples.length; ++i) {
 
317
  offset += 2;
318
  }
319
 
320
+ return new Blob([view], {type: 'audio/wav'});
321
  }
322
 
323
  // this function is copied from
sherpa-onnx-wasm-main-asr.wasm CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:2d80cd3ff901b04a4cf85dba75b2de85d180f522dc9a31819214647c92b8fdce
3
  size 11545586
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aba480b3c6a213b252a36040dea28c8b8746bda51ca21b5871d659382e4beeb4
3
  size 11545586