]> Repos - mime-chat/commitdiff
Merge remote-tracking branch 'origin/main'
authorriqo <hernandeze2@xavier.edu>
Fri, 5 Feb 2021 22:47:03 +0000 (16:47 -0600)
committerriqo <hernandeze2@xavier.edu>
Fri, 5 Feb 2021 22:47:03 +0000 (16:47 -0600)
1  2 
testAudio.js
tsconfig.json

diff --cc testAudio.js
index de50527c1da7c9bbd2fbbbac041f7a9dd1239f61,815964a39623056236d35fceb919d83c7485106a..a83ef639f20aff60eb17ea7580bcaa30eeffd9cd
@@@ -7,96 -9,79 +9,90 @@@ const {Writable} = require('stream')
  // Creates a client
  const client = new speech.SpeechClient();
  
--// connect to test server
--// const ws = new WebSocket("ws://localhost:8080");
- // Creates a client const client = new speech.SpeechClient();
--
 -// google cloud speech to text settings
  const encoding = 'LINEAR16';
 -const sampleRateHertz = 44100;
 +const sampleRateHertz = 16000;
  const languageCode = 'en-US';
  
 -const audioChunks = [];
 -
 -const audioInputStreamTransform = new Writable({
 -  write(chunk, encoding, next) {
 -    console.log(chunk);
 -    audioChunks.push(chunk);
 -    next();
 -  },
 -
 -  final() {
 -    console.log(audioChunks);
 -  },
 -});
 -
  const config = {
    encoding: encoding,
    sampleRateHertz: sampleRateHertz,
    languageCode: languageCode,
  };
  
 -const request = {
 -  config,
 -  interimResults: true,
 -};
 -
 -const speechCallback = (data) => {
 -  console.log(
 -      `Transcription: ${data.results[0].alternatives[0].transcript}`
 -    );
 +/**
 + * Note that transcription is limited to 60 seconds audio.
 + * Use a GCS file for audio longer than 1 minute.
 + */
 +let audio;
 +
 +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}`);
  }
  
- const audioChunks = [];
 -// const recognizeStream = client
 -// .streamingRecognize(request)
 -// .on('error', err => {
 -//   if (err.code === 11) {
 -//     // restartStream();
 -//   } else {
 -//     console.error('API request error ' + err);
 -//   }
 -// })
 -// .on('data', speechCallback);
--
 +let audioInput = [];
  // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
- let ia = new portAudio.AudioIO({
 -const recorderOptions = {
 -  channelCount: 1,
 -  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
 -}
 -
 -const ai = new portAudio.AudioIO({
 -  inOptions: recorderOptions
++const ia = new portAudio.AudioIO({
 +  inOptions: {
 +    channelCount: 1,
 +    sampleFormat: 16,
 +    sampleRate: 16000,
 +    deviceId: -1,
 +    closeOnError: false,
 +  }
 +});
 +ia.setEncoding('base64');
 +ia.start();
 +ia.on('error', (e) => {
 +    console.log('error recording audio', + e);
 +});
 +ia.on('data', (chunk) => {
 +  audioInput.push(chunk);
 +  console.log('Got %d characters of string data:', chunk.length);
  });
  
 -// manipulate individual chunks as they're available
 -// const audioData = [];
 -// ai.on('data', (d) => {
 -//   audioData.push(d.toString('binary'))
 -// })
 +async function processAudio() {
 +    audio = '';
 +    audioInput.forEach(s => {
 +        audio += s;
 +    })
 +    const buffer = Buffer.from(audio, 'base64');
 +    audioInput = [];
 +    await transcribeSpeech({
 +        content: buffer
 +    });
 +}
 +
 +setTimeout(async () => {
 +    ia.pause();
 +    await processAudio();
 +    // ia.resume();
 +}, 3000);
  
 -ai.pipe(audioInputStreamTransform)
 -ai.start();
  setTimeout(() => {
 -  ai.quit();
 -}, 2000)
 +    ia.resume();
 +}, 4000)
  
 +setTimeout(async () => {
 +    ia.pause();
 +    await processAudio();
 +    // ia.resume();
 +}, 8000);
  
 +setTimeout(() => {
 +    ia.pause();
 +}, 9000)
diff --cc tsconfig.json
Simple merge