Skip to content

Commit

Permalink
Fix Scale iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders committed Dec 26, 2023
1 parent 641b2fe commit acf3a22
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/MusicTheory.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand Down
2 changes: 0 additions & 2 deletions src/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/notes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 13 additions & 11 deletions src/scales.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


struct Scale{T<:Union{Note, Pitch}}
tonic::T
steps::Dict{Note, Interval}
Expand All @@ -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


Expand Down Expand Up @@ -76,4 +71,11 @@ end



# iterate(s)
# iterate(s)

const M = MusicTheory
s = Scale(M.C4, major_scale)

# notes = Base.Iterators.take(s, 8) |> collect

iterate(s, C4)

0 comments on commit acf3a22

Please sign in to comment.