songbird/input/adapters/cached/
mod.rs

1//! In-memory, shared input sources for reuse between calls, fast seeking, and
2//! direct Opus frame passthrough.
3
4mod compressed;
5mod decompressed;
6mod error;
7mod hint;
8mod memory;
9mod util;
10
11pub(crate) use self::util::*;
12pub use self::{compressed::*, decompressed::*, error::*, hint::*, memory::*};
13
14use crate::constants::*;
15use crate::input::utils;
16use audiopus::Bitrate;
17use std::{mem, time::Duration};
18use streamcatcher::{Config as ScConfig, GrowthStrategy};
19
20/// Estimates the cost, in B/s, of audio data compressed at the given bitrate.
21#[must_use]
22pub fn compressed_cost_per_sec(bitrate: Bitrate) -> usize {
23    let framing_cost_per_sec = AUDIO_FRAME_RATE * mem::size_of::<u16>();
24
25    let bitrate_raw = match bitrate {
26        Bitrate::BitsPerSecond(i) => i,
27        Bitrate::Auto => 64_000,
28        Bitrate::Max => 512_000,
29    } as usize;
30
31    (bitrate_raw / 8) + framing_cost_per_sec
32}
33
34/// Calculates the cost, in B/s, of raw floating-point audio data.
35#[must_use]
36pub fn raw_cost_per_sec(stereo: bool) -> usize {
37    utils::timestamp_to_byte_count(Duration::from_secs(1), stereo)
38}
39
40/// Provides the default config used by a cached source.
41///
42/// This maps to the default configuration in [`streamcatcher`], using
43/// a constant chunk size of 5s worth of audio at the given bitrate estimate.
44///
45/// [`streamcatcher`]: https://docs.rs/streamcatcher/0.1.0/streamcatcher/struct.Config.html
46#[must_use]
47pub fn default_config(cost_per_sec: usize) -> ScConfig {
48    ScConfig::new().chunk_size(GrowthStrategy::Constant(5 * cost_per_sec))
49}