Skip to content

Commit

Permalink
WIP: Performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tsionyx committed Feb 9, 2024
1 parent 7ce5f8f commit 794a564
Show file tree
Hide file tree
Showing 6 changed files with 646 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ repository = "https://github.com/tsionyx/musik"
[dependencies]
num-rational = "0.4"
num-integer = "0.1"
num-traits = "0.2"
enum-map ="2.6"
enum-iterator = "1.4"
ordered-float = "4.2"
itertools = "0.12"
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub use music::{
adapters::TrillOptions,
duration::Dur,
interval::{AbsPitch, Interval, Octave},
performance::Performance,
phrases::PhraseAttribute,
pitch::{Pitch, PitchClass},
rests, Music,
};
8 changes: 8 additions & 0 deletions src/music/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ impl From<Octave> for AbsPitch {
}
}

impl Add for AbsPitch {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}

impl Add<Interval> for AbsPitch {
type Output = Self;

Expand Down
41 changes: 30 additions & 11 deletions src/music/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ use super::instruments::InstrumentName;
pub(crate) mod adapters;
pub(crate) mod duration;
pub(crate) mod interval;
pub(crate) mod performance;
pub(crate) mod phrases;
pub(crate) mod pitch;

use self::{
duration::Dur,
interval::{AbsPitch, Octave},
interval::{Interval, Octave},
performance::NoteAttribute,
phrases::PhraseAttribute,
pitch::{Pitch, PitchClass},
};

Expand All @@ -33,8 +37,7 @@ impl<P> Primitive<P> {
}
}

#[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
pub struct PlayerName(String);
pub type PlayerName = String;

#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
pub enum Mode {
Expand All @@ -45,9 +48,9 @@ pub enum Mode {
#[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
pub enum Control {
Tempo(Ratio<u8>), // scale the tempo
Transpose(AbsPitch),
Transpose(Interval),
Instrument(InstrumentName),
//TODO: Phrase(Vec<PhraseAttribute>),
Phrase(Vec<PhraseAttribute>),
Player(PlayerName),
KeySig(PitchClass, Mode), // key signature and mode
}
Expand Down Expand Up @@ -128,17 +131,17 @@ impl<P> Music<P> {
self.with(Control::Tempo(tempo.into()))
}

pub fn with_transpose(self, abs_pitch: AbsPitch) -> Self {
self.with(Control::Transpose(abs_pitch))
pub fn with_transpose(self, delta: Interval) -> Self {
self.with(Control::Transpose(delta))
}

pub fn with_instrument(self, name: impl Into<InstrumentName>) -> Self {
self.with(Control::Instrument(name.into()))
}

//fn with_phrase(self, attributes: Vec<PhraseAttribute>) -> Self {
// self.with(Control::Phrase(attributes))
//}
pub fn with_phrase(self, attributes: Vec<PhraseAttribute>) -> Self {
self.with(Control::Phrase(attributes))
}

pub fn with_player(self, name: PlayerName) -> Self {
self.with(Control::Player(name))
Expand Down Expand Up @@ -203,7 +206,7 @@ impl<P> Music<P> {
}
}

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Volume(pub u8);

impl Volume {
Expand All @@ -222,6 +225,22 @@ impl Music {
}
}

pub type AttrNote = (Pitch, Vec<NoteAttribute>);

pub type MusicAttr = Music<AttrNote>;

impl From<Music> for MusicAttr {
fn from(value: Music) -> Self {
value.map(|pitch| (pitch, vec![]))
}
}

impl From<Music<(Pitch, Volume)>> for MusicAttr {
fn from(value: Music<(Pitch, Volume)>) -> Self {
value.map(|(pitch, vol)| (pitch, vec![NoteAttribute::Volume(vol)]))
}
}

impl Music {
def_note_constructor![Aff, Af, A, As, Ass];
def_note_constructor![Bff, Bf, B, Bs, Bss];
Expand Down
Loading

0 comments on commit 794a564

Please sign in to comment.