-
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.
Add Note type with duration and rests (#9)
- Loading branch information
Showing
4 changed files
with
51 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
struct Rest end | ||
|
||
const NoteTypes = Union{Pitch, Chord{Pitch}, Rest} | ||
|
||
""" | ||
struct Note | ||
A note is a pitch and a duration. | ||
The pitch can be a chord, or a rest | ||
""" | ||
struct Note | ||
pitch::NoteTypes | ||
duration::Rational{Int} | ||
end | ||
|
||
# use / and * to specify durations: | ||
Base.:(/)(p::NoteTypes, n::Int) = Note(p, 1 // n) | ||
Base.:(*)(p::NoteTypes, n::Rational{Int}) = Note(p, n) | ||
|
||
const rest = Rest() | ||
|
||
|
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,9 +1,18 @@ | ||
using MusicTheory.PitchNames | ||
@testset "Notes with durations" begin | ||
|
||
@testset "Notes" begin | ||
note = C[4] | ||
using MusicTheory.PitchNames | ||
|
||
@test PitchClass(note) == C | ||
@test accidental(note) == ♮ | ||
@test octave(note) == 4 | ||
end | ||
n = C[4] / 8 | ||
@test n isa Note | ||
@test n.pitch == C[4] | ||
@test n.duration == 1 // 8 | ||
|
||
n = B[5] * 3 // 8 | ||
@test n.pitch == B[5] | ||
@test n.duration == 3 // 8 | ||
|
||
n = rest / 4 | ||
@test n isa Note | ||
@test n.pitch == rest | ||
@test n.duration == 1 // 4 | ||
end |
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,9 @@ | ||
using MusicTheory.PitchNames | ||
|
||
@testset "Pitches" begin | ||
note = C[4] | ||
|
||
@test PitchClass(note) == C | ||
@test accidental(note) == ♮ | ||
@test octave(note) == 4 | ||
end |