File: MIDIFileReader.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (1069 lines) | stat: -rw-r--r-- 31,559 bytes parent folder | download | duplicates (2)
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


/*
   This is a modified version of a source file from the 
   Rosegarden MIDI and audio sequencer and notation editor.
   This file copyright 2000-2006 Richard Bown and Chris Cannam.
*/


#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <filesystem>

#include "MIDIFileReader.h"

#include "data/midi/MIDIEvent.h"

#include "model/Model.h"
#include "base/Pitch.h"
#include "base/RealTime.h"
#include "model/NoteModel.h"

#include <QString>
#include <QFileInfo>

#include <sstream>

#include "base/Debug.h"

using std::string;
using std::ifstream;
using std::stringstream;
using std::ends;
using std::ios;
using std::vector;
using std::map;
using std::set;

namespace sv {

using namespace MIDIConstants;

//#define MIDI_DEBUG 1


MIDIFileReader::MIDIFileReader(QString path,
                               MIDIFileImportPreferenceAcquirer *acquirer,
                               sv_samplerate_t mainModelSampleRate,
                               ProgressReporter *) : // we don't actually report progress
    m_smpte(false),
    m_timingDivision(0),
    m_fps(0),
    m_subframes(0),
    m_format(MIDI_FILE_BAD_FORMAT),
    m_numberOfTracks(0),
    m_trackByteCount(0),
    m_decrementCount(false),
    m_path(path),
    m_midiFile(nullptr),
    m_fileSize(0),
    m_mainModelSampleRate(mainModelSampleRate),
    m_acquirer(acquirer)
{
    if (parseFile()) {
        m_error = "";
    }
}

MIDIFileReader::~MIDIFileReader()
{
    for (MIDIComposition::iterator i = m_midiComposition.begin();
         i != m_midiComposition.end(); ++i) {
        
        for (MIDITrack::iterator j = i->second.begin();
             j != i->second.end(); ++j) {
            delete *j;
        }

        i->second.clear();
    }

    m_midiComposition.clear();
}

bool
MIDIFileReader::isOK() const
{
    return (m_error == "");
}

QString
MIDIFileReader::getError() const
{
    return m_error;
}

long
MIDIFileReader::midiBytesToLong(const string& bytes)
{
    if (bytes.length() != 4) {
        throw MIDIException(tr("Wrong length for long data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(4));
    }

    long longRet = ((long)(((MIDIByte)bytes[0]) << 24)) |
                   ((long)(((MIDIByte)bytes[1]) << 16)) |
                   ((long)(((MIDIByte)bytes[2]) << 8)) |
                   ((long)((MIDIByte)(bytes[3])));

    return longRet;
}

int
MIDIFileReader::midiBytesToInt(const string& bytes)
{
    if (bytes.length() != 2) {
        throw MIDIException(tr("Wrong length for int data in MIDI stream (%1, should be %2)").arg(bytes.length()).arg(2));
    }

    int intRet = ((int)(((MIDIByte)bytes[0]) << 8)) |
                 ((int)(((MIDIByte)bytes[1])));
    return(intRet);
}


// Gets a single byte from the MIDI byte stream.  For each track
// section we can read only a specified number of bytes held in
// m_trackByteCount.
//
MIDIByte
MIDIFileReader::getMIDIByte()
{
    if (!m_midiFile) {
        throw MIDIException(tr("getMIDIByte called but no MIDI file open"));
    }

    if (m_midiFile->eof()) {
        throw MIDIException(tr("End of MIDI file encountered while reading"));
    }

    if (m_decrementCount && m_trackByteCount <= 0) {
        throw MIDIException(tr("Attempt to get more bytes than expected on Track"));
    }

    char byte;
    if (m_midiFile->read(&byte, 1)) {
        --m_trackByteCount;
        return (MIDIByte)byte;
    }

    throw MIDIException(tr("Attempt to read past MIDI file end"));
}


// Gets a specified number of bytes from the MIDI byte stream.  For
// each track section we can read only a specified number of bytes
// held in m_trackByteCount.
//
string
MIDIFileReader::getMIDIBytes(unsigned long numberOfBytes)
{
    if (!m_midiFile) {
        throw MIDIException(tr("getMIDIBytes called but no MIDI file open"));
    }

    if (m_midiFile->eof()) {
        throw MIDIException(tr("End of MIDI file encountered while reading"));
    }

    if (m_decrementCount && (numberOfBytes > (unsigned long)m_trackByteCount)) {
        throw MIDIException(tr("Attempt to get more bytes than available on Track (%1, only have %2)").arg(numberOfBytes).arg(m_trackByteCount));
    }

    string stringRet;
    char fileMIDIByte;

    while (stringRet.length() < numberOfBytes &&
           m_midiFile->read(&fileMIDIByte, 1)) {
        stringRet += fileMIDIByte;
    }

    // if we've reached the end of file without fulfilling the
    // quota then panic as our parsing has performed incorrectly
    //
    if (stringRet.length() < numberOfBytes) {
        stringRet = "";
        throw MIDIException(tr("Attempt to read past MIDI file end"));
    }

    // decrement the byte count
    if (m_decrementCount)
        m_trackByteCount -= stringRet.length();

    return stringRet;
}


// Get a long number of variable length from the MIDI byte stream.
//
long
MIDIFileReader::getNumberFromMIDIBytes(int firstByte)
{
    if (!m_midiFile) {
        throw MIDIException(tr("getNumberFromMIDIBytes called but no MIDI file open"));
    }

    long longRet = 0;
    MIDIByte midiByte;

    if (firstByte >= 0) {
        midiByte = (MIDIByte)firstByte;
    } else if (m_midiFile->eof()) {
        return longRet;
    } else {
        midiByte = getMIDIByte();
    }

    longRet = midiByte;
    if (midiByte & 0x80) {
        longRet &= 0x7F;
        do {
            midiByte = getMIDIByte();
            longRet = (longRet << 7) + (midiByte & 0x7F);
        } while (!m_midiFile->eof() && (midiByte & 0x80));
    }

    return longRet;
}


// Seek to the next track in the midi file and set the number
// of bytes to be read in the counter m_trackByteCount.
//
bool
MIDIFileReader::skipToNextTrack()
{
    if (!m_midiFile) {
        throw MIDIException(tr("skipToNextTrack called but no MIDI file open"));
    }

    string buffer, buffer2;
    m_trackByteCount = -1;
    m_decrementCount = false;

    while (!m_midiFile->eof() && (m_decrementCount == false)) {
        buffer = getMIDIBytes(4); 
        if (buffer.compare(0, 4, MIDI_TRACK_HEADER) == 0) {
            m_trackByteCount = midiBytesToLong(getMIDIBytes(4));
            m_decrementCount = true;
        }
    }

    if (m_trackByteCount == -1) { // we haven't found a track
        return false;
    } else {
        return true;
    }
}


// Read in a MIDI file.  The parsing process throws exceptions back up
// here if we run into trouble which we can then pass back out to
// whoever called us using a nice bool.
//
bool
MIDIFileReader::parseFile()
{
    m_error = "";

#ifdef MIDI_DEBUG
    SVDEBUG << "MIDIFileReader::open() : fileName = " << m_fileName.c_str() << endl;
#endif

    // Open the file
    m_midiFile = new ifstream(std::filesystem::u8path(m_path.toUtf8().data()),
                              ios::in | ios::binary);

    if (!*m_midiFile) {
        m_error = "File not found or not readable.";
        m_format = MIDI_FILE_BAD_FORMAT;
        delete m_midiFile;
        m_midiFile = nullptr;
        return false;
    }

    bool retval = false;

    try {

        // Set file size so we can count it off
        //
        m_midiFile->seekg(0, ios::end);
        std::streamoff off = m_midiFile->tellg();
        m_fileSize = 0;
        if (off > 0) m_fileSize = off;
        m_midiFile->seekg(0, ios::beg);

        // Parse the MIDI header first.  The first 14 bytes of the file.
        if (!parseHeader(getMIDIBytes(14))) {
            m_format = MIDI_FILE_BAD_FORMAT;
            m_error = "Not a MIDI file.";
            goto done;
        }

        unsigned int i = 0;

        for (unsigned int j = 0; j < m_numberOfTracks; ++j) {

#ifdef MIDI_DEBUG
            SVDEBUG << "Parsing Track " << j << endl;
#endif

            if (!skipToNextTrack()) {
#ifdef MIDI_DEBUG
                SVDEBUG << "Couldn't find Track " << j << endl;
#endif
                m_error = "File corrupted or in non-standard format?";
                m_format = MIDI_FILE_BAD_FORMAT;
                goto done;
            }

#ifdef MIDI_DEBUG
            SVDEBUG << "Track has " << m_trackByteCount << " bytes" << endl;
#endif

            // Run through the events taking them into our internal
            // representation.
            if (!parseTrack(i)) {
#ifdef MIDI_DEBUG
                SVDEBUG << "Track " << j << " parsing failed" << endl;
#endif
                m_error = "File corrupted or in non-standard format?";
                m_format = MIDI_FILE_BAD_FORMAT;
                goto done;
            }

            ++i; // j is the source track number, i the destination
        }
        
        m_numberOfTracks = i;
        retval = true;

    } catch (const MIDIException &e) {

        SVDEBUG << "MIDIFileReader::open() - caught exception - " << e.what() << endl;
        m_error = e.what();
    }
    
done:
    m_midiFile->close();
    delete m_midiFile;

    for (unsigned int track = 0; track < m_numberOfTracks; ++track) {

        // Convert the deltaTime to an absolute time since the track
        // start.  The addTime method returns the sum of the current
        // MIDI Event delta time plus the argument.

        unsigned long acc = 0;

        for (MIDITrack::iterator i = m_midiComposition[track].begin();
             i != m_midiComposition[track].end(); ++i) {
            acc = (*i)->addTime(acc);
        }

        if (consolidateNoteOffEvents(track)) { // returns true if some notes exist
            m_loadableTracks.insert(track);
        }
    }

    for (unsigned int track = 0; track < m_numberOfTracks; ++track) {
        updateTempoMap(track);
    }

    calculateTempoTimestamps();

    return retval;
}

// Parse and ensure the MIDI Header is legitimate
//
bool
MIDIFileReader::parseHeader(const string &midiHeader)
{
    if (midiHeader.size() < 14) {
#ifdef MIDI_DEBUG
        SVDEBUG << "MIDIFileReader::parseHeader() - file header undersized" << endl;
#endif
        return false;
    }

    if (midiHeader.compare(0, 4, MIDI_FILE_HEADER) != 0) {
#ifdef MIDI_DEBUG
        SVDEBUG << "MIDIFileReader::parseHeader()"
             << "- file header not found or malformed"
             << endl;
#endif
        return false;
    }

    if (midiBytesToLong(midiHeader.substr(4,4)) != 6L) {
#ifdef MIDI_DEBUG
        SVDEBUG << "MIDIFileReader::parseHeader()"
             << " - header length incorrect"
             << endl;
#endif
        return false;
    }

    m_format = (MIDIFileFormatType) midiBytesToInt(midiHeader.substr(8,2));
    m_numberOfTracks = midiBytesToInt(midiHeader.substr(10,2));
    m_timingDivision = midiBytesToInt(midiHeader.substr(12,2));

    if (m_timingDivision >= 32768) {
        m_smpte = true;
        m_fps = 256 - (m_timingDivision >> 8);
        m_subframes = (m_timingDivision & 0xff);
    } else {
        m_smpte = false;
    }

    return true; 
}

// Extract the contents from a MIDI file track and places it into
// our local map of MIDI events.
//
bool
MIDIFileReader::parseTrack(unsigned int &lastTrackNum)
{
    MIDIByte midiByte, metaEventCode, data1, data2;
    MIDIByte eventCode = 0x80;
    string metaMessage;
    long messageLength;
    long deltaTime;
    long accumulatedTime = 0;

    // The trackNum passed in to this method is the default track for
    // all events provided they're all on the same channel.  If we find
    // events on more than one channel, we increment trackNum and record
    // the mapping from channel to trackNum in this channelTrackMap.
    // We then return the new trackNum by reference so the calling
    // method knows we've got more tracks than expected.

    // This would be a vector<unsigned int> but we need -1 to indicate
    // "not yet used"
    vector<int> channelTrackMap(16, -1);

    // This is used to store the last absolute time found on each track,
    // allowing us to modify delta-times correctly when separating events
    // out from one to multiple tracks
    //
    map<int, unsigned long> trackTimeMap;

    // Meta-events don't have a channel, so we place them in a fixed
    // track number instead
    unsigned int metaTrack = lastTrackNum;

    // Remember the last non-meta status byte (-1 if we haven't seen one)
    int runningStatus = -1;

    bool firstTrack = true;

    while (!m_midiFile->eof() && (m_trackByteCount > 0)) {

        if (eventCode < 0x80) {
#ifdef MIDI_DEBUG
            SVDEBUG << "WARNING: Invalid event code " << eventCode
                 << " in MIDI file" << endl;
#endif
            throw MIDIException(tr("Invalid event code %1 found").arg(int(eventCode)));
        }

        deltaTime = getNumberFromMIDIBytes();

#ifdef MIDI_DEBUG
        SVDEBUG << "read delta time " << deltaTime << endl;
#endif

        // Get a single byte
        midiByte = getMIDIByte();

        if (!(midiByte & MIDI_STATUS_BYTE_MASK)) {

            if (runningStatus < 0) {
                throw MIDIException(tr("Running status used for first event in track"));
            }

            eventCode = (MIDIByte)runningStatus;
            data1 = midiByte;

#ifdef MIDI_DEBUG
            SVDEBUG << "using running status (byte " << int(midiByte) << " found)" << endl;
#endif
        } else {
#ifdef MIDI_DEBUG
            SVDEBUG << "have new event code " << int(midiByte) << endl;
#endif
            eventCode = midiByte;
            data1 = getMIDIByte();
        }

        if (eventCode == MIDI_FILE_META_EVENT) {

            metaEventCode = data1;
            messageLength = getNumberFromMIDIBytes();

//#ifdef MIDI_DEBUG
                SVDEBUG << "Meta event of type " << int(metaEventCode) << " and " << messageLength << " bytes found, putting on track " << metaTrack << endl;
//#endif
            metaMessage = getMIDIBytes(messageLength);

            long gap = accumulatedTime - trackTimeMap[metaTrack];
            accumulatedTime += deltaTime;
            deltaTime += gap;
            trackTimeMap[metaTrack] = accumulatedTime;

            MIDIEvent *e = new MIDIEvent(deltaTime,
                                         MIDI_FILE_META_EVENT,
                                         metaEventCode,
                                         metaMessage);

            m_midiComposition[metaTrack].push_back(e);

            if (metaEventCode == MIDI_TRACK_NAME) {
                m_trackNames[metaTrack] = metaMessage.c_str();
            }

        } else { // non-meta events

            runningStatus = eventCode;

            MIDIEvent *midiEvent;

            int channel = (eventCode & MIDI_CHANNEL_NUM_MASK);
            if (channelTrackMap[channel] == -1) {
                if (!firstTrack) ++lastTrackNum;
                else firstTrack = false;
                channelTrackMap[channel] = lastTrackNum;
            }

            unsigned int trackNum = channelTrackMap[channel];
            
            // accumulatedTime is abs time of last event on any track;
            // trackTimeMap[trackNum] is that of last event on this track
            
            long gap = accumulatedTime - trackTimeMap[trackNum];
            accumulatedTime += deltaTime;
            deltaTime += gap;
            trackTimeMap[trackNum] = accumulatedTime;

            switch (eventCode & MIDI_MESSAGE_TYPE_MASK) {

            case MIDI_NOTE_ON:
            case MIDI_NOTE_OFF:
            case MIDI_POLY_AFTERTOUCH:
            case MIDI_CTRL_CHANGE:
                data2 = getMIDIByte();

                // create and store our event
                midiEvent = new MIDIEvent(deltaTime, eventCode, data1, data2);

                /*
                SVDEBUG << "MIDI event for channel " << channel << " (track "
                          << trackNum << ")" << endl;
                midiEvent->print();
                          */


                m_midiComposition[trackNum].push_back(midiEvent);

                if (midiEvent->getChannelNumber() == MIDI_PERCUSSION_CHANNEL) {
                    m_percussionTracks.insert(trackNum);
                }

                break;

            case MIDI_PITCH_BEND:
                data2 = getMIDIByte();

                // create and store our event
                midiEvent = new MIDIEvent(deltaTime, eventCode, data1, data2);
                m_midiComposition[trackNum].push_back(midiEvent);
                break;

            case MIDI_PROG_CHANGE:
            case MIDI_CHNL_AFTERTOUCH:
                // create and store our event
                midiEvent = new MIDIEvent(deltaTime, eventCode, data1);
                m_midiComposition[trackNum].push_back(midiEvent);
                break;

            case MIDI_SYSTEM_EXCLUSIVE:
                messageLength = getNumberFromMIDIBytes(data1);

#ifdef MIDI_DEBUG
                SVDEBUG << "SysEx of " << messageLength << " bytes found" << endl;
#endif

                metaMessage= getMIDIBytes(messageLength);

                if (MIDIByte(metaMessage[metaMessage.length() - 1]) !=
                        MIDI_END_OF_EXCLUSIVE)
                {
#ifdef MIDI_DEBUG
                    SVDEBUG << "MIDIFileReader::parseTrack() - "
                              << "malformed or unsupported SysEx type"
                              << endl;
#endif
                    continue;
                }

                // chop off the EOX 
                // length fixed by Pedro Lopez-Cabanillas (20030523)
                //
                metaMessage = metaMessage.substr(0, metaMessage.length()-1);

                midiEvent = new MIDIEvent(deltaTime,
                                          MIDI_SYSTEM_EXCLUSIVE,
                                          metaMessage);
                m_midiComposition[trackNum].push_back(midiEvent);
                break;

            default:
#ifdef MIDI_DEBUG
                SVDEBUG << "MIDIFileReader::parseTrack()" 
                          << " - Unsupported MIDI Event Code:  "
                          << (int)eventCode << endl;
#endif
                break;
            } 
        }
    }

    if (lastTrackNum > metaTrack) {
        for (unsigned int track = metaTrack + 1; track <= lastTrackNum; ++track) {
            m_trackNames[track] = QString("%1 <%2>")
                .arg(m_trackNames[metaTrack]).arg(track - metaTrack + 1);
        }
    }

    return true;
}

// Delete dead NOTE OFF and NOTE ON/Zero Velocity Events after
// reading them and modifying their relevant NOTE ONs.  Return true
// if there are some notes in this track.
//
bool
MIDIFileReader::consolidateNoteOffEvents(unsigned int track)
{
    bool notesOnTrack = false;
    bool noteOffFound;

    for (MIDITrack::iterator i = m_midiComposition[track].begin();
         i != m_midiComposition[track].end(); i++) {

        if ((*i)->getMessageType() == MIDI_NOTE_ON && (*i)->getVelocity() > 0) {

            notesOnTrack = true;
            noteOffFound = false;

            for (MIDITrack::iterator j = i;
                 j != m_midiComposition[track].end(); j++) {

                if (((*j)->getChannelNumber() == (*i)->getChannelNumber()) &&
                    ((*j)->getPitch() == (*i)->getPitch()) &&
                    ((*j)->getMessageType() == MIDI_NOTE_OFF ||
                    ((*j)->getMessageType() == MIDI_NOTE_ON &&
                     (*j)->getVelocity() == 0x00))) {

                    (*i)->setDuration((*j)->getTime() - (*i)->getTime());

                    delete *j;
                    m_midiComposition[track].erase(j);

                    noteOffFound = true;
                    break;
                }
            }

            // If no matching NOTE OFF has been found then set
            // Event duration to length of track
            //
            if (!noteOffFound) {
                MIDITrack::iterator j = m_midiComposition[track].end();
                --j;
                (*i)->setDuration((*j)->getTime() - (*i)->getTime());
            }
        }
    }

    return notesOnTrack;
}

// Add any tempo events found in the given track to the global tempo map.
//
void
MIDIFileReader::updateTempoMap(unsigned int track)
{
    SVDEBUG << "updateTempoMap for track " << track << " (" << m_midiComposition[track].size() << " events)" << endl;

    for (MIDITrack::iterator i = m_midiComposition[track].begin();
         i != m_midiComposition[track].end(); ++i) {

        if ((*i)->isMeta() &&
            (*i)->getMetaEventCode() == MIDI_SET_TEMPO) {

            MIDIByte m0 = (*i)->getMetaMessage()[0];
            MIDIByte m1 = (*i)->getMetaMessage()[1];
            MIDIByte m2 = (*i)->getMetaMessage()[2];
            
            long tempo = (((m0 << 8) + m1) << 8) + m2;

            SVDEBUG << "updateTempoMap: have tempo, it's " << tempo << " at " << (*i)->getTime() << endl;

            if (tempo != 0) {
                double qpm = 60000000.0 / double(tempo);
                m_tempoMap[(*i)->getTime()] =
                    TempoChange(RealTime::zeroTime, qpm);
            }
        }
    }
}

void
MIDIFileReader::calculateTempoTimestamps()
{
    unsigned long lastMIDITime = 0;
    RealTime lastRealTime = RealTime::zeroTime;
    double tempo = 120.0;
    int td = m_timingDivision;
    if (td == 0) td = 96;

    for (TempoMap::iterator i = m_tempoMap.begin(); i != m_tempoMap.end(); ++i) {
        
        unsigned long mtime = i->first;
        unsigned long melapsed = mtime - lastMIDITime;
        double quarters = double(melapsed) / double(td);
        double seconds = (60.0 * quarters) / tempo;

        RealTime t = lastRealTime + RealTime::fromSeconds(seconds);

        i->second.first = t;

        lastRealTime = t;
        lastMIDITime = mtime;
        tempo = i->second.second;
    }
}

RealTime
MIDIFileReader::getTimeForMIDITime(unsigned long midiTime) const
{
    unsigned long tempoMIDITime = 0;
    RealTime tempoRealTime = RealTime::zeroTime;
    double tempo = 120.0;

    TempoMap::const_iterator i = m_tempoMap.lower_bound(midiTime);
    if (i != m_tempoMap.begin()) {
        --i;
        tempoMIDITime = i->first;
        tempoRealTime = i->second.first;
        tempo = i->second.second;
    }

    int td = m_timingDivision;
    if (td == 0) td = 96;

    unsigned long melapsed = midiTime - tempoMIDITime;
    double quarters = double(melapsed) / double(td);
    double seconds = (60.0 * quarters) / tempo;

/*
    SVDEBUG << "MIDIFileReader::getTimeForMIDITime(" << midiTime << ")"
              << endl;
    SVDEBUG << "timing division = " << td << endl;
    SVDEBUG << "nearest tempo event (of " << m_tempoMap.size() << ") is at " << tempoMIDITime << " ("
              << tempoRealTime << ")" << endl;
    SVDEBUG << "quarters since then = " << quarters << endl;
    SVDEBUG << "tempo = " << tempo << " quarters per minute" << endl;
    SVDEBUG << "seconds since then = " << seconds << endl;
    SVDEBUG << "resulting time = " << (tempoRealTime + RealTime::fromSeconds(seconds)) << endl;
*/

    return tempoRealTime + RealTime::fromSeconds(seconds);
}

Model *
MIDIFileReader::load() const
{
    if (!isOK()) return nullptr;

    if (m_loadableTracks.empty()) {
        if (m_acquirer) {
            m_acquirer->showError
                (tr("MIDI file \"%1\" has no notes in any track").arg(m_path));
        }
        return nullptr;
    }

    std::set<unsigned int> tracksToLoad;

    if (m_loadableTracks.size() == 1) {

        tracksToLoad.insert(*m_loadableTracks.begin());

    } else {

        QStringList displayNames;

        for (set<unsigned int>::iterator i = m_loadableTracks.begin();
             i != m_loadableTracks.end(); ++i) {

            unsigned int trackNo = *i;
            QString label;

            QString perc;
            if (m_percussionTracks.find(trackNo) != m_percussionTracks.end()) {
                perc = tr(" - uses GM percussion channel");
            }

            if (m_trackNames.find(trackNo) != m_trackNames.end()) {
                label = tr("Track %1 (%2)%3")
                    .arg(trackNo).arg(m_trackNames.find(trackNo)->second)
                    .arg(perc);
            } else {
                label = tr("Track %1 (untitled)%3").arg(trackNo).arg(perc);
            }

            displayNames << label;
        }

        QString singleTrack;

        bool haveSomePercussion = 
            (!m_percussionTracks.empty() &&
             (m_percussionTracks.size() < m_loadableTracks.size()));

        MIDIFileImportPreferenceAcquirer::TrackPreference pref;

        if (m_acquirer) {
            pref = m_acquirer->getTrackImportPreference(displayNames,
                                                        haveSomePercussion,
                                                        singleTrack);
        } else {
            pref = MIDIFileImportPreferenceAcquirer::MergeAllTracks;
        }

        if (pref == MIDIFileImportPreferenceAcquirer::ImportNothing) return nullptr;

        if (pref == MIDIFileImportPreferenceAcquirer::MergeAllTracks ||
            pref == MIDIFileImportPreferenceAcquirer::MergeAllNonPercussionTracks) {
            
            for (set<unsigned int>::iterator i = m_loadableTracks.begin();
                 i != m_loadableTracks.end(); ++i) {
                
                if (pref == MIDIFileImportPreferenceAcquirer::MergeAllTracks ||
                    m_percussionTracks.find(*i) == m_percussionTracks.end()) {
                    
                    tracksToLoad.insert(*i);
                }
            }

        } else {
            
            int j = 0;

            for (set<unsigned int>::iterator i = m_loadableTracks.begin();
                 i != m_loadableTracks.end(); ++i) {
                
                if (singleTrack == displayNames[j]) {
                    tracksToLoad.insert(*i);
                    break;
                }
                
                ++j;
            }
        }
    }

    if (tracksToLoad.empty()) return nullptr;

    int n = int(tracksToLoad.size()), count = 0;
    Model *model = nullptr;

    for (std::set<unsigned int>::iterator i = tracksToLoad.begin();
         i != tracksToLoad.end(); ++i) {

        int minProgress = (100 * count) / n;
        int progressAmount = 100 / n;

        model = loadTrack(*i, model, minProgress, progressAmount);

        ++count;
    }

    if (dynamic_cast<NoteModel *>(model)) {
        dynamic_cast<NoteModel *>(model)->setCompletion(100);
    }

    return model;
}

Model *
MIDIFileReader::loadTrack(unsigned int trackToLoad,
                          Model *existingModel,
                          int minProgress,
                          int progressAmount) const
{
    if (m_midiComposition.find(trackToLoad) == m_midiComposition.end()) {
        return nullptr;
    }

    NoteModel *model = nullptr;

    if (existingModel) {
        model = dynamic_cast<NoteModel *>(existingModel);
        if (!model) {
            SVDEBUG << "WARNING: MIDIFileReader::loadTrack: Existing model given, but it isn't a NoteModel -- ignoring it" << endl;
        }
    }

    if (!model) {
        model = new NoteModel(m_mainModelSampleRate, 1, false);
        model->setValueQuantization(1.0);
        model->setObjectName(QFileInfo(m_path).fileName());
    }

    const MIDITrack &track = m_midiComposition.find(trackToLoad)->second;

    int totalEvents = int(track.size());
    int count = 0;

    bool sharpKey = true;

    for (MIDITrack::const_iterator i = track.begin(); i != track.end(); ++i) {

        RealTime rt;
        unsigned long midiTime = (*i)->getTime();

        if (m_smpte) {
            rt = RealTime::frame2RealTime(midiTime, m_fps * m_subframes);
        } else {
            rt = getTimeForMIDITime(midiTime);
        }

        // We ignore most of these event types for now, though in
        // theory some of the text ones could usefully be incorporated

        if ((*i)->isMeta()) {

            switch((*i)->getMetaEventCode()) {

            case MIDI_KEY_SIGNATURE:
                // minorKey = (int((*i)->getMetaMessage()[1]) != 0);
                sharpKey = (int((*i)->getMetaMessage()[0]) >= 0);
                break;

            case MIDI_TEXT_EVENT:
            case MIDI_LYRIC:
            case MIDI_TEXT_MARKER:
            case MIDI_COPYRIGHT_NOTICE:
            case MIDI_TRACK_NAME:
                // The text events that we could potentially use
                break;

            case MIDI_SET_TEMPO:
                // Already dealt with in a separate pass previously
                break;

            case MIDI_TIME_SIGNATURE:
                // Not yet!
                break;

            case MIDI_SEQUENCE_NUMBER:
            case MIDI_CHANNEL_PREFIX_OR_PORT:
            case MIDI_INSTRUMENT_NAME:
            case MIDI_CUE_POINT:
            case MIDI_CHANNEL_PREFIX:
            case MIDI_SEQUENCER_SPECIFIC:
            case MIDI_SMPTE_OFFSET:
            default:
                break;
            }

        } else {

            switch ((*i)->getMessageType()) {

            case MIDI_NOTE_ON:

                if ((*i)->getVelocity() == 0) break; // effective note-off
                else {
                    RealTime endRT;
                    unsigned long endMidiTime = (*i)->getTime() + (*i)->getDuration();
                    if (m_smpte) {
                        endRT = RealTime::frame2RealTime(endMidiTime, m_fps * m_subframes);
                    } else {
                        endRT = getTimeForMIDITime(endMidiTime);
                    }

                    long startFrame = RealTime::realTime2Frame
                        (rt, model->getSampleRate());

                    long endFrame = RealTime::realTime2Frame
                        (endRT, model->getSampleRate());

                    QString pitchLabel = Pitch::getPitchLabel((*i)->getPitch(),
                                                              0, 
                                                              !sharpKey);

                    QString noteLabel = tr("%1 - vel %2")
                        .arg(pitchLabel).arg(int((*i)->getVelocity()));

                    float level = float((*i)->getVelocity()) / 128.f;

                    Event note(startFrame, (*i)->getPitch(),
                               endFrame - startFrame, level, noteLabel);

//                    SVDEBUG << "Adding note " << startFrame << "," << (endFrame-startFrame) << " : " << int((*i)->getPitch()) << endl;

                    model->add(note);
                    break;
                }

            case MIDI_PITCH_BEND:
                // I guess we could make some use of this...
                break;

            case MIDI_NOTE_OFF:
            case MIDI_PROG_CHANGE:
            case MIDI_CTRL_CHANGE:
            case MIDI_SYSTEM_EXCLUSIVE:
            case MIDI_POLY_AFTERTOUCH:
            case MIDI_CHNL_AFTERTOUCH:
                break;

            default:
                break;
            }
        }

        model->setCompletion(minProgress +
                             (count * progressAmount) / totalEvents);
        ++count;
    }

    return model;
}

} // end namespace sv