Use model sample rate in examples

This commit is contained in:
Reuben Morais 2019-10-10 22:04:33 +02:00
parent 0be2787e4e
commit 5cb15ca6ed
2 changed files with 9 additions and 10 deletions

View File

@ -202,15 +202,13 @@ namespace DeepSpeechWPF
{ {
_audioCapture.Device = _audioCaptureDevices[cbxAudioInputs.SelectedIndex]; _audioCapture.Device = _audioCaptureDevices[cbxAudioInputs.SelectedIndex];
} }
InitilizeAudioCapture(); InitializeAudioCapture(_sttClient.GetModelSampleRate());
} }
/// <summary> /// <summary>
/// Initializes the recorder and setup the native stream. /// Initializes the recorder and setup the native stream.
/// </summary> /// </summary>
private void InitilizeAudioCapture() private void InitializeAudioCapture(int desiredSampleRate)
{ {
_audioCapture.Initialize(); _audioCapture.Initialize();
_audioCapture.DataAvailable += _capture_DataAvailable; _audioCapture.DataAvailable += _capture_DataAvailable;
@ -218,7 +216,7 @@ namespace DeepSpeechWPF
//create a source, that converts the data provided by the //create a source, that converts the data provided by the
//soundInSource to required by the deepspeech model //soundInSource to required by the deepspeech model
_convertedSource = _soundInSource _convertedSource = _soundInSource
.ChangeSampleRate(16000) // sample rate .ChangeSampleRate(desiredSampleRate) // sample rate
.ToSampleSource() .ToSampleSource()
.ToWaveSource(16); //bits per sample .ToWaveSource(16); //bits per sample

View File

@ -11,6 +11,8 @@ let alphabetPath = './models/alphabet.txt';
let model = new DeepSpeech.Model(modelPath, alphabetPath, BEAM_WIDTH); let model = new DeepSpeech.Model(modelPath, alphabetPath, BEAM_WIDTH);
let desiredSampleRate = model.sampleRate();
const LM_ALPHA = 0.75; const LM_ALPHA = 0.75;
const LM_BETA = 1.85; const LM_BETA = 1.85;
let lmPath = './models/lm.binary'; let lmPath = './models/lm.binary';
@ -28,8 +30,8 @@ if (!Fs.existsSync(audioFile)) {
const buffer = Fs.readFileSync(audioFile); const buffer = Fs.readFileSync(audioFile);
const result = Wav.decode(buffer); const result = Wav.decode(buffer);
if (result.sampleRate < 16000) { if (result.sampleRate < desiredSampleRate) {
console.error('Warning: original sample rate (' + result.sampleRate + ') is lower than 16kHz. Up-sampling might produce erratic speech recognition.'); console.error('Warning: original sample rate (' + result.sampleRate + ') is lower than ' + desiredSampleRate + 'Hz. Up-sampling might produce erratic speech recognition.');
} }
function bufferToStream(buffer) { function bufferToStream(buffer) {
@ -47,7 +49,7 @@ pipe(Sox({
}, },
output: { output: {
bits: 16, bits: 16,
rate: 16000, rate: desiredSampleRate,
channels: 1, channels: 1,
encoding: 'signed-integer', encoding: 'signed-integer',
endian: 'little', endian: 'little',
@ -58,10 +60,9 @@ pipe(Sox({
pipe(audioStream); pipe(audioStream);
audioStream.on('finish', () => { audioStream.on('finish', () => {
let audioBuffer = audioStream.toBuffer(); let audioBuffer = audioStream.toBuffer();
const audioLength = (audioBuffer.length / 2) * ( 1 / 16000); const audioLength = (audioBuffer.length / 2) * (1 / desiredSampleRate);
console.log('audio length', audioLength); console.log('audio length', audioLength);
let result = model.stt(audioBuffer.slice(0, audioBuffer.length / 2)); let result = model.stt(audioBuffer.slice(0, audioBuffer.length / 2));