Skip to content

Commit

Permalink
Allow adding intervals together
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders committed Dec 31, 2023
1 parent 5601ba4 commit b7e2426
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ function Base.:+(p::Pitch, interval::Interval)
end


function Base.:+(i1::Interval, i2::Interval)
bottom = C[4]
top = bottom + i1 + i2

return Interval(bottom, top)
end



const Minor_2nd = Interval(2, Minor)
const Major_2nd = Interval(2, Major)

Expand Down
12 changes: 10 additions & 2 deletions src/scales.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# Represents a musical scale
# I.e. an object that repeats each octave
"""
struct Scale{T<:Union{PitchClass, Pitch}}
A musical scale divides up an octave.
A scale is represented as an iterator.
"""
struct Scale{T<:Union{PitchClass, Pitch}}
tonic::T
steps::Dict{PitchClass, Interval}

function Scale(tonic::T, steps::Vector{Interval}) where {T}

# steps must add up to an octave:
@assert sum(steps) == Interval(8, Perfect)

steps_dict = Dict{PitchClass, Interval}()

current = tonic
Expand Down
13 changes: 13 additions & 0 deletions test/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ end

@test C[4] + Interval(2, Major) == D[4]
@test B[4] + Interval(2, Major) == C♯[5]
end

@testset "Sum of intervals" begin
@test Interval(3, Major) + Interval(3, Minor) == Interval(5, Perfect)
@test Interval(3, Major) + Interval(3, Major) == Interval(5, Augmented)
@test Interval(3, Major) + Interval(4, Diminished) == Interval(6, Minor)
@test Interval(4, Perfect) + Interval(5, Perfect) == Interval(8, Perfect)

major_scale = let M = Major_2nd, m = Minor_2nd
[M, M, m, M, M, M, m]
end

@test sum(major_scale) == Interval(8, Perfect)
end

0 comments on commit b7e2426

Please sign in to comment.