Skip to content

Commit

Permalink
chore: move away from hard-coded BEATS_PER_LOOP (cont5)
Browse files Browse the repository at this point in the history
Did the following manual test, which worked!

> Create a non-default duration loop.
>
> Verify the app works when swapping from one to the other, i.e.
> - the audio loops,
> - the visuals look correct,
> - etc
  • Loading branch information
nathanleiby committed Nov 19, 2024
1 parent 04c999e commit cf7c3a1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions assets/loops/short_loop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"bpm": 120,
"voices": {
"closed_hihat": [1, 2, 3, 4, 5, 6, 7],
"snare": [1, 3, 5, 7],
"kick": [0, 2, 4, 6],
"open_hihat": [],
"ride": [],
"crash": [0]
},
"id": "cb104eca-2d38-4e6b-ad74-ae5f624d9484",
"name": "Short Loop",
"length_in_beats": 8
}
5 changes: 5 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ impl Audio {
self.configured_audio_latency_seconds = latency;
}

// beats per loop
pub fn set_beats_per_loop(&mut self, val: usize) {
self.beats_per_loop = val;
}

// TODO: Move this outside and then use it to summary loop accuracy
fn check_if_new_beat_or_new_loop(&mut self) {
// For debugging, print when we pass an integer beat
Expand Down
4 changes: 4 additions & 0 deletions src/egui_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl UIState {
self.bpm = bpm;
}

pub fn set_beats_per_loop(&mut self, value: usize) {
self.beats_per_loop = value;
}

pub fn set_audio_latency_s(&mut self, offset: f32) {
self.latency_offset_s = offset;
}
Expand Down
5 changes: 4 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub fn compute_ui_state(gs: &GameState, audio: &Audio, midi_device_name: &str) -
ui_state.set_current_loop(audio.current_loop() as usize);
ui_state.set_is_playing(!audio.is_paused());
ui_state.set_bpm(audio.get_bpm() as f32);
ui_state.set_beats_per_loop(gs.beats_per_loop);
ui_state.set_audio_latency_s(audio.get_configured_audio_latency_seconds() as f32);
ui_state.set_user_hits(&audio.user_hits);
ui_state.set_desired_hits(&gs.voices);
Expand Down Expand Up @@ -213,6 +214,7 @@ pub fn process_user_events(
correct_margin: &mut f64,
miss_margin: &mut f64,
midi_input: &mut MidiInputHandler,
beats_per_loop: &mut usize,
) -> Result<(), Box<dyn Error>> {
for event in events {
info!("[user event] {:?}", event);
Expand Down Expand Up @@ -275,7 +277,8 @@ pub fn process_user_events(
let new_loop = loops.as_slice()[*loop_num].clone().1;
*voices = Voices::new_from_voices_old_model(&new_loop.voices);
audio.set_bpm(new_loop.bpm as f64);

*beats_per_loop = new_loop.length_in_beats;
audio.set_beats_per_loop(new_loop.length_in_beats);
*selected_loop_idx = *loop_num;
}
Events::ToggleDevToolsVisibility => {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
&mut gs.correct_margin,
&mut gs.miss_margin,
&mut midi_input,
&mut gs.beats_per_loop,
)?;

audio.schedule(&gs.voices).await?;
Expand Down

0 comments on commit cf7c3a1

Please sign in to comment.