1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
use super::{
Note, PC, PCs, Octave, OctaveShift, Interval, NamedInterval, NamedOctaveInterval, Letter,
EnharmonicNote, Mode, Scale, Steps, Intervals, ModeIterator, Chord, RootedChord, ScaleIterator
};
use std::marker::Sized;
// General Traits
/// Wrapper around existing types.
/// Used for the new type pattern.
pub trait Wrapper where Self: Sized{
/// The type the new type wraps.
type Inner;
/// Try to wrap a [Self::Inner][Self::Inner] type into the [Self][Self] type.
fn wrap(inner: Self::Inner) -> Option<Self>;
/// Unwrap a [Self][Self] type into a [Self::Inner][Self::Inner] type.
fn unwrap(self) -> Self::Inner;
}
/// Wrapper around existing vector types.
/// Used for the new type pattern.
pub trait VecWrapper{
/// The type the new type wraps.
type Item;
/// Return the length of the inner vector.
fn len(&self) -> usize;
/// Return whether the inner vector is empty or not.
fn is_empty(&self) -> bool;
/// Iterate over the inner vector.
fn iter(&self) -> std::slice::Iter<'_, Self::Item>;
/// Returns whether the inner vector contains an item.
fn contains(&self, item: &Self::Item) -> bool;
/// Returns whether the inner vector contains all of the given items.
fn contains_all(&self, items: &[Self::Item]) -> bool;
/// Returns whether the inner vector contains any of the given items.
fn contains_any(&self, items: &[Self::Item]) -> bool;
}
macro_rules! ImplVecWrapper{
($type:ty, $item:ty) => {
impl VecWrapper for $type{
type Item = $item;
fn len(&self) -> usize{
self.0.len()
}
fn is_empty(&self) -> bool{
self.0.is_empty()
}
fn iter(&self) -> std::slice::Iter<'_, Self::Item>{
self.0.iter()
}
fn contains(&self, item: &Self::Item) -> bool{
self.0.contains(item)
}
fn contains_all(&self, items: &[Self::Item]) -> bool{
items.iter().all(|x| self.contains(x))
}
fn contains_any(&self, items: &[Self::Item]) -> bool{
self.iter().any(|x| items.contains(x))
}
}
impl crate::theory::traits::AsSubs for $type{
fn as_subs(&self, max_len: Option<usize>) -> Vec<Self>{
use crate::utils::sub_vecs;
let subs = sub_vecs(&self.0, max_len);
subs.into_iter().map(|s| Self(s)).collect::<Vec<_>>()
}
}
impl std::ops::Index<usize> for $type{
type Output = $item;
fn index(&self, index: usize) -> &Self::Output{
&self.0[index]
}
}
impl std::ops::IndexMut<usize> for $type{
fn index_mut(&mut self, index: usize) -> &mut Self::Output{
&mut self.0[index]
}
}
}
}
/// Can always generate a next and previous value.
pub trait Cyclic{
/// Generate the next item.
fn next(self) -> Self;
/// Generate the previous item.
fn prev(self) -> Self;
}
/// Like [Cyclic][Cyclic] you can generate next and previous items.
/// However, the operation might fail and return [None][Option::None].
pub trait GeneratablePartialOrder where Self: Sized{
/// Generate the next item.
fn next(self) -> Option<Self>;
/// Generate the previous item.
fn prev(self) -> Option<Self>;
}
/// Music theory types that have a notion of being in a certain octave, and can shift or set that
/// octave.
pub trait OctaveShiftable{
/// Have the value with everything the same except change the octave to the given one.
fn with_octave(self, octave: Octave) -> Self;
/// Have the value with everything the same except shift the octave from the current one with
/// the given shift.
fn shift_octave(self, shift: OctaveShift) -> Self;
}
/// Music theory types that have the ability to have an [Interval][Interval] added to them.
pub trait AddInterval where Self: Sized{
/// Add an [Interval][Interval].
fn add_interval(self, interval: Interval) -> Option<Self>;
}
/// Types that have modes (rotations of scales).
pub trait ModeTrait{
/// Modify the value so it turns into it's next mode.
fn next_mode_mut(&mut self);
/// Take the value and give back it's next mode.
fn next_mode(self) -> Self;
/// Take the value and give back it's Nth mode.
fn mode(self, mode: Mode) -> Self;
}
/// The ability to spawn an iterator that yields modes.
pub trait ModeIteratorSpawner<T: ModeTrait + VecWrapper>{
/// Spawn the mode iterator.
fn mode_iter(self) -> ModeIterator<T>;
}
/// The ability to spawn an iterator that yields notes.
/// Not very useful for types that already yield notes with their natural iterator.
pub trait ScaleIteratorSpawner{
/// Spawn the iterator, with the root or starting note.
fn scale_iter(&self, root: Note) -> ScaleIterator;
}
/// The ability to generate subs sets of it's self.
pub trait AsSubs where Self: Sized{
/// Generate all subs sets with optional maximal subset length.
/// The output size of this grows really fast with the input size so be aware:
/// 2, 5, 16, 65, 326, 1957, 13700, 109601, 986410, 9864101, 108505112.
fn as_subs(&self, max_len: Option<usize>) -> Vec<Self>;
}
// Conversion Traits
/// Convert to [Note][Note].
pub trait ToNote{
/// Take self and return a [Note][Note].
fn to_note(self) -> Note;
}
/// Convert to [PC][PC].
pub trait ToPC{
/// Take self and return [PC][PC].
fn to_pc(self) -> PC;
}
/// Convert to [Interval][Interval].
pub trait ToInterval{
/// Take self and return [Interval][Interval].
fn to_interval(self) -> Interval;
}
/// Convert to [NamedInterval][NamedInterval].
pub trait ToNamedInterval{
/// Try to convert to [NamedInterval][NamedInterval].
fn to_named_interval_try(self) -> Option<NamedInterval>;
/// Convert to [NamedInterval][NamedInterval] with wrapping around the octaves.
fn to_named_interval_mod(self) -> NamedInterval;
}
/// Convert to [NamedOctaveInterval][NamedOctaveInterval].
pub trait ToNamedOctaveInterval{
/// Try to convert to [NamedOctaveInterval][NamedOctaveInterval].
fn to_named_octave_interval_try(self) -> Option<NamedOctaveInterval>;
/// Convert to [NamedOctaveInterval][NamedOctaveInterval] with wrapping around the octave.
fn to_named_octave_interval_mod(self) -> NamedOctaveInterval;
}
/// Try to convert to [Letter][Letter].
pub trait ToLetterTry{
/// Take self and try to return [Letter][Letter].
fn to_letter_try(&self) -> Option<Letter>;
}
/// Convert to [EnharmonicNote][EnharmonicNote].
pub trait ToEnharmonicNote{
/// Take self and return [EnharmonicNote][EnharmonicNote].
fn to_enharmonic_note(self) -> EnharmonicNote;
}
/// Try to convert to [EnharmonicNote][EnharmonicNote].
pub trait ToEnharmonicNoteTry{
/// Take self and try to return [EnharmonicNote][EnharmonicNote].
fn to_enharmonic_note_try(&self) -> Option<EnharmonicNote>;
}
/// Convert to [Scale][Scale].
pub trait AsScale{
/// Borrow self and return [Scale][Scale].
/// Note is either the root of the scale or the octave.
fn as_scale(&self, note: Note) -> Scale;
}
/// Convert to [Scale][Scale].
pub trait ToScale{
/// Take self and return [Scale][Scale].
/// Note is either the root of the scale or the octave.
fn to_scale(self, note: Note) -> Scale;
}
impl<T: AsScale> ToScale for T{
fn to_scale(self, note: Note) -> Scale{
self.as_scale(note)
}
}
/// Try to convert to [Scale][Scale].
pub trait AsScaleTry{
/// Borrow self and try to return [Scale][Scale].
/// Note is either the root of the scale or the octave.
fn as_scale_try(&self, note: Note) -> Option<Scale>;
}
/// Try to convert to [Scale][Scale].
pub trait ToScaleTry{
/// Take self and try to return [Scale][Scale].
/// Note is either the root of the scale or the octave.
fn to_scale_try(self, note: Note) -> Option<Scale>;
}
impl<T: AsScaleTry> ToScaleTry for T{
fn to_scale_try(self, note: Note) -> Option<Scale>{
self.as_scale_try(note)
}
}
/// Convert to [Steps][Steps].
pub trait AsSteps{
/// Borrow self and return [Steps][Steps].
/// `complete_octave_cycle` instructs whether the steps should wrap around the starting note to
/// complete the octave.
fn as_steps(&self, complete_octave_cycle: bool) -> Steps;
}
/// Convert to [Steps][Steps].
pub trait ToSteps{
/// Take self and return [Steps][Steps].
/// `complete_octave_cycle` instructs whether the steps should wrap around the starting note to
/// complete the octave.
fn to_steps(self, complete_octave_cycle: bool) -> Steps;
}
impl<T: AsSteps> ToSteps for T{
fn to_steps(self, complete_octave_cycle: bool) -> Steps{
self.as_steps(complete_octave_cycle)
}
}
/// Try convert to [Steps][Steps].
pub trait AsStepsTry{
/// Borrow self and try to return [Steps][Steps].
/// `complete_octave_cycle` instructs whether the steps should wrap around the starting note to
/// complete the octave.
fn as_steps_try(&self, complete_octave_cycle: bool) -> Option<Steps>;
}
/// Try convert to [Steps][Steps].
pub trait ToStepsTry{
/// Take self and try to return [Steps][Steps].
/// `complete_octave_cycle` instructs whether the steps should wrap around the starting note to
/// complete the octave.
fn to_steps_try(self, complete_octave_cycle: bool) -> Option<Steps>;
}
impl<T: AsStepsTry> ToStepsTry for T{
fn to_steps_try(self, complete_octave_cycle: bool) -> Option<Steps>{
self.as_steps_try(complete_octave_cycle)
}
}
/// Convert to PCs.
pub trait AsPCs{
/// Borrow self and return [PCs][PCs].
fn as_pcs(&self) -> PCs;
}
/// Convert to PCs.
pub trait ToPCs{
/// Take self and return [PCs][PCs].
fn to_pcs(self) -> PCs;
}
impl<T: AsPCs> ToPCs for T{
fn to_pcs(self) -> PCs{
self.as_pcs()
}
}
/// Try to convert to relative intervals.
pub trait AsRelativeIntervals{
/// Borrow self and a reference and compare piecewise.
/// Try to return the differences as [Interval][Interval]'s.
/// Inputs must be of equal length.
fn as_relative_intervals(&self, reference: &Self) -> Option<Intervals>;
}
/// Try to convert to relative intervals.
pub trait ToRelativeIntervals{
/// Borrow self and a reference and compare piecewise.
/// Try to return the differences as [Interval][Interval]'s.
/// Inputs must be of equal length.
fn to_relative_intervals(self, reference: &Self) -> Option<Intervals>;
}
impl<T: AsRelativeIntervals> ToRelativeIntervals for T{
fn to_relative_intervals(self, reference: &Self) -> Option<Intervals>{
self.as_relative_intervals(reference)
}
}
/// Try to convert to an Ionian relative string.
/// For example the Ionian relative string of Phrygian is 1 ♭2, ♭3, 4, 5, ♭6, ♭7.
pub trait AsIonianRelativeStringTry{
/// Borrow self and try to return a Ionian relative string.
/// `nonnat` determines if natural intervals have the natural '♮' accidental prefixed.
fn as_ionian_relative_string_try(&self, nonnat: bool) -> Option<String>;
}
/// Try to convert to an Ionian relative string.
/// For example the Ionian relative string of Phrygian is 1 ♭2, ♭3, 4, 5, ♭6, ♭7.
pub trait ToIonianRelativeStringTry{
/// Take self and try to return a Ionian relative string.
/// `nonnat` determines if natural intervals have the natural '♮' accidental prefixed.
fn to_ionian_relative_string_try(self, nonnat: bool) -> Option<String>;
}
impl<T: AsIonianRelativeStringTry> ToIonianRelativeStringTry for T{
fn to_ionian_relative_string_try(self, nonnat: bool) -> Option<String>{
self.as_ionian_relative_string_try(nonnat)
}
}
/// Convert to a vector of [EnharmonicNote][EnharmonicNote].
pub trait AsEnharmonicNotes{
/// Borrow self and return a vector of [EnharmonicNote][EnharmonicNote].
fn as_enharmonic_notes(&self) -> Vec<EnharmonicNote>;
}
/// Convert to a vector of [EnharmonicNote][EnharmonicNote].
pub trait ToEnharmonicNotes{
/// Take self and return a vector of [EnharmonicNote][EnharmonicNote].
fn to_enharmonic_notes(self) -> Vec<EnharmonicNote>;
}
impl<T: AsEnharmonicNotes> ToEnharmonicNotes for T{
fn to_enharmonic_notes(self) -> Vec<EnharmonicNote>{
self.as_enharmonic_notes()
}
}
/// Convert to a vector of [EnharmonicNote][EnharmonicNote].
pub trait AsEnharmonicNotesWithStart{
/// Borrow self and return a vector of [EnharmonicNote][EnharmonicNote].
/// You can optionally specify a starting note.
/// This will affect the spelling.
/// Subsequent notes will be spelled with subsequent letters.
fn as_enharmonic_notes_with_start(&self, start: Option<EnharmonicNote>) -> Vec<EnharmonicNote>;
}
/// Convert to a vector of [EnharmonicNote][EnharmonicNote].
pub trait ToEnharmonicNotesWithStart{
/// Take self and return a vector of [EnharmonicNote][EnharmonicNote].
/// You can optionally specify a starting note.
/// This will affect the spelling.
/// Subsequent notes will be spelled with subsequent letters.
fn to_enharmonic_notes_with_start(self, start: Option<EnharmonicNote>) -> Vec<EnharmonicNote>;
}
impl<T: AsEnharmonicNotesWithStart> ToEnharmonicNotesWithStart for T{
fn to_enharmonic_notes_with_start(self, start: Option<EnharmonicNote>) -> Vec<EnharmonicNote>{
self.as_enharmonic_notes_with_start(start)
}
}
/// Convert to [Chord][Chord].
pub trait AsChord{
/// Borrow self and return a [Chord][Chord].
fn as_chord(&self) -> Chord;
}
/// Convert to [Chord][Chord].
pub trait ToChord{
/// Take self and return [Chord][Chord].
fn to_chord(self) -> Chord;
}
impl<T: AsChord> ToChord for T{
fn to_chord(self) -> Chord{
self.as_chord()
}
}
/// Convert to [RootedChord][RootedChord].
pub trait AsRootedChord{
/// Borrow self and return a [RootedChord][RootedChord].
fn as_rooted_chord(&self) -> RootedChord;
}
/// Convert to [RootedChord][RootedChord].
pub trait ToRootedChord{
/// Take self and return a [RootedChord][RootedChord].
fn to_rooted_chord(self) -> RootedChord;
}
impl<T: AsRootedChord> ToRootedChord for T{
fn to_rooted_chord(self) -> RootedChord{
self.as_rooted_chord()
}
}