#android #api-bindings #aaudio #opensles

oboe

Safe interface for oboe an android library for low latency audio IO

14 releases

0.6.1 Mar 3, 2024
0.5.0 Jan 17, 2023
0.4.6 Apr 30, 2022
0.4.5 Jan 11, 2022
0.1.0 Jan 7, 2020

#13 in Audio

Download history 11066/week @ 2023-12-23 9472/week @ 2023-12-30 11755/week @ 2024-01-06 13618/week @ 2024-01-13 12302/week @ 2024-01-20 13009/week @ 2024-01-27 14791/week @ 2024-02-03 13931/week @ 2024-02-10 14027/week @ 2024-02-17 12611/week @ 2024-02-24 16378/week @ 2024-03-02 15753/week @ 2024-03-09 16549/week @ 2024-03-16 17674/week @ 2024-03-23 17391/week @ 2024-03-30 16290/week @ 2024-04-06

69,750 downloads per month
Used in 449 crates (2 directly)

Apache-2.0

745KB
14K SLoC

Rust 9K SLoC // 0.1% comments C++ 5.5K SLoC // 0.2% comments

Rust bindings for Oboe library

github Crates.io Package Docs.rs API Docs License: Apache-2.0 CI Status

Safe Rust interface for Oboe High-Performance Audio library for Android. Also it provides interface for some platform APIs significant to Audio IO.

Oboe is a C++ library which makes it easy to build high-performance audio apps on Android. It was created primarily to allow developers to target a simplified API that works across multiple API levels back to API level 16 (Jelly Bean).

Crate features

  • java-interface Add interface for some Android platform APIs.
  • generate-bindings Generate bindings at compile-time. By default the pregenerated bindings will be used.
  • compile-library Compile oboe C++ library at compile-time using cmake. By default the precompiled library will be used.
  • shared-link Use shared linking. By default the static Oboe libarary will be used.

The crate already has pregenerated bindings and precompiled static libraries for the following Android targets:

  • armv7
  • aarch64
  • i686
  • x86_64

Build issues

The clang-sys crate uses llvm-config for searching libclang library and preparing C/C++ compiler configuration. In order to get proper setup you should add llvm-config to your executables search path.

In case of using tools with libclang under the hood like bindgen you must be sure in proper your setup. Otherwise you get an errors related to missing headers or definitions.

To build applications you need recent version of cargo-apk, which supports latest Android SDK (28+) and NDK (20+). Don't forget to set ANDROID_SDK_ROOT environment variable with paths to installed SDK.

For building host crates which requires C-compiler you may also set HOST_CC environment variable with path to your C-compiler.

Usage example

Playing sine wave in asynchronous (callback-driven) mode:

use oboe::{
    AudioOutputCallback,
    AudioOutputStream,
    AudioStreamBuilder,
    DataCallbackResult,
    PerformanceMode,
    SharingMode,
    Mono,
};

// Structure for sound generator
pub struct SineWave {
    frequency: f32,
    gain: f32,
    phase: f32,
    delta: Option<f32>,
}

// Default constructor for sound generator
impl Default for SineWave {
    fn default() -> Self {
        Self {
            frequency: 440.0,
            gain: 0.5,
            phase: 0.0,
            delta: None,
        }
    }
}

// Audio output callback trait implementation
impl AudioOutputCallback for SineWave {
    // Define type for frames which we would like to process
    type FrameType = (f32, Mono);

    // Implement sound data output callback
    fn on_audio_ready(&mut self, stream: &mut dyn AudioOutputStream, frames: &mut [f32]) -> DataCallbackResult {
        // Configure out wave generator
        if self.delta.is_none() {
            let sample_rate = stream.get_sample_rate() as f32;
            self.delta = (self.frequency * 2.0 * PI / sample_rate).into();
            println!("Prepare sine wave generator: samplerate={}, time delta={}", sample_rate, self.delta.unwrap());
        }

        let delta = self.delta.unwrap();

        // Generate audio frames to fill the output buffer
        for frame in frames {
            *frame = self.gain * self.phase.sin();
            self.phase += delta;
            while self.phase > 2.0 * PI {
                self.phase -= 2.0 * PI;
            }
        }

        // Notify the oboe that stream is continued
        DataCallbackResult::Continue
    }
}

// ...

// Create playback stream
let mut sine = AudioStreamBuilder::default()
    // select desired performance mode
    .set_performance_mode(PerformanceMode::LowLatency)
    // select desired sharing mode
    .set_sharing_mode(SharingMode::Shared)
    // select sound sample format
    .set_format::<f32>()
    // select channels configuration
    .set_channel_count::<Mono>()
    // set our generator as callback
    .set_callback(SineWave::default())
    // open the output stream
    .open_stream()
    .unwrap();

// Start playback
sine.start().unwrap();

// ...

Dependencies

~0.4–13MB
~110K SLoC