Skip to content

Commit

Permalink
Update Speech samples. (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored Feb 1, 2017
1 parent cd64522 commit 4d1179d
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 86 deletions.
3 changes: 2 additions & 1 deletion speech/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"test": "cd ..; npm run st -- --verbose speech/system-test/*.test.js"
},
"dependencies": {
"@google-cloud/speech": "0.5.0",
"@google-cloud/speech": "0.6.0",
"@google-cloud/storage": "0.6.1",
"node-record-lpcm16": "0.2.0",
"yargs": "6.6.0"
},
Expand Down
244 changes: 188 additions & 56 deletions speech/recognize.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,143 +23,275 @@

'use strict';

const Speech = require('@google-cloud/speech');
function syncRecognize (filename, encoding, sampleRate) {
// [START speech_sync_recognize]
// Imports the Google Cloud client library
const Speech = require('@google-cloud/speech');

// [START speech_sync_recognize]
function syncRecognize (filename) {
// Instantiates a client
const speech = Speech();

const config = {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
// The path to the local file on which to perform speech recognition, e.g. /path/to/audio.raw
// const filename = '/path/to/audio.raw';

// The encoding of the audio file, e.g. 'LINEAR16'
// const encoding = 'LINEAR16';

// The sample rate of the audio file, e.g. 16000
// const sampleRate = 16000;

const request = {
encoding: encoding,
sampleRate: sampleRate
};

// Detects speech in the audio file, e.g. "./resources/audio.raw"
return speech.recognize(filename, config)
// Detects speech in the audio file
speech.recognize(filename, request)
.then((results) => {
const transcription = results[0];

console.log(`Transcription: ${transcription}`);
});
// [END speech_sync_recognize]
}

function syncRecognizeGCS (gcsUri, encoding, sampleRate) {
// [START speech_sync_recognize_gcs]
// Imports the Google Cloud client library
const Speech = require('@google-cloud/speech');

// Instantiates a client
const speech = Speech();

// The Google Cloud Storage URI of the file on which to perform speech recognition, e.g. gs://my-bucket/audio.raw
// const gcsUri = 'gs://my-bucket/audio.raw';

// The encoding of the audio file, e.g. 'LINEAR16'
// const encoding = 'LINEAR16';

// The sample rate of the audio file, e.g. 16000
// const sampleRate = 16000;

const request = {
encoding: encoding,
sampleRate: sampleRate
};

// Detects speech in the audio file
speech.recognize(gcsUri, request)
.then((results) => {
const transcription = results[0];

console.log(`Transcription: ${transcription}`);
});
// [END speech_sync_recognize_gcs]
}

function asyncRecognize (filename, encoding, sampleRate) {
// [START speech_async_recognize]
// Imports the Google Cloud client library
const Speech = require('@google-cloud/speech');

// Instantiates a client
const speech = Speech();

// The path to the local file on which to perform speech recognition, e.g. /path/to/audio.raw
// const filename = '/path/to/audio.raw';

// The encoding of the audio file, e.g. 'LINEAR16'
// const encoding = 'LINEAR16';

// The sample rate of the audio file, e.g. 16000
// const sampleRate = 16000;

const request = {
encoding: encoding,
sampleRate: sampleRate
};

// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
speech.startRecognition(filename, request)
.then((results) => {
const operation = results[0];
// Get a Promise represention of the final result of the job
return operation.promise();
})
.then((transcription) => {
console.log(`Transcription: ${transcription}`);
return transcription;
});
// [END speech_async_recognize]
}
// [END speech_sync_recognize]

// [START speech_async_recognize]
function asyncRecognize (filename) {
function asyncRecognizeGCS (gcsUri, encoding, sampleRate) {
// [START speech_async_recognize_gcs]
// Imports the Google Cloud client library
const Speech = require('@google-cloud/speech');

// Instantiates a client
const speech = Speech();

const config = {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
// The Google Cloud Storage URI of the file on which to perform speech recognition, e.g. gs://my-bucket/audio.raw
// const gcsUri = 'gs://my-bucket/audio.raw';

// The encoding of the audio file, e.g. 'LINEAR16'
// const encoding = 'LINEAR16';

// The sample rate of the audio file, e.g. 16000
// const sampleRate = 16000;

const request = {
encoding: encoding,
sampleRate: sampleRate
};

// Detects speech in the audio file, e.g. "./resources/audio.raw"
// This creates a recognition job that you can wait for now, or get its result
// later.
return speech.startRecognition(filename, config)
// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
speech.startRecognition(gcsUri, request)
.then((results) => {
const operation = results[0];
// Get a Promise represention the final result of the job
// Get a Promise represention of the final result of the job
return operation.promise();
})
.then((transcription) => {
console.log(`Transcription: ${transcription}`);
return transcription;
});
// [END speech_async_recognize_gcs]
}
// [END speech_async_recognize]

// [START speech_streaming_recognize]
const fs = require('fs');
function streamingRecognize (filename, encoding, sampleRate) {
// [START speech_streaming_recognize]
const fs = require('fs');

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

function streamingRecognize (filename, callback) {
// Instantiates a client
const speech = Speech();

const options = {
// The path to the local file on which to perform speech recognition, e.g. /path/to/audio.raw
// const filename = '/path/to/audio.raw';

// The encoding of the audio file, e.g. 'LINEAR16'
// const encoding = 'LINEAR16';

// The sample rate of the audio file, e.g. 16000
// const sampleRate = 16000;

const request = {
config: {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
encoding: encoding,
sampleRate: sampleRate
}
};

// Create a recognize stream
const recognizeStream = speech.createRecognizeStream(options)
.on('error', callback)
// Stream the audio to the Google Cloud Speech API
const recognizeStream = speech.createRecognizeStream(request)
.on('error', console.error)
.on('data', (data) => {
console.log('Data received: %j', data);
callback();
});

// Stream an audio file from disk to the Speech API, e.g. "./resources/audio.raw"
fs.createReadStream(filename).pipe(recognizeStream);
// [END speech_streaming_recognize]
}
// [END speech_streaming_recognize]

// [START speech_streaming_mic_recognize]
const record = require('node-record-lpcm16');
function streamingMicRecognize (encoding, sampleRate) {
// [START speech_streaming_mic_recognize]
const record = require('node-record-lpcm16');

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

function streamingMicRecognize () {
// Instantiates a client
const speech = Speech();

const options = {
// The encoding of the audio file, e.g. 'LINEAR16'
// const encoding = 'LINEAR16';

// The sample rate of the audio file, e.g. 16000
// const sampleRate = 16000;

const request = {
config: {
// Configure these settings based on the audio you're transcribing
encoding: 'LINEAR16',
sampleRate: 16000
encoding: encoding,
sampleRate: sampleRate
}
};

// Create a recognize stream
const recognizeStream = speech.createRecognizeStream(options)
const recognizeStream = speech.createRecognizeStream(request)
.on('error', console.error)
.on('data', (data) => process.stdout.write(data.results));

// Start recording and send the microphone input to the Speech API
record.start({
sampleRate: 16000,
sampleRate: sampleRate,
threshold: 0
}).pipe(recognizeStream);

console.log('Listening, press Ctrl+C to stop.');
// [END speech_streaming_mic_recognize]
}
// [END speech_streaming_mic_recognize]

require(`yargs`)
.demand(1)
.command(
`sync <filename>`,
`Detects speech in an audio file.`,
`Detects speech in a local audio file.`,
{},
(opts) => syncRecognize(opts.filename)
(opts) => syncRecognize(opts.filename, opts.encoding, opts.sampleRate)
)
.command(
`sync-gcs <gcsUri>`,
`Detects speech in an audio file located in a Google Cloud Storage bucket.`,
{},
(opts) => syncRecognizeGCS(opts.gcsUri, opts.encoding, opts.sampleRate)
)
.command(
`async <filename>`,
`Creates a job to detect speech in an audio file, and waits for the job to complete.`,
`Creates a job to detect speech in a local audio file, and waits for the job to complete.`,
{},
(opts) => asyncRecognize(opts.filename)
(opts) => asyncRecognize(opts.filename, opts.encoding, opts.sampleRate)
)
.command(
`async-gcs <gcsUri>`,
`Creates a job to detect speech in an audio file located in a Google Cloud Storage bucket, and waits for the job to complete.`,
{},
(opts) => asyncRecognizeGCS(opts.gcsUri, opts.encoding, opts.sampleRate)
)
.command(
`stream <filename>`,
`Detects speech in an audio file by streaming it to the Speech API.`,
`Detects speech in a local audio file by streaming it to the Speech API.`,
{},
(opts) => streamingRecognize(opts.filename, () => {})
(opts) => streamingRecognize(opts.filename, opts.encoding, opts.sampleRate)
)
.command(
`listen`,
`Detects speech in a microphone input stream.`,
{},
streamingMicRecognize
(opts) => streamingMicRecognize(opts.encoding, opts.sampleRate)
)
.example(`node $0 sync ./resources/audio.raw`)
.example(`node $0 async ./resources/audio.raw`)
.example(`node $0 stream ./resources/audio.raw`)
.options({
encoding: {
alias: 'e',
default: 'LINEAR16',
global: true,
requiresArg: true,
type: 'string'
},
sampleRate: {
alias: 'r',
default: 16000,
global: true,
requiresArg: true,
type: 'number'
}
})
.example(`node $0 sync ./resources/audio.raw -e LINEAR16 -r 16000`)
.example(`node $0 async-gcs gs://my-bucket/audio.raw -e LINEAR16 -r 16000`)
.example(`node $0 stream ./resources/audio.raw -e LINEAR16 -r 16000`)
.example(`node $0 listen`)
.wrap(120)
.recommendCommands()
Expand Down
36 changes: 31 additions & 5 deletions speech/system-test/recognize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,49 @@
require(`../../system-test/_setup`);

const path = require(`path`);
const storage = require(`@google-cloud/storage`)();
const uuid = require(`uuid`);

const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node recognize.js`;
const cwd = path.join(__dirname, `..`);
const filename = `./resources/audio.raw`;
const filename = `audio.raw`;
const filepath = path.join(__dirname, `../resources/${filename}`);
const text = `how old is the Brooklyn Bridge`;

test.before(async () => {
const [bucket] = await storage.createBucket(bucketName);
await bucket.upload(filepath);
});

test.after.always(async () => {
const bucket = storage.bucket(bucketName);
await bucket.deleteFiles({ force: true });
await bucket.deleteFiles({ force: true }); // Try a second time...
await bucket.delete();
});

test(`should run sync recognize`, async (t) => {
const output = await runAsync(`${cmd} sync ${filename}`, cwd);
const output = await runAsync(`${cmd} sync ${filepath}`, cwd);
t.true(output.includes(`Transcription: ${text}`));
});

test(`should run sync recognize on a GCS file`, async (t) => {
const output = await runAsync(`${cmd} sync-gcs gs://${bucketName}/${filename}`, cwd);
t.true(output.includes(`Transcription: ${text}`));
});

test(`should run async recognize on a local file`, async (t) => {
const output = await runAsync(`${cmd} async ${filepath}`, cwd);
t.true(output.includes(`Transcription: ${text}`));
});

test(`should run async recognize`, async (t) => {
const output = await runAsync(`${cmd} async ${filename}`, cwd);
test(`should run async recognize on a GCS file`, async (t) => {
const output = await runAsync(`${cmd} async-gcs gs://${bucketName}/${filename}`, cwd);
t.true(output.includes(`Transcription: ${text}`));
});

test(`should run streaming recognize`, async (t) => {
const output = await runAsync(`${cmd} stream ${filename}`, cwd);
const output = await runAsync(`${cmd} stream ${filepath}`, cwd);
t.true(output.includes(text));
});
Loading

0 comments on commit 4d1179d

Please sign in to comment.