Skip to content

Commit

Permalink
docs: modernize patterns in subset of samples (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Oct 21, 2020
1 parent fe5e3c3 commit 42637ed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/google-cloud-node/samples/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
rules:
no-console: off
node/no-unsupported-features/node-builtins: off
8 changes: 6 additions & 2 deletions packages/google-cloud-node/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
"name": "nodejs-docs-samples-speech",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"author": "Google LLC",
"repository": "googleapis/nodejs-speech",
"engines": {
"node": ">=8"
"node": ">=10.17.0"
},
"files": [
"*.js",
"resources/"
],
"scripts": {
"test": "c8 mocha system-test --timeout 600000"
},
Expand Down
73 changes: 41 additions & 32 deletions packages/google-cloud-node/samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,51 @@

'use strict';

// [START speech_quickstart]
async function main() {
function main() {
// [START speech_quickstart]
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');
const fs = require('fs');
const fs = require('fs').promises;

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

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

// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');

// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};

// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
async function quickstart() {
// The name of the audio file to transcribe
const fileName = './resources/audio.raw';

// Reads a local audio file and converts it to base64
const file = await fs.readFile(fileName);
const audioBytes = file.toString('base64');

// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};

// Detects speech in the audio file
const [response] = await client.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
}
quickstart();
// [END speech_quickstart]
}
main().catch(console.error);
// [END speech_quickstart]

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));

0 comments on commit 42637ed

Please sign in to comment.