diff --git a/src/MusicTheory.jl b/src/MusicTheory.jl index aaea273..c8f1eb6 100644 --- a/src/MusicTheory.jl +++ b/src/MusicTheory.jl @@ -1,7 +1,6 @@ module MusicTheory export Pitch, Note, Accidental, NoteClass, - MajorScale, MinorScale, 𝄫, ♭, ♮, ♯, 𝄪, C, D, E, F, G, A, B, C♮, D♮, E♮, F♮, G♮, A♮, B♮, C♯, D♯, E♯, F♯, G♯, A♯, B♯, C♭, D♭, E♭, F♭, G♭, A♭, B♭, C𝄫, D𝄫, E𝄫, F𝄫, G𝄫, A𝄫, B𝄫, C𝄪, D𝄪, E𝄪, F𝄪, G, note_names @@ -12,8 +11,11 @@ export Interval, IntervalType, Major, Minor, Perfect, Augmented, Diminished, export Major_2nd, Minor_2nd, Major_3rd, Minor_3rd, Perfect_4th, Perfect_5th, Major_6th, Minor_6th, Major_7th, Minor_7th, Octave +export Scale export major_scale, natural_minor_scale, melodic_minor_scale, harmonic_minor_scale + + include("notes.jl") include("intervals.jl") include("scales.jl") diff --git a/src/intervals.jl b/src/intervals.jl index 737e3dd..17750ab 100644 --- a/src/intervals.jl +++ b/src/intervals.jl @@ -92,8 +92,6 @@ end tone(interval::Interval) = interval.distance -semitone(interval::Interval) = - interval_semitones[interval.distance] + interval_quality_semitones[interval.quality] function add_interval(n::Note, interval::Interval) new_tone = (tone(n) + tone(interval)) % 7 diff --git a/src/notes.jl b/src/notes.jl index a273b9d..735e29c 100644 --- a/src/notes.jl +++ b/src/notes.jl @@ -76,16 +76,12 @@ semitone(note::NoteClass) = note_semitones[note] semitone(note::Note) = semitone(note.noteclass) + semitone(note.accidental) +"Treats C0 as semitone 0" semitone(pitch::Pitch) = semitone(pitch.note) + 12 * pitch.octave NoteClass(pitch::Pitch) = pitch.note.noteclass Note(pitch::Pitch) = pitch.note -"Treats C0 as semitone 0" -function semitone(pitch::Pitch) - return semitone(pitch.note) + 12*pitch.octave -end - function find_accidental(which_semitone, noteclass) basic_semitone = semitone(noteclass) diff --git a/src/scales.jl b/src/scales.jl index 136c31f..33e8a2b 100644 --- a/src/scales.jl +++ b/src/scales.jl @@ -1,5 +1,3 @@ - - struct Scale{T<:Union{Note, Pitch}} tonic::T steps::Dict{Note, Interval} @@ -17,23 +15,20 @@ struct Scale{T<:Union{Note, Pitch}} end end -# Scale2(M.C4, major_scale) - -Base.iterate(s::Scale{T}) where {T} = s.tonic -function Base.iterate(s::Scale{T}, p::T) where {T} +function Base.iterate(s::Scale{T}, p::T = s.tonic) where {T <: Union{Note, Pitch}} n = Note(p) !haskey(s.steps, n) && error("Note $n not in scale") - step = s.steps[n] - new_pitch = p + step + step = s.steps[n] # an interval + new_p = p + step # a note or a pitch, according to the type of p - return p, new_pitch + return (p, new_p) end Base.IteratorSize(::Type{Scale{T}}) where {T} = Base.IsInfinite() -Base.IteratorEltype(::Type{Scale{T}}) where {T} = Base.HasEltype() +Base.IteratorEltype(::Type{Scale{T}}) where {T} = Base.HasEltype() Base.eltype(::Type{Scale{T}}) where {T} = T @@ -76,4 +71,11 @@ end -# iterate(s) \ No newline at end of file +# iterate(s) + +const M = MusicTheory +s = Scale(M.C4, major_scale) + +# notes = Base.Iterators.take(s, 8) |> collect + +iterate(s, C4) \ No newline at end of file