-
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
2 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
# A motif is a repeating pattern within a scale | ||
# Specify by how many scale notes to increment for each subsequent note | ||
|
||
arpeggio = [2, 2, 3] | ||
# broken thirds = [2, -1] | ||
# broken octaves = [7, -6] | ||
# broken arpeggio = [4, -2, 5] | ||
|
||
# stateful iterator: | ||
mutable struct Motif | ||
scale::Scale | ||
pattern::Vector{Int} | ||
current::Pitch | ||
i::Int | ||
end | ||
|
||
Motif(scale, pattern) = Motif(scale, pattern, first(iterate(scale)), 1) | ||
|
||
function Base.iterate(m::Motif) | ||
return_value = m.current | ||
current, next = iterate(scale) | ||
|
||
for i in 1:m.pattern[m.i] - 1 | ||
current, next = iterate(scale, next) | ||
end | ||
|
||
m.i += 1 | ||
if m.i > length(m.pattern) | ||
m.i = 1 | ||
end | ||
|
||
m.current = return_value | ||
return return_value, next | ||
end | ||
|
||
function Base.iterate(m::Motif, next) | ||
|
||
return_value = next | ||
|
||
for i in 1:m.pattern[m.i] | ||
current, next = iterate(scale, next) | ||
end | ||
|
||
m.i += 1 | ||
if m.i > length(m.pattern) | ||
m.i = 1 | ||
end | ||
|
||
m.current = return_value | ||
|
||
return return_value, next | ||
end | ||
|
||
|
||
Base.IteratorSize(::Type{Motif}) = Base.IsInfinite() | ||
Base.IteratorEltype(::Type{Motif}) = Base.HasEltype() | ||
Base.eltype(::Type{Motif}) = Pitch |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@testset "Arpeggio" begin | ||
scale = Scale(C[4], major_scale) | ||
arpeggio_motif = Motif(scale, [2, 2, 3]) | ||
|
||
arpeggio = collect(Base.Iterators.take(arpeggio_motif, 6)) | ||
|
||
@test arpeggio == Pitch[C[4], E[4], G[4], C[5], E[5], G[5]] | ||
end |