Skip to content

Commit f87553e

Browse files
jmdobryAce Nassri
authored andcommitted
New quickstarts. (#226)
* New quickstarts. * Address comments.
1 parent e6ea367 commit f87553e

File tree

4 files changed

+157
-2
lines changed

4 files changed

+157
-2
lines changed

speech/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
"dependencies": {
1212
"@google-cloud/speech": "^0.1.1",
1313
"node-record-lpcm16": "^0.1.4",
14-
"yargs": "^5.0.0"
14+
"yargs": "^6.0.0"
1515
},
1616
"devDependencies": {
17-
"mocha": "^3.0.2"
17+
"mocha": "^3.1.0"
1818
},
1919
"engines": {
2020
"node": ">=4.3.2"

speech/quickstart.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START speech_quickstart]
19+
// Imports the Google Cloud client library
20+
const Speech = require('@google-cloud/speech');
21+
22+
// Your Google Cloud Platform project ID
23+
const projectId = 'YOUR_PROJECT_ID';
24+
25+
// Instantiates a client
26+
const speechClient = Speech({
27+
projectId: projectId
28+
});
29+
30+
// The name of the audio file to transcribe
31+
const fileName = './resources/audio.raw';
32+
33+
// The audio file's encoding and sample rate
34+
const options = {
35+
encoding: 'LINEAR16',
36+
sampleRate: 16000
37+
};
38+
39+
// 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+
});
48+
// [END speech_quickstart]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const path = require(`path`);
19+
const proxyquire = require(`proxyquire`).noPreserveCache();
20+
const speech = proxyquire(`@google-cloud/speech`, {})();
21+
22+
const fileName = path.join(__dirname, `../resources/audio.raw`);
23+
const config = {
24+
encoding: `LINEAR16`,
25+
sampleRate: 16000
26+
};
27+
28+
describe(`speech:quickstart`, () => {
29+
let speechMock, SpeechMock;
30+
31+
it(`should detect speech`, (done) => {
32+
const expectedFileName = `./resources/audio.raw`;
33+
const expectedText = `how old is the Brooklyn Bridge`;
34+
35+
speechMock = {
36+
recognize: (_fileName, _config, _callback) => {
37+
assert.equal(_fileName, expectedFileName);
38+
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+
});
50+
}
51+
};
52+
SpeechMock = sinon.stub().returns(speechMock);
53+
54+
proxyquire(`../quickstart`, {
55+
'@google-cloud/speech': SpeechMock
56+
});
57+
});
58+
});

speech/test/quickstart.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const proxyquire = require(`proxyquire`).noCallThru();
19+
20+
const config = {
21+
encoding: 'LINEAR16',
22+
sampleRate: 16000
23+
};
24+
25+
describe(`speech:quickstart`, () => {
26+
let speechMock, SpeechMock;
27+
const error = new Error(`error`);
28+
const fileName = `./resources/audio.raw`;
29+
30+
before(() => {
31+
speechMock = {
32+
recognize: sinon.stub().yields(error)
33+
};
34+
SpeechMock = sinon.stub().returns(speechMock);
35+
});
36+
37+
it(`should handle error`, () => {
38+
proxyquire(`../quickstart`, {
39+
'@google-cloud/speech': SpeechMock
40+
});
41+
42+
assert.equal(SpeechMock.calledOnce, true);
43+
assert.deepEqual(SpeechMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
44+
assert.equal(speechMock.recognize.calledOnce, true);
45+
assert.deepEqual(speechMock.recognize.firstCall.args.slice(0, -1), [fileName, config]);
46+
assert.equal(console.error.calledOnce, true);
47+
assert.deepEqual(console.error.firstCall.args, [error]);
48+
});
49+
});

0 commit comments

Comments
 (0)