Crate oboe

source · []
Expand description

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();

// ...

Structs

AudioDeviceInfojava-interface

  • The Android audio device info
  • The audio stream for asynchronous (callback-driven) mode
  • Factory for an audio stream.
  • Factory for an audio stream.
  • Reference to the audio stream for passing to callbacks
  • The audio stream for synchronous (blocking) mode
  • The default (optimal) audio streaming values.
  • On API 16 to 26 OpenSL ES will be used.
  • When using OpenSL ES the optimal values for sample_rate and
  • frames_per_burst are not known by the native code.
  • On API 17+ these values should be obtained from the AudioManager using this code:
  • The time at which the frame at position was presented
  • The input direction marker
  • The single mono channel configuration marker
  • The output direction marker
  • The dual stereo channels configuration marker
  • Unspecified marker type for use everywhere
  • The version info
  • Enums

  • The underlying audio API used by the audio stream.
  • AudioDeviceDirectionjava-interface

  • The direction of audio device
  • AudioDeviceTypejava-interface

  • The type of audio device
  • AudioFeaturejava-interface

  • The Android audio features
  • The format of audio samples.
  • The channel count of the audio stream.
  • Use of this enum is convenient to avoid “magic”
  • numbers when specifying the channel count.
  • For example, you can write
  • builder.set_channel_count(ChannelCount::Stereo)
  • rather than `builder.set_channel_count(2).
  • The ContentType attribute describes what you are playing.
  • It expresses the general category of the content. This information is optional.
  • But in case it is known (for instance {@link Movie} for a
  • movie streaming service or {@link Speech} for
  • an audio book application) this information might be used by the audio framework to
  • enforce audio focus.
  • Note that these match the equivalent values in AudioAttributes in the Android Java API.
  • This attribute only has an effect on Android API 28+.
  • The result of an audio callback.
  • The direction of the stream.
  • The error of an operation.
  • Defines the audio source.
  • An audio source defines both a default physical source of audio signal, and a recording
  • configuration.
  • Note that these match the equivalent values in MediaRecorder.AudioSource in the Android Java API.
  • This attribute only has an effect on Android API 28+.
  • The performance mode of the audio stream.
  • Specifies the quality of the sample rate conversion performed by Oboe.
  • Higher quality will require more CPU load.
  • Higher quality conversion will probably be implemented using a sinc based resampler.
  • This attribute can be used to allocate a session ID to the audio stream.
  • This attribute only has an effect on Android API 28+.
  • The sharing mode of the audio stream.
  • The state of the audio stream.
  • The Usage attribute expresses why you are playing a sound, what is this sound used for.
  • This information is used by certain platforms or routing policies
  • to make more refined volume or routing decisions.
  • Note that these match the equivalent values in AudioAttributes in the Android Java API.
  • This attribute only has an effect on Android API 28+.
  • Constants

  • The default number of nanoseconds to wait for when performing state change operations on the
  • stream, such as start and stop.
  • See AudioStream::start_with_timeout
  • The number of milliseconds in a second. 1,000.
  • The number of nanoseconds in a microsecond. 1,000.
  • The number of nanoseconds in a millisecond. 1,000,000.
  • The number of nanoseconds in a second. 1,000,000,000.
  • Traits

  • This trait defines a callback interface for:
  • moving data to/from an audio stream using on_audio_ready
  • being alerted when a stream has an error using on_error_* methods
  • The stream which is used for audio input
  • The stream which is used for async audio input
  • The stream which can be used for audio input in synchronous mode
  • This trait defines a callback interface for:
  • moving data to/from an audio stream using on_audio_ready
  • being alerted when a stream has an error using on_error_* methods
  • The stream which has pause/flush capabilities
  • The stream which is used for async audio output
  • The stream which can be used for audio output in synchronous mode
  • Base trait for Oboe audio stream.
  • Base trait containing parameters for audio streams and builders.
  • Safe base trait for Oboe audio stream.
  • The trait for channel count marker types
  • The trait for direction marker types
  • The traint for format marker types
  • The trait for frame type marker types
  • Type Definitions

  • The result of an operation with value
  • The result of operation without value