Skip to content

Commit

Permalink
Sample code for Audio Logging (Enhanced Model) (#61)
Browse files Browse the repository at this point in the history
* Sample code for Audio Logging (Enhanced Model)

* Ran prettier
  • Loading branch information
happyhuman authored Apr 24, 2018
1 parent 7b0115f commit 554d80e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
72 changes: 72 additions & 0 deletions speech/recognize.v1p1beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,65 @@ function syncRecognizeWithMetaData(
// [END speech_transcribe_file_with_metadata]
}

function syncRecognizeWithEnhancedModel(
filename,
encoding,
sampleRateHertz,
languageCode
) {
// [START speech_transcribe_file_with_enhanced_model]
// Imports the Google Cloud client library
const fs = require('fs');

// Imports the Google Cloud client library for Beta API
/**
* TODO(developer): Update client library import to use new
* version of API when desired features become available
*/
const speech = require('@google-cloud/speech').v1p1beta1;

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

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const config = {
encoding: encoding,
languageCode: languageCode,
useEnhanced: true,
model: 'phone_call',
};
const audio = {
content: fs.readFileSync(filename).toString('base64'),
};

const request = {
config: config,
audio: audio,
};

// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
response.results.forEach(result => {
const alternative = result.alternatives[0];
console.log(alternative.transcript);
});
})
.catch(err => {
console.error('ERROR:', err);
});
// [END speech_transcribe_file_with_enhanced_model]
}

require(`yargs`)
.demand(1)
.command(
Expand Down Expand Up @@ -297,6 +356,18 @@ require(`yargs`)
opts.languageCode
)
)
.command(
`sync-enhanced-model <filename>`,
`Detects speech in a local audio file using an enhanced model.`,
{},
opts =>
syncRecognizeWithEnhancedModel(
opts.filename,
opts.encoding,
opts.sampleRateHertz,
opts.languageCode
)
)
.options({
encoding: {
alias: 'e',
Expand Down Expand Up @@ -328,6 +399,7 @@ require(`yargs`)
)
.example(`node $0 sync-auto-punctuation ./resources/commercial_mono.wav`)
.example(`node $0 sync-metadata ./resources/commercial_mono.wav`)
.example(`node $0 sync-enhanced-model ./resources/commercial_mono.wav`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/speech/docs`)
Expand Down
5 changes: 5 additions & 0 deletions speech/system-test/recognize.v1p1beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ test(`should run sync recognize with metadata`, async t => {
const output = await runAsync(`${cmd} sync-metadata ${filepath2}`, cwd);
t.true(output.includes(text3));
});

test(`should run sync recognize with enhanced model`, async t => {
const output = await runAsync(`${cmd} sync-enhanced-model ${filepath2}`, cwd);
t.true(output.includes(text3));
});

0 comments on commit 554d80e

Please sign in to comment.