Skip to content

Commit

Permalink
Add Note type with duration and rests (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders authored Jan 1, 2024
1 parent 39ecef0 commit f5584ad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/MusicTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ export Chord

export make_triad, is_triad

export Note, rest


include("pitches.jl")
include("pitch_names.jl")
include("intervals.jl")
include("scales.jl")
include("chords.jl")
include("triads.jl")
include("notes.jl")
end
23 changes: 23 additions & 0 deletions src/notes.jl
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()


23 changes: 16 additions & 7 deletions test/notes.jl
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
9 changes: 9 additions & 0 deletions test/pitches.jl
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

0 comments on commit f5584ad

Please sign in to comment.