add audio worklet
This commit is contained in:
parent
21687f4b7f
commit
5872d245f0
15 changed files with 347 additions and 117 deletions
107
tests/audio.js
107
tests/audio.js
|
|
@ -1,84 +1,38 @@
|
|||
|
||||
const speech = require('@google-cloud/speech');
|
||||
// const speech = require('@google-cloud/speech');
|
||||
const portAudio = require('naudiodon');
|
||||
// const rs = fs.createReadStream('rawAudio.wav');
|
||||
|
||||
// Creates a client
|
||||
const client = new speech.SpeechClient();
|
||||
|
||||
const encoding = 'LINEAR16';
|
||||
const sampleRateHertz = 16000;
|
||||
const languageCode = 'en-US';
|
||||
|
||||
const audioContainer = {
|
||||
input: '',
|
||||
buffers: []
|
||||
}
|
||||
|
||||
const config = {
|
||||
encoding: encoding,
|
||||
sampleRateHertz: sampleRateHertz,
|
||||
languageCode: languageCode,
|
||||
};
|
||||
|
||||
/**
|
||||
* Note that transcription is limited to 60 seconds audio.
|
||||
* Use a GCS file for audio longer than 1 minute.
|
||||
*/
|
||||
async function transcribeSpeech (audio) {
|
||||
const request = {
|
||||
config: config,
|
||||
audio: audio,
|
||||
};
|
||||
|
||||
// Detects speech in the audio file. This creates a recognition job that you
|
||||
// can wait for now, or get its result later.
|
||||
const [operation] = await client.longRunningRecognize(request);
|
||||
|
||||
// Get a Promise representation of the final result of the job
|
||||
const [response] = await operation.promise();
|
||||
|
||||
const transcription = response.results
|
||||
.map(result => result.alternatives[0].transcript)
|
||||
.join('\n');
|
||||
console.log(`Transcription: ${transcription}`);
|
||||
}
|
||||
|
||||
let record = true;
|
||||
|
||||
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
|
||||
const ia = new portAudio.AudioIO({
|
||||
const aio = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
channelCount: 1,
|
||||
sampleFormat: 16,
|
||||
sampleRate: 16000,
|
||||
deviceId: -1,
|
||||
closeOnError: false,
|
||||
channelCount: 2,
|
||||
sampleFormat: portAudio.SampleFormat16Bit,
|
||||
sampleRate: 44100,
|
||||
deviceId: -1 // Use -1 or omit the deviceId to select the default device
|
||||
},
|
||||
});
|
||||
ia.setEncoding('base64');
|
||||
ia.start();
|
||||
ia.on('data', (chunk) => {
|
||||
if (record) {
|
||||
console.log('recording data');
|
||||
audioContainer.input += chunk;
|
||||
} else {
|
||||
if (audioContainer.input.length) audioContainer.input = "";
|
||||
}
|
||||
outOptions: {
|
||||
channelCount: 2,
|
||||
sampleFormat: portAudio.SampleFormat16Bit,
|
||||
sampleRate: 44100,
|
||||
deviceId: -1 // Use -1 or omit the deviceId to select the default device
|
||||
}
|
||||
});
|
||||
|
||||
const ao = new portAudio.AudioIO({
|
||||
outOptions: {
|
||||
sampleFormat: 16,
|
||||
channelCount: 1,
|
||||
sampleRate: 16000,
|
||||
deviceId: -1,
|
||||
closeOnError: false,
|
||||
}
|
||||
});
|
||||
ao.start();
|
||||
|
||||
let counter = 0;
|
||||
aio.start()
|
||||
aio.read()
|
||||
aio.on('data', buf => console.log(buf.timestamp));
|
||||
|
||||
const counter = 0;
|
||||
const tests = [];
|
||||
|
||||
function testCallback() {
|
||||
|
|
@ -143,13 +97,6 @@ function bufSplit(input){
|
|||
}
|
||||
|
||||
async function test() {
|
||||
transcribeSpeech({
|
||||
content: Buffer.from(audioContainer.input, 'base64')
|
||||
});
|
||||
tests.push(audioContainer.input)
|
||||
counter++;
|
||||
console.log('audio string length:', audioContainer.input.length)
|
||||
console.log('buffers written: ', audioContainer.buffers.length)
|
||||
}
|
||||
|
||||
setTimeout(async () => {
|
||||
|
|
@ -157,26 +104,6 @@ setTimeout(async () => {
|
|||
test()
|
||||
}, 4000);
|
||||
|
||||
// setTimeout(() => {
|
||||
// record = true;
|
||||
// }, 6000)
|
||||
//
|
||||
// setTimeout(async () => {
|
||||
// record = false;
|
||||
// test();
|
||||
// }, 9000);
|
||||
//
|
||||
// setTimeout(() => {
|
||||
// record = true;
|
||||
// }, 11000)
|
||||
//
|
||||
// setTimeout(async () => {
|
||||
// record = false;
|
||||
// test();
|
||||
// }, 14000);
|
||||
//
|
||||
setTimeout(async () => {
|
||||
ia.quit();
|
||||
testCallback()
|
||||
return;
|
||||
}, 6000);
|
||||
|
|
|
|||
38
tests/transcribe.js
Normal file
38
tests/transcribe.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
const speech = require('@google-cloud/speech');
|
||||
|
||||
// Creates a client
|
||||
const client = new speech.SpeechClient();
|
||||
|
||||
const encoding = 'LINEAR16';
|
||||
const sampleRateHertz = 16000;
|
||||
const languageCode = 'en-US';
|
||||
|
||||
const config = {
|
||||
encoding: encoding,
|
||||
sampleRateHertz: sampleRateHertz,
|
||||
languageCode: languageCode,
|
||||
};
|
||||
|
||||
/**
|
||||
* Note that transcription is limited to 60 seconds audio.
|
||||
* Use a GCS file for audio longer than 1 minute.
|
||||
*/
|
||||
async function transcribeSpeech (audio) {
|
||||
const request = {
|
||||
config: config,
|
||||
audio: audio,
|
||||
};
|
||||
|
||||
// Detects speech in the audio file. This creates a recognition job that you
|
||||
// can wait for now, or get its result later.
|
||||
const [operation] = await client.longRunningRecognize(request);
|
||||
|
||||
// Get a Promise representation of the final result of the job
|
||||
const [response] = await operation.promise();
|
||||
|
||||
const transcription = response.results
|
||||
.map(result => result.alternatives[0].transcript)
|
||||
.join('\n');
|
||||
console.log(`Transcription: ${transcription}`);
|
||||
}
|
||||
Loading…
Reference in a new issue