Skip to content

Commit

Permalink
Move all "note" code to one note.rs location
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Nov 18, 2024
1 parent 272b2c5 commit 94a464e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 68 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ Under heavy development - Updates forthcoming
* Take into account `time-only` attributes
* Scan text attributes for common items (rall., etc.)
* Scan `sound` attributes for items maybe not recognized otherwise (rall., etc.)
* Remove `pub(crate)` from Phrase and make `MultiVoice` okay with this
* Make composition timeslice iterator a real iterator (and check that staff timeslices line up correctly)
61 changes: 59 additions & 2 deletions amm_sdk/src/note/note.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{Accidental, Duration, Pitch};
use crate::context::{generate_id, Key};
use crate::context::{generate_id, Key, Tempo};
use crate::modification::{NoteModification, NoteModificationType};
use crate::temporal::Timeslice;
use amm_internal::amm_prelude::*;
use amm_macros::{JsonDeserialize, JsonSerialize};
#[cfg(target_arch = "wasm32")]
Expand All @@ -15,10 +16,26 @@ pub struct Note {
pub pitch: Pitch,
pub duration: Duration,
pub accidental: Accidental,
pub(crate) modifications: BTreeSet<NoteModification>,
modifications: BTreeSet<NoteModification>,
}

impl Note {
#[must_use]
pub fn new(pitch: Pitch, duration: Duration, accidental: Option<Accidental>) -> Self {
Self {
id: generate_id(),
pitch,
duration,
accidental: accidental.unwrap_or_default(),
modifications: BTreeSet::new(),
}
}

#[must_use]
pub fn get_id(&self) -> usize {
self.id
}

#[must_use]
fn semitone_distance(&self, key_accidentals: [Accidental; 8]) -> i8 {
let (pitch_index, num_semitones) = self.pitch.value();
Expand Down Expand Up @@ -69,6 +86,46 @@ impl Note {
self.duration.beats(base_beat_value)
}
}

pub fn add_modification(&mut self, mod_type: NoteModificationType) -> usize {
let modification = NoteModification::new(mod_type);
let modification_id = modification.get_id();
self.modifications.replace(modification);
modification_id
}

#[must_use]
pub fn get_modification(&self, id: usize) -> Option<&NoteModification> {
self
.iter_modifications()
.find(|modification| modification.get_id() == id)
}

#[must_use]
pub fn get_beats(&self, beat_base: &Duration, tuplet_ratio: Option<f64>) -> f64 {
self.beats(beat_base.value()) * tuplet_ratio.unwrap_or(1.0)
}

#[must_use]
pub fn get_duration(&self, tempo: &Tempo, tuplet_ratio: Option<f64>) -> f64 {
self.get_beats(&tempo.base_note, tuplet_ratio) * 60.0 / f64::from(tempo.beats_per_minute)
}

pub fn remove_modification(&mut self, id: usize) -> &mut Self {
self.modifications.retain(|modification| modification.get_id() != id);
self
}

pub fn iter_modifications(&self) -> alloc::collections::btree_set::Iter<'_, NoteModification> {
self.modifications.iter()
}

#[must_use]
pub fn to_timeslice(&self) -> Timeslice {
let mut timeslice = Timeslice::new();
timeslice.add_note(self.clone());
timeslice
}
}

impl PartialEq for Note {
Expand Down
1 change: 0 additions & 1 deletion amm_sdk/src/structure/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod chord;
mod multivoice;
mod note;
mod part;
mod phrase;
mod section;
Expand Down
63 changes: 0 additions & 63 deletions amm_sdk/src/structure/note.rs

This file was deleted.

0 comments on commit 94a464e

Please sign in to comment.