Skip to content

Commit

Permalink
refactor: move files around
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanleiby committed Dec 17, 2023
1 parent 6b2fb8c commit 4197739
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 25 deletions.
4 changes: 0 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# TODO

- [ ] loop editing
- [x] clicking to add remove notes via UI, and properly scheduling audio for that
- [x] having it work while playing, not just paused
- [ ] read loop data from file -- see `voices.rs`
- [ ] capture and display user timing input for beat -- see `input.rs` (will want a new data model for user activity, e.g. note/timing/velocity data)
- [ ] save user timing data to a file (e.g. start with flatfile, someday a DB)
- [ ] use a midi library for input -- see `input.rs`
Expand Down
5 changes: 1 addition & 4 deletions res/loops/bulls_on_parade_1.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"metronome": [
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0,
14.0, 15.0
],
"metronome": [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0],
"closed_hihat": [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0],
"snare": [4.0, 12.0],
"kick": [0.0, 1.0, 6.0, 7.0, 10.0, 13.0],
Expand Down
7 changes: 7 additions & 0 deletions res/loops/bulls_on_parade_1b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"metronome": [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0],
"closed_hihat": [],
"snare": [4.0, 12.0],
"kick": [0.0, 1.0, 6.0, 7.0, 10.0, 13.0],
"open_hihat": [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0]
}
7 changes: 7 additions & 0 deletions res/loops/bulls_on_parade_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"metronome": [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0],
"closed_hihat": [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0],
"snare": [4.0, 12.0],
"kick": [0.0, 13.0, 2.0, 9.0, 11.0],
"open_hihat": []
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 13 additions & 16 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ impl Audio {
let tick_to_schedule = current + TICK_SCHEDULE_AHEAD;

for pair in [
(&voices.metronome, "click"),
(&voices.closed_hihat, "closed-hihat"),
(&voices.snare, "snare"),
(&voices.kick, "kick"),
(&voices.open_hihat, "open-hihat"),
(&voices.metronome, "res/sounds/click.wav"),
(&voices.closed_hihat, "res/sounds/closed-hihat.wav"),
(&voices.snare, "res/sounds/snare.wav"),
(&voices.kick, "res/sounds/kick.wav"),
(&voices.open_hihat, "res/sounds/open-hihat.wav"),
] {
let (voice, instrument_name) = pair;
schedule_audio(
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Audio {

fn schedule_audio(
notes: &Vec<f64>,
instrument_name: &str,
sound_path: &str,
manager: &mut AudioManager,
clock: &ClockHandle,
last_scheduled_tick: f64,
Expand All @@ -123,23 +123,23 @@ fn schedule_audio(
let next_beat = tick_to_schedule % BEATS_PER_LOOP;
debug!(
"Scheduling {} from {} to {}",
instrument_name, prev_beat, next_beat
sound_path, prev_beat, next_beat
);
let loop_num = (last_scheduled_tick / BEATS_PER_LOOP) as i32; // floor
for note in notes.iter() {
if note > &prev_beat && note <= &next_beat {
schedule_note(note, loop_num, clock, manager, instrument_name);
schedule_note(note, loop_num, clock, manager, sound_path);
};

// handle wrap-around case
if next_beat < prev_beat {
// from prev_beat to end of loop
if *note > prev_beat && *note <= BEATS_PER_LOOP as f64 {
schedule_note(note, loop_num, clock, manager, instrument_name);
schedule_note(note, loop_num, clock, manager, sound_path);
}
// from start of loop to next beat
if *note >= 0. && *note <= next_beat {
schedule_note(note, loop_num + 1, clock, manager, instrument_name);
schedule_note(note, loop_num + 1, clock, manager, sound_path);
}
}
}
Expand All @@ -150,15 +150,12 @@ fn schedule_note(
loop_num: i32,
clock: &ClockHandle,
manager: &mut AudioManager,
instrument_name: &str,
sound_path: &str,
) {
let note_tick = (*note + (loop_num as f64) * BEATS_PER_LOOP) as u64;
debug!(
"\tScheduling {} ({}) at {}",
instrument_name, note, note_tick
);
debug!("\tScheduling {} ({}) at {}", sound_path, note, note_tick);
let sound = StaticSoundData::from_file(
format!("res/{}.wav", instrument_name),
sound_path,
StaticSoundSettings::new().start_time(ClockTime {
clock: clock.id(),
ticks: note_tick,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn window_conf() -> Conf {
#[macroquad::main(window_conf)]
async fn main() -> Result<(), Box<dyn Error>> {
// Setup global game state
let l = "res/loops/bulls_on_parade_1.json";
let l = "res/loops/bulls_on_parade_1b.json";
// let l = "res/loops/samba.json";
let mut voices = Voices::new_from_file(l)?;

Expand Down

0 comments on commit 4197739

Please sign in to comment.