add audio test python

This commit is contained in:
riqo 2021-02-01 15:31:38 -06:00
commit ead59fc9f2
8 changed files with 809 additions and 42 deletions

View file

@ -8,12 +8,46 @@ import * as path from "path";
const fs = require('fs');
import WebSocket from "ws";
const portAudio = require('naudiodon');
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
};
const request = {
config,
interimResults: true,
};
const speechCallback = (d: any) => {
console.log(d)
}
const recognizeStream = client
.streamingRecognize(request)
.on('error', err => {
if (err.code === 11) {
// restartStream();
} else {
console.error('API request error ' + err);
}
})
.on('data', speechCallback);
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
const audioOptions = {
channelCount: 2,
sampleFormat: portAudio.SampleFormat16Bit,
sampleRate: 44100,
sampleFormat: portAudio.sampleFormat16Bit,
sampleRate: 16000,
deviceId: -1, // Use -1 or omit the deviceId to select the default device
closeOnError: false // Close the stream if an audio error is detected, if set false then just log the error
}
@ -21,17 +55,13 @@ const audioOptions = {
const ai = new portAudio.AudioIO({
inOptions: audioOptions
});
let audioData = "";
ai.on('data', (buf: Buffer) => {
const base64Data = buf.toString('base64');
audioData += base64Data;
});
ai.pipe(recognizeStream);
ai.start();
ai.pause();
const filename = "rawAudio.wav";
// const filename = "rawAudio.wav";
// Create a write stream to write out to a raw audio file
const writeStream = fs.createWriteStream(filename);
// const writeStream = fs.createWriteStream(filename, { encoding: 'binary'});
// ai.pipe(writeStream);
// Keep a global reference of the window object, if you don't, the window will
@ -96,19 +126,19 @@ function createWindow() {
ipcMain.on('update-recorder', (Event: any, record: boolean) => {
if (record) {
ai.resume();
// ai.start();
} else {
ai.pause();
// ai.quit();
const audio = {
content: fs.readFileSync(filename).toString('base64'),
}
const message = {
text: "",
audio: audioData
}
ws.send(JSON.stringify(message))
audioData = "";
// const audio = {
// content: fs.readFileSync(filename).toString('base64'),
// }
// const message = {
// text: "",
// audio: audioData
// }
// ws.send(JSON.stringify(message))
// audioData = "";
}
})
})