Skip to content

Commit

Permalink
New quickstarts. (#226)
Browse files Browse the repository at this point in the history
* New quickstarts.

* Address comments.
  • Loading branch information
jmdobry authored and Ace Nassri committed Nov 17, 2022
1 parent e6ea367 commit f87553e
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
4 changes: 2 additions & 2 deletions speech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"dependencies": {
"@google-cloud/speech": "^0.1.1",
"node-record-lpcm16": "^0.1.4",
"yargs": "^5.0.0"
"yargs": "^6.0.0"
},
"devDependencies": {
"mocha": "^3.0.2"
"mocha": "^3.1.0"
},
"engines": {
"node": ">=4.3.2"
Expand Down
48 changes: 48 additions & 0 deletions speech/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// [START speech_quickstart]
// Imports the Google Cloud client library
const Speech = require('@google-cloud/speech');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Instantiates a client
const speechClient = Speech({
projectId: projectId
});

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

// The audio file's encoding and sample rate
const options = {
encoding: 'LINEAR16',
sampleRate: 16000
};

// Detects speech in the audio file
speechClient.recognize(fileName, options, (err, result) => {
if (err) {
console.error(err);
return;
}

console.log(`Transcription: ${result}`);
});
// [END speech_quickstart]
58 changes: 58 additions & 0 deletions speech/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();
const speech = proxyquire(`@google-cloud/speech`, {})();

const fileName = path.join(__dirname, `../resources/audio.raw`);
const config = {
encoding: `LINEAR16`,
sampleRate: 16000
};

describe(`speech:quickstart`, () => {
let speechMock, SpeechMock;

it(`should detect speech`, (done) => {
const expectedFileName = `./resources/audio.raw`;
const expectedText = `how old is the Brooklyn Bridge`;

speechMock = {
recognize: (_fileName, _config, _callback) => {
assert.equal(_fileName, expectedFileName);
assert.deepEqual(_config, config);
assert.equal(typeof _callback, `function`);

speech.recognize(fileName, config, (err, transcription, apiResponse) => {
_callback(err, transcription, apiResponse);
assert.ifError(err);
assert.equal(transcription, expectedText);
assert.notEqual(apiResponse, undefined);
assert.equal(console.log.calledOnce, true);
assert.deepEqual(console.log.firstCall.args, [`Transcription: ${expectedText}`]);
done();
});
}
};
SpeechMock = sinon.stub().returns(speechMock);

proxyquire(`../quickstart`, {
'@google-cloud/speech': SpeechMock
});
});
});
49 changes: 49 additions & 0 deletions speech/test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const proxyquire = require(`proxyquire`).noCallThru();

const config = {
encoding: 'LINEAR16',
sampleRate: 16000
};

describe(`speech:quickstart`, () => {
let speechMock, SpeechMock;
const error = new Error(`error`);
const fileName = `./resources/audio.raw`;

before(() => {
speechMock = {
recognize: sinon.stub().yields(error)
};
SpeechMock = sinon.stub().returns(speechMock);
});

it(`should handle error`, () => {
proxyquire(`../quickstart`, {
'@google-cloud/speech': SpeechMock
});

assert.equal(SpeechMock.calledOnce, true);
assert.deepEqual(SpeechMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
assert.equal(speechMock.recognize.calledOnce, true);
assert.deepEqual(speechMock.recognize.firstCall.args.slice(0, -1), [fileName, config]);
assert.equal(console.error.calledOnce, true);
assert.deepEqual(console.error.firstCall.args, [error]);
});
});

0 comments on commit f87553e

Please sign in to comment.