#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#[cfg(feature = "alloc")]
extern crate alloc;
pub use rusty_esp_core as esp_core;
pub mod chip;
pub mod codec;
pub mod elements;
pub mod pipeline;
pub mod ring;
pub mod source;
pub use pipeline::{Element, Pipeline};
pub use ring::RingBuffer;
pub use source::{AudioSink, AudioSource};
pub mod prelude {
pub use rusty_esp_core::prelude::*;
pub use crate::codec::pcm::convert as convert_pcm;
pub use crate::elements::{
Agc, AgcConfig, Biquad, BiquadKind, Convert, DcBlock, EnergyVad, Gain, LinearResampler,
MonoToStereo, StereoToMono, VadConfig,
};
pub use crate::pipeline::{Element, Pipeline};
pub use crate::ring::RingBuffer;
pub use crate::source::{AudioSink, AudioSource, CountingSink, SineSource};
}
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub use rusty_esp_dsp::sample::rms_dbfs_i16;
#[inline]
pub(crate) fn put_i16(out: &mut [u8], v: i16) {
let b = v.to_le_bytes();
out[0] = b[0];
out[1] = b[1];
}
#[inline]
pub(crate) fn get_i16(b: &[u8]) -> i16 {
i16::from_le_bytes([b[0], b[1]])
}
#[inline]
pub(crate) fn sat16(v: i32) -> i16 {
v.clamp(i32::from(i16::MIN), i32::from(i16::MAX)) as i16
}
#[inline]
pub(crate) fn round_sat16(v: f32) -> i16 {
let r = libm::roundf(v);
if r >= 32767.0 {
i16::MAX
} else if r <= -32768.0 {
i16::MIN
} else {
r as i16
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rounding_and_saturation() {
assert_eq!(round_sat16(0.4), 0);
assert_eq!(round_sat16(0.5), 1);
assert_eq!(round_sat16(-0.5), -1);
assert_eq!(round_sat16(40000.0), i16::MAX);
assert_eq!(round_sat16(-40000.0), i16::MIN);
assert_eq!(sat16(70000), i16::MAX);
assert_eq!(sat16(-70000), i16::MIN);
}
}