Skip to content

Commit f5584ad

Browse files
authored
Add Note type with duration and rests (#9)
1 parent 39ecef0 commit f5584ad

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/MusicTheory.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ export Chord
1717

1818
export make_triad, is_triad
1919

20+
export Note, rest
21+
2022

2123
include("pitches.jl")
2224
include("pitch_names.jl")
2325
include("intervals.jl")
2426
include("scales.jl")
2527
include("chords.jl")
2628
include("triads.jl")
29+
include("notes.jl")
2730
end

src/notes.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
struct Rest end
3+
4+
const NoteTypes = Union{Pitch, Chord{Pitch}, Rest}
5+
6+
"""
7+
struct Note
8+
9+
A note is a pitch and a duration.
10+
The pitch can be a chord, or a rest
11+
"""
12+
struct Note
13+
pitch::NoteTypes
14+
duration::Rational{Int}
15+
end
16+
17+
# use / and * to specify durations:
18+
Base.:(/)(p::NoteTypes, n::Int) = Note(p, 1 // n)
19+
Base.:(*)(p::NoteTypes, n::Rational{Int}) = Note(p, n)
20+
21+
const rest = Rest()
22+
23+

test/notes.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
using MusicTheory.PitchNames
1+
@testset "Notes with durations" begin
22

3-
@testset "Notes" begin
4-
note = C[4]
3+
using MusicTheory.PitchNames
54

6-
@test PitchClass(note) == C
7-
@test accidental(note) ==
8-
@test octave(note) == 4
9-
end
5+
n = C[4] / 8
6+
@test n isa Note
7+
@test n.pitch == C[4]
8+
@test n.duration == 1 // 8
9+
10+
n = B[5] * 3 // 8
11+
@test n.pitch == B[5]
12+
@test n.duration == 3 // 8
13+
14+
n = rest / 4
15+
@test n isa Note
16+
@test n.pitch == rest
17+
@test n.duration == 1 // 4
18+
end

test/pitches.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using MusicTheory.PitchNames
2+
3+
@testset "Pitches" begin
4+
note = C[4]
5+
6+
@test PitchClass(note) == C
7+
@test accidental(note) ==
8+
@test octave(note) == 4
9+
end

0 commit comments

Comments
 (0)