Git Product home page Git Product logo

android-opus-codec's Introduction

android-opus-codec

Android wrapper around libopus 1.3.1 written on C++ and Kotlin.

Supported features:

  1. Encoding PCM audio into Opus.
  2. Decoding Opus audio into PCM.
  3. Sample rate of input audio from 8000Hz to 48000Hz.
  4. Different frame sizes.
  5. Mono or stereo input audio.
  6. Input in bytes or shorts.
  7. Output in bytes or shorts.
  8. Convert from bytes to shorts and vice versa.
  9. Bitrate setting.
  10. Complexity setting.

Supported ABIs:

armeabi-v7a, arm64-v8a, x86, x86_64

How to use

Init encoder and decoder:

val SAMPLE_RATE = Constants.SampleRate._48000()       // samlpe rate of the input audio
val CHANNELS = Constants.Channels.stereo()            // type of the input audio mono or stereo 
val APPLICATION = Constants.Application.audio()       // coding mode of the encoder
var FRAME_SIZE = Constants.FrameSize._120()           // default frame size for 48000Hz

val codec = Opus()                                    // getting Codec instance
codec.encoderInit(SAMPLE_RATE, CHANNELS, APPLICATION) // init encoder
codec.decoderInit(SAMPLE_RATE, CHANNELS)              // init decoder

Setup the encoder:

/* this step is optional because the encoder can use default values */
val COMPLEXITY = Constants.Complexity.instance(10)    // encoder's algorithmic complexity 
val BITRATE = Constants.Bitrate.max()                 // encoder's bitrate
codec.encoderSetComplexity(COMPLEXITY)                // complexity setup
codec.encoderSetBitrate(BITRATE)                      // bitrate setup

Encoding:

val frame = ...                                       // get a chunk of audio from some source as an array of bytes or shorts
val encoded = codec.encode(frame, FRAME_SIZE)         // encode the audio chunk into Opus
if (encoded != null) Log.d("Opus", "encoded chunk size: ${encoded.size}")

Decoding:

val decoded = codec.decode(encoded, FRAME_SIZE)       // decode a chunk of audio into PCM
if (decoded != null) Log.d("Opus", "decoded chunk size: ${decoded.size}")

Releasing resources when the app closes:

codec.encoderRelease()
codec.decoderRelease()

Project structure

Project consists of two modules:

  • app - here you can find a sample app that demonsrates ecoding, decoding and converting procedures by capturing an audio from device's mic and play it from a loud speaker. I recommend to check this app using a headphones, otherwise there may be echo in a hight levels of volume.
  • opus - here you can find a C++ class that interacts with libopus 1.3.1 and JNI wrapper for interacting with it from Java/Kotlin layer.

Compiled library:

  • opus.aar - it's a compiled library of opus module that mentioned above, it placed in a root directory of the project, you can easily add it to your project using gradle dependencies. First you have to place opus.aar in the libs folder of your project and then add to your build.gradle the following:
dependencies {
    api fileTree(dir: 'libs', include: '*.jar')       // this line is necessary in order to allow gradle to take opus.aar from "libs" dir
    api files('libs/opus.aar')                        // dependency for opus.aar library
    ...                                               // other dependencies
}

android-opus-codec's People

Contributors

gamelaster avatar theeasiestway avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

android-opus-codec's Issues

FEC - how to use

Thank you for this library.

We have a situation where 2G is used with VoIP and it tends to have very long latency and large packet loss and jitter.
Apparently, from what I have read, OPUS codec with Forward Error Correction (FEC) enabled can improve call quality.

Wondering if you have an example of how to use FEC (what parameters and other configuration are needed)?

Thanks.

crashed decoder on Android 6.0

A/libc: invalid address or address of corrupt block 0x557a131c90 passed to dlfree

and

A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xa1ff86ff6aff65 in tid 4654

am usage opus.aar

encoder very good

Build failed with an exception

