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 SequencerSpecificEvent , and empty!(::MIDITrack) #173

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ 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.7.0
* New meta event type `SequencerSpecificEvent`.
* Implement `Base.empty!(::MIDITrack)` and `Base.isempty(t::MIDITrack)`.
# v2.6.0
* New functions `metric_time`.
* New functions `duration_metric_time`.
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.6.0"
version = "2.7.0"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand Down
31 changes: 24 additions & 7 deletions src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ const MIDI_EVENTS_DEFS = Dict(
decode = :(Int.(data)),
encode = :(UInt8.([event.semitones, event.scale]))
),

0x7f => (
type = :SequencerSpecificEvent,
fields = ["ssdata::Vector{UInt8}"],
decode = :(data),
encode = :(event.ssdata)
),
# MidiEvents
0x80 => (
type = :NoteOffEvent,
Expand Down Expand Up @@ -124,12 +129,14 @@ function define_type(type, fields, decode, encode_, supertype, typebyte)
$(fields...)
end

""" $($type)(dT::Int, typebyte::UInt8, data::Vector{UInt8})
Returns a `$($type)` event from it's byte representation.
The parameter `typebyte::UInt8` is its's $(($type <: MetaEvent) ? "metatype" : "status") byte.
"""
function $type(dT::Int, typebyte::UInt8, data::Vector{UInt8})
$type(dT, typebyte, $decode...)
if !($type.types[end] <: AbstractArray)
""" $($type)(dT::Int, typebyte::UInt8, data::Vector{UInt8})
Returns a `$($type)` event from it's byte representation.
The parameter `typebyte::UInt8` is its's $(($type <: MetaEvent) ? "metatype" : "status") byte.
"""
function $type(dT::Int, typebyte::UInt8, data::Vector{UInt8})
$type(dT, typebyte, $decode...)
end
end

if $type <: MetaEvent
Expand Down Expand Up @@ -407,3 +414,13 @@ The `PitchBendEvent` informs a MIDI device to modify the pitch in a specific cha
* `pitch::Int` : Value of the pitch bend.
"""
PitchBendEvent

""" SequencerSpecificEvent <: MetaEvent
The `SequencerSpecificEvent` is used to store vendor-proprietary data in a MIDI file.

## Fields:
* `dT::Int` : Delta time in ticks.
* `metatype::UInt8` : Meta type byte of the event.
* `ssdata::Vector{UInt8}` : Vendor-proprietary data.
"""
SequencerSpecificEvent
4 changes: 4 additions & 0 deletions src/miditrack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Track chunks begin with four bytes spelling out "MTrk", followed by the length
(in bytes) of the track (see [`readvariablelength`](@ref)), followed by a sequence
of events.

`MIDITrack` implements the `isempty` and `empty!` functions.
"""
mutable struct MIDITrack
events::Vector{TrackEvent}
Expand All @@ -21,6 +23,8 @@ function Base.show(io::IO, t::MIDITrack)
print(io, "$(L)-event MIDITrack: $M MIDI, $T Meta, $X Sysex")
end

Base.empty!(t::MIDITrack) = empty!(t.events)
Base.isempty(t::MIDITrack)::Bool = isempty(t.events)

function readtrack(f::IO)

Expand Down
Binary file added test/SequencerSpecific.mid
Binary file not shown.
18 changes: 18 additions & 0 deletions test/metaevent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@
@test_throws errtype MIDI.readsysexevent(Int(input[1]), IOBuffer(input[2:length(input)]))
end
end

@testset "SequencerSpecificEvent" begin
midi = load("SequencerSpecific.mid")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't undertstand what you are doing here. You are loading this midi data, but you don't use them next in the test suite...? The current test suite is meaningless you are only testing whether you initialized the SequencerSpecificEvent with the values you initialized it with. Rather trivial, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I don't think it's necessary to actually use this midi data.
If the added code is incorrect, an error will be throwed on this line when initialization, and causing the test to fail.
The fact that no exception is thrown already indicates that SequencerSpecificEvent is being parsed and instantiated correctly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests have been added

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What, no not at all. What we want to know is whether we correctly find this special midi type in an actual midi track that we load. Instantiating the type is trivial, you aren't testing anything really if you only test that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But now we have this midi file, and it is very small, so thank you, this is exactly what I was asking for!

sse = midi.tracks[1].events[1]
@test sse isa MIDI.SequencerSpecificEvent
sse.ssdata == [0x11, 0x21, 0x53, 0x1F]

sse = MIDI.SequencerSpecificEvent(0, 0x7f, [0x11, 0x21, 0x53, 0x1F])
@test sse.ssdata == [0x11, 0x21, 0x53, 0x1F]
end

@testset "check empty" begin
midi = load(testmidi())
track = midi.tracks[1]
@test !isempty(track)
empty!(track)
@test isempty(track)
end
end