question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebRTC to .NetCore API, GoogleSpeechApi return nothing

See original GitHub issue

Hi,

I send my webrtc audio to my .netcore api.

Angular TypeScript

record() {
    this.isRecording = true;

    navigator.mediaDevices.getUserMedia({
      audio: true
    })
      .then((stream) => {
        var options = {
          audioBitsPerSecond: 128000,
          mimeType: 'audio/webm\;codecs=opus'
        }

        if (!MediaRecorder.isTypeSupported(options.mimeType)) options.mimeType = "audio/ogg;codecs=opus";

        this.stream = stream;
        this.mediaRecorder = new MediaRecorder(stream, options);
        this.mediaRecorder.onstop = e => {
          console.log("stop recording => OK");
          const blob = new Blob(this.chunks); 
          this.chunks.length = 0;

          console.log("blob => OK");

          var reader = new FileReader();
          reader.readAsDataURL(blob)
          reader.onloadend = () => {
            this.sampleService.sendFile(<FileDto>{
              name: 'record.webm',
              content: reader.result.split(',')[1], //because first is data:;base64 and i dont want this in my content
              contentLength: 0,
              contentType: options.mimeType
            })
              .subscribe(result => {
                console.log("sendFile=> OK");
              }, error => {
                console.error(error);
              });
          }
        }
        this.mediaRecorder.ondataavailable = e => {
          this.chunks.push(e.data);
        }
        this.mediaRecorder.start();
      });
  }

  stop() {
    this.isRecording = false;

    this.stream.getAudioTracks().forEach(track => track.stop());
    this.stream.getVideoTracks().forEach(track => track.stop());

    this.mediaRecorder.stop();
  }

C#

        [HttpPost]
        [ProducesResponseType(200)]
        public async Task<IActionResult> SendFile([FromBody] FileDto file)
        {
                       var byteContent = Convert.FromBase64String(file.Content);

            var speech = await SpeechClient.CreateAsync();
            var response = await speech.RecognizeAsync(new RecognitionConfig()
            {
                Encoding = RecognitionConfig.Types.AudioEncoding.OggOpus,
                SampleRateHertz = 48000,
                LanguageCode = "en-CA"
            }, RecognitionAudio.FromBytes(byteContent));
            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    Console.WriteLine(alternative.Transcript);
                }
            }

            return Ok();
       }

How did I know about the good SampleRateHertz? I copy the byteContent to a localFile:

            var path = "C:\\Users\\Cedric\\Downloads\\Temp\\" + Guid.NewGuid().ToString();
            if (file.ContentType.Contains("webm"))
            {
                path += ".webm";
            }
            else
            {
                path += ".ogg";
            }
            System.IO.File.WriteAllBytes(path, byteContent);

First I played it to verify that I can listen my voice, and It works! Then I open the details tab property in the file properties and I saw 48 000 kHz

How did I know that Encoding is OggOpus? When I created the mediaRecorder I specified in the option : mimeType: ‘audio/webm;codecs=opus’

It was pain in the ass to understand that because I had no knowledge about Audio in general so for those who dont know that too, It could be helpfull.

Of course, The Google Environnment Variable is set in the LaunchSettings, I added in the environmentVariables section: “GOOGLE_APPLICATION_CREDENTIALS”: “C:\MYPATH\XXX-202418-645374f15cb1.json”

About the issue I have. on the audio recorded from WebRtc, I can cleary hear “This is a test”. But I always have no answer from Google Speech, the Api doesnt failed, it just returns nothing.

Did I miss something? How can I make it work?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
jskeetcommented, May 11, 2018

Hooray! Very glad to hear that 😃

1reaction
jskeetcommented, May 11, 2018

As per your screenshot, that’s now Ogg Vorbis, not Ogg Opus. Ogg isn’t the audio format - that’s the container format. Opus is the audio format, and Google Cloud Speech supports Opus within an Ogg container.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Speech API returns empty result for some, but not ...
First check that the file is 16-bit PCM Mono and not stereo. Easy to do with http://www.audacityteam.org/ · enter image description here.
Read more >
Get started with WebRTC - web.dev
Want to try it out? WebRTC is available on desktop and mobile in Google Chrome, Safari, Firefox, and Opera. A good place to...
Read more >
Transcribe audio from streaming input
This section demonstrates how to transcribe streaming audio, like the input from a microphone, to text. Streaming speech recognition allows you to stream ......
Read more >
Real time communication with WebRTC
Introduction. WebRTC is an open source project to enable realtime communication of audio, video and data in Web and native apps.
Read more >
Web Speech API - MDN Web Docs - Mozilla
The Web Speech API enables you to incorporate voice data into web apps. The Web Speech API has two parts: SpeechSynthesis (Text-to-Speech), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found