Skip to content

Commit

Permalink
midi: snake-cased event types and event names (cf. #1004 (comment))
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Aug 27, 2024
1 parent ea3e089 commit c8d9397
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 492 deletions.
78 changes: 39 additions & 39 deletions format/midi/metaevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ const (
)

var metaevents = scalar.UintMapSymStr{
0xff00: "Sequence Number",
0xff01: "Text",
0xff02: "Copyright",
0xff03: "Track Name",
0xff04: "Instrument Name",
0xff05: "Lyric",
0xff06: "Marker",
0xff07: "Cue Point",
0xff08: "Program Name",
0xff09: "Device Name",
0xff20: "Midi Channel Prefix",
0xff21: "Midi Port",
0xff51: "Tempo",
0xff54: "Smpte Offset",
0xff58: "Time Signature",
0xff59: "Key Signature",
0xff2f: "End Of Track",
0xff7f: "Sequencer Specific Event",
0xff00: "sequence_number",
0xff01: "text",
0xff02: "copyright",
0xff03: "track_name",
0xff04: "instrument_name",
0xff05: "lyric",
0xff06: "marker",
0xff07: "cue_point",
0xff08: "program_name",
0xff09: "device_name",
0xff20: "midi_channel_prefix",
0xff21: "midi_port",
0xff51: "tempo",
0xff54: "smpte_offset",
0xff58: "time_signature",
0xff59: "key_signature",
0xff2f: "end_of_track",
0xff7f: "sequencer_specific_event",
}

var framerates = scalar.UintMapSymUint{
Expand Down Expand Up @@ -79,66 +79,66 @@ func decodeMetaEvent(d *decode.D, event uint8, ctx *context) {

switch MetaEventType(event) {
case TypeSequenceNumber:
metaevent("SequenceNumber", decodeSequenceNumber)
metaevent("sequence_number", decodeSequenceNumber)

case TypeText:
metaevent("Text", decodeText)
metaevent("text", decodeText)

case TypeCopyright:
metaevent("Copyright", decodeCopyright)
metaevent("copyright", decodeCopyright)

case TypeTrackName:
metaevent("TrackName", decodeTrackName)
metaevent("track_name", decodeTrackName)

case TypeInstrumentName:
metaevent("InstrumentName", decodeInstrumentName)
metaevent("instrument_name", decodeInstrumentName)

case TypeLyric:
metaevent("Lyric", decodeLyric)
metaevent("lyric", decodeLyric)

case TypeMarker:
metaevent("Marker", decodeMarker)
metaevent("marker", decodeMarker)

case TypeCuePoint:
metaevent("CuePoint", decodeCuePoint)
metaevent("cue_point", decodeCuePoint)

case TypeProgramName:
metaevent("ProgramName", decodeProgramName)
metaevent("program_name", decodeProgramName)

case TypeDeviceName:
metaevent("DeviceName", decodeDeviceName)
metaevent("device_name", decodeDeviceName)

case TypeMIDIChannelPrefix:
metaevent("MIDIChannelPrefix", decodeMIDIChannelPrefix)
metaevent("midi_channel_prefix", decodeMIDIChannelPrefix)

case TypeMIDIPort:
metaevent("MIDIPort", decodeMIDIPort)
metaevent("midi_port", decodeMIDIPort)

case TypeTempo:
metaevent("Tempo", decodeTempo)
metaevent("tempo", decodeTempo)

case TypeSMPTEOffset:
metaevent("SMPTEOffset", decodeSMPTEOffset)
metaevent("smpte_offset", decodeSMPTEOffset)

case TypeTimeSignature:
metaevent("TimeSignature", decodeTimeSignature)
metaevent("time_signature", decodeTimeSignature)

case TypeKeySignature:
metaevent("KeySignature", decodeKeySignature)
metaevent("key_signature", decodeKeySignature)

case TypeEndOfTrack:
metaevent("EndOfTrack", decodeEndOfTrack)
metaevent("end_of_track", decodeEndOfTrack)

case TypeSequencerSpecificEvent:
metaevent("SequencerSpecific", decodeSequencerSpecificEvent)
metaevent("sequencer_specific_event", decodeSequencerSpecificEvent)

default:
flush(d, "unknown meta event (%02x)", event)
}
}

func decodeSequenceNumber(d *decode.D) {
d.FieldUintFn("sequenceNumber", func(d *decode.D) uint64 {
d.FieldUintFn("sequence_number", func(d *decode.D) uint64 {
seqno := uint64(0)

if data, err := vlf(d); err != nil {
Expand Down Expand Up @@ -315,11 +315,11 @@ func decodeTimeSignature(d *decode.D) {
}

if len(data) > 2 {
d.FieldValueUint("ticksPerClick", uint64(data[2]))
d.FieldValueUint("ticks_per_click", uint64(data[2]))
}

if len(data) > 3 {
d.FieldValueUint("thirtySecondsPerQuarter", uint64(data[3]))
d.FieldValueUint("thirty_seconds_per_quarter", uint64(data[3]))
}
})
}
Expand Down
28 changes: 14 additions & 14 deletions format/midi/midievents.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const (
)

var midievents = scalar.UintMapSymStr{
0x80: "Note Off",
0x90: "Note On",
0xa0: "Polyphonic Pressure",
0xb0: "Controller",
0xc0: "Program Change",
0xd0: "Channel Pressure",
0xe0: "Pitch Bend",
0x80: "note_off",
0x90: "note_on",
0xa0: "polyphonic_pressure",
0xb0: "controller",
0xc0: "program_change",
0xd0: "channel_pressure",
0xe0: "pitch_bend",
}

func decodeMIDIEvent(d *decode.D, status uint8, ctx *context) {
Expand Down Expand Up @@ -65,25 +65,25 @@ func decodeMIDIEvent(d *decode.D, status uint8, ctx *context) {

switch MidiEventType(event) {
case TypeNoteOff:
midievent("NoteOff", decodeNoteOff)
midievent("note_off", decodeNoteOff)

case TypeNoteOn:
midievent("NoteOn", decodeNoteOn)
midievent("note_on", decodeNoteOn)

case TypePolyphonicPressure:
midievent("PolyphonicPressure", decodePolyphonicPressure)
midievent("polyphonic_pressure", decodePolyphonicPressure)

case TypeController:
midievent("Controller", decodeController)
midievent("controller", decodeController)

case TypeProgramChange:
midievent("ProgramChange", decodeProgramChange)
midievent("program_change", decodeProgramChange)

case TypeChannelPressure:
midievent("ChannelPressure", decodeChannelPressure)
midievent("channel_pressure", decodeChannelPressure)

case TypePitchBend:
midievent("PitchBend", decodePitchBend)
midievent("pitch_bend", decodePitchBend)

default:
flush(d, "unknown MIDI event (%02x)", status&0xf0)
Expand Down
12 changes: 6 additions & 6 deletions format/midi/sysex.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

var sysex = scalar.UintMapSymStr{
0x00f0: "Sysex Message",
0x80f7: "Sysex Continuation",
0x00f7: "Sysex Escape",
0x00f0: "sysex_message",
0x80f7: "sysex_continuation",
0x00f7: "sysex_escape",
}

func decodeSysExEvent(d *decode.D, status uint8, ctx *context) {
Expand All @@ -28,7 +28,7 @@ func decodeSysExEvent(d *decode.D, status uint8, ctx *context) {
d.Errorf("SysExMessage F0 start byte without terminating F7")

case status == 0xf0:
d.FieldStruct("SysExMessage", func(d *decode.D) {
d.FieldStruct("sysex_message", func(d *decode.D) {
d.FieldStruct("time", delta)
d.FieldUintFn("event", func(d *decode.D) uint64 {
d.U8()
Expand All @@ -39,7 +39,7 @@ func decodeSysExEvent(d *decode.D, status uint8, ctx *context) {
})

case status == 0xf7 && ctx.casio:
d.FieldStruct("SysExContinuation", func(d *decode.D) {
d.FieldStruct("sysex_continuation", func(d *decode.D) {
d.FieldStruct("time", delta)
d.FieldUintFn("event", func(d *decode.D) uint64 {
d.U8()
Expand All @@ -50,7 +50,7 @@ func decodeSysExEvent(d *decode.D, status uint8, ctx *context) {
})

case status == 0xf7:
d.FieldStruct("SysExEscape", func(d *decode.D) {
d.FieldStruct("sysex_escape", func(d *decode.D) {
d.FieldStruct("time", delta)
d.FieldUintFn("event", func(d *decode.D) uint64 {
d.U8()
Expand Down
37 changes: 37 additions & 0 deletions format/midi/testdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# NOTES

## Files

1. _format-0.mid_
MIDI format 0 reference file. Contains a single track with all supported MIDI events.

2. _format-1.mid_
MIDI format 1 reference file. Contains two tracks:
- _Track 0_, a tempo track with the _Time Signature_ and _Tempo_ events
- _Track 1_, with all the other supported MIDI events

3. _format-2.mid_
MIDI format 2 reference file. Contains two tracks:
- _Track 0_, a track with all supported MIDI events
- _Track 1_, a reversed version of _Track 0_

4. _empty.mid_
Empty MIDI file to verify MIDI decoder handles empty files.

5. _unknown_chunks.mid_

MIDI file with non-MIDI chunks interleaved between the _MTrk_ track chunks.

6. _key_signatures.mid_

Test file with all supported MIDI key signatures.

7. _notes.mid_

Test file with all supported MIDI notes.


8. _twinkle.mid_

Sample MIDI file for the example queries in the help.

Loading

0 comments on commit c8d9397

Please sign in to comment.