google-cloud-speech-webaudio
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "google-cloud-speech-webaudio", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Google Cloud speech recognition and synthesis integrated with WebAudio, fully functional in the browser.", | ||
@@ -10,2 +10,7 @@ "author": "Andrei Gheorghe <andreig@gmail.com>", | ||
}, | ||
"keywords": [ | ||
"google", | ||
"speech", | ||
"webaudio" | ||
], | ||
"homepage": "https://github.com/idevelop/google-cloud-speech-webaudio", | ||
@@ -12,0 +17,0 @@ "repository": { |
const base64 = require('base64-js'); | ||
async function speechToText(audioBuffer, sampleRate, apiKey) { | ||
// https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/recognize | ||
async function speechToText(audioBuffer, sampleRate, languageCode, apiKey) { | ||
// Reference: https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/recognize | ||
@@ -17,3 +17,3 @@ const response = await fetch( | ||
sampleRateHertz: sampleRate, | ||
languageCode: 'en-US', | ||
languageCode: languageCode, | ||
}, | ||
@@ -37,3 +37,3 @@ audio: { | ||
async function textToSpeech(text, apiKey) { | ||
// https://cloud.google.com/text-to-speech/docs/reference/rest/v1/text/synthesize | ||
// Reference: https://cloud.google.com/text-to-speech/docs/reference/rest/v1/text/synthesize | ||
@@ -40,0 +40,0 @@ const response = await fetch( |
@@ -5,4 +5,4 @@ const { speechToText } = require('./rest-api'); | ||
// by default, browsers will return a 48000 sample rate audio context, | ||
// which leads to an HTTP request size of 1.3 MB for 10 seconds of audio. | ||
// by default, browsers will create a 48000 sample rate audio context, | ||
// which leads to an HTTP request size of ~1.3 MB for 10 seconds of audio. | ||
// setting the sample rate to 16000 lowers it to a more reasonable 420 KB. | ||
@@ -57,3 +57,3 @@ // the size could be further reduced through client-size gzip, TBD if it's worth it. | ||
// with a sample rate of 16k. if that happens, recreate the audio context without | ||
// any explicit sample rate | ||
// an explicit sample rate. | ||
this.audioContext = new AudioContext(); | ||
@@ -84,3 +84,3 @@ this.microphone = this.audioContext.createMediaStreamSource(stream); | ||
disconnectMicrophoneAndProcessor() { | ||
if (this.microphone != null) { | ||
if (this.microphone) { | ||
this.microphone.disconnect(); | ||
@@ -91,23 +91,30 @@ this.microphone.mediaStream.getTracks().forEach(track => track.stop()); | ||
this.processor.disconnect(); | ||
if (this.processor) { | ||
this.processor.disconnect(); | ||
} | ||
} | ||
async startListening() { | ||
if (this.starting) { | ||
// already starting | ||
return; | ||
} | ||
this.starting = true; | ||
this.outputBuffer = new ArrayBuffer(0); | ||
await this.connectMicrophoneAndProcessor(); | ||
if (!this.starting) { | ||
if (this.cancelStart) { | ||
this.disconnectMicrophoneAndProcessor(); | ||
return; | ||
this.cancelStart = false; | ||
} | ||
this.outputBuffer = new ArrayBuffer(0); | ||
this.starting = false; | ||
} | ||
async stopListening() { | ||
async stopListening(languageCode = 'en-US') { | ||
if (this.starting) { | ||
// use "starting" flag to resolve race condition between start and stop | ||
this.starting = false; | ||
// use "cancelStart" flag to resolve race condition between start and stop | ||
this.cancelStart = true; | ||
} | ||
@@ -121,5 +128,6 @@ | ||
this.audioContext.sampleRate, | ||
languageCode, | ||
this.apiKey | ||
); | ||
this.outputBuffer = null; | ||
this.outputBuffer = new ArrayBuffer(0); | ||
return apiResult; | ||
@@ -126,0 +134,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
9367
7
192
1
50