concentus icon indicating copy to clipboard operation
concentus copied to clipboard

Code settings for 16 KHZ MONO 256 bit rate audio

Open viju2008 opened this issue 4 years ago • 3 comments

Dear Sir,

I have am recording PCM 16 BIT audio using Android at Sample rate of 16000 . Now i have raw PCM Audio.

For this case what should be the value of the variables OpusEncoder encoder = new OpusEncoder(16000, 1, OpusApplication.OPUS_APPLICATION_VOIP); encoder.setBitrate(32000); int packetSamples = 960;

I have set the above values however when i listen to the OPUS audio it is playing at a very faster speed. What am i doing wrong.

I am converting it using the sample code provided.

public static void main(File fin, File fout) {

    try {
        FileInputStream fileIn = new FileInputStream(fin);

        OpusEncoder encoder = new OpusEncoder(16000, 1, OpusApplication.OPUS_APPLICATION_VOIP);
       encoder.setBitrate(32000);
        encoder.setSignalType(OpusSignal.OPUS_SIGNAL_VOICE);
        encoder.setComplexity(0);

        FileOutputStream fileOut = new FileOutputStream(fout);
        OpusInfo info = new OpusInfo();
        info.setNumChannels(1);
        info.setSampleRate(16000);

        OpusTags tags = new OpusTags();
        //tags.setVendor("Concentus");
        //tags.addComment("title", "A test!");
        OpusFile file = new OpusFile(fileOut, info, tags);
        int packetSamples = 960;
        byte[] inBuf = new byte[packetSamples * 2 * 2];
        byte[] data_packet = new byte[1275];
        long start = System.currentTimeMillis();
        while (fileIn.available() >= inBuf.length) {
            int bytesRead = fileIn.read(inBuf, 0, inBuf.length);
            short[] pcm = BytesToShorts(inBuf, 0, inBuf.length);
            int bytesEncoded = encoder.encode(pcm, 0, packetSamples, data_packet, 0, 1275);
            byte[] packet = new byte[bytesEncoded];
            System.arraycopy(data_packet, 0, packet, 0, bytesEncoded);
            OpusAudioData data = new OpusAudioData(packet);
            file.writeAudioData(data);
        }
        file.close();

        long end = System.currentTimeMillis();
        System.out.println("Time was " + (end - start) + "ms");
        fileIn.close();
        //fileOut.close();
        System.out.println("Done!");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } catch (OpusException e) {
        System.out.println(e.getMessage());
    }
}

viju2008 avatar Apr 17 '20 12:04 viju2008

Where is OpusFile coming from? If you are trying to make OggOpus formatted output, why not just use Concentus.OggFile? I suspect your error has something to do with buffer sizes / interleaving / sample rate, and using the OggFile library will handle most of that for you.

lostromb avatar Apr 17 '20 17:04 lostromb

Short answer: I think the bug is in new byte[packetSamples * 2 * 2]; . You seem to be multiplying samples by (2 bytes per sample) * (2 channels), except you only have one channel. So it should be new byte[packetSamples * 2];

lostromb avatar Apr 17 '20 17:04 lostromb

Short answer: I think the bug is in new byte[packetSamples * 2 * 2]; . You seem to be multiplying samples by (2 bytes per sample) * (2 channels), except you only have one channel. So it should be new byte[packetSamples * 2];

Yes that was the issue ... now it is coming at proper speed...

What should be the value for

Encoder.setbitrate for 16khz sample rate , 16 bit pcm encoded audio recorded from android mobile.

OpusEncoder encoder = new OpusEncoder(16000, 1, OpusApplication.OPUS_APPLICATION_VOIP); encoder.setBitrate(32000);

I just set it to 32000 because i felt it might be the right value..

Will increasing the bitrate to 48000 or 96000 help. The Wav file has higher volume than the opus. Will increasing bitrate improve quality ? Also some times the last 1-2 seconds of the audio in a 10 sec audio are not audible. The Audio Player dosent play the last 1-2 seconds and stops. However, the speech recognition engine i am using is able to decode the audio but not correctly always, but the engine is able to understand that there is audio in the last 1-2 seconds even though I cannot hear it . Is there anything that i need to change more so that the last 1-2 seconds is hearable.

viju2008 avatar Apr 19 '20 14:04 viju2008