Driver/Input: Migrate audio backend to Symphonia (#89)
This extensive PR rewrites the internal mixing logic of the driver to use symphonia for parsing and decoding audio data, and rubato to resample audio. Existing logic to decode DCA and Opus formats/data have been reworked as plugins for symphonia. The main benefit is that we no longer need to keep yt-dlp and ffmpeg processes alive, saving a lot of memory and CPU: all decoding can be done in Rust! In exchange, we now need to do a lot of the HTTP handling and resumption ourselves, but this is still a huge net positive. `Input`s have been completely reworked such that all default (non-cached) sources are lazy by default, and are no longer covered by a special-case `Restartable`. These now span a gamut from a `Compose` (lazy), to a live source, to a fully `Parsed` source. As mixing is still sync, this includes adapters for `AsyncRead`/`AsyncSeek`, and HTTP streams. `Track`s have been reworked so that they only contain initialisation state for each track. `TrackHandles` are only created once a `Track`/`Input` has been handed over to the driver, replacing `create_player` and related functions. `TrackHandle::action` now acts on a `View` of (im)mutable state, and can request seeks/readying via `Action`. Per-track event handling has also been improved -- we can now determine and propagate the reason behind individual track errors due to the new backend. Some `TrackHandle` commands (seek etc.) benefit from this, and now use internal callbacks to signal completion. Due to associated PRs on felixmcfelix/songbird from avid testers, this includes general clippy tweaks, API additions, and other repo-wide cleanup. Thanks go out to the below co-authors. Co-authored-by: Gnome! <45660393+GnomedDev@users.noreply.github.com> Co-authored-by: Alakh <36898190+alakhpc@users.noreply.github.com>
This commit is contained in:
@@ -1,29 +1,102 @@
|
||||
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
|
||||
use songbird::{constants::*, input::Input};
|
||||
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
|
||||
use songbird::{
|
||||
constants::*,
|
||||
driver::{
|
||||
bench_internals::mixer::{mix_logic, state::DecodeState},
|
||||
MixMode,
|
||||
},
|
||||
input::{codecs::*, Input, LiveInput, Parsed},
|
||||
};
|
||||
use std::io::Cursor;
|
||||
use symphonia_core::audio::{AudioBuffer, Layout, SampleBuffer, Signal, SignalSpec};
|
||||
|
||||
pub fn mix_one_frame(c: &mut Criterion) {
|
||||
let floats = utils::make_sine(STEREO_FRAME_SIZE, true);
|
||||
let mut raw_buf = [0f32; STEREO_FRAME_SIZE];
|
||||
let floats = utils::make_sine(1 * STEREO_FRAME_SIZE, true);
|
||||
|
||||
c.bench_function("Mix stereo source", |b| {
|
||||
b.iter_batched_ref(
|
||||
|| black_box(Input::float_pcm(true, floats.clone().into())),
|
||||
|input| {
|
||||
input.mix(black_box(&mut raw_buf), black_box(1.0));
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
let symph_layout = MixMode::Stereo.into();
|
||||
|
||||
c.bench_function("Mix mono source", |b| {
|
||||
b.iter_batched_ref(
|
||||
|| black_box(Input::float_pcm(false, floats.clone().into())),
|
||||
|input| {
|
||||
input.mix(black_box(&mut raw_buf), black_box(1.0));
|
||||
let mut symph_mix = AudioBuffer::<f32>::new(
|
||||
MONO_FRAME_SIZE as u64,
|
||||
symphonia_core::audio::SignalSpec::new_with_layout(SAMPLE_RATE_RAW as u32, symph_layout),
|
||||
);
|
||||
let mut resample_scratch = AudioBuffer::<f32>::new(
|
||||
MONO_FRAME_SIZE as u64,
|
||||
SignalSpec::new_with_layout(SAMPLE_RATE_RAW as u32, Layout::Stereo),
|
||||
);
|
||||
|
||||
let mut group = c.benchmark_group("Stereo Target");
|
||||
|
||||
for (pres, hz) in [("", 48_000), (" (Resample)", 44_100)] {
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new(format!("Stereo Source{}", pres), hz),
|
||||
&hz,
|
||||
|b, i| {
|
||||
b.iter_batched_ref(
|
||||
|| black_box(make_src(&floats, 2, *i)),
|
||||
|(ref mut input, ref mut local_input)| {
|
||||
symph_mix.clear();
|
||||
symph_mix.render_reserved(Some(MONO_FRAME_SIZE));
|
||||
resample_scratch.clear();
|
||||
|
||||
black_box(mix_logic::mix_symph_indiv(
|
||||
&mut symph_mix,
|
||||
&mut resample_scratch,
|
||||
input,
|
||||
local_input,
|
||||
black_box(1.0),
|
||||
None,
|
||||
));
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
);
|
||||
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new(format!("Mono Source{}", pres), hz),
|
||||
&hz,
|
||||
|b, i| {
|
||||
b.iter_batched_ref(
|
||||
|| black_box(make_src(&floats, 1, *i)),
|
||||
|(ref mut input, ref mut local_input)| {
|
||||
symph_mix.clear();
|
||||
symph_mix.render_reserved(Some(MONO_FRAME_SIZE));
|
||||
resample_scratch.clear();
|
||||
|
||||
black_box(mix_logic::mix_symph_indiv(
|
||||
&mut symph_mix,
|
||||
&mut resample_scratch,
|
||||
input,
|
||||
local_input,
|
||||
black_box(1.0),
|
||||
None,
|
||||
));
|
||||
},
|
||||
BatchSize::SmallInput,
|
||||
)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn make_src(src: &Vec<u8>, chans: u32, hz: u32) -> (Parsed, DecodeState) {
|
||||
let local_input = Default::default();
|
||||
|
||||
let adapted: Input =
|
||||
songbird::input::RawAdapter::new(Cursor::new(src.clone()), hz, chans).into();
|
||||
let promoted = match adapted {
|
||||
Input::Live(l, _) => l.promote(&CODEC_REGISTRY, &PROBE),
|
||||
_ => panic!("Failed to create a guaranteed source."),
|
||||
};
|
||||
let parsed = match promoted {
|
||||
Ok(LiveInput::Parsed(parsed)) => parsed,
|
||||
Err(e) => panic!("AR {:?}", e),
|
||||
_ => panic!("Failed to create a guaranteed source."),
|
||||
};
|
||||
|
||||
(parsed, local_input)
|
||||
}
|
||||
|
||||
criterion_group!(benches, mix_one_frame);
|
||||
|
||||
Reference in New Issue
Block a user