Skip to content

Commit

Permalink
feat: adds media translation file, mic samples
Browse files Browse the repository at this point in the history
* feat: adds streaming samples

* fix: linter errors

* fix: per reviewer

* fix: per reviewer

* fix: updates package.json
  • Loading branch information
telpirion authored and kweinmeister committed Nov 8, 2022
1 parent 518e44b commit 28ee4a0
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 30 deletions.
3 changes: 2 additions & 1 deletion media-translation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test": "c8 mocha --timeout 600000 test/*.js"
},
"dependencies": {
"@google-cloud/media-translation": "^1.0.0"
"@google-cloud/media-translation": "^1.0.0",
"node-record-lpcm16": "1.0.1"
},
"devDependencies": {
"c8": "^7.0.0",
Expand Down
91 changes: 77 additions & 14 deletions media-translation/quickstart.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,101 @@
// Copyright 2020 Google LLC
//
// Copyright 2020, Google LLC.
// 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
//
// https://www.apache.org/licenses/LICENSE-2.0
// https://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';

async function quickstart() {
// parent = 'projects/my-project' // Project to list dashboards for.
/**
* Translate text from an audio file.
* @param {string} filename local path to
* @param {string} encoding the encoding of the audio rate, e.g. Linear16
* @param {string} sourceLanguage language translating from, as BCP-47 code
* @param {string} targetLanguage languate translating to, as BCP-47 code
*/
function main(filename, encoding, sourceLanguage, targetLanguage) {
// [START media_translation_quickstart]
// Imports the Google Cloud client library
const fs = require('fs');

// Imports the CLoud Media Translation client library
const {
SpeechTranslationServiceClient,
} = require('@google-cloud/media-translation');

// Creates a client
const translate = new SpeechTranslationServiceClient();
const client = new SpeechTranslationServiceClient();

async function quickstart() {
/**
* 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 sourceLanguage = 'BCP-47 source language code, e.g. en-US';
// const targetLangauge = 'BCP-47 target language code, e.g. es-ES';

const config = {
audioConfig: {
audioEncoding: encoding,
sourceLanguageCode: sourceLanguage,
targetLanguageCode: targetLanguage,
},
};

// First request needs to have only a streaming config, no data.
const initialRequest = {
streamingConfig: config,
audioContent: null,
};

const readStream = fs.createReadStream(filename, {
highWaterMark: 4096,
encoding: 'base64',
});

// parent = 'projects/my-project', // Project to list dashboards for.
const chunks = [];
readStream
.on('data', chunk => {
const request = {
streamingConfig: config,
audioContent: chunk.toString(),
};
chunks.push(request);
})
.on('close', () => {
// Config-only request should be first in stream of requests
stream.write(initialRequest);
for (let i = 0; i < chunks.length; i++) {
stream.write(chunks[i]);
}
stream.end();
});

// TODO: add an actual sample.
console.info(translate);
const stream = client.streamingTranslateSpeech().on('data', response => {
const {result} = response;
if (result.textTranslationResult.isFinal) {
console.log(
`\nFinal translation: ${result.textTranslationResult.translation}`
);
console.log(`Final recognition result: ${result.recognitionResult}`);
} else {
console.log(
`\nPartial translation: ${result.textTranslationResult.translation}`
);
console.log(`Partial recognition result: ${result.recognitionResult}`);
}
});

// [END media_translation_quickstart]
// [END media_translation_quickstart]
}
quickstart();
}

const args = process.argv.slice(2);
quickstart(...args).catch(console.error);
main(...process.argv.slice(2));
Binary file added media-translation/resources/audio.raw
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
// Copyright 2020 Google LLC
// Copyright 2017 Google LLC
//
// 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
//
// https://www.apache.org/licenses/LICENSE-2.0
// https://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 {assert} = require('chai');
const cp = require('child_process');
const {describe, it} = require('mocha');
const execSync = require('child_process').execSync;
const cmd = 'node quickstart.js';

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const cwd = path.join(__dirname, '..');

const PROJECT_ID = process.env.GCLOUD_PROJECT;
const filePath = path.join(__dirname, '..', 'resources/audio.raw');
const exec = cmd => execSync(cmd, {encoding: 'utf-8'});

describe('Sample Integration Tests', () => {
it('should run quickstart.js', async () => {
const stdout = execSync(`node ./quickstart.js projects/${PROJECT_ID}`, {
cwd,
});
assert(stdout);
describe('Quickstart', () => {
it('should translate from a streamed file', async () => {
const stdout = exec(`${cmd} ${filePath} linear16 en-US es-ES`);
assert.include(stdout, 'Partial translation');
});
});
31 changes: 31 additions & 0 deletions media-translation/test/translate_from_file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017 Google LLC
//
// 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
//
// https://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 {assert} = require('chai');
const {describe, it} = require('mocha');
const execSync = require('child_process').execSync;
const cmd = 'node translate_from_file.js';

const filePath = path.join(__dirname, '..', 'resources/audio.raw');
const exec = cmd => execSync(cmd, {encoding: 'utf-8'});

describe('MediaTranslation', () => {
it('should translate from a streamed file', async () => {
const stdout = exec(`${cmd} ${filePath} linear16 en-US es-ES`);
assert.include(stdout, 'Partial translation');
});
});
101 changes: 101 additions & 0 deletions media-translation/translate_from_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2020, Google LLC.
// 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
//
// https://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';

/**
* Translate text from an audio file.
* @param {string} filename local path to
* @param {string} encoding the encoding of the audio rate, e.g. Linear16
* @param {string} sourceLanguage language translating from, as BCP-47 code
* @param {string} targetLanguage languate translating to, as BCP-47 code
*/
function main(filename, encoding, sourceLanguage, targetLanguage) {
// [START media_translation_translate_from_file]
const fs = require('fs');

// Imports the CLoud Media Translation client library
const {
SpeechTranslationServiceClient,
} = require('@google-cloud/media-translation');

// Creates a client
const client = new SpeechTranslationServiceClient();

async function translate_from_file() {
/**
* 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 sourceLanguage = 'BCP-47 source language code, e.g. en-US';
// const targetLangauge = 'BCP-47 target language code, e.g. es-ES';

const config = {
audioConfig: {
audioEncoding: encoding,
sourceLanguageCode: sourceLanguage,
targetLanguageCode: targetLanguage,
},
};

// First request needs to have only a streaming config, no data.
const initialRequest = {
streamingConfig: config,
audioContent: null,
};

const readStream = fs.createReadStream(filename, {
highWaterMark: 4096,
encoding: 'base64',
});

const chunks = [];
readStream
.on('data', chunk => {
const request = {
streamingConfig: config,
audioContent: chunk.toString(),
};
chunks.push(request);
})
.on('close', () => {
// Config-only request should be first in stream of requests
stream.write(initialRequest);
for (let i = 0; i < chunks.length; i++) {
stream.write(chunks[i]);
}
stream.end();
});

const stream = client.streamingTranslateSpeech().on('data', response => {
const {result} = response;
if (result.textTranslationResult.isFinal) {
console.log(
`\nFinal translation: ${result.textTranslationResult.translation}`
);
console.log(`Final recognition result: ${result.recognitionResult}`);
} else {
console.log(
`\nPartial translation: ${result.textTranslationResult.translation}`
);
console.log(`Partial recognition result: ${result.recognitionResult}`);
}
});

// [END media_translation_translate_from_file]
}
translate_from_file();
}

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

0 comments on commit 28ee4a0

Please sign in to comment.