Skip to content

Commit

Permalink
add function to read time signature (#130)
Browse files Browse the repository at this point in the history
* Added time signature

* Added tests

* Correct time_signature

* Make requested changes

* Update Project.toml and CHANGELOG.md
  • Loading branch information
VasanthManiVasi authored Apr 1, 2021
1 parent 7be2697 commit 629bba3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
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).
# v1.10.0
* Added `time_signature` that returns the time signature of a given MIDI.
# v1.9.0
* `name_to_pitch` now accepts flat names and `pitch_to_name` accepts `flat` keyword argument.
# v1.8.0
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 = "1.9.0"
version = "1.10.0"

[compat]
julia = "1"
Expand Down
24 changes: 23 additions & 1 deletion src/midifile.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export MIDIFile, readMIDIFile, writeMIDIFile
export BPM, ms_per_tick
export BPM, time_signature, ms_per_tick

"""
MIDIFile <: Any
Expand Down Expand Up @@ -150,7 +150,29 @@ function BPM(t::MIDI.MIDIFile)
bpm = 60000000/μs
end

"""
time_signature(midi)
Return the time signature of the given `MIDIFile`.
Returns 4/4 if it doesn't find a time signature.
"""
function time_signature(t::MIDI.MIDIFile)
# Find the one that corresponds to Time Signature:
# FF 58 04 nn dd cc bb Time Signature
# See here (page 8):
# http://www.cs.cmu.edu/~music/cmsip/readings/Standard-MIDI-file-format-updated.pdf
for event in t.tracks[1].events
if typeof(event) == MetaEvent
if event.metatype == 0x58
nn, dd = event.data
ts = string(nn) * "/" * string(2^dd)
return ts
end
end
end

# Default time signature if it is not present in the file
return "4/4"
end

"""
ms_per_tick(tpq, bpm)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ include("metaevent.jl")
include("miditrack.jl")
include("midiio.jl")
include("negative_delta.jl")
include("utils.jl")
7 changes: 7 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cd(@__DIR__)

@testset "Time Signature" begin
midi = readMIDIFile("doxy.mid")
@test time_signature(midi) == "4/4"
end

0 comments on commit 629bba3

Please sign in to comment.