-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New quickstarts. * Address comments.
- Loading branch information
Showing
4 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |