Skip to content

Commit 89580c2

Browse files
authored
Allow adding intervals together (#7)
1 parent 5601ba4 commit 89580c2

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/intervals.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ function Base.:+(p::Pitch, interval::Interval)
112112
end
113113

114114

115+
function Base.:+(i1::Interval, i2::Interval)
116+
bottom = C[4]
117+
top = bottom + i1 + i2
118+
119+
return Interval(bottom, top)
120+
end
121+
122+
123+
115124
const Minor_2nd = Interval(2, Minor)
116125
const Major_2nd = Interval(2, Major)
117126

src/scales.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
# Represents a musical scale
2-
# I.e. an object that repeats each octave
1+
"""
2+
struct Scale{T<:Union{PitchClass, Pitch}}
3+
4+
A musical scale divides up an octave.
5+
A scale is represented as an iterator.
6+
"""
37
struct Scale{T<:Union{PitchClass, Pitch}}
48
tonic::T
59
steps::Dict{PitchClass, Interval}
610

711
function Scale(tonic::T, steps::Vector{Interval}) where {T}
12+
13+
# steps must add up to an octave:
14+
@assert sum(steps) == Interval(8, Perfect)
15+
816
steps_dict = Dict{PitchClass, Interval}()
917

1018
current = tonic

test/intervals.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,17 @@ end
1717

1818
@test C[4] + Interval(2, Major) == D[4]
1919
@test B[4] + Interval(2, Major) == C♯[5]
20+
end
21+
22+
@testset "Sum of intervals" begin
23+
@test Interval(3, Major) + Interval(3, Minor) == Interval(5, Perfect)
24+
@test Interval(3, Major) + Interval(3, Major) == Interval(5, Augmented)
25+
@test Interval(3, Major) + Interval(4, Diminished) == Interval(6, Minor)
26+
@test Interval(4, Perfect) + Interval(5, Perfect) == Interval(8, Perfect)
27+
28+
major_scale = let M = Major_2nd, m = Minor_2nd
29+
[M, M, m, M, M, M, m]
30+
end
31+
32+
@test sum(major_scale) == Interval(8, Perfect)
2033
end

0 commit comments

Comments
 (0)