Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to read time signature #130

Merged
merged 5 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 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,28 @@ function BPM(t::MIDI.MIDIFile)
bpm = 60000000/μs
end

"""
time_signature(midi)
Return the time signature of the given `MIDIFile`.
"""
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 = join(map(string, [nn, 2^dd]), '/')
VasanthManiVasi marked this conversation as resolved.
Show resolved Hide resolved
return ts
end
end
end

# Default time signature if it is not present in the file
return "4/4"
VasanthManiVasi marked this conversation as resolved.
Show resolved Hide resolved
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
Datseris marked this conversation as resolved.
Show resolved Hide resolved