Skip to content

Commit

Permalink
Add frequency in equal temperament (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpsanders authored Jan 20, 2024
1 parent e99fdc1 commit 61aa313
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/MusicTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ include("scales.jl")
include("chords.jl")
include("triads.jl")
include("notes.jl")
include("equal_temperament.jl")
end
13 changes: 13 additions & 0 deletions src/equal_temperament.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
abstract type Temperament end

struct EqualTemperament
base_pitch::Pitch
base_frequency::Float64
end


function frequency(temperament::EqualTemperament, pitch::Pitch)
semitone_distance = semitone(pitch) - semitone(temperament.base_pitch)
return 2.0^(semitone_distance // 12) * temperament.base_frequency
end

5 changes: 5 additions & 0 deletions test/equal_temperament.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@testest "Equal temperament" begin
temperament = EqualTemperament(A[4], 440)

@test frequency(temperament, C[4]) == 261.6255653005986
end

0 comments on commit 61aa313

Please sign in to comment.