Skip to content

Commit

Permalink
Change scale indexing to be 1-based
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders committed Jan 20, 2024
1 parent 7970d87 commit 0398dce
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/scales.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,22 @@ Base.eltype(::Type{Scale{T}}) where {T} = T

function Base.getindex(s::Scale{Pitch}, n::Int)

octave = n ÷ length(s.notes)
n = n % length(s.notes)
if n == 0
error("Scale indices start at 1 or must be negative")
end

if n < 1
n += 2 # offset so that scale[-2] means "go down by a second from the tonic"
end

octave = fld1(n, length(s.notes)) - 1
n = mod1(n, length(s.notes))
if n < 0
n += length(s.notes)
octave -= 1
end

return Pitch(PitchClass(s.notes[n + 1]), s.notes[n + 1].octave + octave)
return Pitch(PitchClass(s.notes[n]), s.notes[n].octave + octave)
end


Expand Down

0 comments on commit 0398dce

Please sign in to comment.