I am getting error. I have added opus module in my project and other required files

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':opus:mergeDebugNativeLibs'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > More than one file was found with OS independent path 'lib/x86_64/libopus.so'. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
 

I have added files from your demo project

app(level) -> jni
app(level) -> libs with all arms folders

Any idea how to resolve ?

transcode m4a to opus

i've written this code but the output file fails to play



            Constants.SampleRate SAMPLE_RATE =  Constants.SampleRate.Companion._48000();
            Constants.Channels CHANNELS = Constants.Channels.Companion.mono();
            Constants.Application APPLICATION = Constants.Application.Companion.voip();
            Constants.FrameSize FRAME_SIZE = Constants.FrameSize.Companion._320();

try {
                    try (OutputStream output = new FileOutputStream(dest)) {
                        codec = new Opus();
                        codec.encoderInit(SAMPLE_RATE, CHANNELS, APPLICATION);
                        byte[] buffer = new byte[320];
                        int read;
                        while ((read = target.read(buffer)) != -1) {
                            if (isCancelled()) {
                                break;
                            }
                            byte[] encodedBuffer=  codec.encode(buffer, FRAME_SIZE);
                            int lenEncodedBytes = encodedBuffer.length;
                            if (lenEncodedBytes > 0)
                            {
                                output.write(lenEncodedBytes);
                                output.write(encodedBuffer,0,lenEncodedBytes);
                                Log.e("Encoder","Encode Bytes "+lenEncodedBytes);
                            }
                            currentProgress += lenEncodedBytes;
                            notifyProgress();
                        }
                        publishProgress(100);
                        output.flush();
                        success = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        target.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

License

I found nothing about the LICENSE.

Two OPUS instances mixing audio

I have two decoders running in my application (both mono) and they work great individually, but as soon as I try to use them at the same time the output on both are garbled, and you can clearly hear audio leaking between the decoders. Is there a singleton decoder in the core of this library? Is this to be expected?

Thanks.

Java Usage

Please provide java usage examples

What I've tried

    Opus codec = new Opus();
    Constants.SampleRate SAMPLE_RATE = new Constants.SampleRate(8000); 

'SampleRate(int)' has private access in 'com.theeasiestway.opus.Constants.SampleRate'

.aar Issue libeasyopus.so not coming with Android .jar.. Only getting libopus.so and another .so file.. but no easyopus.so file.. ALSO ANOTHER ERROR

.aar Issue libeasyopus.so not coming with Android .jar.. Only getting libopus.so and another .so file.. but no easyopus.so file

Another error:
2020-11-11 19:59:55.034 9778-9778/com.kabbodev.grnclient E/bodev.grnclien: No implementation found for int com.kabbodev.grnclient.audio.opus.Opus.encoderInit(int, int, int) (tried Java_com_kabbodev_grnclient_audio_opus_Opus_encoderInit and Java_com_kabbodev_grnclient_audio_opus_Opus_encoderInit__III)
2020-11-11 19:59:55.037 9778-9778/com.kabbodev.grnclient E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kabbodev.grnclient, PID: 9778
java.lang.UnsatisfiedLinkError: No implementation found for int com.kabbodev.grnclient.audio.opus.Opus.encoderInit(int, int, int) (tried Java_com_kabbodev_grnclient_audio_opus_Opus_encoderInit and Java_com_kabbodev_grnclient_audio_opus_Opus_encoderInit__III)

App crash due to java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

Here is the logs

2020-06-11 12:38:40.335 14318-14318/com.theeasiestway.opus E/easiestway.opu: [qarth_debug:]  get PatchStore::createDisableExceptionQarthFile method fail.
2020-06-11 12:38:40.340 14318-14318/com.theeasiestway.opus E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.theeasiestway.opus, PID: 14318
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=123, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.theeasiestway.opus/com.theeasiestway.opusapp.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5078)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5120)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
        at com.theeasiestway.opusapp.MainActivity.onRequestPermissionsResult(MainActivity.kt:208)

Just reported

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.