Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Address comments and refactor another sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Mar 11, 2019
1 parent 290fc40 commit 2754994
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 9 deletions.
12 changes: 6 additions & 6 deletions samples/speech_transcribe_async.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ function main(

// Wait for operation to complete.
const [response] = await operation.promise();

const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
const transcriptions = response.results;
console.log(`Transcriptions: ${transcriptions.length}`);
for (const transcription of transcriptions) {
console.log(transcription.alternatives[0].transcript);
}
}

transcribeAsync(filepath, encoding, sampleRateHertz, languageCode);
transcribeAsync();
// [END speech_transcribe_async]
}

Expand Down
54 changes: 54 additions & 0 deletions samples/speech_transcribe_sync_gcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright 2019, 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';

function main() {
// [START speech_transcribe_sync_gcs]
// Imports the Google Cloud Speech API client library.
const speech = require('@google-cloud/speech');

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

async function transcribeSyncGcs() {
const request = {
audio: {
uri: 'gs://cloud-samples-data/speech/brooklyn_bridge.raw',
},
config: {
// Encoding of the audio file, e.g. 'LINEAR16'.
encoding: 'LINEAR16',
// Sample rate in Hertz of the audio data sent.
sampleRateHertz: 16000,
// BCP-47 language code, e.g. 'en-US'.
languageCode: 'en-US',
},
};

// Detects speech in the audio file.
const [response] = await client.recognize(request);
const transcriptions = response.results;
console.log(`Transcriptions: ${transcriptions.length}`);
for (const transcription of transcriptions) {
console.log(transcription.alternatives[0].transcript);
}
}

transcribeSyncGcs();
// [END speech_transcribe_sync_gcs]
}

main();
8 changes: 5 additions & 3 deletions samples/system-test/speech_transcribe_async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ const test = require(`ava`);
const {runAsync} = require(`@google-cloud/nodejs-repo-tools`);

const filepath = path.join(__dirname, `../resources/audio.raw`);
const REGION_TAG = 'speech_transcribe_async';

test(`speech_transcribe_async`, async t => {
test(REGION_TAG, async t => {
const output = await runAsync(
`node speech_transcribe_async.js ${filepath}`,
`node ${REGION_TAG}.js ${filepath}`,
path.join(__dirname, `..`)
);
t.true(output.includes(`Transcription: how old is the Brooklyn Bridge`));
t.true(output.includes(`Transcriptions: 1`));
t.true(output.includes(`how old is the Brooklyn Bridge`));
});
31 changes: 31 additions & 0 deletions samples/system-test/speech_transcribe_sync_gcs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2019, 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 test = require(`ava`);
const {runAsync} = require(`@google-cloud/nodejs-repo-tools`);

const REGION_TAG = 'speech_transcribe_sync_gcs';

test(REGION_TAG, async t => {
const output = await runAsync(
`node ${REGION_TAG}.js`,
path.join(__dirname, `..`)
);
t.true(output.includes(`Transcriptions: 1`));
t.true(output.includes(`how old is the Brooklyn Bridge`));
});

0 comments on commit 2754994

Please sign in to comment.