Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
6.5 kB · 229 lines
Rust
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230// std required for fuzzing#![cfg_attr(not(feature = "arbitrary"), no_std)]#![cfg_attr(feature = "rtsan", feature(sanitize))]
pub mod audio_processing;pub mod file;mod live_audio;pub mod project;pub mod sample;
use core::{fmt::Debug, num::NonZero, ops::Deref, time::Duration};
extern crate alloc;
use alloc::vec::Vec;
use simple_left_right::{WriteGuard, Writer};
use crate::{ audio_processing::{Interpolation, playback::PlaybackStatus}, live_audio::LiveAudio, project::{ note_event::NoteEvent, pattern_order::PatternNumber, song::{Song, SongOperation, ValidOperation}, }, sample::Sample,};
#[derive(Debug, Clone, Copy)]pub enum ToWorkerMsg { Playback(PlaybackSettings), StopPlayback, PlayEvent(NoteEvent), StopLiveNote, SetInterpolation(Interpolation),}
// don't leak the dependency typespub struct StatusRecv<StreamData>(rt_write_lock::Reader<(Option<PlaybackStatus>, StreamData)>);
// don't leak the dependency typespub struct StatusRead<'a, StreamData>( rt_write_lock::ReadGuard<'a, (Option<PlaybackStatus>, StreamData)>,);
impl<S> Deref for StatusRead<'_, S> { type Target = (Option<PlaybackStatus>, S);
fn deref(&self) -> &Self::Target { &self.0 }}
impl<S> StatusRecv<S> { pub fn try_get(&mut self) -> Option<StatusRead<'_, S>> { self.0.try_read().map(|r| StatusRead(r)) }}
pub struct StreamSend(rtrb::Producer<ToWorkerMsg>);
impl StreamSend { #[expect(clippy::result_unit_err)] pub fn try_msg_worker(&mut self, msg: ToWorkerMsg) -> Result<(), ()> { match self.0.push(msg) { Ok(_) => Ok(()), Err(_) => Err(()), } }}
#[derive(Debug, Default)]pub(crate) struct Collector { samples: Vec<Sample>,}
impl Collector { pub fn add_sample(&mut self, sample: Sample) { self.samples.push(sample); }
fn collect(&mut self) { self.samples.retain(|s| s.strongcount() != 1); }}
pub struct AudioManager { song: Writer<Song, ValidOperation>, gc: Collector, buffer_time: Duration,}
impl AudioManager { pub fn new(song: Song) -> Self { let mut gc = Collector::default(); for (_, sample) in song.samples.iter().flatten() { gc.add_sample(sample.clone()); } let left_right = simple_left_right::Writer::new(song);
Self { song: left_right, gc, buffer_time: Duration::ZERO, } }
/// If this returns None, waiting buffer_time should (weird threading issues aside) always be enough time /// and it should return Some after that. pub fn try_edit_song(&mut self) -> Option<SongEdit<'_>> { self.song.try_lock().map(|song| SongEdit { song, gc: &mut self.gc, }) }
pub fn get_song(&self) -> &Song { self.song.read() }
pub fn collect_garbage(&mut self) { self.gc.collect(); }
/// If the config specifies more than two channels only the first two will be filled with audio. /// The rest get silence. /// /// The callback in for example Cpal provides an additional arguement, where a timestamp is give. /// That should ba handled by wrapping this function in another callback, where this argument could /// then be ignored or send somewhere for processing. This Sending needs to happen wait-free!! There are /// a couple of libaries that can do this, i would recommend triple_buffer or rtrb. /// /// The OutputConfig has to match the config of the AudioStream that will call this. If for example the /// buffer size is different Panics may occur. /// /// In my testing i noticed that when using Cpal with non-standard buffer sizes Cpal would just give /// another buffer size. This may also lead to panics. /// /// ## Panics /// /// Will panic if there is an active stream. pub fn get_callback<StreamData: Clone>( &mut self, config: OutputConfig, initial_stream_data: StreamData, ) -> ( LiveAudio<StreamData>, Duration, StatusRecv<StreamData>, StreamSend, ) { const SEND_SIZE: usize = 10; let mut from_worker = rt_write_lock::Reader::new((None, initial_stream_data)); // let from_worker = triple_buffer::triple_buffer(&(None, initial_stream_data)); let to_worker = rtrb::RingBuffer::new(SEND_SIZE); let reader = self.song.build_reader().expect("another stream is active");
let audio_worker = LiveAudio::<StreamData>::new( reader, to_worker.1, // doesn't panic because i just created the reader from_worker.get_writer().unwrap(), config, ); let buffer_time = Duration::from_millis((config.buffer_size * 1000 / config.sample_rate).into());
self.buffer_time = buffer_time;
( audio_worker, buffer_time, StatusRecv(from_worker), StreamSend(to_worker.0), ) }
/// Buffer time of the last created callback. pub fn last_buffer_time(&self) -> Duration { self.buffer_time }}
/// the changes made to the song will be made available to the playing live audio as soon as/// this struct is dropped.////// With this you can load the full song without ever playing a half initialised state/// when doing mulitple operations this object should be kept as it is#[derive(Debug)]pub struct SongEdit<'a> { song: WriteGuard<'a, Song, ValidOperation>, gc: &'a mut Collector,}
impl SongEdit<'_> { pub fn apply_operation(&mut self, op: SongOperation) -> Result<(), SongOperation> { let valid_operation = ValidOperation::new(op, self.gc, self.song.read())?; self.song.apply_op(valid_operation); Ok(()) }
pub fn song(&self) -> &Song { self.song.read() }
/// Finish the changes and publish them to the live playing song. /// Equivalent to std::mem::drop(SongEdit) pub fn finish(self) {}}
#[derive(Debug, Clone, Copy)]pub struct OutputConfig { pub buffer_size: u32, pub sample_rate: NonZero<u32>, pub interpolation: Interpolation,}
#[derive(Debug, Clone, Copy)]pub enum PlaybackSettings { Pattern { idx: PatternNumber, should_loop: bool, }, Order { idx: u16, should_loop: bool, },}