From b79f51dbc0b3d905e1021cfe31b8d006c7256ef1 Mon Sep 17 00:00:00 2001 From: vasanth Date: Wed, 31 Mar 2021 21:34:42 +0530 Subject: [PATCH 1/5] Added time signature --- src/midifile.jl | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/midifile.jl b/src/midifile.jl index 062789e..0d73e3d 100644 --- a/src/midifile.jl +++ b/src/midifile.jl @@ -1,5 +1,5 @@ export MIDIFile, readMIDIFile, writeMIDIFile -export BPM, ms_per_tick +export BPM, time_signature, ms_per_tick """ MIDIFile <: Any @@ -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, dd^2]), '/') + 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) From 895afcf7a663efa86dbf49caccda93290128918b Mon Sep 17 00:00:00 2001 From: vasanth Date: Wed, 31 Mar 2021 21:44:34 +0530 Subject: [PATCH 2/5] Added tests --- test/runtests.jl | 1 + test/utils.jl | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 test/utils.jl diff --git a/test/runtests.jl b/test/runtests.jl index 58f2ebf..ccf81ec 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,3 +9,4 @@ include("metaevent.jl") include("miditrack.jl") include("midiio.jl") include("negative_delta.jl") +include("utils.jl") diff --git a/test/utils.jl b/test/utils.jl new file mode 100644 index 0000000..0c2865d --- /dev/null +++ b/test/utils.jl @@ -0,0 +1,7 @@ +cd(@__DIR__) + +@testset "Time Signature" begin + midi = readMIDIFile("doxy.mid") + @test time_signature(midi) == "4/4" +end + From 17076d4bde0fa0f931f10947b3d9e40cf238ce26 Mon Sep 17 00:00:00 2001 From: vasanth Date: Wed, 31 Mar 2021 23:01:49 +0530 Subject: [PATCH 3/5] Correct time_signature --- src/midifile.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/midifile.jl b/src/midifile.jl index 0d73e3d..b991596 100644 --- a/src/midifile.jl +++ b/src/midifile.jl @@ -163,7 +163,7 @@ function time_signature(t::MIDI.MIDIFile) if typeof(event) == MetaEvent if event.metatype == 0x58 nn, dd = event.data - ts = join(map(string, [nn, dd^2]), '/') + ts = join(map(string, [nn, 2^dd]), '/') return ts end end From 710218b6d220854ba5011393221de986967e6498 Mon Sep 17 00:00:00 2001 From: vasanth Date: Thu, 1 Apr 2021 10:26:01 +0530 Subject: [PATCH 4/5] Make requested changes --- src/midifile.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/midifile.jl b/src/midifile.jl index b991596..e377643 100644 --- a/src/midifile.jl +++ b/src/midifile.jl @@ -153,6 +153,7 @@ 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: @@ -163,7 +164,7 @@ function time_signature(t::MIDI.MIDIFile) if typeof(event) == MetaEvent if event.metatype == 0x58 nn, dd = event.data - ts = join(map(string, [nn, 2^dd]), '/') + ts = string(nn) * "/" * string(2^dd) return ts end end From 891ee7e163624d3b4da7ebca6e9ebdd868c70a14 Mon Sep 17 00:00:00 2001 From: vasanth Date: Thu, 1 Apr 2021 10:26:21 +0530 Subject: [PATCH 5/5] Update Project.toml and CHANGELOG.md --- CHANGELOG.md | 2 ++ Project.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62c9832..22284df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Project.toml b/Project.toml index b021b80..29c213c 100644 --- a/Project.toml +++ b/Project.toml @@ -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"