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

201
src/audio.ts Normal file
View file

@ -0,0 +1,201 @@
function main(
encoding = 'LINEAR16',
sampleRateHertz = 16000,
languageCode = 'en-US',
streamingLimit = 290000
) {
// [START speech_transcribe_infinite_streaming]
// const encoding = 'LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'en-US';
// const streamingLimit = 10000; // ms - set to low number for demo purposes
const chalk = require('chalk');
const {Writable} = require('stream');
const recorder = require('node-record-lpcm16');
const fs = require('fs');
const writeStream = fs.createWriteStream('test.wav', { encoding: 'binary'});
// // Imports the Google Cloud client library
// // Currently, only v1p1beta1 contains result-end-time
// const speech = require('@google-cloud/speech').v1p1beta1;
// const client = new speech.SpeechClient();
// const config = {
// encoding: encoding,
// sampleRateHertz: sampleRateHertz,
// languageCode: languageCode,
// };
// const request = {
// config,
// interimResults: true,
// };
// let recognizeStream = null;
// let restartCounter = 0;
// let audioInput = [];
// let lastAudioInput = [];
// let resultEndTime = 0;
// let isFinalEndTime = 0;
// let finalRequestEndTime = 0;
// let newStream = true;
// let bridgingOffset = 0;
// let lastTranscriptWasFinal = false;
// function startStream() {
// // Clear current audioInput
// audioInput = [];
// // Initiate (Reinitiate) a recognize stream
// recognizeStream = client
// .streamingRecognize(request)
// .on('error', err => {
// if (err.code === 11) {
// // restartStream();
// } else {
// console.error('API request error ' + err);
// }
// })
// .on('data', speechCallback);
// // Restart stream when streamingLimit expires
// setTimeout(restartStream, streamingLimit);
// }
// const speechCallback = stream => {
// // Convert API result end time from seconds + nanoseconds to milliseconds
// resultEndTime =
// stream.results[0].resultEndTime.seconds * 1000 +
// Math.round(stream.results[0].resultEndTime.nanos / 1000000);
// // Calculate correct time based on offset from audio sent twice
// const correctedTime =
// resultEndTime - bridgingOffset + streamingLimit * restartCounter;
// process.stdout.clearLine();
// process.stdout.cursorTo(0);
// let stdoutText = '';
// if (stream.results[0] && stream.results[0].alternatives[0]) {
// stdoutText =
// correctedTime + ': ' + stream.results[0].alternatives[0].transcript;
// }
// if (stream.results[0].isFinal) {
// process.stdout.write(chalk.green(`${stdoutText}\n`));
// isFinalEndTime = resultEndTime;
// lastTranscriptWasFinal = true;
// } else {
// // Make sure transcript does not exceed console character length
// if (stdoutText.length > process.stdout.columns) {
// stdoutText =
// stdoutText.substring(0, process.stdout.columns - 4) + '...';
// }
// process.stdout.write(chalk.red(`${stdoutText}`));
// lastTranscriptWasFinal = false;
// }
// };
// const audioInputStreamTransform = new Writable({
// write(chunk, encoding, next) {
// if (newStream && lastAudioInput.length !== 0) {
// // Approximate math to calculate time of chunks
// const chunkTime = streamingLimit / lastAudioInput.length;
// if (chunkTime !== 0) {
// if (bridgingOffset < 0) {
// bridgingOffset = 0;
// }
// if (bridgingOffset > finalRequestEndTime) {
// bridgingOffset = finalRequestEndTime;
// }
// const chunksFromMS = Math.floor(
// (finalRequestEndTime - bridgingOffset) / chunkTime
// );
// bridgingOffset = Math.floor(
// (lastAudioInput.length - chunksFromMS) * chunkTime
// );
// for (let i = chunksFromMS; i < lastAudioInput.length; i++) {
// recognizeStream.write(lastAudioInput[i]);
// }
// }
// newStream = false;
// }
// audioInput.push(chunk);
// if (recognizeStream) {
// recognizeStream.write(chunk);
// }
// next();
// },
// final() {
// if (recognizeStream) {
// recognizeStream.end();
// }
// },
// });
// function restartStream() {
// if (recognizeStream) {
// recognizeStream.end();
// recognizeStream.removeListener('data', speechCallback);
// recognizeStream = null;
// }
// if (resultEndTime > 0) {
// finalRequestEndTime = isFinalEndTime;
// }
// resultEndTime = 0;
// lastAudioInput = [];
// lastAudioInput = audioInput;
// restartCounter++;
// if (!lastTranscriptWasFinal) {
// process.stdout.write('\n');
// }
// process.stdout.write(
// chalk.yellow(`${streamingLimit * restartCounter}: RESTARTING REQUEST\n`)
// );
// newStream = true;
// startStream();
// }
// Start recording and send the microphone input to the Speech API
recorder
.record({
sampleRateHertz: sampleRateHertz,
threshold: 0, // Silence threshold
silence: 1000,
keepSilence: true,
recordProgram: 'rec', // Try also "arecord" or "sox"
})
.stream()
.on('error', err => {
console.error('Audio recording error ' + err);
})
.pipe(audioInputStreamTransform);
console.log('');
console.log('Listening, press Ctrl+C to stop.');
console.log('');
console.log('End (ms) Transcript Results/Status');
console.log('=========================================================');
// startStream();
// [END speech_transcribe_infinite_streaming]
}
// process.on('unhandledRejection', err => {
// console.error(err.message);
// process.exitCode = 1;
// });
main(...process.argv.slice(2));

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 = "";
}
})
})