MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / speechTranscribeDiarization

Function speechTranscribeDiarization

speech/recognize.js:723–777  ·  view source on GitHub ↗
(fileName)

Source from the content-addressed store, hash-verified

721}
722
723async function speechTranscribeDiarization(fileName) {
724 // [START speech_transcribe_diarization]
725 const fs = require('fs');
726
727 // Imports the Google Cloud client library
728 const speech = require('@google-cloud/speech');
729
730 // Creates a client
731 const client = new speech.SpeechClient();
732
733 // Set config for Diarization
734 const diarizationConfig = {
735 enableSpeakerDiarization: true,
736 maxSpeakerCount: 2,
737 };
738
739 const config = {
740 encoding: 'LINEAR16',
741 sampleRateHertz: 8000,
742 languageCode: 'en-US',
743 diarizationConfig: diarizationConfig,
744 model: 'phone_call',
745 };
746
747 /**
748 * TODO(developer): Uncomment the following lines before running the sample.
749 */
750 // const fileName = 'Local path to audio file, e.g. /path/to/audio.raw';
751
752 const audio = {
753 content: fs.readFileSync(fileName).toString('base64'),
754 };
755
756 const request = {
757 config: config,
758 audio: audio,
759 };
760
761 const [response] = await client.recognize(request);
762 const transcription = response.results
763 .map(result => result.alternatives[0].transcript)
764 .join('\n');
765 console.log(`Transcription: ${transcription}`);
766 console.log('Speaker Diarization:');
767 const result = response.results[response.results.length - 1];
768 const wordsInfo = result.alternatives[0].words;
769 // Note: The transcript within each result is separate and sequential per result.
770 // However, the words list within an alternative includes all the words
771 // from all the results thus far. Thus, to get all the words with speaker
772 // tags, you only have to take the words list from the last result:
773 wordsInfo.forEach(a =>
774 console.log(` word: ${a.word}, speakerTag: ${a.speakerTag}`)
775 );
776 // [END speech_transcribe_diarization]
777}
778
779require(`yargs`) // eslint-disable-line
780 .demand(1)

Callers 1

recognize.jsFile · 0.70

Calls 1

toStringMethod · 0.80

Tested by

no test coverage detected