diff --git a/plugins/MidiImport/MidiImport.cpp b/plugins/MidiImport/MidiImport.cpp index e83ef8c2736..f0c4888da67 100644 --- a/plugins/MidiImport/MidiImport.cpp +++ b/plugins/MidiImport/MidiImport.cpp @@ -215,8 +215,7 @@ class smfMidiChannel p( NULL ), it_inst( NULL ), isSF2( false ), - hasNotes( false ), - lastEnd( 0 ) + hasNotes( false ) { } InstrumentTrack * it; @@ -224,7 +223,6 @@ class smfMidiChannel Instrument * it_inst; bool isSF2; bool hasNotes; - MidiTime lastEnd; QString trackName; smfMidiChannel * create( TrackContainer* tc, QString tn ) @@ -255,9 +253,11 @@ class smfMidiChannel if( trackName != "") { it->setName( tn ); } - lastEnd = 0; // General MIDI default it->pitchRangeModel()->setInitValue( 2 ); + + // Create a default pattern + p = dynamic_cast(it->createTCO(0)); } return this; } @@ -265,16 +265,37 @@ class smfMidiChannel void addNote( Note & n ) { - if( !p || n.pos() > lastEnd + DefaultTicksPerTact ) + if (!p) { - MidiTime pPos = MidiTime( n.pos().getTact(), 0 ); - p = dynamic_cast( it->createTCO( 0 ) ); - p->movePosition( pPos ); + p = dynamic_cast(it->createTCO(0)); } + p->addNote(n, false); hasNotes = true; - lastEnd = n.pos() + n.length(); - n.setPos( n.pos( p->startPosition() ) ); - p->addNote( n, false ); + } + + void splitPatterns() + { + Pattern * newPattern = nullptr; + MidiTime lastEnd(0); + + p->rearrangeAllNotes(); + for (auto n : p->notes()) + { + if (!newPattern || n->pos() > lastEnd + DefaultTicksPerTact) + { + MidiTime pPos = MidiTime(n->pos().getTact(), 0); + newPattern = dynamic_cast(it->createTCO(0)); + newPattern->movePosition(pPos); + } + lastEnd = n->pos() + n->length(); + + Note newNote(*n); + newNote.setPos(n->pos(newPattern->startPosition())); + newPattern->addNote(newNote, false); + } + + delete p; + p = nullptr; } }; @@ -534,7 +555,11 @@ bool MidiImport::readSMF( TrackContainer* tc ) for( int c=0; c < 256; ++c ) { - if( !chs[c].hasNotes && chs[c].it ) + if (chs[c].hasNotes) + { + chs[c].splitPatterns(); + } + else if (chs[c].it) { printf(" Should remove empty track\n"); // must delete trackView first - but where is it?