How to correctly send audio files to Google Speech API?

Viewed 1029

I'm trying to implement Google Speech API in Android by following this demo: https://github.com/GoogleCloudPlatform/android-docs-samples

I was able to successfully reproduce the example in my app by using the given "audio.raw" file located in R.raw, and everything works perfectly. However, when I try to use my own audio files, it returns "API successful" without any transcription text. I'm not sure if it has to do with the files' path or the encoding, so I'll include information on both just in case.

Encoding

My audio files are obtained by recording a voice through MediaRecorder. These are the settings:

    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_WB);
    myAudioRecorder.setAudioSamplingRate(16000);
    myAudioRecorder.setAudioEncodingBitRate(16000);
    myAudioRecorder.setAudioChannels(1);
    myAudioRecorder.setOutputFile(outputFile);

SpeechService's recognizeInputStream() function in the API:

mApi.recognize(
      RecognizeRequest.newBuilder()
              .setConfig(RecognitionConfig.newBuilder()
                      .setEncoding(RecognitionConfig.AudioEncoding.AMR_WB) //originally it was LINEAR16
                      .setLanguageCode("en-US")
                      .setSampleRateHertz(16000)
                      .build())
              .setAudio(RecognitionAudio.newBuilder()
                      .setContent(ByteString.readFrom(stream))
                      .build())
              .build(),
      mFileResponseObserver);

Encoding guidelines by Google: https://cloud.google.com/speech/docs/best-practices

From what I understand, I can use AMR_WB and 16kHz instead of the default LINEAR16, I'm just not sure if I'm doing it right.

Path

This is the example that is fully working (with the audio file from the repo):

mSpeechService.recognizeInputStream(getResources().openRawResource(R.raw.audio));

However, none of the following options work, even with the exact same file:

InputStream inputStream = new URL("[website]/test/audio.raw").openStream();
mSpeechService.recognizeInputStream(inputStream);

Neither:

Uri uri = Uri.parse("android.resource://[package]/raw/audio");
InputStream inputStream = getActivity().getContentResolver().openInputStream(uri); //"getActivity()" because this is in a Fragment
mSpeechService.recognizeInputStream(inputStream);

To be clear, the result on the above paths is the same as on my custom audio files: "API successful" with no transcription. One of the options I have tried for my custom audio files, with the same thing happening, is this:

FileInputStream fis = new FileInputStream(filePath);
mSpeechService.recognizeInputStream(fis);

The only reason I'm not 100% sure the problem is in the path is because if the API is returning with success, then the file was found in the specified path. The problem should be the encoding, but then it's weird that the same file ("audio.raw") sent in different ways produces different results.

Anyway, thank you in advance! :)

EDIT:

To be clear, it's not that it returns an empty string in the transcription. It just never enters the "onSpeechRecognized" function that also exists in the demo, so no transcription is given.

0 Answers
Related