Skip to content

Commit

Permalink
Added Frequency conversions (#154)
Browse files Browse the repository at this point in the history
* added function and teests

* added functions for frequency conversions

* forgot to export new functions

* removed erroneous export that i added accidentally

* fixed some formatting and removed two functions

* added changes to changelog

* Update CHANGELOG.md

Co-authored-by: George Datseris <[email protected]>

* Update Project.toml

Co-authored-by: George Datseris <[email protected]>

* Update src/note.jl

Co-authored-by: George Datseris <[email protected]>

* Update src/note.jl

Co-authored-by: George Datseris <[email protected]>

* Update src/note.jl

Co-authored-by: George Datseris <[email protected]>

* Update src/note.jl

Co-authored-by: George Datseris <[email protected]>
  • Loading branch information
justinbroce and Datseris authored Feb 8, 2022
1 parent dddf5a0 commit ba0237d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
# v2.2.0
* New functions `pitch_to_hz ` and `hz_to_pitch `
# v2.1.0
* Default value of `MIDIFile.format` changed from 0 to 1
* Warning introduced to `fileio_save` for format 0 files with multiple tracks
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MIDI"
uuid = "f57c4921-e30c-5f49-b073-3f2f2ada663e"
repo = "https://github.com/JuliaMusic/MIDI.jl.git"
version = "2.1.0"
version = "2.2.0"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand Down
1 change: 1 addition & 0 deletions src/MIDI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export getnotes, addnote!, addnotes!, addevent!, addevents!
export MIDITrack
export Note, Notes, AbstractNote, DrumNote
export pitch_to_name, name_to_pitch
export pitch_to_hz, hz_to_pitch
export TrackEvent, MetaEvent, MIDIEvent, SysexEvent
export readvariablelength, writevariablelength

Expand Down
18 changes: 18 additions & 0 deletions src/note.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ function name_to_pitch(name)
return pitch + x + 12(octave+1) # lowest possible octave is -1 but pitch starts from 0
end

"""
pitch_to_hz(pitch::Integer, A4::Real = 440) -> hz::Real
Return the frequency value of the given midi note, optionally given the reference for middle A.
See https://en.wikipedia.org/wiki/Piano_key_frequencies
and https://librosa.org/doc/main/_modules/librosa/core/convert.html#midi_to_hz.
"""
function pitch_to_hz(pitch, A4 = 440)
return A4 * (2^ ((pitch-69) / 12))
end

"""
hz_to_pitch(hz::Real, A4::Real = 440) -> pitch::Int
Inverse of [`pitch_to_hz`](@ref).
"""
function hz_to_pitch(freq, A4 = 440)
return 12 * (log2(freq) - log2(A4)) + 69
end

#######################################################
# pretty printing
#######################################################
Expand Down
15 changes: 15 additions & 0 deletions test/note.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ end

@test all([name_to_pitch(pitch_to_name(i)) == i for i in 0:255])
end
@testset "frequency" begin
tol = 10e-3
@test pitch_to_hz(69,440)==440
@test pitch_to_hz(69,432)==432
@test abs( pitch_to_hz(44)-103.8262 ) < tol
@test abs(pitch_to_hz(105)-3520.000) < tol
@test abs(pitch_to_hz(89,432)-1371.51) < tol
#hz to pitch
@test hz_to_pitch(440) == 69
@test hz_to_pitch(432,432) == 69
@test round(hz_to_pitch(7040.000)) == 117
@test round(hz_to_pitch(123.4708)) == 47


end

@testset "copying notes" begin
midi = load("doxy.mid")
Expand Down

0 comments on commit ba0237d

Please sign in to comment.