-
Notifications
You must be signed in to change notification settings - Fork 0
/
midifile.go
591 lines (515 loc) · 12.7 KB
/
midifile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package kit
/*
import (
"container/list"
"encoding/binary"
"fmt"
"io"
"math"
"os"
)
// UnfinishedDuration is an 'unset' value for Duration
const UnfinishedDuration = math.MaxInt32 - 1
const defaultReleaseVelocity = byte(0)
// MIDIFile lets you read a MIDI File
type MIDIFile struct {
path string
dat []byte
sofar int
toberead int
format int
ntracks int
division int
parsed bool
tracks []*Phrase
currentTrackPhrase *Phrase // used while reading the file
phrase *Phrase // all tracks merged into a single phrase
clickfactor float32
currtime int
dosysexcontinue bool
bytes []byte
// noteq *Phrase // noteons to be completed when noteoffs found
noteq *list.List
onoffmerge bool
numq int
}
// NewMIDIFile creates a MIDIFile
func NewMIDIFile(path string) (*MIDIFile, error) {
m := &MIDIFile{
path: path,
dosysexcontinue: true,
tracks: make([]*Phrase, 0),
onoffmerge: true,
numq: 0,
}
err := m.Parse()
if err != nil {
return nil, fmt.Errorf("error in MIDIFile.Phrase: %s", err)
}
return m, nil
}
// Phrase returns a single Phrase containing all tracks in the MIDIFile
func (m *MIDIFile) Phrase() *Phrase {
if m.phrase == nil {
m.phrase = NewPhrase()
for n := 0; n < m.ntracks; n++ {
m.phrase = m.phrase.Merge(m.tracks[n])
}
}
return m.phrase
}
// Parse reads the contents of a MIDIFile and creates Phrases for each track
func (m *MIDIFile) Parse() error {
if m.parsed {
return nil
}
dat, err := os.ReadFile(m.path)
if err != nil {
return fmt.Errorf("unable to read midifile: path=%s err=%s", m.path, err)
}
m.dat = dat
m.sofar = 0
err = m.readHeader()
if err != nil {
return err
}
for len(m.tracks) < m.ntracks {
err := m.readTrack()
if err == io.EOF {
// Normal EOF
break
} else if err != nil {
return fmt.Errorf("error while reading track: %s", err)
}
}
if len(m.tracks) != m.ntracks {
return fmt.Errorf("ntracks in header (%d) doesn't match number of tracks read (%d)",
m.ntracks, len(m.tracks))
}
// Expect an EOF if we try to read more
if m.readTrack() != io.EOF {
Warn("Hmmm, there's extra stuff in the MIDIFile after reading all tracks!")
}
m.parsed = true
return nil
}
func (m *MIDIFile) readMT() (string, error) {
b0, _ := m.readc()
b1, _ := m.readc()
b2, _ := m.readc()
b3, err := m.readc()
if err != nil {
return "", err
}
s := string(b0) + string(b1) + string(b2) + string(b3)
return s, nil
}
func (m *MIDIFile) readHeader() error {
s, err := m.readMT()
if err != nil {
return err
}
if s != "MThd" {
return fmt.Errorf("bad header in midifile, expecting MThd, got %s", s)
}
m.toberead, _ = m.read32bit()
m.format, _ = m.read16bit()
m.ntracks, _ = m.read16bit()
m.division, err = m.read16bit()
if err != nil {
return err
}
var clicks float32 = 96 // constant, not based on defaultClicksPerSecond or anything that changes
var tempo float32 = 500000
if (0x8000 & m.division) != 0 {
// It's SMPTE, frame-per-second and ticks per frame
framesPerSecond := (m.division >> 8) & 0x7f
ticksPerFrame := m.division & 0xff
m.clickfactor = (float32)(framesPerSecond*ticksPerFrame) / (clicks * (1000000.0 / tempo))
} else {
m.clickfactor = (float32)(m.division) / clicks
}
// flush any extra, in case header length is not 6
for m.toberead > 0 {
m.readc()
}
return nil
}
func (m *MIDIFile) starttrack() {
m.currentTrackPhrase = NewPhrase()
m.noteq = list.New()
}
// output the top Noteq and remove it from the list
func (m *MIDIFile) putnfree() {
n := m.noteq.Remove(m.noteq.Front())
m.numq--
if n.Duration == UnfinishedDuration {
n.Duration = m.clicks() - n.Clicks
}
m.currentTrackPhrase = m.currentTrackPhrase.InsertNoLock(n)
m.currentTrackPhrase.ResetLengthNoLock()
}
func (m *MIDIFile) putallnotes() {
for m.noteq.Front() != nil {
m.putnfree()
}
}
func (m *MIDIFile) endtrack() {
m.putallnotes()
if m.currentTrackPhrase == nil {
Warn("unexpected nil value of m.currentTrackPhrase")
} else {
m.tracks = append(m.tracks, m.currentTrackPhrase)
m.currentTrackPhrase = nil
}
}
func (m *MIDIFile) msginit() {
m.bytes = []byte{}
}
func (m *MIDIFile) msgadd(c byte) {
m.bytes = append(m.bytes, c)
}
func (m *MIDIFile) msgbytes() []byte {
return m.bytes
}
func (m *MIDIFile) metaevent(metatype byte) {
}
func (m *MIDIFile) noteon(synth string, pitch, velocity byte) {
if velocity == 0 {
m.noteoff(synth, pitch, defaultReleaseVelocity)
} else {
m.queuenote(synth, pitch, velocity, "noteon")
}
}
func (m *MIDIFile) noteoff(synth string, pitch, velocity byte) {
// find the first note-on (if any) that matches this one
n := m.noteq.Front()
for ; n != nil; n = n.Next() {
if n.Synth() == synth && n.Pitch() == pitch && n.TypeOf() == "noteon" {
break
}
}
if n == nil {
// it's an isolated note-off
n = m.queuenote(synth, pitch, velocity, "noteoff")
n.Duration = 0
} else if !m.onoffmerge && velocity != defaultReleaseVelocity {
// If the note-off matches a previous note-on, but has a
// non-default velocity, then we have to turn it into a
// separate keykit note-off, instead of merging it with
// the note-on into a single note.
o := m.queuenote(synth, pitch, velocity, "noteoff")
n.Duration = 0
o.Duration = 0
} else {
// A completed note.
n.TypeOf = "note"
n.Duration = m.clicks() - n.Clicks
// If the MIDI File contains negative delta times (which
// probably aren't legal!) the duration turns out to be
// negative. Here we deal with that.
if n.Duration < 0 {
n.Clicks = n.Clicks + n.Duration
n.Duration = -n.Duration
}
}
// Now start at the beginning of the list and put out any
// notes we've completed. This guarantees that the starting
// times of the notes are in the proper (ie. monotonically
// progressing) order.
for {
n := m.noteq.Front()
if n == nil {
break
}
// quit when we get to the first unfinished note
if n.TypeOf != "notebytes" && n.Duration == UnfinishedDuration {
break
}
m.putnfree()
}
// If the number of notes int Noteq gets too big, then we're
// probably suffering from a note-on that never had a note-off
// Force it out.
if m.numq > 1024 {
m.putnfree()
}
}
func (m *MIDIFile) pressure(synth string, c1, c2 byte) {
m.queuebytes(synth, []byte{PressureStatus, c1, c2})
}
func (m *MIDIFile) controller(synth string, c1, c2 byte) {
m.queuebytes(synth, []byte{ControllerStatus, c1, c2})
}
func (m *MIDIFile) pitchbend(synth string, c1, c2 byte) {
m.queuebytes(synth, []byte{PitchbendStatus, c1, c2})
}
func (m *MIDIFile) program(synth string, c1 byte) {
m.queuebytes(synth, []byte{ProgramStatus, c1})
}
func (m *MIDIFile) chanpressure(synth string, c1 byte) {
m.queuebytes(synth, []byte{ChanPressureStatus, c1})
}
func (m *MIDIFile) queuebytes(synth string, bytes []byte) {
n := &Note{
TypeOf: "notebytes",
Clicks: m.clicks(),
bytes: bytes,
Synth: synth,
next: nil,
}
m.add2noteq(n)
}
func (m *MIDIFile) sysex(synth string, bytes []byte) {
m.queuebytes(synth, bytes)
}
func (m *MIDIFile) chanmessage(status, c1, c2 byte) {
channel := status & 0xf
synth := fmt.Sprintf("channel%d", channel+1)
switch status & 0xf0 {
case NoteOnStatus:
m.noteon(synth, c1, c2)
case NoteOffStatus:
m.noteoff(synth, c1, c2)
case PressureStatus:
m.pressure(synth, c1, c2)
case ControllerStatus:
m.controller(synth, c1, c2)
case PitchbendStatus:
m.pitchbend(synth, c1, c2)
case ProgramStatus:
m.program(synth, c1)
case ChanPressureStatus:
m.chanpressure(synth, c1)
}
}
func (m *MIDIFile) arbitrary(synth string, bytes []byte) {
m.queuebytes(synth, bytes)
}
func (m *MIDIFile) clicks() Clicks {
clks := float32(m.currtime) / m.clickfactor
return (Clicks)(clks + 0.5) // round it
}
func (m *MIDIFile) queuenote(synth string, pitch, velocity byte, notetype string) *Note {
n := &Note{
TypeOf: notetype,
Clicks: m.clicks(),
Pitch: pitch,
Velocity: velocity,
Duration: UnfinishedDuration,
Synth: synth,
next: nil,
}
m.add2noteq(n)
return n
}
func (m *MIDIFile) add2noteq(n *Note) {
m.noteq = m.noteq.InsertNoLock(n)
m.noteq.ResetLengthNoLock()
m.numq++
}
func (m *MIDIFile) readTrack() error {
// This array is indexed by the high half of a status byte. It's
// value is either the number of bytes needed (1 or 2) for a channel
// message, or 0 (meaning it's not a channel message).
chantype := []int{
0, 0, 0, 0, 0, 0, 0, 0, // 0x00 through 0x70
2, 2, 2, 2, 1, 1, 2, 0, // 0x80 through 0xf0
}
sysexcontinue := false // if last message was an unfinished sysex
running := false // true when running status used
status := byte(0) // (possibly running) status byte
s, err := m.readMT()
if err != nil {
return err
}
if s != "MTrk" {
return fmt.Errorf("unexpected string, looking for MTrk, got %s", s)
}
m.toberead, err = m.read32bit()
if err != nil {
return err
}
m.currtime = 0
m.starttrack()
for m.toberead > 0 {
dt, err := m.readvarinum() // delta time
if err != nil {
return err
}
if dt < 0 {
return fmt.Errorf("warning: negative delta time (%d) in MIDIFile", dt)
}
m.currtime += dt
c, err := m.readc() // decrements m.toberead - does toberead need to be inside MIDIFile?
if err != nil {
return err
}
if sysexcontinue && c != 0xf7 {
Warn("Didn't find expected continuation of a sysex?")
}
if (c & 0x80) == 0 { // running status?
if status == 0 {
Warn("Unexpected running status")
}
running = true
} else {
status = c
running = false
}
needed := chantype[(status>>4)&0xf]
if needed != 0 { // ie. is it a channel message?
var c1 byte
if running {
c1 = c
} else {
c1, err = m.readc()
if err != nil {
return err
}
c1 = c1 & 0x7f
}
// The &0xf7 here may seem unnecessary, but I've seen
// 'bad' midi files that had, e.g., volume bytes
// with the upper bit set. This code should not harm
// proper data.
if needed == 1 {
m.chanmessage(status, c1, 0)
} else if needed == 2 {
c2, err := m.readc()
if err != nil {
return err
}
c2 = c2 & 0x7f
m.chanmessage(status, c1, c2)
} else {
return fmt.Errorf("unexpected value for needed: %d", needed)
}
continue
}
// Non-channel messages end up here
switch c {
case 0xff: // meta event
metatype, err := m.readc()
if err != nil {
return err
}
// watch out - Don't combine the next 2 statements
lng, err := m.readvarinum()
if err != nil {
return err
}
lookfor := m.toberead - lng
m.msginit()
for m.toberead > lookfor {
c, err := m.readc()
if err != nil {
return err
}
m.msgadd(c)
}
m.metaevent(metatype)
case 0xf0: // start of system exclusive
// watch out - Don't combine the next 2 statements
lng, err := m.readvarinum()
if err != nil {
return err
}
lookfor := m.toberead - lng
m.msginit()
m.msgadd(0xf0)
for m.toberead > lookfor {
c, err := m.readc()
if err != nil {
return err
}
m.msgadd(c)
}
if c == 0xf7 || m.dosysexcontinue {
m.sysex("", m.msgbytes())
} else {
sysexcontinue = true
}
case 0xf7: // sysex continuation or arbitrary stuff
// watch out - Don't combine the next 2 statements
lng, err := m.readvarinum()
if err != nil {
return err
}
lookfor := m.toberead - lng
if !sysexcontinue {
m.msginit()
}
for m.toberead > lookfor {
c, err := m.readc()
if err != nil {
return err
}
m.msgadd(c)
}
if !sysexcontinue {
m.arbitrary("", m.msgbytes())
} else if c == 0xf7 {
m.sysex("", m.msgbytes())
sysexcontinue = false
}
default:
Warn("unexpected midi byte", "byte", c)
}
}
m.endtrack()
return nil
}
func (m *MIDIFile) readc() (byte, error) {
if m.sofar < len(m.dat) {
c := m.dat[m.sofar]
m.sofar++
m.toberead--
return c, nil
}
return 0, io.EOF
}
func (m *MIDIFile) read32bit() (int, error) {
b0, _ := m.readc()
b1, _ := m.readc()
b2, _ := m.readc()
b3, err := m.readc()
if err != nil {
return 0, err
}
var bytes = []byte{b0, b1, b2, b3}
value := binary.BigEndian.Uint32(bytes)
return int(value), nil
}
func (m *MIDIFile) read16bit() (int, error) {
b0, _ := m.readc()
b1, err := m.readc()
if err != nil {
return 0, err
}
var bytes = []byte{b0, b1}
value := binary.BigEndian.Uint16(bytes)
return int(value), nil
}
func (m *MIDIFile) readvarinum() (int, error) {
c, err := m.readc()
if err != nil {
return 0, err
}
value := int(c)
if (c & 0x80) != 0 {
value &= 0x7f
for {
c, err := m.readc()
if err != nil {
return 0, err
}
value = (value << 7) + int(c&0x7f)
if (c & 0x80) == 0 {
break
}
}
}
return value, nil
}
*/