Skip to content

Commit fc065d2

Browse files
JustinBeckwithAce Nassri
authored andcommitted
docs: modernize patterns in subset of samples (#660)
1 parent 4165450 commit fc065d2

File tree

9 files changed

+123
-214
lines changed

9 files changed

+123
-214
lines changed

speech/.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
---
22
rules:
33
no-console: off
4+
node/no-unsupported-features/node-builtins: off

speech/infiniteStreaming.js

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131

3232
'use strict';
3333

34+
// sample-metadata:
35+
// title: Infinite Streaming
36+
// description: Performs infinite streaming using the streamingRecognize operation with the Cloud Speech API.
37+
// usage: node infiniteStreaming.js <encoding> <sampleRateHertz> <languageCode> <streamingLimit>
38+
3439
/**
3540
* Note: Correct microphone settings required: check enclosed link, and make
3641
* sure the following conditions are met:
@@ -45,11 +50,11 @@
4550
* Maximum streaming limit should be 1/2 of SpeechAPI Streaming Limit.
4651
*/
4752

48-
function infiniteStream(
49-
encoding,
50-
sampleRateHertz,
51-
languageCode,
52-
streamingLimit
53+
function main(
54+
encoding = 'LINEAR16',
55+
sampleRateHertz = 16000,
56+
languageCode = 'en-US',
57+
streamingLimit = 290000
5358
) {
5459
// [START speech_transcribe_infinite_streaming]
5560

@@ -60,8 +65,6 @@ function infiniteStream(
6065

6166
const chalk = require('chalk');
6267
const {Writable} = require('stream');
63-
64-
// Node-Record-lpcm16
6568
const recorder = require('node-record-lpcm16');
6669

6770
// Imports the Google Cloud client library
@@ -240,53 +243,9 @@ function infiniteStream(
240243
// [END speech_transcribe_infinite_streaming]
241244
}
242245

243-
require('yargs')
244-
.demand(1)
245-
.command(
246-
'infiniteStream',
247-
'infinitely streams audio input from microphone to speech API',
248-
{},
249-
opts =>
250-
infiniteStream(
251-
opts.encoding,
252-
opts.sampleRateHertz,
253-
opts.languageCode,
254-
opts.streamingLimit
255-
)
256-
)
257-
.options({
258-
encoding: {
259-
alias: 'e',
260-
default: 'LINEAR16',
261-
global: true,
262-
requiresArg: true,
263-
type: 'string',
264-
},
265-
sampleRateHertz: {
266-
alias: 'r',
267-
default: 16000,
268-
global: true,
269-
requiresArg: true,
270-
type: 'number',
271-
},
272-
languageCode: {
273-
alias: 'l',
274-
default: 'en-US',
275-
global: true,
276-
requiresArg: true,
277-
type: 'string',
278-
},
279-
streamingLimit: {
280-
alias: 's',
281-
default: 290000,
282-
global: true,
283-
requiresArg: true,
284-
type: 'number',
285-
},
286-
})
287-
.example('node $0 infiniteStream')
288-
.wrap(120)
289-
.recommendCommands()
290-
.epilogue('For more information, see https://cloud.google.com/speech/docs')
291-
.help()
292-
.strict().argv;
246+
process.on('unhandledRejection', err => {
247+
console.error(err.message);
248+
process.exitCode = 1;
249+
});
250+
251+
main(...process.argv.slice(2));

speech/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
"name": "nodejs-docs-samples-speech",
33
"private": true,
44
"license": "Apache-2.0",
5-
"author": "Google Inc.",
5+
"author": "Google LLC",
66
"repository": "googleapis/nodejs-speech",
77
"engines": {
8-
"node": ">=8"
8+
"node": ">=10.17.0"
99
},
10+
"files": [
11+
"*.js",
12+
"resources/"
13+
],
1014
"scripts": {
1115
"test": "c8 mocha system-test --timeout 600000"
1216
},

speech/quickstart.js

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,51 @@
1414

1515
'use strict';
1616

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

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

26-
// The name of the audio file to transcribe
27-
const fileName = './resources/audio.raw';
28-
29-
// Reads a local audio file and converts it to base64
30-
const file = fs.readFileSync(fileName);
31-
const audioBytes = file.toString('base64');
32-
33-
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
34-
const audio = {
35-
content: audioBytes,
36-
};
37-
const config = {
38-
encoding: 'LINEAR16',
39-
sampleRateHertz: 16000,
40-
languageCode: 'en-US',
41-
};
42-
const request = {
43-
audio: audio,
44-
config: config,
45-
};
46-
47-
// Detects speech in the audio file
48-
const [response] = await client.recognize(request);
49-
const transcription = response.results
50-
.map(result => result.alternatives[0].transcript)
51-
.join('\n');
52-
console.log(`Transcription: ${transcription}`);
26+
async function quickstart() {
27+
// The name of the audio file to transcribe
28+
const fileName = './resources/audio.raw';
29+
30+
// Reads a local audio file and converts it to base64
31+
const file = await fs.readFile(fileName);
32+
const audioBytes = file.toString('base64');
33+
34+
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
35+
const audio = {
36+
content: audioBytes,
37+
};
38+
const config = {
39+
encoding: 'LINEAR16',
40+
sampleRateHertz: 16000,
41+
languageCode: 'en-US',
42+
};
43+
const request = {
44+
audio: audio,
45+
config: config,
46+
};
47+
48+
// Detects speech in the audio file
49+
const [response] = await client.recognize(request);
50+
const transcription = response.results
51+
.map(result => result.alternatives[0].transcript)
52+
.join('\n');
53+
console.log(`Transcription: ${transcription}`);
54+
}
55+
quickstart();
56+
// [END speech_quickstart]
5357
}
54-
main().catch(console.error);
55-
// [END speech_quickstart]
58+
59+
process.on('unhandledRejection', err => {
60+
console.error(err.message);
61+
process.exitCode = 1;
62+
});
63+
64+
main(...process.argv.slice(2));

speech/recognize.v1p1beta1.js

Lines changed: 52 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222

2323
'use strict';
2424

25-
async function syncRecognizeWithMetaData(
25+
// sample-metadata:
26+
// title: Recognize speech with metadata
27+
// description: Analyzes an audio stream, and detects speech along with metadata.
28+
// usage: node recognize.v1p1beta1.js ./resources/commercial_mono.wav <encoding> <sampleRateHertz> <languageCode>
29+
30+
function main(
2631
filename,
27-
encoding,
28-
sampleRateHertz,
29-
languageCode
32+
encoding = 'LINEAR16',
33+
sampleRateHertz = 16000,
34+
languageCode = 'en-US'
3035
) {
3136
// [START speech_transcribe_recognition_metadata_beta]
3237
// Imports the Google Cloud client library for Beta API
@@ -40,87 +45,53 @@ async function syncRecognizeWithMetaData(
4045
// Creates a client
4146
const client = new speech.SpeechClient();
4247

43-
/**
44-
* TODO(developer): Uncomment the following lines before running the sample.
45-
*/
46-
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
47-
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
48-
// const sampleRateHertz = 16000;
49-
// const languageCode = 'BCP-47 language code, e.g. en-US';
48+
async function syncRecognizeWithMetaData() {
49+
/**
50+
* TODO(developer): Uncomment the following lines before running the sample.
51+
*/
52+
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
53+
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
54+
// const sampleRateHertz = 16000;
55+
// const languageCode = 'BCP-47 language code, e.g. en-US';
5056

51-
const recognitionMetadata = {
52-
interactionType: 'DISCUSSION',
53-
microphoneDistance: 'NEARFIELD',
54-
recordingDeviceType: 'SMARTPHONE',
55-
recordingDeviceName: 'Pixel 2 XL',
56-
industryNaicsCodeOfAudio: 519190,
57-
};
57+
const recognitionMetadata = {
58+
interactionType: 'DISCUSSION',
59+
microphoneDistance: 'NEARFIELD',
60+
recordingDeviceType: 'SMARTPHONE',
61+
recordingDeviceName: 'Pixel 2 XL',
62+
industryNaicsCodeOfAudio: 519190,
63+
};
5864

59-
const config = {
60-
encoding: encoding,
61-
sampleRateHertz: sampleRateHertz,
62-
languageCode: languageCode,
63-
metadata: recognitionMetadata,
64-
};
65+
const config = {
66+
encoding: encoding,
67+
sampleRateHertz: sampleRateHertz,
68+
languageCode: languageCode,
69+
metadata: recognitionMetadata,
70+
};
6571

66-
const audio = {
67-
content: fs.readFileSync(filename).toString('base64'),
68-
};
72+
const audio = {
73+
content: fs.readFileSync(filename).toString('base64'),
74+
};
6975

70-
const request = {
71-
config: config,
72-
audio: audio,
73-
};
76+
const request = {
77+
config: config,
78+
audio: audio,
79+
};
7480

75-
// Detects speech in the audio file
76-
const [response] = await client.recognize(request);
77-
response.results.forEach(result => {
78-
const alternative = result.alternatives[0];
79-
console.log(alternative.transcript);
80-
});
81-
// [END speech_transcribe_recognition_metadata_beta]
81+
// Detects speech in the audio file
82+
const [response] = await client.recognize(request);
83+
response.results.forEach(result => {
84+
const alternative = result.alternatives[0];
85+
console.log(alternative.transcript);
86+
});
87+
// [END speech_transcribe_recognition_metadata_beta]
88+
}
89+
syncRecognizeWithMetaData();
8290
}
8391

84-
require('yargs')
85-
.demand(1)
86-
.command(
87-
'sync-metadata <filename>',
88-
'Detects speech in a local audio file with metadata.',
89-
{},
90-
opts =>
91-
syncRecognizeWithMetaData(
92-
opts.filename,
93-
opts.encoding,
94-
opts.sampleRateHertz,
95-
opts.languageCode
96-
)
97-
)
98-
.options({
99-
encoding: {
100-
alias: 'e',
101-
default: 'LINEAR16',
102-
global: true,
103-
requiresArg: true,
104-
type: 'string',
105-
},
106-
sampleRateHertz: {
107-
alias: 'r',
108-
default: 16000,
109-
global: true,
110-
requiresArg: true,
111-
type: 'number',
112-
},
113-
languageCode: {
114-
alias: 'l',
115-
default: 'en-US',
116-
global: true,
117-
requiresArg: true,
118-
type: 'string',
119-
},
120-
})
121-
.example('node $0 sync-metadata ./resources/commercial_mono.wav')
122-
.wrap(120)
123-
.recommendCommands()
124-
.epilogue('For more information, see https://cloud.google.com/speech/docs')
125-
.help()
126-
.strict().argv;
92+
process.on('unhandledRejection', err => {
93+
console.error(err.message);
94+
process.exitCode = 1;
95+
});
96+
97+
main(...process.argv.slice(2));

speech/system-test/.eslintrc.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

speech/system-test/MicrophoneStream.test.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)