-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,79 @@ | ||
|
||
|
||
mutable struct Scale | ||
current::Pitch | ||
steps | ||
end | ||
struct Scale{T<:Union{Note, Pitch}} | ||
tonic::T | ||
steps::Dict{Note, Interval} | ||
|
||
# state is a counter that loops through the steps vector | ||
function next_state(s::Scale, state) | ||
if state == length(s.steps) | ||
state = 1 | ||
else | ||
state += 1 | ||
end | ||
function Scale(tonic::T, steps::Vector{Interval}) where {T} | ||
steps_dict = Dict{Note, Interval}() | ||
|
||
return state | ||
end | ||
current = tonic | ||
for step in steps | ||
steps_dict[Note(current)] = step | ||
current += step | ||
end | ||
|
||
function next_value!(s::Scale, state) | ||
s.current = s.current + s.steps[state] | ||
return s.current | ||
return new{T}(tonic, steps_dict) | ||
end | ||
end | ||
|
||
Scale(state, steps) = Scale(state, steps, 1) | ||
# Scale2(M.C4, major_scale) | ||
|
||
Base.IteratorSize(::Type{Scale}) = Base.IsInfinite() | ||
Base.IteratorEltype(::Type{Scale}) = Base.HasEltype() | ||
Base.eltype(::Type{Scale}) = Pitch | ||
Base.iterate(s::Scale{T}) where {T} = s.tonic | ||
|
||
function iterate!(s::Scale, state) | ||
next_value!(s, state) | ||
state = next_state(s, state) | ||
function Base.iterate(s::Scale{T}, p::T) where {T} | ||
n = Note(p) | ||
|
||
return state | ||
end | ||
!haskey(s.steps, n) && error("Note $n not in scale") | ||
|
||
function Base.iterate(s::Scale, state=1) | ||
current = s.current | ||
state = iterate!(s, state) | ||
step = s.steps[n] | ||
new_pitch = p + step | ||
|
||
return current, state | ||
return p, new_pitch | ||
end | ||
|
||
Base.IteratorSize(::Type{Scale{T}}) where {T} = Base.IsInfinite() | ||
Base.IteratorEltype(::Type{Scale{T}}) where {T} = Base.HasEltype() | ||
Base.eltype(::Type{Scale{T}}) where {T} = T | ||
|
||
|
||
major_scale = let M = Major_2nd, m = Minor_2nd | ||
const major_scale = let M = Major_2nd, m = Minor_2nd | ||
[M, M, m, M, M, M, m] | ||
end | ||
|
||
natural_minor_scale = let M = Major_2nd, m = Minor_2nd | ||
[M, m, M, M, M, m, M] | ||
const natural_minor_scale = let M = Major_2nd, m = Minor_2nd | ||
[M, m, M, M, M, m, M] | ||
end | ||
|
||
melodic_minor_scale = let M = Major_2nd, m = Minor_2nd | ||
[M, m, M, M, M, M, m] | ||
const melodic_minor_scale = let M = Major_2nd, m = Minor_2nd | ||
[M, m, M, M, M, M, m] | ||
end | ||
|
||
harmonic_minor_scale = let M = Major_2nd, m = Minor_2nd | ||
[M, m, M, M, m, Interval(1, Augmented), m] | ||
const harmonic_minor_scale = let M = Major_2nd, m = Minor_2nd | ||
[M, m, M, M, m, Interval(1, Augmented), m] | ||
end | ||
|
||
|
||
s = Scale(MusicTheory.C4, major_scale) | ||
# s = Scale(MusicTheory.D4, major_scale) | ||
|
||
|
||
# popfirst!(s) | ||
|
||
# collect(Iterators.take(s, 20)) | ||
|
||
# s2 = Base.Iterators.Stateful(Scale(MusicTheory.C4, harmonic_minor_scale)) | ||
|
||
# Base.Iterators.take(s2, 10) |> collect | ||
|
||
s | ||
# eltype(Scale) | ||
|
||
s = Base.Iterators.Stateful(Scale(MusicTheory.C4, major_scale)) | ||
|
||
popfirst!(s) | ||
# # @edit Base.Iterators.Stateful(Scale(MusicTheory.C4, major_scale)) | ||
|
||
Iterators.take(s, 10) |> collect | ||
|
||
s2 = Base.Iterators.Stateful(Scale(MusicTheory.C4, harmonic_minor_scale)) | ||
# interval(C4, B4) | ||
|
||
Base.Iterators.take(s2, 10) |> collect | ||
|
||
eltype(Scale) | ||
|
||
|
||
@edit Base.Iterators.Stateful(Scale(MusicTheory.C4, major_scale)) | ||
# iterate(s) |