/
qtractorMidiClip.cpp
1499 lines (1224 loc) · 37.9 KB
/
qtractorMidiClip.cpp
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
// qtractorMidiClip.cpp
//
/****************************************************************************
Copyright (C) 2005-2018, rncbc aka Rui Nuno Capela. All rights reserved.
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*****************************************************************************/
#include "qtractorAbout.h"
#include "qtractorMidiClip.h"
#include "qtractorMidiEngine.h"
#include "qtractorSession.h"
#include "qtractorFileList.h"
#include "qtractorDocument.h"
#include "qtractorMidiManager.h"
#include "qtractorPlugin.h"
#include "qtractorMidiEditor.h"
#include "qtractorMidiEditorForm.h"
#include "qtractorMainForm.h"
#if 0//QTRACTOR_MIDI_EDITOR_TOOL
#include "qtractorOptions.h"
#endif
#include <QMessageBox>
#include <QFileInfo>
#include <QPainter>
#include <QDomDocument>
#if QT_VERSION < 0x040500
namespace Qt {
const WindowFlags WindowCloseButtonHint = WindowFlags(0x08000000);
}
#endif
//----------------------------------------------------------------------
// class qtractorMidiClip::Key -- MIDI sequence clip (hash key).
//
class qtractorMidiClip::Key
{
public:
// Constructor.
Key(qtractorMidiClip *pMidiClip)
{ update(pMidiClip); }
// Key settler.
void update(qtractorMidiClip *pMidiClip)
{
qtractorTrack *pTrack = pMidiClip->track();
m_sFilename = pMidiClip->filename();
m_iClipOffset = pMidiClip->clipOffsetTime();
m_iClipLength = pMidiClip->clipLengthTime();
m_iTrackChannel = pMidiClip->trackChannel();
m_iMidiChannel = (pTrack ? pTrack->midiChannel() : 0);
}
// Key accessors.
const QString& filename() const
{ return m_sFilename; }
unsigned long clipOffset() const
{ return m_iClipOffset; }
unsigned long clipLength() const
{ return m_iClipLength; }
unsigned short trackChannel() const
{ return m_iTrackChannel; }
unsigned short midiChannel() const
{ return m_iMidiChannel; }
// Match descriminator.
bool operator== (const Key& other) const
{
return m_sFilename == other.filename()
&& m_iClipOffset == other.clipOffset()
&& m_iClipLength == other.clipLength()
&& m_iTrackChannel == other.trackChannel()
&& m_iMidiChannel == other.midiChannel();
}
private:
// Interesting variables.
QString m_sFilename;
unsigned long m_iClipOffset;
unsigned long m_iClipLength;
unsigned short m_iTrackChannel;
unsigned short m_iMidiChannel;
};
uint qHash ( const qtractorMidiClip::Key& key )
{
return qHash(key.filename())
^ qHash(key.clipOffset())
^ qHash(key.clipLength())
^ qHash(key.trackChannel())
^ qHash(key.midiChannel());
}
qtractorMidiClip::Hash qtractorMidiClip::g_hashTable;
//----------------------------------------------------------------------
// class qtractorMidiClip::FileKey -- MIDI file hash key.
//
class qtractorMidiClip::FileKey
{
public:
// Constructor.
FileKey(qtractorMidiClip::Key *pKey) :
m_sFilename(pKey->filename()),
m_iTrackChannel(pKey->trackChannel()) {}
// Key accessors.
const QString& filename() const
{ return m_sFilename; }
unsigned short trackChannel() const
{ return m_iTrackChannel; }
// Match descriminator.
bool operator== (const FileKey& other) const
{
return m_sFilename == other.filename()
&& m_iTrackChannel == other.trackChannel();
}
private:
// Interesting variables.
QString m_sFilename;
unsigned short m_iTrackChannel;
};
uint qHash ( const qtractorMidiClip::FileKey& key )
{
return qHash(key.filename()) ^ qHash(key.trackChannel());
}
qtractorMidiClip::FileHash qtractorMidiClip::g_hashFiles;
//----------------------------------------------------------------------
// class qtractorMidiClip -- MIDI sequence clip.
//
// Constructor.
qtractorMidiClip::qtractorMidiClip ( qtractorTrack *pTrack )
: qtractorClip(pTrack)
{
m_pFile = NULL;
m_pKey = NULL;
m_pData = NULL;
m_iTrackChannel = 0;
m_iFormat = defaultFormat();
m_bSessionFlag = false;
m_iRevision = 0;
m_noteMin = 0;
m_noteMax = 0;
m_pMidiEditorForm = NULL;
}
// Copy constructor.
qtractorMidiClip::qtractorMidiClip ( const qtractorMidiClip& clip )
: qtractorClip(clip.track())
{
m_pFile = NULL;
m_pKey = NULL;
m_pData = NULL;
setFilename(clip.filename());
setTrackChannel(clip.trackChannel());
setClipGain(clip.clipGain());
setClipPanning(clip.clipPanning());
setClipName(clip.clipName());
m_iFormat = clip.format();
m_bSessionFlag = false;
m_iRevision = clip.revision();
m_noteMin = clip.noteMin();
m_noteMax = clip.noteMax();
m_pMidiEditorForm = NULL;
}
// Destructor.
qtractorMidiClip::~qtractorMidiClip (void)
{
close();
closeMidiFile();
}
// Brand new clip contents new method.
bool qtractorMidiClip::createMidiFile (
const QString& sFilename, int iTrackChannel )
{
closeMidiFile();
qtractorTrack *pTrack = track();
if (pTrack == NULL)
return false;
qtractorSession *pSession = pTrack->session();
if (pSession == NULL)
return false;
#ifdef CONFIG_DEBUG_0
qDebug("qtractorMidiClip[%p]::createMidiFile(\"%s\", %d)", this,
sFilename.toUtf8().constData(), iTrackChannel);
#endif
// Self holds the SMF format,
const unsigned short iFormat = format();
// Which SMF format?
unsigned short iTracks = 1;
if (iFormat == 0) {
// SMF format 0 (1 track, 1 channel)
iTrackChannel = pTrack->midiChannel();
} else {
// SMF format 1 (2 tracks, 1 channel)
iTrackChannel = 1;
++iTracks;
}
// Set local properties...
setFilename(sFilename);
setTrackChannel(iTrackChannel);
setDirty(false);
// Register file path...
pSession->files()->addClipItem(qtractorFileList::Midi, this, true);
// Create and open up the MIDI file...
m_pFile = new qtractorMidiFile();
if (!m_pFile->open(sFilename, qtractorMidiFile::Write)) {
delete m_pFile;
m_pFile = NULL;
return false;
}
// Initialize MIDI event container...
m_pKey = new Key(this);
m_pData = new Data();
m_pData->attach(this);
// Right on then...
insertHashKey();
qtractorMidiSequence *pSeq = m_pData->sequence();
pSeq->clear();
pSeq->setTicksPerBeat(pSession->ticksPerBeat());
pSeq->setName(shortClipName(QFileInfo(sFilename).baseName()));
pSeq->setChannel(pTrack->midiChannel());
// Make it a brand new revision...
setRevision(1);
// Write SMF header...
if (m_pFile->writeHeader(iFormat, iTracks, pSeq->ticksPerBeat())) {
// Set initial local properties...
if (m_pFile->tempoMap()) {
m_pFile->tempoMap()->fromTimeScale(
pSession->timeScale(),
pSession->tickFromFrame(clipStart()));
}
// Sure this is a brand new file...
if (iFormat == 1)
m_pFile->writeTrack(NULL);
m_pFile->writeTrack(pSeq);
m_pFile->close();
}
// It's there now.
delete m_pFile;
m_pFile = NULL;
// Clip name should be clear about it all.
if (clipName().isEmpty())
setClipName(pSeq->name());
if (clipName().isEmpty())
setClipName(shortClipName(QFileInfo(filename()).baseName()));
// Uh oh...
m_playCursor.reset(pSeq);
m_drawCursor.reset(pSeq);
return true;
}
// The main use method.
bool qtractorMidiClip::openMidiFile (
const QString& sFilename, int iTrackChannel, int iMode )
{
closeMidiFile();
qtractorTrack *pTrack = track();
if (pTrack == NULL)
return false;
qtractorSession *pSession = pTrack->session();
if (pSession == NULL)
return false;
#ifdef CONFIG_DEBUG_0
qDebug("qtractorMidiClip[%p]::openMidiFile(\"%s\", %d, %d)", this,
sFilename.toUtf8().constData(), iTrackChannel, iMode);
#endif
// Check file primordial state...
const bool bWrite = (iMode & qtractorMidiFile::Write);
// Set local properties...
setFilename(sFilename);
setDirty(false);
// Register file path...
pSession->files()->addClipItem(qtractorFileList::Midi, this, bWrite);
// New key-data sequence...
if (!bWrite) {
m_pKey = new Key(this);
m_pData = g_hashTable.value(*m_pKey, NULL);
if (m_pData) {
m_pData->attach(this);
qtractorMidiSequence *pSeq = m_pData->sequence();
// Clip name should be clear about it all.
if (clipName().isEmpty())
setClipName(pSeq->name());
if (clipName().isEmpty())
setClipName(shortClipName(QFileInfo(filename()).baseName()));
// Uh oh...
m_playCursor.reset(pSeq);
m_drawCursor.reset(pSeq);
return true;
}
}
// Create and open up the real MIDI file...
m_pFile = new qtractorMidiFile();
if (!m_pFile->open(sFilename, iMode)) {
delete m_pFile;
m_pFile = NULL;
return false;
}
// Initialize MIDI event container...
m_pData = new Data();
m_pData->attach(this);
qtractorMidiSequence *pSeq = m_pData->sequence();
pSeq->clear();
pSeq->setTicksPerBeat(pSession->ticksPerBeat());
const unsigned long iClipStart = clipStart();
const unsigned long iClipOffset = clipOffset();
qtractorTimeScale::Cursor cursor(pSession->timeScale());
qtractorTimeScale::Node *pNode = cursor.seekFrame(iClipStart);
const unsigned long t0 = pNode->tickFromFrame(iClipStart);
const unsigned long iClipOffset2 = iClipStart + iClipOffset;
// pNode = cursor.seekFrame(iClipOffset2);
pSeq->setTimeOffset(pNode->tickFromFrame(iClipOffset2) - t0);
const unsigned long iClipLength = clipLength();
const unsigned long iClipEnd = iClipStart + iClipLength;
pNode = cursor.seekFrame(iClipEnd);
pSeq->setTimeLength(pNode->tickFromFrame(iClipEnd) - t0);
// Initial statistics...
pSeq->setNoteMin(m_noteMin);
pSeq->setNoteMax(m_noteMax);
// Are we on a pre-writing status?
if (bWrite) {
// On write mode, iTrackChannel holds the SMF format,
// so we'll convert it here as properly.
const unsigned short iFormat
= qtractorMidiClip::defaultFormat();
unsigned short iTracks = 1;
if (iFormat == 1) {
// SMF format 1 (2 tracks, 1 channel)
iTrackChannel = 1;
++iTracks;
}
// That's it.
setFormat(iFormat);
// Write SMF header...
if (m_pFile->writeHeader(iFormat, iTracks, pSeq->ticksPerBeat())) {
// Set initial local properties...
if (m_pFile->tempoMap()) {
m_pFile->tempoMap()->fromTimeScale(
pSession->timeScale(), pSeq->timeOffset());
}
}
// And initial clip name...
pSeq->setName(shortClipName(QFileInfo(m_pFile->filename()).baseName()));
pSeq->setChannel(pTrack->midiChannel());
// Nothing more as for writing...
} else {
// On read mode, SMF format is properly given by open file.
setFormat(m_pFile->format());
// Read the event sequence in...
m_pFile->readTrack(pSeq, iTrackChannel);
// For immediate feedback, once...
m_noteMin = pSeq->noteMin();
m_noteMax = pSeq->noteMax();
// FIXME: On demand, set session time properties from MIDI file...
if (m_bSessionFlag) {
#if 0
// Import eventual SysEx setup...
// - take care that given track might not be currently open,
// so that we'll resolve MIDI output bus somehow...
qtractorMidiBus *pMidiBus = NULL;
qtractorMidiEngine *pMidiEngine = pSession->midiEngine();
if (pMidiEngine) {
pMidiBus = static_cast<qtractorMidiBus *> (
pMidiEngine->findOutputBus(pTrack->outputBusName()));
if (pMidiBus == NULL) {
for (qtractorBus *pBus = pMidiEngine->buses().first();
pBus; pBus = pBus->next()) {
if (pBus->busMode() & qtractorBus::Output) {
pMidiBus = static_cast<qtractorMidiBus *> (pBus);
break;
}
}
}
}
// Import eventual SysEx setup...
if (pMidiBus)
pMidiBus->importSysexList(pSeq);
#endif
// Import tempo map as well...
qtractorMidiFileTempo *pTempoMap = m_pFile->tempoMap();
if (pTempoMap) {
pTempoMap->intoTimeScale(pSession->timeScale(), t0);
pSession->updateTimeScale();
}
// Reset session flag now.
m_bSessionFlag = false;
}
// We should have events, otherwise this clip is of no use...
//if (m_pSeq->events().count() < 1)
// return false;
// And initial clip name,
// if not already set from SMF TRACKNAME meta-event...
if (pSeq->name().isEmpty()) {
pSeq->setName(shortClipName(
QFileInfo(m_pFile->filename()).baseName()));
}
}
// Actual track-channel is set by now...
setTrackChannel(iTrackChannel);
// Make it a brand new revision...
// setRevision(1);
// Default clip length will be whole sequence duration.
if (iClipLength == 0) {
const unsigned long t1 = t0 + pSeq->timeLength();
setClipLength(pSession->frameFromTick(t1) - iClipStart);
}
// Clip name should be clear about it all.
if (clipName().isEmpty())
setClipName(pSeq->name());
if (clipName().isEmpty())
setClipName(shortClipName(QFileInfo(filename()).baseName()));
// Uh oh...
m_playCursor.reset(pSeq);
m_drawCursor.reset(pSeq);
// Something might have changed...
updateHashKey();
insertHashKey();
// Update/reset MIDI clip editor if any...
if (m_pMidiEditorForm)
m_pMidiEditorForm->setup(this);
return true;
}
// Private cleanup.
void qtractorMidiClip::closeMidiFile (void)
{
if (m_pData) {
m_pData->detach(this);
if (m_pData->count() < 1) {
removeHashKey();
delete m_pData;
}
m_pData = NULL;
// Unregister file path...
qtractorSession *pSession = qtractorSession::getInstance();
if (pSession)
pSession->files()->removeClipItem(qtractorFileList::Midi, this);
}
if (m_pKey) {
delete m_pKey;
m_pKey = NULL;
}
if (m_pFile) {
delete m_pFile;
m_pFile = NULL;
}
}
// Revisionist method.
QString qtractorMidiClip::createFilePathRevision ( bool bForce )
{
QString sFilename = filename();
// Check file-hash reference...
if (m_iRevision > 0 && m_pKey) {
FileKey fkey(m_pKey);
FileHash::ConstIterator fiter = g_hashFiles.constFind(fkey);
if (fiter != g_hashFiles.constEnd() && fiter.value() > 1)
m_iRevision = 0;
}
if (m_iRevision == 0 || bForce) {
qtractorTrack *pTrack = track();
qtractorSession *pSession = qtractorSession::getInstance();
if (pTrack && pSession)
sFilename = pSession->createFilePath(pTrack->trackName(), "mid");
sFilename = qtractorMidiFile::createFilePathRevision(sFilename);
#ifdef CONFIG_DEBUG
qDebug("qtractorMidiClip::createFilePathRevision(%d): \"%s\" (%d)",
int(bForce), sFilename.toUtf8().constData(), m_iRevision);
#endif
m_iRevision = 0;
}
++m_iRevision;
return sFilename;
}
// Sync all ref-counted filenames.
void qtractorMidiClip::setFilenameEx (
const QString& sFilename, bool bUpdate )
{
qtractorTrack *pTrack = track();
if (pTrack == NULL)
return;
qtractorSession *pSession = pTrack->session();
if (pSession == NULL)
return;
if (m_pData == NULL)
return;
removeHashKey();
QListIterator<qtractorMidiClip *> iter(m_pData->clips());
while (iter.hasNext()) {
qtractorMidiClip *pMidiClip = iter.next();
pSession->files()->removeClipItem(qtractorFileList::Midi, pMidiClip);
pMidiClip->setFilename(sFilename);
pMidiClip->updateHashKey();
pSession->files()->addClipItem(qtractorFileList::Midi, pMidiClip, true);
if (bUpdate) {
pMidiClip->setDirty(false);
pMidiClip->updateEditor(true);
}
}
insertHashKey();
}
// Sync all ref-counted clip-lengths.
void qtractorMidiClip::setClipLengthEx ( unsigned long iClipLength )
{
if (m_pData == NULL)
return;
removeHashKey();
QListIterator<qtractorMidiClip *> iter(m_pData->clips());
while (iter.hasNext()) {
qtractorMidiClip *pMidiClip = iter.next();
pMidiClip->setClipLength(iClipLength);
pMidiClip->updateHashKey();
}
insertHashKey();
}
// Sync all ref-counted clip editors.
void qtractorMidiClip::updateEditorEx ( bool bSelectClear )
{
if (m_pData == NULL)
return;
QListIterator<qtractorMidiClip *> iter(m_pData->clips());
while (iter.hasNext())
iter.next()->updateEditor(bSelectClear);
}
void qtractorMidiClip::resetEditorEx ( bool bSelectClear )
{
if (m_pData == NULL)
return;
QListIterator<qtractorMidiClip *> iter(m_pData->clips());
while (iter.hasNext())
iter.next()->resetEditor(bSelectClear);
}
// Sync all ref-counted clip-dirtyness.
void qtractorMidiClip::setDirtyEx ( bool bDirty )
{
if (m_pData == NULL)
return;
QListIterator<qtractorMidiClip *> iter(m_pData->clips());
while (iter.hasNext())
iter.next()->setDirty(bDirty);
}
// Manage local hash key.
void qtractorMidiClip::insertHashKey (void)
{
if (m_pKey) {
// Increment file-hash reference...
FileKey fkey(m_pKey);
FileHash::Iterator fiter = g_hashFiles.find(fkey);
if (fiter == g_hashFiles.end())
fiter = g_hashFiles.insert(fkey, 0);
++fiter.value();
// Insert actual clip-hash reference....
g_hashTable.insert(*m_pKey, m_pData);
}
}
void qtractorMidiClip::updateHashKey (void)
{
if (m_pKey == NULL)
m_pKey = new Key(this);
else
m_pKey->update(this);
}
void qtractorMidiClip::removeHashKey (void)
{
if (m_pKey) {
// Decrement file-hash reference...
FileKey fkey(m_pKey);
FileHash::Iterator fiter = g_hashFiles.find(fkey);
if (fiter != g_hashFiles.end()) {
if (--fiter.value() < 1)
g_hashFiles.remove(fkey);
}
// Remove actual clip-hash reference....
g_hashTable.remove(*m_pKey);
}
}
// Unlink (clone) local hash data.
void qtractorMidiClip::unlinkHashData (void)
{
if (m_pData == NULL)
return;
if (m_pData->count() < 2)
return;
m_pData->detach(this);
Data *pNewData = new Data();
qtractorMidiSequence *pOldSeq = m_pData->sequence();
qtractorMidiSequence *pNewSeq = pNewData->sequence();
pNewSeq->setName(pOldSeq->name());
pNewSeq->setChannel(pOldSeq->channel());
pNewSeq->setBank(pOldSeq->bank());
pNewSeq->setProg(pOldSeq->prog());
pNewSeq->setTicksPerBeat(pOldSeq->ticksPerBeat());
pNewSeq->setTimeOffset(pOldSeq->timeOffset());
pNewSeq->setTimeLength(pOldSeq->timeLength());
pNewSeq->setDuration(pOldSeq->duration());
pNewSeq->setNoteMin(pOldSeq->noteMin());
pNewSeq->setNoteMax(pOldSeq->noteMax());
pNewSeq->copyEvents(pOldSeq);
m_pData = pNewData;
m_pData->attach(this);
updateHashKey();
insertHashKey();
}
// Relink local hash data.
void qtractorMidiClip::relinkHashData (void)
{
if (m_pData == NULL)
return;
if (m_pData->count() > 1)
return;
removeHashKey();
updateHashKey();
Data *pNewData = g_hashTable.value(*m_pKey, NULL);
if (pNewData == NULL) {
delete m_pKey;
m_pKey = NULL;
} else {
m_pData->detach(this);
delete m_pData;
m_pData = pNewData;
m_pData->attach(this);
}
insertHashKey();
}
// Whether local hash is being shared.
bool qtractorMidiClip::isHashLinked (void) const
{
return (m_pData && m_pData->count() > 1);
}
// Check whether a MIDI clip is hash-linked to another.
bool qtractorMidiClip::isLinkedClip ( qtractorMidiClip *pMidiClip ) const
{
return (m_pData ? m_pData->clips().contains(pMidiClip) : false);
}
// Get all hash-linked clips (including self).
QList<qtractorMidiClip *> qtractorMidiClip::linkedClips (void) const
{
QList<qtractorMidiClip *> clips;
if (m_pData)
clips = m_pData->clips();
return clips;
}
// Make sure the clip hash-table gets reset.
void qtractorMidiClip::clearHashTable (void)
{
g_hashTable.clear();
g_hashFiles.clear();
}
// Intra-clip playback frame positioning.
void qtractorMidiClip::seek ( unsigned long iFrame )
{
#ifdef CONFIG_DEBUG_0
qDebug("qtractorMidiClip[%p]::seek(%lu)", this, iFrame);
#endif
qtractorTrack *pTrack = track();
if (pTrack == NULL)
return;
qtractorSession *pSession = pTrack->session();
if (pSession == NULL)
return;
qtractorMidiSequence *pSeq = sequence();
if (pSeq == NULL)
return;
const unsigned long t0 = pSession->tickFromFrame(clipStart());
const unsigned long t1 = pSession->tickFromFrame(iFrame);
// Seek for the nearest sequence event...
m_playCursor.seek(pSeq, (t1 > t0 ? t1 - t0 : 0));
}
// Reset clip state.
void qtractorMidiClip::reset ( bool /* bLooping */ )
{
qtractorMidiSequence *pSeq = sequence();
if (pSeq == NULL)
return;
// Reset to the first sequence event...
m_playCursor.reset(pSeq);
}
// Loop positioning.
void qtractorMidiClip::setLoop (
unsigned long /* iLoopStart */, unsigned long /* iLoopEnd */ )
{
// Do nothing?
}
// Clip close-commit (record specific)
void qtractorMidiClip::close (void)
{
#ifdef CONFIG_DEBUG_0
qDebug("qtractorMidiClip[%p]::close(%d)\n", this);
#endif
qtractorTrack *pTrack = track();
if (pTrack == NULL)
return;
qtractorSession *pSession = pTrack->session();
if (pSession == NULL)
return;
// Take pretended clip-length...
unsigned long iClipLength = clipLength();
qtractorMidiSequence *pSeq = sequence();
if (pSeq) {
if (iClipLength > 0)
pSeq->setTimeLength(pSession->tickFromFrame(iClipLength));
// Final read statistics...
m_noteMin = pSeq->noteMin();
m_noteMax = pSeq->noteMax();
// Actual sequence closure...
pSeq->close();
// Commit the final clip length...
if (iClipLength < 1) {
iClipLength = pSession->frameFromTick(pSeq->duration());
setClipLength(iClipLength);
}
}
// Now's time to write the whole thing...
const bool bNewFile
= (m_pFile && m_pFile->mode() == qtractorMidiFile::Write);
if (bNewFile && iClipLength > 0 && pSeq) {
// Write channel tracks...
if (m_iFormat == 1)
m_pFile->writeTrack(NULL); // Setup track (SMF format 1).
m_pFile->writeTrack(pSeq); // Channel track.
m_pFile->close();
}
// Sure close MIDI clip editor if any...
if (m_pMidiEditorForm) {
m_pMidiEditorForm->close();
delete m_pMidiEditorForm;
m_pMidiEditorForm = NULL;
}
// Just to be sure things get deallocated..
closeMidiFile();
// If proven empty, remove the file.
if (bNewFile && iClipLength < 1)
QFile::remove(filename());
}
// MIDI clip (re)open method.
void qtractorMidiClip::open (void)
{
const QString sFilename(filename());
openMidiFile(sFilename, m_iTrackChannel);
}
// Audio clip special process cycle executive.
void qtractorMidiClip::process (
unsigned long iFrameStart, unsigned long iFrameEnd )
{
#ifdef CONFIG_DEBUG_0
qDebug("qtractorMidiClip[%p]::process(%lu, %lu)\n",
this, iFrameStart, iFrameEnd);
#endif
qtractorTrack *pTrack = track();
if (pTrack == NULL)
return;
qtractorSession *pSession = pTrack->session();
if (pSession == NULL)
return;
qtractorMidiEngine *pMidiEngine = pSession->midiEngine();
if (pMidiEngine == NULL)
return;
qtractorMidiSequence *pSeq = sequence();
if (pSeq == NULL)
return;
// Track mute state...
const bool bMute = (pTrack->isMute()
|| (pSession->soloTracks() && !pTrack->isSolo()));
const unsigned long t0 = pSession->tickFromFrame(clipStart());
const unsigned long iTimeStart = pSession->tickFromFrame(iFrameStart);
const unsigned long iTimeEnd = pSession->tickFromFrame(iFrameEnd);
// Enqueue the requested events...
const float fGain = clipGain();
qtractorMidiEvent *pEvent
= m_playCursor.seek(pSeq, iTimeStart > t0 ? iTimeStart - t0 : 0);
while (pEvent) {
const unsigned long t1 = t0 + pEvent->time();
if (t1 >= iTimeEnd)
break;
if (t1 >= iTimeStart
&& (!bMute || pEvent->type() != qtractorMidiEvent::NOTEON))
pMidiEngine->enqueue(pTrack, pEvent, t1, fGain
* fadeInOutGain(pSession->frameFromTick(t1) - clipStart()));
pEvent = pEvent->next();
}
}
// MIDI clip freewheeling process cycle executive (needed for export).
void qtractorMidiClip::process_export (
unsigned long iFrameStart, unsigned long iFrameEnd )
{
qtractorTrack *pTrack = track();
if (pTrack == NULL)
return;
qtractorSession *pSession = pTrack->session();
if (pSession == NULL)
return;
qtractorMidiSequence *pSeq = sequence();
if (pSeq == NULL)
return;
// Track mute state...
const bool bMute = (pTrack->isMute()
|| (pSession->soloTracks() && !pTrack->isSolo()));
const unsigned long t0 = pSession->tickFromFrame(clipStart());
const unsigned long iTimeStart = pSession->tickFromFrame(iFrameStart);
const unsigned long iTimeEnd = pSession->tickFromFrame(iFrameEnd);
// Enqueue the requested events...
const float fGain = clipGain();
qtractorMidiEvent *pEvent
= m_playCursor.seek(pSeq, iTimeStart > t0 ? iTimeStart - t0 : 0);
while (pEvent) {
const unsigned long t1 = t0 + pEvent->time();