How to use the Google Speech API from Codename One?

I want to record audio from a phone and then send it to Google without using the streaming API. I can record using Capture.captureAudio (), but then I do not know what the audio encoding and sampling rate are, since they are necessary for the api request . How can I get the audio encoding and sample rate so that I can send them using my API request?

+4
source share
1 answer

If you check the sources on Android, it will record in AMR-WB

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder.setOutputFile(temp.getAbsolutePath());

API Google AMR-WB, .

, AMR-WB 3GPP, 3GPP, :

// #!AMR\n
private static byte[] AMR_MAGIC_HEADER = {0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a};


public byte[] convert3gpDataToAmr(byte[] data) {
    if (data == null) {
        return null;
    }

    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    // read FileTypeHeader
    FileTypeBox ftypHeader = new FileTypeBox(bis);
    // You can check if it is correct here
    // read MediaDataHeader
    MediaDataBox mdatHeader = new MediaDataBox(bis);
    // You can check if it is correct here
    int rawAmrDataLength = mdatHeader.getDataLength();
    int fullAmrDataLength = AMR_MAGIC_HEADER.length + rawAmrDataLength;
    byte[] amrData = new byte[fullAmrDataLength];
    System.arraycopy(AMR_MAGIC_HEADER, 0, amrData, 0, AMR_MAGIC_HEADER.length);
    bis.read(amrData, AMR_MAGIC_HEADER.length, rawAmrDataLength);
    return amrData;
}

, AMR-WB , API, .

+1

Source: https://habr.com/ru/post/1654252/


All Articles