Skip to content

Commit

Permalink
Merge pull request #34 from nbennett320/develop
Browse files Browse the repository at this point in the history
refactoring audio playback, event dispatching, adding simple interpreter
  • Loading branch information
nbennett320 committed Apr 2, 2024
2 parents 66d12d0 + 738cbf1 commit 7397ef0
Show file tree
Hide file tree
Showing 23 changed files with 2,836 additions and 607 deletions.
2 changes: 1 addition & 1 deletion engine/src/app/workspaces/playlist.rs
Expand Up @@ -12,7 +12,7 @@ pub fn calc_sample_offset(
let width = max_bound_x - min_bound_x;
// let adjusted_min_x = 0. as f32;
let adjusted_max_x = width;
let adjusted_drop_x = drop_x - min_bound_x;
let adjusted_drop_x = drop_x - min_bound_x - 100.;
println!("drop_x: {}, min_bound_x: {}, max_bound_x: {}, adjusted_drop_x: {}", drop_x, min_bound_x, max_bound_x, adjusted_drop_x);

let ratio = adjusted_drop_x / max_bound_x;
Expand Down
164 changes: 83 additions & 81 deletions engine/src/daw/daw.rs
Expand Up @@ -15,7 +15,7 @@ use std::sync::{
use std::sync::atomic::{Ordering};
use std::thread;
use std::time::{
Duration,
Duration,
Instant,
};
use rodio::{Source};
Expand Down Expand Up @@ -152,88 +152,90 @@ pub fn run_playlist(state_ref: &Arc<state::InnerState>) {
.unwrap()
.interval_of_subdivision(daw_core::timing::WholeNote::new());

pool.exec(move || {
thread::spawn(move || loop {
println!("tick: {}ms", &state.playlist.audiograph.lock().unwrap().current_offset.unwrap().as_millis());

// play metronome if enabled
// if state.metronome_enabled.load(Ordering::SeqCst)
// && state.playlist.playing() {
// play_metronome(&state);
// }

// run ahead n milliseconds and schedule the next
// samples in the audio graph to be played
let mut audiograph_ref = state.playlist.audiograph.lock().unwrap();

audiograph_ref.run_for(tempo_interval);
// let m = audiograph_ref.buffer_for(tempo_interval);
// match m {
// Some(x) => {
// futures::executor::block_on(play_buffer(x.take_duration(tempo_interval).buffered()));
// }
// None => {}
// }

// sleep this thread for the length of a single beat
thread::sleep(tempo_interval);
let graph = &state.playlist.audiograph.lock().unwrap();
graph.run();
// pool.exec(move || {
// thread::spawn(move || loop {
// println!("tick: {}ms", &state.playlist.audiograph.lock().unwrap().current_offset.unwrap().as_millis());

// // play metronome if enabled
// // if state.metronome_enabled.load(Ordering::SeqCst)
// // && state.playlist.playing() {
// // play_metronome(&state);
// // }

// // run ahead n milliseconds and schedule the next
// // samples in the audio graph to be played
// let mut audiograph_ref = state.playlist.audiograph.lock().unwrap();

// audiograph_ref.run_for(tempo_interval);
// // let m = audiograph_ref.buffer_for(tempo_interval);
// // match m {
// // Some(x) => {
// // futures::executor::block_on(play_buffer(x.take_duration(tempo_interval).buffered()));
// // }
// // None => {}
// // }

// // sleep this thread for the length of a single beat
// thread::sleep(tempo_interval);

let curr = audiograph_ref.current_offset.unwrap();

if state.playlist.playing() {
audiograph_ref.set_current_offset(Some(curr + tempo_interval));
} else {
audiograph_ref.set_current_offset(Some(curr));
}

// increment the beat counter
let current_time_signature =
state.playlist.time_signature.lock().unwrap();
let current_beat =
state.playlist.current_beat.load(Ordering::SeqCst);
let next_beat = (current_beat + 1) % current_time_signature.numerator;
// let curr = audiograph_ref.current_offset.unwrap();

// if state.playlist.playing() {
// audiograph_ref.set_current_offset(Some(curr + tempo_interval));
// } else {
// audiograph_ref.set_current_offset(Some(curr));
// }

// // increment the beat counter
// let current_time_signature =
// state.playlist.time_signature.lock().unwrap();
// let current_beat =
// state.playlist.current_beat.load(Ordering::SeqCst);
// let next_beat = (current_beat + 1) % current_time_signature.numerator;

if state.playlist.playing() {
// loop the playlist by setting the current offset to zero,
// if looping is enabled, otherwise, add and store the next
// current beat
if state.playlist.loop_enabled.load(Ordering::SeqCst)
&& ((state.playlist.total_beats.load(Ordering::SeqCst) + 1) as u64
>= state.playlist.max_beats.load(Ordering::SeqCst)) {
audiograph_ref.set_current_offset(Some(Duration::ZERO));

state
.playlist
.current_beat
.store(next_beat, Ordering::SeqCst);
state
.playlist
.total_beats
.store(0, Ordering::SeqCst);
} else {
state
.playlist.current_beat
.store(next_beat, Ordering::SeqCst);
state
.playlist.total_beats
.fetch_add(1, Ordering::SeqCst);
}
} else {
state.playlist.current_beat.store(0, Ordering::SeqCst);
}

println!(
"current beat: {}, total beats played: {}, looping: {}",
state.playlist.current_beat.load(Ordering::SeqCst),
state.playlist.total_beats.load(Ordering::SeqCst),
state.playlist.loop_enabled.load(Ordering::SeqCst)
);

if !state.playlist.playing() {
break;
}
});
});
// if state.playlist.playing() {
// // loop the playlist by setting the current offset to zero,
// // if looping is enabled, otherwise, add and store the next
// // current beat
// if state.playlist.loop_enabled.load(Ordering::SeqCst)
// && ((state.playlist.total_beats.load(Ordering::SeqCst) + 1) as u64
// >= state.playlist.max_beats.load(Ordering::SeqCst)) {
// audiograph_ref.set_current_offset(Some(Duration::ZERO));

// state
// .playlist
// .current_beat
// .store(next_beat, Ordering::SeqCst);
// state
// .playlist
// .total_beats
// .store(0, Ordering::SeqCst);
// } else {
// state
// .playlist.current_beat
// .store(next_beat, Ordering::SeqCst);
// state
// .playlist.total_beats
// .fetch_add(1, Ordering::SeqCst);
// }
// } else {
// state.playlist.current_beat.store(0, Ordering::SeqCst);
// }

// println!(
// "current beat: {}, total beats played: {}, looping: {}",
// state.playlist.current_beat.load(Ordering::SeqCst),
// state.playlist.total_beats.load(Ordering::SeqCst),
// state.playlist.loop_enabled.load(Ordering::SeqCst)
// );

// if !state.playlist.playing() {
// break;
// }
// });
// });

println!("continuing");
}
Expand Down

0 comments on commit 7397ef0

Please sign in to comment.