Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New file stop music #26

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions audio/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ impl Conn {
*self.export_state.lock() != ExportState::NotExporting
}

/// When a new save file is loaded or a new file is opened, stop playing music if any music is playing.
pub fn on_new_file(&mut self, state: &State) {
let play_state = *self.play_state.lock();
if let PlayState::Playing(_) = play_state {
self.stop_music(&state.music);
}
}

/// Schedule MIDI events and start to play music.
fn start_music(&mut self, state: &State) {
// Get the start time.
Expand Down
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 0.2.2

- There was an input bug where the play/start key (spacebar) was sometimes unresponsive for the first few presses. This is because audio was still decaying from a previous play, meaning that technically the previous play was still ongoing. I fixed it.
- Fixed: There was an input bug where the play/start key (spacebar) was sometimes unresponsive for the first few presses. This is because audio was still decaying from a previous play, meaning that technically the previous play was still ongoing.
- Fixed: When a new file is created or when a new save file loaded, the app didn't reset correctly.

## 0.2.1

Expand Down
5 changes: 5 additions & 0 deletions common/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ impl Time {
pub fn samples_to_ppq(&self, samples: u64, framerate: f32) -> u64 {
((self.bpm.get_f() * samples as f32) / (BPM_TO_SECONDS * framerate) * PPQ_F) as u64
}

pub fn reset(&mut self) {
self.cursor = 0;
self.playback = 0;
}
}

impl Default for Time {
Expand Down
18 changes: 17 additions & 1 deletion common/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct View {
zoom_increments: HashMap<EditMode, usize>,
/// The default zoom index.
initial_zoom_index: usize,
#[serde(skip)]
initial_dn: [u8; 2],
}

impl View {
Expand Down Expand Up @@ -95,6 +97,7 @@ impl View {
zoom_index,
zoom_increments,
initial_zoom_index,
initial_dn: dn,
}
}

Expand Down Expand Up @@ -172,7 +175,20 @@ impl View {
}
// Get the time delta.
let dt = self.zoom_levels[self.zoom_index.get()];
self.dt = [self.dt[0], dt];
self.dt = [self.dt[0], self.dt[0] + dt];
}

/// Reset the view.
pub fn reset(&mut self) {
// Reset the zoom.
self.zoom_index.set(self.initial_zoom_index);
// Reset the time.
let dt = self.zoom_levels[self.initial_zoom_index];
self.dt = [0, dt];
// Reset the notes.
self.dn = self.initial_dn;
// Single track view.
self.single_track = true;
}

/// Returns the note delta.
Expand Down
16 changes: 15 additions & 1 deletion io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,22 @@ impl IO {
}
// New file.
if input.happened(&InputEvent::NewFile) {
// This prevents the previous file from being overwritten.
paths_state.saves.filename = None;
// Stop playing music.
conn.on_new_file(state);
// Reset the music.
state.music = Music::default();
// Clear the selection.
state.select_mode = SelectMode::Single(None);
// Reset the view.
state.view.reset();
// Reset the time.
state.time.reset();
// Clear the undo/redo stacks.
self.undo.clear();
self.redo.clear();
state.unsaved_changes = false;
}
// Open file.
else if input.happened(&InputEvent::OpenFile) {
Expand Down Expand Up @@ -416,7 +430,7 @@ impl IO {
false
}

/// Open a save file from a path.
/// Open a save file from a path. This is called from main.rs
pub fn load_save(
&self,
save_path: &Path,
Expand Down
2 changes: 2 additions & 0 deletions io/src/open_file_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ impl Panel for OpenFilePanel {
if let Some(selected) = paths_state.children.selected {
// Disable the panel.
self.disable(state);
// Stop the music.
conn.on_new_file(state);
// Get the path.
let path = paths_state.children.children[selected].path.clone();
// Read the save file.
Expand Down
Loading