-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathGigPlayer.cpp
1398 lines (1068 loc) · 33.5 KB
/
GigPlayer.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
/*
* GigPlayer.cpp - a GIG player using libgig (based on Sf2 player plugin)
*
* Copyright (c) 2008 Paul Giblock <drfaygo/at/gmail/dot/com>
* Copyright (c) 2009-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* A few lines of code taken from LinuxSampler (also GPLv2) where noted:
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck
* Copyright (C) 2005-2008 Christian Schoenebeck
* Copyright (C) 2009-2010 Christian Schoenebeck and Grigor Iliev
*
* This file is part of LMMS - https://lmms.io
*
* 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 (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include <cstring>
#include <QDebug>
#include <QLayout>
#include <QLabel>
#include <QDomDocument>
#include "FileDialog.h"
#include "GigPlayer.h"
#include "Engine.h"
#include "InstrumentTrack.h"
#include "InstrumentPlayHandle.h"
#include "Mixer.h"
#include "NotePlayHandle.h"
#include "Knob.h"
#include "SampleBuffer.h"
#include "Song.h"
#include "ConfigManager.h"
#include "endian_handling.h"
#include "PatchesDialog.h"
#include "ToolTip.h"
#include "LcdSpinBox.h"
#include "embed.h"
#include "plugin_export.h"
extern "C"
{
Plugin::Descriptor PLUGIN_EXPORT gigplayer_plugin_descriptor =
{
STRINGIFY( PLUGIN_NAME ),
"GIG Player",
QT_TRANSLATE_NOOP( "pluginBrowser", "Player for GIG files" ),
"Garrett Wilson <g/at/floft/dot/net>",
0x0100,
Plugin::Instrument,
new PluginPixmapLoader( "logo" ),
"gig",
NULL
} ;
}
GigInstrument::GigInstrument( InstrumentTrack * _instrument_track ) :
Instrument( _instrument_track, &gigplayer_plugin_descriptor ),
m_instance( NULL ),
m_instrument( NULL ),
m_filename( "" ),
m_bankNum( 0, 0, 999, this, tr( "Bank" ) ),
m_patchNum( 0, 0, 127, this, tr( "Patch" ) ),
m_gain( 1.0f, 0.0f, 5.0f, 0.01f, this, tr( "Gain" ) ),
m_interpolation( SRC_LINEAR ),
m_RandomSeed( 0 ),
m_currentKeyDimension( 0 )
{
InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrument_track );
Engine::mixer()->addPlayHandle( iph );
updateSampleRate();
connect( &m_bankNum, SIGNAL( dataChanged() ), this, SLOT( updatePatch() ) );
connect( &m_patchNum, SIGNAL( dataChanged() ), this, SLOT( updatePatch() ) );
connect( Engine::mixer(), SIGNAL( sampleRateChanged() ), this, SLOT( updateSampleRate() ) );
}
GigInstrument::~GigInstrument()
{
Engine::mixer()->removePlayHandlesOfTypes( instrumentTrack(),
PlayHandle::TypeNotePlayHandle
| PlayHandle::TypeInstrumentPlayHandle );
freeInstance();
}
void GigInstrument::saveSettings( QDomDocument & _doc, QDomElement & _this )
{
_this.setAttribute( "src", m_filename );
m_patchNum.saveSettings( _doc, _this, "patch" );
m_bankNum.saveSettings( _doc, _this, "bank" );
m_gain.saveSettings( _doc, _this, "gain" );
}
void GigInstrument::loadSettings( const QDomElement & _this )
{
openFile( _this.attribute( "src" ), false );
m_patchNum.loadSettings( _this, "patch" );
m_bankNum.loadSettings( _this, "bank" );
m_gain.loadSettings( _this, "gain" );
updatePatch();
}
void GigInstrument::loadFile( const QString & _file )
{
if( !_file.isEmpty() && QFileInfo( _file ).exists() )
{
openFile( _file, false );
updatePatch();
updateSampleRate();
}
}
AutomatableModel * GigInstrument::childModel( const QString & _modelName )
{
if( _modelName == "bank" )
{
return &m_bankNum;
}
else if( _modelName == "patch" )
{
return &m_patchNum;
}
qCritical() << "requested unknown model " << _modelName;
return NULL;
}
QString GigInstrument::nodeName() const
{
return gigplayer_plugin_descriptor.name;
}
void GigInstrument::freeInstance()
{
QMutexLocker synthLock( &m_synthMutex );
QMutexLocker notesLock( &m_notesMutex );
if( m_instance != NULL )
{
delete m_instance;
m_instance = NULL;
// If we're changing instruments, we got to make sure that we
// remove all pointers to the old samples and don't try accessing
// that instrument again
m_instrument = NULL;
m_notes.clear();
}
}
void GigInstrument::openFile( const QString & _gigFile, bool updateTrackName )
{
emit fileLoading();
// Remove the current instrument if one is selected
freeInstance();
{
QMutexLocker locker( &m_synthMutex );
try
{
m_instance = new GigInstance( SampleBuffer::tryToMakeAbsolute( _gigFile ) );
m_filename = SampleBuffer::tryToMakeRelative( _gigFile );
}
catch( ... )
{
m_instance = NULL;
m_filename = "";
}
}
emit fileChanged();
if( updateTrackName == true )
{
instrumentTrack()->setName( QFileInfo( _gigFile ).baseName() );
updatePatch();
}
}
void GigInstrument::updatePatch()
{
if( m_bankNum.value() >= 0 && m_patchNum.value() >= 0 )
{
getInstrument();
}
}
QString GigInstrument::getCurrentPatchName()
{
QMutexLocker locker( &m_synthMutex );
if( m_instance == NULL )
{
return "";
}
int iBankSelected = m_bankNum.value();
int iProgSelected = m_patchNum.value();
gig::Instrument * pInstrument = m_instance->gig.GetFirstInstrument();
while( pInstrument != NULL )
{
int iBank = pInstrument->MIDIBank;
int iProg = pInstrument->MIDIProgram;
if( iBank == iBankSelected && iProg == iProgSelected )
{
QString name = QString::fromStdString( pInstrument->pInfo->Name );
if( name == "" )
{
name = "<no name>";
}
return name;
}
pInstrument = m_instance->gig.GetNextInstrument();
}
return "";
}
// A key has been pressed
void GigInstrument::playNote( NotePlayHandle * _n, sampleFrame * )
{
const float LOG440 = 2.643452676f;
const f_cnt_t tfp = _n->totalFramesPlayed();
int midiNote = (int) floor( 12.0 * ( log2( _n->unpitchedFrequency() ) - LOG440 ) - 4.0 );
// out of range?
if( midiNote <= 0 || midiNote >= 128 )
{
return;
}
if( tfp == 0 )
{
GIGPluginData * pluginData = new GIGPluginData;
pluginData->midiNote = midiNote;
_n->m_pluginData = pluginData;
const int baseVelocity = instrumentTrack()->midiPort()->baseVelocity();
const uint velocity = _n->midiVelocity( baseVelocity );
QMutexLocker locker( &m_notesMutex );
m_notes.push_back( GigNote( midiNote, velocity, _n->unpitchedFrequency(), pluginData ) );
}
}
// Process the notes and output a certain number of frames (e.g. 256, set in
// the preferences)
void GigInstrument::play( sampleFrame * _working_buffer )
{
const fpp_t frames = Engine::mixer()->framesPerPeriod();
const int rate = Engine::mixer()->processingSampleRate();
// Initialize to zeros
std::memset( &_working_buffer[0][0], 0, DEFAULT_CHANNELS * frames * sizeof( float ) );
m_synthMutex.lock();
m_notesMutex.lock();
if( m_instance == NULL || m_instrument == NULL )
{
m_synthMutex.unlock();
m_notesMutex.unlock();
return;
}
for( QList<GigNote>::iterator it = m_notes.begin(); it != m_notes.end(); ++it )
{
// Process notes in the KeyUp state, adding release samples if desired
if( it->state == KeyUp )
{
// If there are no samples, we're done
if( it->samples.empty() )
{
it->state = Completed;
}
else
{
it->state = PlayingKeyUp;
// Notify each sample that the key has been released
for( QList<GigSample>::iterator sample = it->samples.begin();
sample != it->samples.end(); ++sample )
{
sample->adsr.keyup();
}
// Add release samples if available
if( it->release == true )
{
addSamples( *it, true );
}
}
}
// Process notes in the KeyDown state, adding samples for the notes
else if( it->state == KeyDown )
{
it->state = PlayingKeyDown;
addSamples( *it, false );
}
// Delete ended samples
for( QList<GigSample>::iterator sample = it->samples.begin();
sample != it->samples.end(); ++sample )
{
// Delete if the ADSR for a sample is complete for normal
// notes, or if a release sample, then if we've reached
// the end of the sample
if( sample->sample == NULL || sample->adsr.done() ||
( it->isRelease == true &&
sample->pos >= sample->sample->SamplesTotal - 1 ) )
{
sample = it->samples.erase( sample );
if( sample == it->samples.end() )
{
break;
}
}
}
// Delete ended notes (either in the completed state or all the samples ended)
if( it->state == Completed || it->samples.empty() )
{
it = m_notes.erase( it );
if( it == m_notes.end() )
{
break;
}
}
}
// Fill buffer with portions of the note samples
for( QList<GigNote>::iterator it = m_notes.begin(); it != m_notes.end(); ++it )
{
// Only process the notes if we're in a playing state
if( !( it->state == PlayingKeyDown ||
it->state == PlayingKeyUp ) )
{
continue;
}
for( QList<GigSample>::iterator sample = it->samples.begin();
sample != it->samples.end(); ++sample )
{
if( sample->sample == NULL || sample->region == NULL )
{
continue;
}
// Will change if resampling
bool resample = false;
f_cnt_t samples = frames; // How many to grab
f_cnt_t used = frames; // How many we used
float freq_factor = 1.0; // How to resample
// Resample to be the correct pitch when the sample provided isn't
// solely for this one note (e.g. one or two samples per octave) or
// we are processing at a different sample rate
if( sample->region->PitchTrack == true || rate != sample->sample->SamplesPerSecond )
{
resample = true;
// Factor just for resampling
freq_factor = 1.0 * rate / sample->sample->SamplesPerSecond;
// Factor for pitch shifting as well as resampling
if( sample->region->PitchTrack == true )
{
freq_factor *= sample->freqFactor;
}
// We need a bit of margin so we don't get glitching
samples = frames / freq_factor + MARGIN[m_interpolation];
}
// Load this note's data
sampleFrame sampleData[samples];
loadSample( *sample, sampleData, samples );
// Apply ADSR using a copy so if we don't use these samples when
// resampling, the ADSR doesn't get messed up
ADSR copy = sample->adsr;
for( f_cnt_t i = 0; i < samples; ++i )
{
float amplitude = copy.value();
sampleData[i][0] *= amplitude;
sampleData[i][1] *= amplitude;
}
// Output the data resampling if needed
if( resample == true )
{
sampleFrame convertBuf[frames];
// Only output if resampling is successful (note that "used" is output)
if( sample->convertSampleRate( *sampleData, *convertBuf, samples, frames,
freq_factor, used ) )
{
for( f_cnt_t i = 0; i < frames; ++i )
{
_working_buffer[i][0] += convertBuf[i][0];
_working_buffer[i][1] += convertBuf[i][1];
}
}
}
else
{
for( f_cnt_t i = 0; i < frames; ++i )
{
_working_buffer[i][0] += sampleData[i][0];
_working_buffer[i][1] += sampleData[i][1];
}
}
// Update note position with how many samples we actually used
sample->pos += used;
sample->adsr.inc( used );
}
}
m_notesMutex.unlock();
m_synthMutex.unlock();
// Set gain properly based on volume control
for( f_cnt_t i = 0; i < frames; ++i )
{
_working_buffer[i][0] *= m_gain.value();
_working_buffer[i][1] *= m_gain.value();
}
instrumentTrack()->processAudioBuffer( _working_buffer, frames, NULL );
}
void GigInstrument::loadSample( GigSample& sample, sampleFrame* sampleData, f_cnt_t samples )
{
if( sampleData == NULL || samples < 1 )
{
return;
}
// Determine if we need to loop part of this sample
bool loop = false;
gig::loop_type_t loopType = gig::loop_type_normal;
f_cnt_t loopStart = 0;
f_cnt_t loopLength = 0;
if( sample.region->pSampleLoops != NULL )
{
for( uint32_t i = 0; i < sample.region->SampleLoops; ++i )
{
loop = true;
loopType = static_cast<gig::loop_type_t>( sample.region->pSampleLoops[i].LoopType );
loopStart = sample.region->pSampleLoops[i].LoopStart;
loopLength = sample.region->pSampleLoops[i].LoopLength;
// Currently only support at max one loop
break;
}
}
unsigned long allocationsize = samples * sample.sample->FrameSize;
int8_t buffer[allocationsize];
// Load the sample in different ways depending on if we're looping or not
if( loop == true && ( sample.pos >= loopStart || sample.pos + samples > loopStart ) )
{
// Calculate the new position based on the type of loop
if( loopType == gig::loop_type_bidirectional )
{
sample.pos = getPingPongIndex( sample.pos, loopStart, loopStart + loopLength );
}
else
{
sample.pos = getLoopedIndex( sample.pos, loopStart, loopStart + loopLength );
// TODO: also implement loop_type_backward support
}
sample.sample->SetPos( sample.pos );
// Load the samples (based on gig::Sample::ReadAndLoop) even around the end
// of a loop boundary wrapping to the beginning of the loop region
long samplestoread = samples;
long samplestoloopend = 0;
long readsamples = 0;
long totalreadsamples = 0;
long loopEnd = loopStart + loopLength;
do
{
samplestoloopend = loopEnd - sample.sample->GetPos();
readsamples = sample.sample->Read( &buffer[totalreadsamples * sample.sample->FrameSize],
min( samplestoread, samplestoloopend ) );
samplestoread -= readsamples;
totalreadsamples += readsamples;
if( readsamples >= samplestoloopend )
{
sample.sample->SetPos( loopStart );
}
}
while( samplestoread > 0 && readsamples > 0 );
}
else
{
sample.sample->SetPos( sample.pos );
unsigned long size = sample.sample->Read( &buffer, samples ) * sample.sample->FrameSize;
std::memset( (int8_t*) &buffer + size, 0, allocationsize - size );
}
// Convert from 16 or 24 bit into 32-bit float
if( sample.sample->BitDepth == 24 ) // 24 bit
{
uint8_t * pInt = reinterpret_cast<uint8_t*>( &buffer );
for( f_cnt_t i = 0; i < samples; ++i )
{
// libgig gives 24-bit data as little endian, so we must
// convert if on a big endian system
int32_t valueLeft = swap32IfBE(
( pInt[ 3 * sample.sample->Channels * i ] << 8 ) |
( pInt[ 3 * sample.sample->Channels * i + 1 ] << 16 ) |
( pInt[ 3 * sample.sample->Channels * i + 2 ] << 24 ) );
// Store the notes to this buffer before saving to output
// so we can fade them out as needed
sampleData[i][0] = 1.0 / 0x100000000 * sample.attenuation * valueLeft;
if( sample.sample->Channels == 1 )
{
sampleData[i][1] = sampleData[i][0];
}
else
{
int32_t valueRight = swap32IfBE(
( pInt[ 3 * sample.sample->Channels * i + 3 ] << 8 ) |
( pInt[ 3 * sample.sample->Channels * i + 4 ] << 16 ) |
( pInt[ 3 * sample.sample->Channels * i + 5 ] << 24 ) );
sampleData[i][1] = 1.0 / 0x100000000 * sample.attenuation * valueRight;
}
}
}
else // 16 bit
{
int16_t * pInt = reinterpret_cast<int16_t*>( &buffer );
for( f_cnt_t i = 0; i < samples; ++i )
{
sampleData[i][0] = 1.0 / 0x10000 *
pInt[ sample.sample->Channels * i ] * sample.attenuation;
if( sample.sample->Channels == 1 )
{
sampleData[i][1] = sampleData[i][0];
}
else
{
sampleData[i][1] = 1.0 / 0x10000 *
pInt[ sample.sample->Channels * i + 1 ] * sample.attenuation;
}
}
}
}
// These two loop index functions taken from SampleBuffer.cpp
f_cnt_t GigInstrument::getLoopedIndex( f_cnt_t index, f_cnt_t startf, f_cnt_t endf ) const
{
if( index < endf )
{
return index;
}
return startf + ( index - startf )
% ( endf - startf );
}
f_cnt_t GigInstrument::getPingPongIndex( f_cnt_t index, f_cnt_t startf, f_cnt_t endf ) const
{
if( index < endf )
{
return index;
}
const f_cnt_t looplen = endf - startf;
const f_cnt_t looppos = ( index - endf ) % ( looplen * 2 );
return ( looppos < looplen )
? endf - looppos
: startf + ( looppos - looplen );
}
// A key has been released
void GigInstrument::deleteNotePluginData( NotePlayHandle * _n )
{
GIGPluginData * pluginData = static_cast<GIGPluginData *>( _n->m_pluginData );
QMutexLocker locker( &m_notesMutex );
// Mark the note as being released, but only if it was playing or was just
// pressed (i.e., not if the key was already released)
for( QList<GigNote>::iterator i = m_notes.begin(); i != m_notes.end(); ++i )
{
// Find the note by matching pointers to the plugin data
if( i->handle == pluginData &&
( i->state == KeyDown || i->state == PlayingKeyDown ) )
{
i->state = KeyUp;
}
}
// TODO: not sample exact? What about in the middle of us writing out the sample?
delete pluginData;
}
PluginView * GigInstrument::instantiateView( QWidget * _parent )
{
return new GigInstrumentView( this, _parent );
}
// Add the desired samples (either the normal samples or the release samples)
// to the GigNote
//
// Note: not thread safe since libgig stores current region position data in
// the instrument object
void GigInstrument::addSamples( GigNote & gignote, bool wantReleaseSample )
{
// Change key dimension, e.g. change samples based on what key is pressed
// in a certain range. From LinuxSampler
if( wantReleaseSample == true &&
gignote.midiNote >= m_instrument->DimensionKeyRange.low &&
gignote.midiNote <= m_instrument->DimensionKeyRange.high )
{
m_currentKeyDimension = float( gignote.midiNote -
m_instrument->DimensionKeyRange.low ) / (
m_instrument->DimensionKeyRange.high -
m_instrument->DimensionKeyRange.low + 1 );
}
gig::Region* pRegion = m_instrument->GetFirstRegion();
while( pRegion != NULL )
{
Dimension dim = getDimensions( pRegion, gignote.velocity, wantReleaseSample );
gig::DimensionRegion * pDimRegion = pRegion->GetDimensionRegionByValue( dim.DimValues );
gig::Sample * pSample = pDimRegion->pSample;
// If this is a release sample, the note won't ever be
// released, so we handle it differently
gignote.isRelease = wantReleaseSample;
// Does this note have release samples? Set this only on the original
// notes and not when we get the release samples.
if( wantReleaseSample != true )
{
gignote.release = dim.release;
}
if( pSample != NULL && pSample->SamplesTotal != 0 )
{
int keyLow = pRegion->KeyRange.low;
int keyHigh = pRegion->KeyRange.high;
if( gignote.midiNote >= keyLow && gignote.midiNote <= keyHigh )
{
float attenuation = pDimRegion->GetVelocityAttenuation( gignote.velocity );
float length = (float) pSample->SamplesTotal / Engine::mixer()->processingSampleRate();
// TODO: sample panning? crossfade different layers?
if( wantReleaseSample == true )
{
// From LinuxSampler, not sure how it was created
attenuation *= 1 - 0.01053 * ( 256 >> pDimRegion->ReleaseTriggerDecay ) * length;
}
else
{
attenuation *= pDimRegion->SampleAttenuation;
}
gignote.samples.push_back( GigSample( pSample, pDimRegion,
attenuation, m_interpolation, gignote.frequency ) );
}
}
pRegion = m_instrument->GetNextRegion();
}
}
// Based on our input parameters, generate a "dimension" that specifies which
// note we wish to select from the GIG file with libgig. libgig will use this
// information to select the sample.
Dimension GigInstrument::getDimensions( gig::Region * pRegion, int velocity, bool release )
{
Dimension dim;
if( pRegion == NULL )
{
return dim;
}
for( int i = pRegion->Dimensions - 1; i >= 0; --i )
{
switch( pRegion->pDimensionDefinitions[i].dimension )
{
case gig::dimension_layer:
// TODO: implement this
dim.DimValues[i] = 0;
break;
case gig::dimension_velocity:
dim.DimValues[i] = velocity;
break;
case gig::dimension_releasetrigger:
dim.release = true;
dim.DimValues[i] = (uint) release;
break;
case gig::dimension_keyboard:
dim.DimValues[i] = (uint) ( m_currentKeyDimension * pRegion->pDimensionDefinitions[i].zones );
break;
case gig::dimension_roundrobin:
case gig::dimension_roundrobinkeyboard:
// TODO: implement this
dim.DimValues[i] = 0;
break;
case gig::dimension_random:
// From LinuxSampler, untested
m_RandomSeed = m_RandomSeed * 1103515245 + 12345;
dim.DimValues[i] = uint(
m_RandomSeed / 4294967296.0f * pRegion->pDimensionDefinitions[i].bits );
break;
case gig::dimension_samplechannel:
case gig::dimension_channelaftertouch:
case gig::dimension_modwheel:
case gig::dimension_breath:
case gig::dimension_foot:
case gig::dimension_portamentotime:
case gig::dimension_effect1:
case gig::dimension_effect2:
case gig::dimension_genpurpose1:
case gig::dimension_genpurpose2:
case gig::dimension_genpurpose3:
case gig::dimension_genpurpose4:
case gig::dimension_sustainpedal:
case gig::dimension_portamento:
case gig::dimension_sostenutopedal:
case gig::dimension_softpedal:
case gig::dimension_genpurpose5:
case gig::dimension_genpurpose6:
case gig::dimension_genpurpose7:
case gig::dimension_genpurpose8:
case gig::dimension_effect1depth:
case gig::dimension_effect2depth:
case gig::dimension_effect3depth:
case gig::dimension_effect4depth:
case gig::dimension_effect5depth:
case gig::dimension_none:
default:
dim.DimValues[i] = 0;
break;
}
}
return dim;
}
// Get the selected instrument from the GIG file we opened if we haven't gotten
// it already. This is based on the bank and patch numbers.
void GigInstrument::getInstrument()
{
// Find instrument
int iBankSelected = m_bankNum.value();
int iProgSelected = m_patchNum.value();
QMutexLocker locker( &m_synthMutex );
if( m_instance != NULL )
{
gig::Instrument * pInstrument = m_instance->gig.GetFirstInstrument();
while( pInstrument != NULL )
{
int iBank = pInstrument->MIDIBank;
int iProg = pInstrument->MIDIProgram;
if( iBank == iBankSelected && iProg == iProgSelected )
{
break;
}
pInstrument = m_instance->gig.GetNextInstrument();
}
m_instrument = pInstrument;
}
}
// Since the sample rate changes when we start an export, clear all the
// currently-playing notes when we get this signal. Then, the export won't
// include leftover notes that were playing in the program.
void GigInstrument::updateSampleRate()
{
QMutexLocker locker( &m_notesMutex );
m_notes.clear();
}
class gigKnob : public Knob
{
public:
gigKnob( QWidget * _parent ) :
Knob( knobBright_26, _parent )
{
setFixedSize( 31, 38 );
}
} ;
GigInstrumentView::GigInstrumentView( Instrument * _instrument, QWidget * _parent ) :
InstrumentViewFixedSize( _instrument, _parent )
{
GigInstrument * k = castModel<GigInstrument>();
connect( &k->m_bankNum, SIGNAL( dataChanged() ), this, SLOT( updatePatchName() ) );
connect( &k->m_patchNum, SIGNAL( dataChanged() ), this, SLOT( updatePatchName() ) );
// File Button
m_fileDialogButton = new PixmapButton( this );
m_fileDialogButton->setCursor( QCursor( Qt::PointingHandCursor ) );
m_fileDialogButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap( "fileselect_on" ) );
m_fileDialogButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap( "fileselect_off" ) );
m_fileDialogButton->move( 223, 68 );
connect( m_fileDialogButton, SIGNAL( clicked() ), this, SLOT( showFileDialog() ) );
ToolTip::add( m_fileDialogButton, tr( "Open GIG file" ) );
// Patch Button
m_patchDialogButton = new PixmapButton( this );
m_patchDialogButton->setCursor( QCursor( Qt::PointingHandCursor ) );
m_patchDialogButton->setActiveGraphic( PLUGIN_NAME::getIconPixmap( "patches_on" ) );
m_patchDialogButton->setInactiveGraphic( PLUGIN_NAME::getIconPixmap( "patches_off" ) );
m_patchDialogButton->setEnabled( false );
m_patchDialogButton->move( 223, 94 );
connect( m_patchDialogButton, SIGNAL( clicked() ), this, SLOT( showPatchDialog() ) );
ToolTip::add( m_patchDialogButton, tr( "Choose patch" ) );
// LCDs
m_bankNumLcd = new LcdSpinBox( 3, "21pink", this );
m_bankNumLcd->move( 111, 150 );
m_patchNumLcd = new LcdSpinBox( 3, "21pink", this );
m_patchNumLcd->move( 161, 150 );
// Next row
m_filenameLabel = new QLabel( this );
m_filenameLabel->setGeometry( 61, 70, 156, 14 );
m_patchLabel = new QLabel( this );
m_patchLabel->setGeometry( 61, 94, 156, 14 );
// Gain
m_gainKnob = new gigKnob( this );
m_gainKnob->setHintText( tr( "Gain:" ) + " ", "" );
m_gainKnob->move( 32, 140 );
setAutoFillBackground( true );
QPalette pal;
pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap( "artwork" ) );
setPalette( pal );
updateFilename();
}
GigInstrumentView::~GigInstrumentView()
{
}
void GigInstrumentView::modelChanged()
{
GigInstrument * k = castModel<GigInstrument>();
m_bankNumLcd->setModel( &k->m_bankNum );
m_patchNumLcd->setModel( &k->m_patchNum );
m_gainKnob->setModel( &k->m_gain );
connect( k, SIGNAL( fileChanged() ), this, SLOT( updateFilename() ) );
connect( k, SIGNAL( fileLoading() ), this, SLOT( invalidateFile() ) );