Skip to content

Commit

Permalink
docs: modernize patterns in subset of samples (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and Ace Nassri committed Nov 17, 2022
1 parent 4165450 commit fc065d2
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 214 deletions.
1 change: 1 addition & 0 deletions speech/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
rules:
no-console: off
node/no-unsupported-features/node-builtins: off
73 changes: 16 additions & 57 deletions speech/infiniteStreaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@

'use strict';

// sample-metadata:
// title: Infinite Streaming
// description: Performs infinite streaming using the streamingRecognize operation with the Cloud Speech API.
// usage: node infiniteStreaming.js <encoding> <sampleRateHertz> <languageCode> <streamingLimit>

/**
* Note: Correct microphone settings required: check enclosed link, and make
* sure the following conditions are met:
Expand All @@ -45,11 +50,11 @@
* Maximum streaming limit should be 1/2 of SpeechAPI Streaming Limit.
*/

function infiniteStream(
encoding,
sampleRateHertz,
languageCode,
streamingLimit
function main(
encoding = 'LINEAR16',
sampleRateHertz = 16000,
languageCode = 'en-US',
streamingLimit = 290000
) {
// [START speech_transcribe_infinite_streaming]

Expand All @@ -60,8 +65,6 @@ function infiniteStream(

const chalk = require('chalk');
const {Writable} = require('stream');

// Node-Record-lpcm16
const recorder = require('node-record-lpcm16');

// Imports the Google Cloud client library
Expand Down Expand Up @@ -240,53 +243,9 @@ function infiniteStream(
// [END speech_transcribe_infinite_streaming]
}

require('yargs')
.demand(1)
.command(
'infiniteStream',
'infinitely streams audio input from microphone to speech API',
{},
opts =>
infiniteStream(
opts.encoding,
opts.sampleRateHertz,
opts.languageCode,
opts.streamingLimit
)
)
.options({
encoding: {
alias: 'e',
default: 'LINEAR16',
global: true,
requiresArg: true,
type: 'string',
},
sampleRateHertz: {
alias: 'r',
default: 16000,
global: true,
requiresArg: true,
type: 'number',
},
languageCode: {
alias: 'l',
default: 'en-US',
global: true,
requiresArg: true,
type: 'string',
},
streamingLimit: {
alias: 's',
default: 290000,
global: true,
requiresArg: true,
type: 'number',
},
})
.example('node $0 infiniteStream')
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/speech/docs')
.help()
.strict().argv;
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
8 changes: 6 additions & 2 deletions speech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"name": "nodejs-docs-samples-speech",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"author": "Google LLC",
"repository": "googleapis/nodejs-speech",
"engines": {
"node": ">=8"
"node": ">=10.17.0"
},
"files": [
"*.js",
"resources/"
],
"scripts": {
"test": "c8 mocha system-test --timeout 600000"
},
Expand Down
73 changes: 41 additions & 32 deletions speech/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,51 @@

'use strict';

// [START speech_quickstart]
async function main() {
function main() {
// [START speech_quickstart]
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
const fs = require('fs').promises;

// Creates a client
const client = new speech.SpeechClient();

// The name of the audio file to transcribe
const fileName = './resources/audio.raw';

// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');

// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};

// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
async function quickstart() {
// The name of the audio file to transcribe
const fileName = './resources/audio.raw';

// Reads a local audio file and converts it to base64
const file = await fs.readFile(fileName);
const audioBytes = file.toString('base64');

// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};

// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
}
quickstart();
// [END speech_quickstart]
}
main().catch(console.error);
// [END speech_quickstart]

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
133 changes: 52 additions & 81 deletions speech/recognize.v1p1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@

'use strict';

async function syncRecognizeWithMetaData(
// sample-metadata:
// title: Recognize speech with metadata
// description: Analyzes an audio stream, and detects speech along with metadata.
// usage: node recognize.v1p1beta1.js ./resources/commercial_mono.wav <encoding> <sampleRateHertz> <languageCode>

function main(
filename,
encoding,
sampleRateHertz,
languageCode
encoding = 'LINEAR16',
sampleRateHertz = 16000,
languageCode = 'en-US'
) {
// [START speech_transcribe_recognition_metadata_beta]
// Imports the Google Cloud client library for Beta API
Expand All @@ -40,87 +45,53 @@ async function syncRecognizeWithMetaData(
// Creates a client
const client = new speech.SpeechClient();

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';
async function syncRecognizeWithMetaData() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const recognitionMetadata = {
interactionType: 'DISCUSSION',
microphoneDistance: 'NEARFIELD',
recordingDeviceType: 'SMARTPHONE',
recordingDeviceName: 'Pixel 2 XL',
industryNaicsCodeOfAudio: 519190,
};
const recognitionMetadata = {
interactionType: 'DISCUSSION',
microphoneDistance: 'NEARFIELD',
recordingDeviceType: 'SMARTPHONE',
recordingDeviceName: 'Pixel 2 XL',
industryNaicsCodeOfAudio: 519190,
};

const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
metadata: recognitionMetadata,
};
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
metadata: recognitionMetadata,
};

const audio = {
content: fs.readFileSync(filename).toString('base64'),
};
const audio = {
content: fs.readFileSync(filename).toString('base64'),
};

const request = {
config: config,
audio: audio,
};
const request = {
config: config,
audio: audio,
};

// Detects speech in the audio file
const [response] = await client.recognize(request);
response.results.forEach(result => {
const alternative = result.alternatives[0];
console.log(alternative.transcript);
});
// [END speech_transcribe_recognition_metadata_beta]
// Detects speech in the audio file
const [response] = await client.recognize(request);
response.results.forEach(result => {
const alternative = result.alternatives[0];
console.log(alternative.transcript);
});
// [END speech_transcribe_recognition_metadata_beta]
}
syncRecognizeWithMetaData();
}

require('yargs')
.demand(1)
.command(
'sync-metadata <filename>',
'Detects speech in a local audio file with metadata.',
{},
opts =>
syncRecognizeWithMetaData(
opts.filename,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.options({
encoding: {
alias: 'e',
default: 'LINEAR16',
global: true,
requiresArg: true,
type: 'string',
},
sampleRateHertz: {
alias: 'r',
default: 16000,
global: true,
requiresArg: true,
type: 'number',
},
languageCode: {
alias: 'l',
default: 'en-US',
global: true,
requiresArg: true,
type: 'string',
},
})
.example('node $0 sync-metadata ./resources/commercial_mono.wav')
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/speech/docs')
.help()
.strict().argv;
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
5 changes: 0 additions & 5 deletions speech/system-test/.eslintrc.yml

This file was deleted.

35 changes: 0 additions & 35 deletions speech/system-test/MicrophoneStream.test.js

This file was deleted.

Loading

0 comments on commit fc065d2

Please sign in to comment.