-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convolvotron.cpp
1117 lines (874 loc) · 34.3 KB
/
Convolvotron.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
/*
* File: Convolvotron.cpp
*
* Version: 1.0
*
* Created: 5/26/09
*
* Copyright: Copyright (C) 2009 Meatscience, All Rights Reserved
*
*/
#include "Convolvotron.h"
#include "CAXException.h"
#include <boost/bind.hpp>
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <signal.h>
// Used only for converting channel num to string
#include <sstream>
#include "Convolver.h"
using boost::shared_ptr;
using std::vector;
using std::cerr;
using std::cout;
using std::endl;
using std::pair;
Float32 Convolvotron::peak = 0.0f;
#if DEMO
uint32_t blipInterval = 30; // 10 seconds
#endif
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
COMPONENT_ENTRY(Convolvotron)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convolvotron::Convolvotron
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Convolvotron::Convolvotron(AudioUnit component)
: AUEffectBase(component), auConvolver(),
irFilename(), irLoadingThread(NULL), irLoadingStatus(), tailTime(0.0f)
{
#if DEBUG
cout << "Convolvotron::Convolvotron()" << endl;
#endif
#if DEMO
demoExpired = false;
#endif
irLoadingStatus.status = LOAD_STATUS_LOADED;
strcpy(irLoadingStatus.filename, "");
CreateElements();
Globals()->UseIndexedParameters(kNumberOfParameters);
for (int i=0; i < kNumberOfParameters; i++) {
SetParameter(i, parameters[i].defaultValue);
}
#if AU_DEBUG_DISPATCHER
mDebugDispatcher = new AUDebugDispatcher (this);
#endif
#if DEBUG
printf("Convolvotron::Convolvotron(): initializing with %u channels\n", (uint32_t)this->GetNumberOfChannels());
#endif
pthread_mutex_init(&filtersMutex, NULL);
}
void Convolvotron::setFrameSize(uint32_t frameSize) {
if (this->frameSize != frameSize) {
#if DEBUG
cout << endl << "Convolvotron::setFrameSize() changing frameSize from " << this->frameSize << " to " << frameSize << endl << endl;
#endif
this->frameSize = frameSize;
shared_ptr<Convolver::BlockPattern> blockPattern;
if (frameSize > 1024 && frameSize < 2048) {
blockPattern.reset(new Convolver::TwoSizeBlockPattern(frameSize, frameSize*2, 2));
} else if (frameSize > 512 && frameSize <= 1024) {
blockPattern.reset(new Convolver::TwoSizeBlockPattern(frameSize, frameSize*4, 4));
} else if (frameSize <= 512) {
blockPattern.reset(new Convolver::TwoSizeBlockPattern(frameSize, frameSize*8, 8));
} else {
// We're >= 2048
blockPattern.reset(new Convolver::FixedSizeBlockPattern(frameSize));
}
assert(blockPattern != NULL);
auConvolver->setBlockPattern(blockPattern);
ir->setBlockPattern(auConvolver->getKernel(), blockPattern);
filenameToIRCache.clear();
}
}
ComponentResult Convolvotron::Initialize()
{
ComponentResult result = AUEffectBase::Initialize();
// Do this pre-emptively, because ableton likes to send lots of crazy calls
cancelIRLoadingThread();
UInt32 maxFrameSize = GetMaxFramesPerSlice();
this->frameSize = maxFrameSize;
Float32 sampleRate = this->GetSampleRate();
#if DEMO
time(&endTime);
endTime += 600; // 10 min = 1sec * 60sec/min
time(&lastBlipTime);
#endif
// Delete our caches
ir = boost::shared_ptr<Convolver::IR>();
filenameToIRCache.clear();
#if DEBUG
cout << "Convolvolvotron::Initialize(): maxFramesPerSlice=" << maxFrameSize << ", sampleRate=" << sampleRate << endl;
#endif
shared_ptr<Convolver::BlockPattern> blockPattern;
if (maxFrameSize > 1024 && maxFrameSize < 2048) {
blockPattern.reset(new Convolver::TwoSizeBlockPattern(maxFrameSize, maxFrameSize*2, 2));
} else if (maxFrameSize > 512 && maxFrameSize <= 1024) {
blockPattern.reset(new Convolver::TwoSizeBlockPattern(maxFrameSize, maxFrameSize*4, 4));
} else if (maxFrameSize <= 512) {
blockPattern.reset(new Convolver::TwoSizeBlockPattern(maxFrameSize, maxFrameSize*8, 8));
} else {
// We're >= 2048
blockPattern.reset(new Convolver::FixedSizeBlockPattern(maxFrameSize));
}
assert(blockPattern != NULL);
//shared_ptr<Convolver::BlockPattern> blockPattern(new Convolver::FixedSizeBlockPattern(small));
//shared_ptr<Convolver::BlockPattern> blockPattern(new Convolver::TwoSizeBlockPattern(small, big, numSmall));
//shared_ptr<Convolver::BlockPattern> blockPattern(new Convolver::ThreeSizeBlockPattern(small, med, big, numSmall, numMed));
//shared_ptr<Convolver::BlockPattern> blockPattern(new Convolver::DoublingBlockPattern(small, 4096));
shared_ptr<Convolver::AUConvolver> auConvolver(new Convolver::AUConvolver(*this, blockPattern));
this->auConvolver = auConvolver;
if(result == noErr ) {
if (this->irFilename.size() > 0) {
#if DEBUG
cout << "Convolvotron::Initialize(): loading IR Filename " << irFilename << endl;
#endif
// Gotta cache this here, because LoadUnitIR() overwrites it
std::string filename = this->irFilename;
LoadUnitIR();
// We load a unit ir synchronously, and load in background
// because some hosts (Logic, lookin' at you) block their startup
// on file load waiting for this, and the latency is bad
bool inBackground = true;
LoadIR(filename, inBackground);
result = noErr;
} else {
LoadUnitIR();
result = noErr;
}
// in case the AU was un-initialized and parameters were changed, the view can now
// be made aware it needs to update
PropertyChanged(kAudioUnitCustomProperty_Samples, kAudioUnitScope_Global, 0 );
}
#if DEBUG
cout << "Convolvolvotron::Initialize(): done" << endl;
#endif
return result;
}
Convolvotron::~Convolvotron () {
#if DEBUG_MEMORY
cout << endl << "Convolvotron::~Convolvotron()" << endl;
#endif
pthread_mutex_destroy(&filtersMutex);
#if AU_DEBUG_DISPATCHER
delete mDebugDispatcher;
#endif
#if DEBUG_MEMORY
cout << "Convolvotron::~Convolvotron()" << endl << endl;
#endif
}
OSStatus Convolvotron::ProcessBufferLists(AudioUnitRenderActionFlags & ioActionFlags,
const AudioBufferList & inBuffer,
AudioBufferList & outBuffer,
UInt32 inFramesToProcess )
{
bool ioSilence;
bool silentInput = IsInputSilent (ioActionFlags, inFramesToProcess);
ioActionFlags |= kAudioUnitRenderAction_OutputIsSilence;
#if DEMO
static uint32_t beepCounter = 0;
time_t currentTime;
time(¤tTime);
if (currentTime >= endTime) {
demoExpired = true;
return noErr;
}
if (currentTime - lastBlipTime > blipInterval) {
lastBlipTime = currentTime;
beepCounter = 5;
}
#endif
// FIXME: we should pop remaining state out so we "ring out"
if (silentInput) return noErr;
// This should only change frame size if its different
this->setFrameSize(inFramesToProcess);
Float32 wetDryMix = GetParameter( kParam_WetDry ) / 100.0;
Float32 outGain = pow(10.0, GetParameter(kParam_OutputGain) / 20.0);
//Float32 stereoSeparation = GetParameter( kParam_StereoSeparation ) / 100.0;
Float32 dryGain = outGain * sqrt(1.0 - wetDryMix);
Float32 wetGain = outGain * sqrt(wetDryMix);
ioSilence = silentInput;
auConvolver->convolve(inBuffer, outBuffer, inFramesToProcess, dryGain, wetGain);
if (!ioSilence)
ioActionFlags &= ~kAudioUnitRenderAction_OutputIsSilence;
// FIXME: for performance, do this inside convolve as a side effect, and pass back peaks...
// also, this only listens to ONE channel
float biggest = *std::max_element((AudioSampleType *)outBuffer.mBuffers[0].mData, (AudioSampleType *)outBuffer.mBuffers[0].mData + inFramesToProcess);
if (biggest > Convolvotron::peak) {
Convolvotron::peak = biggest;
}
#if DEMO
if (beepCounter > 0) {
beepCounter--;
uint32_t size = outBuffer.mNumberBuffers;
for (uint32_t i=0; i < size; i++) {
float * output = (float *)outBuffer.mBuffers[i].mData;
bool on = true;
uint32_t onCount = 0;
for (uint32_t j=0; j < inFramesToProcess; j++) {
if (onCount > 100) {
onCount = 0;
on = !on;
}
if (on) {
output[j] += 0.2;
} else {
output[j] += -0.2;
}
onCount++;
}
}
}
#endif
return noErr;
/*
// call the kernels to handle either interleaved or deinterleaved
if (inBuffer.mNumberBuffers == 1) {
// interleaved (or mono)
int channel = 0;
for (KernelList::iterator it = mKernelList.begin(); it != mKernelList.end(); ++it, ++channel) {
AUKernelBase *kernel = *it;
if (kernel != NULL) {
ioSilence = silentInput;
// process each interleaved channel individually
kernel->Process(
(const AudioSampleType *)inBuffer.mBuffers[0].mData + channel,
(AudioSampleType *)outBuffer.mBuffers[0].mData + channel,
inFramesToProcess,
inBuffer.mBuffers[0].mNumberChannels,
ioSilence);
if (!ioSilence)
ioActionFlags &= ~kAudioUnitRenderAction_OutputIsSilence;
}
}
} else {
// deinterleaved
const AudioBuffer *srcBuffer = inBuffer.mBuffers;
AudioBuffer *destBuffer = outBuffer.mBuffers;
for (KernelList::iterator it = mKernelList.begin(); it != mKernelList.end();
++it, ++srcBuffer, ++destBuffer) {
AUKernelBase *kernel = *it;
if (kernel != NULL) {
ioSilence = silentInput;
kernel->Process(
(const AudioSampleType *)srcBuffer->mData,
(AudioSampleType *)destBuffer->mData,
inFramesToProcess,
1,
ioSilence);
if (!ioSilence)
ioActionFlags &= ~kAudioUnitRenderAction_OutputIsSilence;
}
}
}
*/
return noErr;
}
void Convolvotron::LoadUnitIR() {
#if DEBUG
cout << "Convolvotron::LoadUnitIR()" << endl;
#endif
irFilename = "";
// FIXME: right now, we gotta normalize, or else no frequency response = CRASH
shared_ptr<Convolver::IR> unitIR(new Convolver::UnitIR(auConvolver->getKernel(), auConvolver->getBlockPattern()));
std::string filename = "";
SetIR(filename, unitIR);
#if DEBUG
cout << "Convolvotron::LoadUnitIR(): done" << endl;
#endif
}
typedef struct {
std::string *filename;
Convolvotron *convolvotron;
} LoadIRArguments;
void Convolvotron::LoadIRInBackground(std::string &filename) {
try {
// LoadIR will call PropertyChanged(kAudioUnitCustomProperty_IRLoadingStatus) if it succeeds
LoadIR(filename, false);
} catch (CAXException &cax) {
#if DEBUG
cerr << "Convolvotron::Initialize(): WARNING, couldn't load stuff. An error occurred in " << cax.mOperation << endl;
#endif
irLoadingStatus.status = LOAD_STATUS_FAILED;
PropertyChanged(kAudioUnitCustomProperty_IRLoadingStatus, kAudioUnitScope_Global, 0);
} catch (...) {
#if DEBUG
printf("Convolvotron::Initialize(): WARNING, couldn't load stuff. An unknown exception occurred.\n");
#endif
irLoadingStatus.status = LOAD_STATUS_FAILED;
PropertyChanged(kAudioUnitCustomProperty_IRLoadingStatus, kAudioUnitScope_Global, 0);
}
}
static void *loadIR(void *argumentsVoid) {
LoadIRArguments *arguments = (LoadIRArguments *)argumentsVoid;
int status = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
status = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
#if DEBUG
cout << "THREAD: loadImpulseResponse()" << "\t filename is: " << arguments->filename << endl;
#endif
arguments->convolvotron->LoadIRInBackground(*arguments->filename);
delete arguments->filename;
delete arguments;
#if DEBUG
printf("THREAD: exiting\n");
#endif
return NULL;
}
void Convolvotron::SetIR(std::string &filename, boost::shared_ptr<Convolver::IR> ir) {
pthread_mutex_lock(&this->filtersMutex); {
// Save the name of the filename, we're gonna need it to SaveState()
this->irFilename = filename;
this->ir = ir;
// FIXME: we don't set stereo separation yet
auConvolver->setFilters(ir->getFilters(), 1.0f);
} pthread_mutex_unlock(&this->filtersMutex);
// Change the tail time
tailTime = ir->getFilters()[0]->getNumSamples() / GetSampleRate();
// Notify everyone that our IR changed
PropertyChanged(kAudioUnitCustomProperty_Samples, kAudioUnitScope_Global, 0 );
irLoadingStatus.status = LOAD_STATUS_LOADED;
PropertyChanged(kAudioUnitCustomProperty_IRLoadingStatus, kAudioUnitScope_Global, 0);
#if DEBUG
cout << "Convolvotron::LoadIR(): changing tail time to " << tailTime << " seconds." << endl;
#endif
}
void Convolvotron::LoadIR(std::string &filename, bool inBackground) {
assert(filename != "");
// Check if we already have this filename loaded into memory
std::map<std::string, boost::shared_ptr<Convolver::IR> >::iterator cacheIter = filenameToIRCache.find(filename);
if (cacheIter != filenameToIRCache.end()) {
// We do! no need to use background loading
#if DEBUG
cout << "Convolvotron::LoadIR(): " << filename << "found in cache, loading" << endl;
#endif
SetIR(filename, cacheIter->second);
return;
}
if (inBackground) {
#if DEBUG
cout << "Convolvotron::LoadIR(inbackground=true): starting thread" << endl;
#endif
cancelIRLoadingThread();
// Notify views that we're loading a file in the background
irLoadingStatus.status = LOAD_STATUS_LOADING;
strncpy(irLoadingStatus.filename, filename.c_str(), IRLoadingStatus_filename_size);
PropertyChanged(kAudioUnitCustomProperty_IRLoadingStatus, kAudioUnitScope_Global, 0);
// Call ourselves on a new thread
LoadIRArguments *arguments = new LoadIRArguments;
arguments->filename = new std::string(filename);
arguments->convolvotron = this;
int result;
pthread_attr_t threadAttr;
result = pthread_attr_init(&threadAttr);
assert(result == 0);
int schedPolicy;
result = pthread_attr_getschedpolicy(&threadAttr, &schedPolicy);
assert(result==0);
sched_param schedParam;
result = pthread_attr_getschedparam(&threadAttr, &schedParam);
assert(result==0);
#if DEBUG
cout << "LoadIR() default priority is " << schedParam.sched_priority << endl;
#endif
schedParam.sched_priority = sched_get_priority_min(schedPolicy);
#if DEBUG
cout << "LoadIR() MIN PRIORITY IS " << schedParam.sched_priority << endl;
#endif
result = pthread_attr_setschedparam(&threadAttr, &schedParam);
assert(result ==0);
pthread_create(&this->irLoadingThread, NULL, &loadIR, arguments);
return;
}
#if DEBUG
cout << endl << "Convolvotron::LoadIR(inbackground=false) {" << endl;
#endif
const char * filename_c = filename.c_str();
#if DEBUG
cout << "\t loading IR: " << filename_c << endl;
#endif
Float64 kGraphSampleRate = this->GetSampleRate();
ExtAudioFileRef xafref = NULL;
float *data = NULL;
uint32_t dataLength = 0;
{
OSStatus err = noErr;
FSRef theRef;
XThrowIfError(FSPathMakeRef((const UInt8 *)filename.c_str(), &theRef, NULL), "FSPathMakeRef");
err = ExtAudioFileOpen(&theRef, &xafref);
XThrowIfError (err, "ExtAudioFileOpen");
UInt32 propSize;
CAStreamBasicDescription clientFormat;
uint32_t numChannels;
propSize = sizeof(clientFormat);
err = ExtAudioFileGetProperty(xafref, kExtAudioFileProperty_FileDataFormat, &propSize, &clientFormat);
XThrowIfError(err, "kExtAudioFileProperty_FileDataFormat");
numChannels = clientFormat.NumberChannels();
SInt64 filelength;
propSize = sizeof(filelength);
err = ExtAudioFileGetProperty(xafref, kExtAudioFileProperty_FileLengthFrames, &propSize, &filelength);
XThrowIfError(err, "kExtAudioFileProperty_FileLengthFrames");
clientFormat.mSampleRate = kGraphSampleRate;
bool interleaved = false;
clientFormat.SetCanonical(numChannels, interleaved);
propSize = sizeof(clientFormat);
err = ExtAudioFileSetProperty(xafref, kExtAudioFileProperty_ClientDataFormat, propSize, &clientFormat);
XThrowIfError(err, "kExtAudioFileProperty_ClientDataFormat");
err = ExtAudioFileGetProperty(xafref, kExtAudioFileProperty_FileLengthFrames, &propSize, &filelength);
XThrowIfError(err, "kExtAudioFileProperty_FileLengthFrames");
#if DEBUG
cout << "Convolvotron::LoadIR(): file has " << filelength << "samples" << endl;
#endif
// If you need to alloc a buffer, you'll need to alloc filelength*channels*rateRatio bytes
double rateRatio = kGraphSampleRate / clientFormat.mSampleRate;
uint32_t numFrames = (UInt32)ceil(filelength * rateRatio);
uint32_t channelLength = numFrames * 2;
dataLength = channelLength * numChannels;
data = new float[dataLength]();
AudioBufferList *bufList = (AudioBufferList*)calloc(1, sizeof(AudioBufferList) + numChannels * sizeof(AudioBuffer));
bufList->mNumberBuffers = numChannels;
for(uint32_t i=0; i < numChannels; i++) {
bufList->mBuffers[i].mNumberChannels = 1;
bufList->mBuffers[i].mData = &data[channelLength*i];
bufList->mBuffers[i].mDataByteSize = channelLength * sizeof(Float32);
}
// FIXME: shouldn't we know precisely how many packets we're loading? this blows...
UInt32 loadedFrames = INT_MAX;
err = ExtAudioFileRead(xafref, &loadedFrames, bufList);
XThrowIfError(err, "ExtAudioFileRead");
//assert(loadedPackets == numFrames);
bool normalize = true;
// Convert from AudioBufferList to vector<const float *>
std::vector<const float *> channels;
channels.reserve(numChannels);
for(uint32_t i=0; i < numChannels; i++) {
channels.push_back((const float *)bufList->mBuffers[i].mData);
}
uint32_t slashLocation = filename.find_last_of("/");
std::string shortName = slashLocation+1 < filename.size() ? filename.substr(slashLocation+1) : filename;
shared_ptr<Convolver::IR> irPtr(new Convolver::IR(auConvolver->getKernel(), auConvolver->getBlockPattern(),
channels, loadedFrames, normalize, shortName));
delete[] data;
delete bufList;
SetIR(filename, irPtr);
// We've loaded a new file, add it to the cache
filenameToIRCache[filename] = ir;
ExtAudioFileDispose(xafref);
}
if (false) { // FIXME: do this if an exception was thrown
if(data) free(data);
data = nil;
if(xafref) ExtAudioFileDispose(xafref);
//NSLog(@"loadSegment: Caught %@: %@", [exception name], [exception reason]);
}
#if DEBUG
cout << "Convolvotron::LoadIR(): exiting" << endl << endl;
#endif
}
Float64 Convolvotron::GetTailTime() {
return tailTime;
}
Float64 Convolvotron::GetLatency() {
return 0.0;
// FIXME: is this right? now we create kernel with same latency as maxSamples, so I guess so???
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convolvotron::GetParameterValueStrings
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus Convolvotron::GetParameterValueStrings(AudioUnitScope inScope,
AudioUnitParameterID inParameterID,
CFArrayRef * outStrings)
{
return kAudioUnitErr_InvalidProperty;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convolvotron::GetParameterInfo
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus Convolvotron::GetParameterInfo(AudioUnitScope inScope,
AudioUnitParameterID inParameterID,
AudioUnitParameterInfo &outParameterInfo )
{
OSStatus result = noErr;
outParameterInfo.flags = kAudioUnitParameterFlag_IsWritable
| kAudioUnitParameterFlag_IsReadable
| parameters[inParameterID].flags;
if (inScope == kAudioUnitScope_Global) {
if (inParameterID < kNumberOfParameters) {
AUBase::FillInParameterName (outParameterInfo, parameters[inParameterID].name, false);
outParameterInfo.unit = parameters[inParameterID].unit;
outParameterInfo.minValue = parameters[inParameterID].minValue;
outParameterInfo.maxValue = parameters[inParameterID].maxValue;
outParameterInfo.defaultValue = parameters[inParameterID].defaultValue;
} else {
result = kAudioUnitErr_InvalidParameter;
}
} else {
result = kAudioUnitErr_InvalidParameter;
}
return result;
}
boost::shared_ptr<Convolver::Filter> Convolvotron::getMonoIR() {
shared_ptr<Convolver::Filter> monoFilter;
pthread_mutex_lock(&this->filtersMutex); {
vector<shared_ptr<Convolver::Filter> > &filters = ir->getFilters();
if (filters.size() > 0) {
monoFilter = filters[0];
}
} pthread_mutex_unlock(&this->filtersMutex);
return monoFilter;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convolvotron::GetPropertyInfo
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus Convolvotron::GetPropertyInfo (AudioUnitPropertyID inID,
AudioUnitScope inScope,
AudioUnitElement inElement,
UInt32 & outDataSize,
Boolean & outWritable)
{
if (inScope == kAudioUnitScope_Global) {
switch (inID) {
case kAudioUnitProperty_CocoaUI:
outWritable = false;
outDataSize = sizeof (AudioUnitCocoaViewInfo);
return noErr;
case kAudioUnitCustomProperty_IRFile: // our custom property
if(inScope != kAudioUnitScope_Global ) return kAudioUnitErr_InvalidScope;
outDataSize = (irFilename.size() + 1) * sizeof(char);
outWritable = true;
return noErr;
case kAudioUnitCustomProperty_FilterFrequencyResponse: // our custom property
if(inScope != kAudioUnitScope_Global ) return kAudioUnitErr_InvalidScope;
outDataSize = kMyNumberOfResponseFrequencies * sizeof(FrequencyResponse);
outWritable = false;
return noErr;
case kAudioUnitCustomProperty_Samples:
if(inScope != kAudioUnitScope_Global ) return kAudioUnitErr_InvalidScope;
{
shared_ptr<Convolver::Filter> ir = getMonoIR();
assert(ir != NULL);
outDataSize = ir->getNumSamples() * sizeof(float);
}
outWritable = false;
return noErr;
case kAudioUnitCustomProperty_Peak:
if(inScope != kAudioUnitScope_Global ) return kAudioUnitErr_InvalidScope;
outDataSize = sizeof(Float32);
outWritable = false;
return noErr;
case kAudioUnitCustomProperty_IRLoadingStatus:
if(inScope != kAudioUnitScope_Global ) return kAudioUnitErr_InvalidScope;
outDataSize = sizeof(IRLoadingStatus);
outWritable = false;
return noErr;
case kAudioUnitCustomProperty_IRInfo:
if(inScope != kAudioUnitScope_Global ) return kAudioUnitErr_InvalidScope;
outDataSize = sizeof(IRInfo);
outWritable = false;
return noErr;
case kAudioUnitCustomProperty_IsDemo:
if(inScope != kAudioUnitScope_Global ) return kAudioUnitErr_InvalidScope;
outDataSize = sizeof(int);
outWritable = false;
return noErr;
}
}
return AUEffectBase::GetPropertyInfo (inID, inScope, inElement, outDataSize, outWritable);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convolvotron::GetProperty
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus Convolvotron::GetProperty( AudioUnitPropertyID inID,
AudioUnitScope inScope,
AudioUnitElement inElement,
void * outData )
{
if (inScope == kAudioUnitScope_Global) {
switch (inID) {
case kAudioUnitProperty_CocoaUI:
{
// Look for a resource in the main bundle by name and type.
CFBundleRef bundle = CFBundleGetBundleWithIdentifier( CFSTR(CONVOLVOTRON_BUNDLE_ID) );
if (bundle == NULL) {
printf("WARNING: failed to load our bundle, where'd it go?\n");
return fnfErr;
}
CFURLRef bundleURL = CFBundleCopyResourceURL( bundle,
CFSTR("MeatscienceConvolvotron_CocoaViewFactory"),
CFSTR("bundle"),
NULL);
if (bundleURL == NULL) return fnfErr;
CFStringRef className = CFSTR("MeatscienceConvolvotron_CocoaViewFactory");
AudioUnitCocoaViewInfo cocoaInfo = { bundleURL, {className} };
*((AudioUnitCocoaViewInfo *)outData) = cocoaInfo;
return noErr;
}
// This is our custom property which reports the current frequency response curve
//
case kAudioUnitCustomProperty_IRFile:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
// the kernels are only created if we are initialized
// since we're using the kernels to get the curve info, let
// the caller know we can't do it if we're un-initialized
// the UI should check for the error and not draw the curve in this case
if(!IsInitialized() ) return kAudioUnitErr_Uninitialized;
char *fileName = (char*)outData;
if (irFilename.size() > 0) {
UInt32 dataSize;
Boolean writable;
ComponentResult result;
result = this->GetPropertyInfo( kAudioUnitCustomProperty_IRFile,
kAudioUnitScope_Global,
0,
dataSize,
writable);
#if DEBUG
cout << "Convolvotron::GetProperty(): returning fileName " << irFilename;
#endif
memcpy(fileName, irFilename.c_str(), dataSize * sizeof(char));
} else {
// We have the Unit Impulse loaded
#if DEBUG
printf("Convolvotron::GetProperty(): no fileName, returning null for unit impulse\n");
#endif
fileName[0] = '\0';
}
return noErr;
}
case kAudioUnitCustomProperty_FilterFrequencyResponse:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
if(!IsInitialized() ) return kAudioUnitErr_Uninitialized;
FrequencyResponse *freqResponseTable = ((FrequencyResponse*)outData);
{
shared_ptr<Convolver::Filter> ir = getMonoIR();
assert(ir != NULL);
const FrequencyResponse *copyTable = ir->getFrequencyResponse();
memcpy(freqResponseTable, copyTable, sizeof(FrequencyResponse) * kMyNumberOfResponseFrequencies);
}
return noErr;
}
case kAudioUnitCustomProperty_Samples:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
if(!IsInitialized() ) return kAudioUnitErr_Uninitialized;
float *samples = (float *)outData;
uint32_t numSamples;
// FIXME: what if the GetPropertyInfo call returned a number of
// samples appropriate for a smaller data size? i.e. a new IR
// was swapped in on another thread between these calls: that would suck
{
shared_ptr<Convolver::Filter> ir = getMonoIR();
assert(ir != NULL);
const float *sampleBuffer = ir->getSamples();
numSamples = ir->getNumSamples();
memcpy(samples, sampleBuffer, sizeof(float) * numSamples);
}
return noErr;
}
case kAudioUnitCustomProperty_Peak:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
if(!IsInitialized() ) return kAudioUnitErr_Uninitialized;
Float32 *peak = (Float32 *)outData;
*peak = Convolvotron::peak;
Convolvotron::peak = 0.0f;
return noErr;
}
case kAudioUnitCustomProperty_IRLoadingStatus:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
if(!IsInitialized() ) return kAudioUnitErr_Uninitialized;
IRLoadingStatus *status = (IRLoadingStatus *)outData;
*status = irLoadingStatus;
return noErr;
}
case kAudioUnitCustomProperty_IRInfo:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
if(!IsInitialized() ) return kAudioUnitErr_Uninitialized;
IRInfo *outInfo = (IRInfo *)outData;
IRInfo info;
info.seconds = (float)getMonoIR()->getNumSamples() / GetSampleRate();
info.numChannels = ir->getFilters().size();
*outInfo = info;
return noErr;
}
case kAudioUnitCustomProperty_IsDemo:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
int *outIsDemo = (int *)outData;
#if DEMO
*outIsDemo = -1;
#else
*outIsDemo = 0;
#endif
return noErr;
}
}
}
return AUEffectBase::GetProperty (inID, inScope, inElement, outData);
}
void handleInterrupt(int signal) {
cout << "Got interrupt" << endl;
throw;
}
void Convolvotron::cancelIRLoadingThread() {
// Stop any existing worker thread, FIXME: this leaks memory, C++ doesn't call destructors
pthread_cancel(this->irLoadingThread);
pthread_join(this->irLoadingThread, NULL);
irLoadingStatus.status = LOAD_STATUS_CANCELED;
PropertyChanged(kAudioUnitCustomProperty_IRLoadingStatus, kAudioUnitScope_Global, 0);
}
OSStatus Convolvotron::SetProperty(AudioUnitPropertyID inID,
AudioUnitScope inScope,
AudioUnitElement inElement,
const void * inData,
UInt32 inDataSize)
{
if (inScope == kAudioUnitScope_Global) {
switch (inID) {
case kAudioUnitCustomProperty_IRFile:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
std::string irFilename = (char *)inData;
assert(irFilename != "");
bool inBackground = true;
LoadIR(irFilename, inBackground);
return noErr;
}
case kAudioUnitCustomProperty_IRLoadingStatus:
{
if(inScope != kAudioUnitScope_Global) return kAudioUnitErr_InvalidScope;
IRLoadingStatus *loadingStatus = (IRLoadingStatus *) inData;
if (loadingStatus->status == LOAD_ACTION_CANCEL) {
#if DEBUG
cout << "Convolvotron::SetProperty(IRLoadingStatus): cancelling background thread" << endl;
#endif
cancelIRLoadingThread();
return noErr;
} else {
return kAudioUnitErr_InvalidPropertyValue;
}
}
}
}
return AUEffectBase::SetProperty (inID, inScope, inElement, inData, inDataSize);
}
ComponentResult Convolvotron::SaveState(CFPropertyListRef * outData) {
#if DEBUG
cout << "Convolvotron::SateState()" << endl;
#endif
ComponentResult result = AUEffectBase::SaveState(outData);
if (result == noErr) {
CFMutableDictionaryRef dict = (CFMutableDictionaryRef)*outData;
if (this->irFilename.size() > 0) {
#if DEBUG
cout << "Convolvotron::SaveState(): saving ir filename " << this->irFilename << endl;
#endif
CFStringRef value = CFStringCreateWithCString (NULL, this->irFilename.c_str(), kCFStringEncodingUTF8);
CFDictionarySetValue (dict, CFSTR("ConvolvotronIRFilename"), value);
CFRelease(value);
} else {
#if DEBUG
cout << "Convolvotron::SaveState(): no filename to save, skipping" << endl;
#endif
}
}