1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! Lightweight game audio
//!
//! ```no_run
//! let (mut scene_handle, mut scene) = oddio::SpatialScene::new();
//!
//! // In audio callback:
//! # let data = &mut [][..];
//! # let output_sample_rate = 44100;
//! let out_frames = oddio::frame_stereo(data);
//! oddio::run(&mut scene, output_sample_rate, out_frames);
//!
//! // In game logic:
//! # let frames = [];
//! # let sample_rate = 44100;
//! # let position = [0.0, 0.0, 0.0].into();
//! # let velocity = [0.0, 0.0, 0.0].into();
//! let frames = oddio::FramesSignal::from(oddio::Frames::from_slice(sample_rate, &frames));
//! let mut handle = scene_handle
//!     .play(frames, oddio::SpatialOptions { position, velocity, ..Default::default() });
//!
//! // When position/velocity changes:
//! handle.set_motion(position, velocity, false);
//! ```
//!
//! To get started, review [the `examples`
//! subdirectory](https://github.com/Ralith/oddio/tree/main/examples) in the crate source.
//!
//! Key primitives:
//! - [`Frames`] stores static audio data, which can be played with a [`FramesSignal`]
//! - [`Mixer`] allows multiple signals to be played concurrently and controlled during playback
//! - [`SpatialScene`] is a mixer that spatializes its signals
//! - [`run`] writes frames from a [`Signal`] into an output buffer

#![allow(unused_imports)]
#![warn(missing_docs)]
#![no_std]

extern crate alloc;
#[cfg(not(feature = "no_std"))]
extern crate std;

mod adapt;
mod constant;
mod cycle;
mod downmix;
mod fader;
mod frame;
mod frames;
mod gain;
mod math;
mod mixer;
mod reinhard;
mod ring;
mod set;
mod signal;
mod sine;
mod smooth;
mod spatial;
mod speed;
mod spsc;
mod stream;
mod swap;
mod tanh;

pub use adapt::{Adapt, AdaptOptions};
pub use constant::Constant;
pub use cycle::Cycle;
pub use downmix::Downmix;
pub use fader::{Fader, FaderControl};
pub use frame::Frame;
pub use frames::*;
pub use gain::{FixedGain, Gain, GainControl};
pub use mixer::*;
pub use reinhard::Reinhard;
use set::*;
pub use signal::*;
pub use sine::*;
pub use smooth::{Interpolate, Smoothed};
pub use spatial::*;
pub use speed::{Speed, SpeedControl};
pub use stream::{Stream, StreamControl};
pub use tanh::Tanh;

/// Unitless instantaneous sound wave amplitude measurement
pub type Sample = f32;

/// Populate `out` with frames from `signal` at `sample_rate`
///
/// Convenience wrapper for [`Signal::sample`].
pub fn run<S: Signal + ?Sized>(signal: &mut S, sample_rate: u32, out: &mut [S::Frame]) {
    let interval = 1.0 / sample_rate as f32;
    signal.sample(interval, out);
}

/// Convert a slice of interleaved stereo data into a slice of stereo frames
///
/// Useful for adapting output buffers obtained externally.
pub fn frame_stereo(xs: &mut [Sample]) -> &mut [[Sample; 2]] {
    unsafe { core::slice::from_raw_parts_mut(xs.as_mut_ptr() as _, xs.len() / 2) }
}

fn flatten_stereo(xs: &mut [[Sample; 2]]) -> &mut [Sample] {
    unsafe { core::slice::from_raw_parts_mut(xs.as_mut_ptr() as _, xs.len() * 2) }
}