Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

google-cloud-speech-webaudio

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google-cloud-speech-webaudio - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

README.md

7

package.json
{
"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": {

8

src/rest-api.js
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 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc