Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record and playback with Opus Codec in Android

I'm working on a project that need to record and playback with Opus Codec, I search a lot but I can't find any demo/example using that solution. I find a demo having encoder but can't find the decoder. I only find the source code of this codec using C, can you help me?

like image 273
Phu Tang Avatar asked Nov 20 '14 14:11

Phu Tang


1 Answers

Hello that demo is a good place to start, he was realy close to solving it. However each package must be sent separetly from the encoder to the decoder. Instead of saving all to a file and then read them back without any regard of package start.
I modified the code to also write the number of encoded bytes and when I decode, I read first the number of bytes in each packet and then the payload.

Here is the modified code in OpusEncoder.java

public void write( short[] buffer ) throws IOException
    {
        byte[] encodedBuffer = new byte[buffer.length];
        int lenEncodedBytes = this.nativeEncodeBytes( buffer , encodedBuffer);
        Log.i(TAG,"encoded "+lenEncodedBytes+" bytes");
        if (lenEncodedBytes > 0)
        {
            this.out.write(lenEncodedBytes);
            this.out.write( encodedBuffer, 0, lenEncodedBytes );
        }
        else
        {
            Log.e( TAG, "Error during Encoding. Error Code: " +  lenEncodedBytes);

            throw new IOException( "Error during Encoding. Error Code: " +  lenEncodedBytes );
        }
    }


Here is the modified code in OpusDecoder.java

        byte[] encodedBuffer;
        int bytesEncoded=this.in.read();
        int bytesDecoded=0;

        Log.d( TAG, bytesEncoded + " bytes read from input stream" );
        if ( bytesEncoded >= 0 )
        {
            encodedBuffer=new byte[bytesEncoded];
            int bytesRead = this.in.read( encodedBuffer );
            bytesDecoded = nativeDecodeBytes( encodedBuffer , buffer);
            Log.d( TAG, bytesEncoded + " bytes decoded" );
        }
like image 116
Cristian Avatar answered Sep 22 '22 07:09

Cristian