Skip to content

Commit

Permalink
MIDI import: fix putting notes before the beginning of a pattern (#5343)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhysSong authored Dec 23, 2019
1 parent 578a947 commit 4bfcc30
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions plugins/MidiImport/MidiImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,14 @@ class smfMidiChannel
p( NULL ),
it_inst( NULL ),
isSF2( false ),
hasNotes( false ),
lastEnd( 0 )
hasNotes( false )
{ }

InstrumentTrack * it;
Pattern* p;
Instrument * it_inst;
bool isSF2;
bool hasNotes;
MidiTime lastEnd;
QString trackName;

smfMidiChannel * create( TrackContainer* tc, QString tn )
Expand Down Expand Up @@ -255,26 +253,49 @@ class smfMidiChannel
if( trackName != "") {
it->setName( tn );
}
lastEnd = 0;
// General MIDI default
it->pitchRangeModel()->setInitValue( 2 );

// Create a default pattern
p = dynamic_cast<Pattern*>(it->createTCO(0));
}
return this;
}


void addNote( Note & n )
{
if( !p || n.pos() > lastEnd + DefaultTicksPerTact )
if (!p)
{
MidiTime pPos = MidiTime( n.pos().getTact(), 0 );
p = dynamic_cast<Pattern*>( it->createTCO( 0 ) );
p->movePosition( pPos );
p = dynamic_cast<Pattern*>(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<Pattern*>(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;
}

};
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 4bfcc30

Please sign in to comment.