old school music tracker audio backend
1pub mod audio_processing;
2pub mod file;
3mod live_audio;
4pub mod project;
5pub mod sample;
6
7use std::{
8 fmt::Debug,
9 num::{NonZero, NonZeroU16},
10 ops::Deref,
11 time::Duration,
12};
13
14use simple_left_right::{WriteGuard, Writer};
15
16use crate::{
17 audio_processing::{Interpolation, playback::PlaybackStatus},
18 live_audio::LiveAudio,
19 project::{
20 note_event::NoteEvent,
21 song::{Song, SongOperation, ValidOperation},
22 },
23 sample::Sample,
24};
25
26#[derive(Debug, Clone, Copy)]
27pub enum ToWorkerMsg {
28 Playback(PlaybackSettings),
29 StopPlayback,
30 PlayEvent(NoteEvent),
31 StopLiveNote,
32 SetInterpolation(Interpolation),
33}
34
35// don't leak the dependency types
36pub struct StatusRecv<StreamData>(rt_write_lock::Reader<(Option<PlaybackStatus>, StreamData)>);
37
38// don't leak the dependency types
39pub struct StatusRead<'a, StreamData>(
40 rt_write_lock::ReadGuard<'a, (Option<PlaybackStatus>, StreamData)>,
41);
42
43impl<S> Deref for StatusRead<'_, S> {
44 type Target = (Option<PlaybackStatus>, S);
45
46 fn deref(&self) -> &Self::Target {
47 &self.0
48 }
49}
50
51impl<S> StatusRecv<S> {
52 pub fn try_get(&mut self) -> Option<StatusRead<'_, S>> {
53 self.0.try_read().map(|r| StatusRead(r))
54 }
55}
56
57pub struct StreamSend(rtrb::Producer<ToWorkerMsg>);
58
59impl StreamSend {
60 #[expect(clippy::result_unit_err)]
61 pub fn try_msg_worker(&mut self, msg: ToWorkerMsg) -> Result<(), ()> {
62 match self.0.push(msg) {
63 Ok(_) => Ok(()),
64 Err(_) => Err(()),
65 }
66 }
67}
68
69#[derive(Debug, Default)]
70pub(crate) struct Collector {
71 samples: Vec<Sample>,
72}
73
74impl Collector {
75 pub fn add_sample(&mut self, sample: Sample) {
76 self.samples.push(sample);
77 }
78
79 fn collect(&mut self) {
80 self.samples.retain(|s| s.strongcount() != 1);
81 }
82}
83
84pub struct AudioManager {
85 song: Writer<Song, ValidOperation>,
86 gc: Collector,
87 buffer_time: Duration,
88}
89
90impl AudioManager {
91 pub fn new(song: Song) -> Self {
92 let mut gc = Collector::default();
93 for (_, sample) in song.samples.iter().flatten() {
94 gc.add_sample(sample.clone());
95 }
96 let left_right = simple_left_right::Writer::new(song);
97
98 Self {
99 song: left_right,
100 gc,
101 buffer_time: Duration::ZERO,
102 }
103 }
104
105 /// If this returns None, waiting buffer_time should (weird threading issues aside) always be enough time
106 /// and it should return Some after that.
107 pub fn try_edit_song(&mut self) -> Option<SongEdit<'_>> {
108 self.song.try_lock().map(|song| SongEdit {
109 song,
110 gc: &mut self.gc,
111 })
112 }
113
114 pub fn get_song(&self) -> &Song {
115 self.song.read()
116 }
117
118 pub fn collect_garbage(&mut self) {
119 self.gc.collect();
120 }
121
122 /// If the config specifies more than two channels only the first two will be filled with audio.
123 /// The rest get silence.
124 ///
125 /// The callback in for example Cpal provides an additional arguement, where a timestamp is give.
126 /// That should ba handled by wrapping this function in another callback, where this argument could
127 /// then be ignored or send somewhere for processing. This Sending needs to happen wait-free!! There are
128 /// a couple of libaries that can do this, i would recommend triple_buffer or rtrb.
129 ///
130 /// The OutputConfig has to match the config of the AudioStream that will call this. If for example the
131 /// buffer size is different Panics may occur.
132 ///
133 /// In my testing i noticed that when using Cpal with non-standard buffer sizes Cpal would just give
134 /// another buffer size. This may also lead to panics.
135 ///
136 /// ## Panics
137 ///
138 /// Will panic if there is an active stream.
139 pub fn get_callback<
140 Sample: dasp::sample::Sample + dasp::sample::FromSample<f32>,
141 StreamData: Clone,
142 >(
143 &mut self,
144 config: OutputConfig,
145 initial_stream_data: StreamData,
146 ) -> (
147 impl FnMut(&mut [Sample], StreamData) + use<Sample, StreamData>,
148 Duration,
149 StatusRecv<StreamData>,
150 StreamSend,
151 ) {
152 const SEND_SIZE: usize = 10;
153 let mut from_worker = rt_write_lock::Reader::new((None, initial_stream_data));
154 // let from_worker = triple_buffer::triple_buffer(&(None, initial_stream_data));
155 let to_worker = rtrb::RingBuffer::new(SEND_SIZE);
156 let reader = self.song.build_reader().expect("another stream is active");
157
158 let audio_worker = LiveAudio::<StreamData>::new(
159 reader,
160 to_worker.1,
161 // doesn't panic because i just created the reader
162 from_worker.get_writer().unwrap(),
163 config,
164 );
165 let buffer_time =
166 Duration::from_millis((config.buffer_size * 1000 / config.sample_rate).into());
167
168 self.buffer_time = buffer_time;
169
170 (
171 audio_worker.get_typed_callback(),
172 buffer_time,
173 StatusRecv(from_worker),
174 StreamSend(to_worker.0),
175 )
176 }
177
178 /// Buffer time of the last created callback.
179 pub fn last_buffer_time(&self) -> Duration {
180 self.buffer_time
181 }
182}
183
184/// the changes made to the song will be made available to the playing live audio as soon as
185/// this struct is dropped.
186///
187/// With this you can load the full song without ever playing a half initialised state
188/// when doing mulitple operations this object should be kept as it is
189#[derive(Debug)]
190pub struct SongEdit<'a> {
191 song: WriteGuard<'a, Song, ValidOperation>,
192 gc: &'a mut Collector,
193}
194
195impl SongEdit<'_> {
196 pub fn apply_operation(&mut self, op: SongOperation) -> Result<(), SongOperation> {
197 let valid_operation = ValidOperation::new(op, self.gc, self.song.read())?;
198 self.song.apply_op(valid_operation);
199 Ok(())
200 }
201
202 pub fn song(&self) -> &Song {
203 self.song.read()
204 }
205
206 /// Finish the changes and publish them to the live playing song.
207 /// Equivalent to std::mem::drop(SongEdit)
208 pub fn finish(self) {}
209}
210
211#[derive(Debug, Clone, Copy)]
212pub struct OutputConfig {
213 pub buffer_size: u32,
214 pub channel_count: NonZeroU16,
215 pub sample_rate: NonZero<u32>,
216 pub interpolation: Interpolation,
217}
218
219#[derive(Debug, Clone, Copy)]
220pub enum PlaybackSettings {
221 Pattern { idx: u8, should_loop: bool },
222 Order { idx: u16, should_loop: bool },
223}