Skip to content

Commit 2f2c260

Browse files
authored
Update Speech samples. (#257)
1 parent e8e7b5f commit 2f2c260

File tree

7 files changed

+107
-189
lines changed

7 files changed

+107
-189
lines changed

speech/package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
"license": "Apache Version 2.0",
66
"author": "Google Inc.",
77
"scripts": {
8-
"test": "mocha -R spec -t 10000 --require intelli-espower-loader ../test/_setup.js test/*.test.js",
9-
"system-test": "mocha -R spec -t 10000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
8+
"test": "cd ..; npm run st -- speech/system-test/*.test.js"
109
},
1110
"dependencies": {
1211
"@google-cloud/speech": "^0.4.0",
1312
"node-record-lpcm16": "^0.1.4",
1413
"yargs": "^6.4.0"
1514
},
16-
"devDependencies": {
17-
"mocha": "^3.1.2"
18-
},
1915
"engines": {
2016
"node": ">=4.3.2"
2117
}

speech/quickstart.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ const options = {
3737
};
3838

3939
// Detects speech in the audio file
40-
speechClient.recognize(fileName, options, (err, result) => {
41-
if (err) {
42-
console.error(err);
43-
return;
44-
}
45-
46-
console.log(`Transcription: ${result}`);
47-
});
40+
speechClient.recognize(fileName, options)
41+
.then((results) => {
42+
const transcription = results[0];
43+
console.log(`Transcription: ${transcription}`);
44+
});
4845
// [END speech_quickstart]

speech/recognize.js

+81-67
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,66 @@
2323

2424
'use strict';
2525

26-
const fs = require('fs');
27-
const record = require('node-record-lpcm16');
28-
const speech = require('@google-cloud/speech')();
26+
const Speech = require('@google-cloud/speech');
2927

3028
// [START speech_sync_recognize]
31-
function syncRecognize (filename, callback) {
32-
// Detect speech in the audio file, e.g. "./resources/audio.raw"
33-
speech.recognize(filename, {
29+
function syncRecognize (filename) {
30+
// Instantiates a client
31+
const speech = Speech();
32+
33+
const config = {
34+
// Configure these settings based on the audio you're transcribing
3435
encoding: 'LINEAR16',
3536
sampleRate: 16000
36-
}, (err, results) => {
37-
if (err) {
38-
callback(err);
39-
return;
40-
}
37+
};
4138

42-
console.log('Results:', results);
43-
callback();
44-
});
39+
// Detects speech in the audio file, e.g. "./resources/audio.raw"
40+
return speech.recognize(filename, config)
41+
.then((results) => {
42+
const transcription = results[0];
43+
console.log(`Transcription: ${transcription}`);
44+
return transcription;
45+
});
4546
}
4647
// [END speech_sync_recognize]
4748

4849
// [START speech_async_recognize]
49-
function asyncRecognize (filename, callback) {
50-
// Detect speech in the audio file, e.g. "./resources/audio.raw"
51-
speech.startRecognition(filename, {
50+
function asyncRecognize (filename) {
51+
// Instantiates a client
52+
const speech = Speech();
53+
54+
const config = {
55+
// Configure these settings based on the audio you're transcribing
5256
encoding: 'LINEAR16',
5357
sampleRate: 16000
54-
}, (err, operation) => {
55-
if (err) {
56-
callback(err);
57-
return;
58-
}
58+
};
5959

60-
operation
61-
.on('error', callback)
62-
.on('complete', (results) => {
63-
console.log('Results:', results);
64-
callback();
65-
});
66-
});
60+
// Detects speech in the audio file, e.g. "./resources/audio.raw"
61+
// This creates a recognition job that you can wait for now, or get its result
62+
// later.
63+
return speech.startRecognition(filename, config)
64+
.then((results) => {
65+
const operation = results[0];
66+
// Get a Promise represention the final result of the job
67+
return operation.promise();
68+
})
69+
.then((transcription) => {
70+
console.log(`Transcription: ${transcription}`);
71+
return transcription;
72+
});
6773
}
6874
// [END speech_async_recognize]
6975

7076
// [START speech_streaming_recognize]
77+
const fs = require('fs');
78+
7179
function streamingRecognize (filename, callback) {
80+
// Instantiates a client
81+
const speech = Speech();
82+
7283
const options = {
7384
config: {
85+
// Configure these settings based on the audio you're transcribing
7486
encoding: 'LINEAR16',
7587
sampleRate: 16000
7688
}
@@ -90,9 +102,15 @@ function streamingRecognize (filename, callback) {
90102
// [END speech_streaming_recognize]
91103

92104
// [START speech_streaming_mic_recognize]
93-
function streamingMicRecognize (filename) {
105+
const record = require('node-record-lpcm16');
106+
107+
function streamingMicRecognize () {
108+
// Instantiates a client
109+
const speech = Speech();
110+
94111
const options = {
95112
config: {
113+
// Configure these settings based on the audio you're transcribing
96114
encoding: 'LINEAR16',
97115
sampleRate: 16000
98116
}
@@ -110,43 +128,39 @@ function streamingMicRecognize (filename) {
110128
}
111129
// [END speech_streaming_mic_recognize]
112130

113-
// The command-line program
114-
var cli = require('yargs');
115-
var utils = require('../utils');
116-
117-
var program = module.exports = {
118-
syncRecognize: syncRecognize,
119-
asyncRecognize: asyncRecognize,
120-
streamingRecognize: streamingRecognize,
121-
streamingMicRecognize: streamingMicRecognize,
122-
main: function (args) {
123-
// Run the command-line program
124-
cli.help().strict().parse(args).argv;
125-
}
126-
};
127-
128-
cli
131+
require(`yargs`)
129132
.demand(1)
130-
.command('sync <filename>', 'Detects speech in an audio file.', {}, function (options) {
131-
program.syncRecognize(options.filename, utils.makeHandler(false));
132-
})
133-
.command('async <filename>', 'Creates a job to detect speech in an audio file, and waits for the job to complete.', {}, function (options) {
134-
program.asyncRecognize(options.filename, utils.makeHandler(false));
135-
})
136-
.command('stream <filename>', 'Detects speech in an audio file by streaming it to the Speech API.', {}, function (options) {
137-
program.streamingRecognize(options.filename, utils.makeHandler(false));
138-
})
139-
.command('listen', 'Detects speech in a microphone input stream.', {}, function () {
140-
program.streamingMicRecognize();
141-
})
142-
.example('node $0 sync ./resources/audio.raw', 'Detects speech in "./resources/audio.raw".')
143-
.example('node $0 async ./resources/audio.raw', 'Creates a job to detect speech in "./resources/audio.raw", and waits for the job to complete.')
144-
.example('node $0 stream ./resources/audio.raw', 'Detects speech in "./resources/audio.raw" by streaming it to the Speech API.')
145-
.example('node $0 listen', 'Detects speech in a microphone input stream.')
133+
.command(
134+
`sync <filename>`,
135+
`Detects speech in an audio file.`,
136+
{},
137+
(opts) => syncRecognize(opts.filename)
138+
)
139+
.command(
140+
`async <filename>`,
141+
`Creates a job to detect speech in an audio file, and waits for the job to complete.`,
142+
{},
143+
(opts) => asyncRecognize(opts.filename)
144+
)
145+
.command(
146+
`stream <filename>`,
147+
`Detects speech in an audio file by streaming it to the Speech API.`,
148+
{},
149+
(opts) => streamingRecognize(opts.filename, () => {})
150+
)
151+
.command(
152+
`listen`,
153+
`Detects speech in a microphone input stream.`,
154+
{},
155+
streamingMicRecognize
156+
)
157+
.example(`node $0 sync ./resources/audio.raw`)
158+
.example(`node $0 async ./resources/audio.raw`)
159+
.example(`node $0 stream ./resources/audio.raw`)
160+
.example(`node $0 listen`)
146161
.wrap(120)
147162
.recommendCommands()
148-
.epilogue('For more information, see https://cloud.google.com/speech/docs');
149-
150-
if (module === require.main) {
151-
program.main(process.argv.slice(2));
152-
}
163+
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
164+
.help()
165+
.strict()
166+
.argv;

speech/system-test/quickstart.test.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,33 @@ const config = {
2626
};
2727

2828
describe(`speech:quickstart`, () => {
29-
let speechMock, SpeechMock;
30-
3129
it(`should detect speech`, (done) => {
3230
const expectedFileName = `./resources/audio.raw`;
3331
const expectedText = `how old is the Brooklyn Bridge`;
3432

35-
speechMock = {
36-
recognize: (_fileName, _config, _callback) => {
33+
const speechMock = {
34+
recognize: (_fileName, _config) => {
3735
assert.equal(_fileName, expectedFileName);
3836
assert.deepEqual(_config, config);
39-
assert.equal(typeof _callback, `function`);
40-
41-
speech.recognize(fileName, config, (err, transcription, apiResponse) => {
42-
_callback(err, transcription, apiResponse);
43-
assert.ifError(err);
44-
assert.equal(transcription, expectedText);
45-
assert.notEqual(apiResponse, undefined);
46-
assert.equal(console.log.calledOnce, true);
47-
assert.deepEqual(console.log.firstCall.args, [`Transcription: ${expectedText}`]);
48-
done();
49-
});
37+
38+
return speech.recognize(fileName, config)
39+
.then((results) => {
40+
const transcription = results[0];
41+
assert.equal(transcription, expectedText);
42+
43+
setTimeout(() => {
44+
assert.equal(console.log.callCount, 1);
45+
assert.deepEqual(console.log.getCall(0).args, [`Transcription: ${expectedText}`]);
46+
done();
47+
}, 200);
48+
49+
return results;
50+
});
5051
}
5152
};
52-
SpeechMock = sinon.stub().returns(speechMock);
5353

5454
proxyquire(`../quickstart`, {
55-
'@google-cloud/speech': SpeechMock
55+
'@google-cloud/speech': sinon.stub().returns(speechMock)
5656
});
5757
});
5858
});

speech/system-test/recognize.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ const text = `how old is the Brooklyn Bridge`;
2525

2626
describe(`speech:recognize`, () => {
2727
it(`should run sync recognize`, () => {
28-
assert.equal(run(`${cmd} sync ${filename}`, cwd), `Results: ${text}`);
28+
assert.equal(run(`${cmd} sync ${filename}`, cwd), `Transcription: ${text}`);
2929
});
3030

3131
it(`should run async recognize`, () => {
32-
assert.equal(run(`${cmd} async ${filename}`, cwd), `Results: ${text}`);
32+
assert.equal(run(`${cmd} async ${filename}`, cwd), `Transcription: ${text}`);
3333
});
3434

3535
it(`should run streaming recognize`, () => {
36-
assert.notEqual(run(`${cmd} stream ${filename}`, cwd).indexOf(text), -1);
36+
assert.equal(run(`${cmd} stream ${filename}`, cwd).includes(text), true);
3737
});
3838
});

speech/test/quickstart.test.js

-49
This file was deleted.

speech/test/recognize.test.js

-40
This file was deleted.

0 commit comments

Comments
 (0)