Synchronously recognize words
Stay organized with collections
Save and categorize content based on your preferences.
Detects speech in the audio file.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["Detects speech in the audio file.\n\nCode sample \n\nNode.js\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nFor more information, see the\n[Speech-to-Text Node.js API\nreference documentation](/nodejs/docs/reference/speech/latest).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Imports the Google Cloud client library\n const fs = require('fs');\n const speech = require('https://cloud.google.com/nodejs/docs/reference/speech/latest/overview.html');\n\n // Creates a client\n const client = new speech.https://cloud.google.com/nodejs/docs/reference/speech/latest/overview.html();\n\n /**\n * TODO(developer): Uncomment the following lines before running the sample.\n */\n // const filename = 'Local path to audio file, e.g. /path/to/audio.raw';\n // const encoding = 'Encoding of the audio file, e.g. LINEAR16';\n // const sampleRateHertz = 16000;\n // const languageCode = 'BCP-47 language code, e.g. en-US';\n\n const config = {\n enableWordTimeOffsets: true,\n encoding: encoding,\n sampleRateHertz: sampleRateHertz,\n languageCode: languageCode,\n };\n const audio = {\n content: fs.readFileSync(filename).toString('base64'),\n };\n\n const request = {\n config: config,\n audio: audio,\n };\n\n // Detects speech in the audio file\n const [response] = await client.recognize(request);\n response.results.forEach(result =\u003e {\n console.log('Transcription: ', result.alternatives[0].transcript);\n result.alternatives[0].words.forEach(wordInfo =\u003e {\n // NOTE: If you have a time offset exceeding 2^32 seconds, use the\n // wordInfo.{x}Time.seconds.high to calculate seconds.\n const startSecs =\n `${wordInfo.startTime.seconds}` +\n '.' +\n wordInfo.startTime.nanos / 100000000;\n const endSecs =\n `${wordInfo.endTime.seconds}` +\n '.' +\n wordInfo.endTime.nanos / 100000000;\n console.log(`Word: ${wordInfo.word}`);\n console.log(`\\t ${startSecs} secs - ${endSecs} secs`);\n });\n });\n\nRuby\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # audio_file_path = \"Path to file on which to perform speech recognition\"\n\n require \"google/cloud/speech\"\n\n speech = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-speech-v1/latest/Google-Cloud-Speech.html.https://cloud.google.com/ruby/docs/reference/google-cloud-speech/latest/Google-Cloud-Speech.html version: :v1\n\n audio_file = File.binread audio_file_path\n\n config = { encoding: :LINEAR16,\n sample_rate_hertz: 16_000,\n language_code: \"en-US\",\n enable_word_time_offsets: true }\n audio = { content: audio_file }\n\n response = speech.recognize config: config, audio: audio\n\n results = response.results\n\n alternatives = results.first.alternatives\n alternatives.each do |alternative|\n puts \"Transcription: #{alternative.transcript}\"\n\n alternative.words.each do |word|\n start_time = word.start_time.seconds + (word.start_time.nanos / 1_000_000_000.0)\n end_time = word.end_time.seconds + (word.end_time.nanos / 1_000_000_000.0)\n\n puts \"Word: #{word.word} #{start_time} #{end_time}\"\n end\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=speech)."]]