-
Notifications
You must be signed in to change notification settings - Fork 6
/
SampleDescript.sc
788 lines (645 loc) · 24.3 KB
/
SampleDescript.sc
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
//Sample Descripter By Allen SC Wu
//Extra classes requirements: wslib Quark.
SampleDescript{
classvar guiTempFile;
classvar <> pitchThresh;
//raw data
var <mirDataByFeatures; //[[RMS], [Pitch], [hasPitch], [centroid], [SensoryDissonance], [SpecFlatness]]
var <rmsData;
var <centroidData;
var <noiseData;
var <rmsDataBySection;
var <pitchData; //[[pitch, hasPitch]....]
var <mfccData;
var <activeCentroidData; //frequency centroid
var <activeNoiseData; // higher value indicates dissonance
var <activeRMSData;
// var <activeSpecFlatness; // 0-1, 0 is single sinesoid wave, 1 is white noise. This indicates the degree of noiseness.
//General information
var <file; //An SCMIRAudioFile.
var <filename;
var <sampleRate;
var <numSamples;
var <numChannels;
var <bufferServer;
var <buffer;
var <activeBuffer; //Array of buffers with each section (is there a way to play one part of buffer without making a copy?)
var <keynumFromFileName; //get key number from file name, nil if there is none
var <keynumFromPitchFound; //get key number from pitch detection. not necessarily an integer.
var <hoptime; //for internal use, time for each hop
var <framehop; //the size of SCMIR frame
var <frameTimes; //An array of time stamp of each hop frame
//****** global description *****
// time domain
var <duration; //Total duration of sound file.
var <numFrames;
var <sectionBreakPoint; //nadir (lowest) point in between onsets.
var <globalPeakIndex;
var <globalPeakAmp; //Amplitude at RMS peak point.
var <globalPeakTime; //Time on the peak point.
// **** local information by Onsets *****
// Temporal information
var <> keynum;
var <activeDuration;
var <mfcc;
var <onsetTime;
var <onsetIndex; //frame index at onset
var <startIndex;
var <startTime; //beginning time of start frame
var <endIndex;
var <endTime;
var <peakIndex; //frame index at the peak time. NOTE: this one might not be an integer
var <peakTime; //global time point of each local peak
var <peakAmp;
var <attackDur; // Attack Time
var <releaseDur; // Release Time
var <temporalCentroid; // Shorter value indicates percussive sounds, longer value indicates sustained sounds.
var <activeEnv;
// Information for Debugging
var <soundFile;
var <soundFileArray;
*initClass {
Platform.case(
\osx, { pitchThresh = 0.9 },
\linux, { pitchThresh = 0.5 },
\windows, { pitchThresh = 0.9 }
);
}
//parameters
//File Name, Normalize, Start Tine, Duration, Threshold for attack point, Threshold for end point, Threshold for onset, Time threshold for onset grouping.
*new {arg filename, normtype=0, start=0, dur=0, startThresh=0.01, endThresh=0.01, onsetThresh=0.5, groupingThresh = 0.32, filenameAsNote = false, loadToBuffer = false, normalize = false, server = Server.default, action;
^super.new.init(filename, normtype, start, dur, startThresh, endThresh, onsetThresh, groupingThresh, filenameAsNote, loadToBuffer, normalize, server, action);
}
init {|fileName, normtype, start, dur, startThresh, endThresh, onsetThresh, groupingThresh, filenameAsNote, loadToBuffer, normalize, server, action|
var cond = Condition.new(false);
//var soundFile, soundFileArray;
buffer = [];
server.postln;
bufferServer = server;
// //If a buffer is loaded to the sampler, write into a file before load to SCMIRAudioFile
// fork{
// //Write Buffer into a file if the input is a buffer.
// if(fileName.class == Buffer)
// {//if input is a buffer, save the buffer into a file before loading to SCMIR
// buffer = fileName;
// loadToBuffer = false;
// filenameAsNote = false;
// //provide a tempbuffer filename and save to a file.
// filename = Platform.defaultTempDir +/+ "supersampler" ++ UniqueID.next ++ ".aiff";
// fileName.write(filename, completionMessage: {cond.test=true;cond.signal;});
// }
// {filename = fileName; cond.test=true; cond.signal;};//if it is not a buffer
//
// cond.wait;
//
// ("Buffer rendered to" + filename).postln;
//Check if the file exist
if(File.exists(fileName).not)
{Error("File % could not be found".format(fileName)).throw}
{filename = fileName;};
file = SS_SCMIRAudioFile(filename, [[\Tartini, 0], \SpecCentroid, \SpecFlatness, [\MFCC, 13], \RMS], normtype, start, dur);
file.extractFeatures(normalize);
file.extractOnsets();
soundFile = SoundFile.openRead(filename);
//soundFile = SS_SCMIR.soundfile;
soundFileArray = FloatArray.newClear(soundFile.numFrames * soundFile.numChannels);
soundFile.readData(soundFileArray);
sampleRate = soundFile.sampleRate;
numChannels = soundFile.numChannels;
numSamples = soundFile.numFrames;
hoptime = SS_SCMIR.hoptime;
framehop = (SS_SCMIR.framehop / SS_SCMIR.samplingrate * this.sampleRate).round.asInteger;
globalPeakAmp = soundFileArray.abs.maxItem;
//get data from SCMIR
duration = file.duration;
mirDataByFeatures = file.featuredata.clump(file.numfeatures).flop;
/*
rmsData = mirDataByFeatures[0];
pitchData = [mirDataByFeatures[1], mirDataByFeatures[2]].flop;
centroidData = mirDataByFeatures[3];
dissonanceData = mirDataByFeatures[4];
*/
//rmsData = soundFileArray.rms(hoptime * soundFile.numChannels); //average multiple channel sounds
pitchData = [mirDataByFeatures[0], mirDataByFeatures[1]].flop;
centroidData = mirDataByFeatures[2];
noiseData = mirDataByFeatures[3];
mfccData = mirDataByFeatures[4..16].flop;
rmsData = mirDataByFeatures[17];
frameTimes = file.frameTimes;// (* SS_SCMIR.samplingrate / sampleRate);
numFrames = frameTimes.size;
this.getOnsetTime(groupingThresh);
this.getOnsetIndex;
this.findBreakPointByOnsets;
this.cleanBreakPointByOnsets(0.2); // clear unnecessary breakpoints
this.sectionRmsDataByBreakPoint;
this.findPeaksByOnsets;
this.arEnv(startThresh, endThresh);
this.getActiveEnv;
this.getActiveData;
this.getKeynum(filenameAsNote);
this.getMFCC;
if(loadToBuffer)
{
server.waitForBoot{
this.loadToBuffer(bufferServer, action: action)};
}
{
action.value;
};
}
free {
file = nil;
sampleRate = nil;
bufferServer = nil;
keynumFromFileName = nil;
keynumFromPitchFound = nil;
frameTimes = nil;
mirDataByFeatures = nil;
rmsData = nil;
rmsDataBySection = nil;
centroidData = nil;
noiseData = nil;
duration = nil;
sectionBreakPoint = nil;
globalPeakIndex = nil;
globalPeakAmp = nil;
globalPeakTime = nil;
keynum = nil;
activeDuration = nil;
onsetTime = nil;
onsetIndex = nil;
startIndex = nil;
startTime = nil;
endIndex = nil;
endTime = nil;
peakIndex = nil;
peakTime = nil;
peakAmp = nil;
attackDur = nil;
releaseDur = nil;
temporalCentroid = nil;
pitchData = nil;
activeCentroidData = nil;
activeNoiseData = nil;
// activeSpecFlatness = nil;
buffer.free;
activeBuffer.do({|thisBuffer, index|
thisBuffer.free;
});
buffer = nil;
activeBuffer = [];
if(PathName(filename).pathOnly == Platform.defaultTempDir)
{
filename.asString + "has been deleted".postln;
File.delete(filename)
};
}
/*
//load sound file into buffers, and subsections into activeBuffer
loadToBuffer {arg server = Server.default, action;
var buf, startSample = 0, durSample;
var cond = Condition(false);
bufferServer = server;
buffer.free;
//free buffers if they were loaded before.
activeBuffer.do({|buffer| buffer.free;});
activeBuffer = [];
"Loading soundfile into Buffer".postln;
server.waitForBoot{
Routine.run{
//load the sound file into a master buffer
buffer = Buffer.read(server, filename, action: {cond.test = true; cond.signal;});
cond.wait;
//load each sections into sub buffers
startIndex.do{|thisSectionStartIndex, sectionIndex|
startSample = thisSectionStartIndex * SCMIR.framehop;
durSample = (activeRMSData[sectionIndex].size - 1) * SCMIR.framehop;
activeBuffer = activeBuffer.add(Buffer.read(server, filename, startSample, durSample, {cond.test = true; cond.signal;}));
cond.wait;
activeBuffer[sectionIndex].path = (PathName(filename).pathOnly ++ PathName(filename).fileNameWithoutExtension ++ "_" ++ sectionIndex ++ "." ++ PathName(filename).extension);
};
action.value;
}
}
}
*/
//load sound file into arrays of mono buffers
//for global sound file [channel 0, channel 1]
//for activeBuffer [[channel 0, channel 1], [channel 0, channel 1], ....]
loadToBuffer {arg server = Server.default, action;
var buf, startSample = 0, durSample;
var cond = Condition(false);
bufferServer = server;
// Clear buffer if the buffer is not empty
if(buffer.isEmpty.not){
"Buffer is not empty, cleaning...".warn;
numChannels.do{|chan| buffer[chan].free;};
buffer = [];
//free buffers if they were loaded before.
activeBuffer.do({|buff| numChannels.do{|chan| buff[chan].free;}});
activeBuffer = [];
};
"Loading soundfile into Buffer".postln;
server.waitForBoot{
Routine.run{
//load the sound file into a master buffer
numChannels.do{|channel|
buffer = buffer.add(Buffer.readChannel(server, filename, channels: channel, action: {cond.test = true; cond.signal;}));
cond.wait;
};
//load each sections into sub buffers
startTime.do{|thisSectionStartTime, sectionIndex|
var monoBufArray = [];
startSample = thisSectionStartTime * sampleRate - 6;
durSample = (endTime[sectionIndex] - thisSectionStartTime) * sampleRate;
numChannels.do{|channel|
var monoBuf;
monoBuf = Buffer.readChannel(server, filename, startSample, durSample, channel, {cond.test = true; cond.signal});
monoBuf.path = (PathName(filename).pathOnly ++ PathName(filename).fileNameWithoutExtension ++ "_" ++ sectionIndex ++ "_[" ++ channel ++ "]." ++ PathName(filename).extension);
monoBufArray = monoBufArray.add(monoBuf);
cond.wait;
};
activeBuffer = activeBuffer.add(monoBufArray);
};
action.value;
}// end of Routine
}
}
freeBuffer{
buffer.free;
Routine{
activeBuffer.do
{
|multipleChannelBuffer, index|
multipleChannelBuffer.do
{
|monoBuffer|
monoBuffer.free;
};
if(index == (activeBuffer.size - 1)){activeBuffer = []};
};
}.play;
}
getKeynum {arg filenameAsNote = false, pitchShift = 0;
var str=PathName(filename).fileNameWithoutExtension;
var l=str.size-1;
var i=l, j, c;
var deg=0, acc=0, oct=0;
var foundPitches = [];
keynumFromFileName = nil;
keynumFromPitchFound = [];
keynum = [];
//retrive the key number from file name, if there is one.
//From VKey by Prof. Heinrich Taube
while ({i>=0 && "0123456789".includes(str.at(i))},{i=i-1});
if (i<l) { // had digit at end, i now before all digits
if (l - i >= 2){// had more than 2 digits at end
if(l - i <=3){//had no more than 3 digits, make it key number;
keynumFromFileName = str.copyRange(i+1,l).asInteger;
}
{// if there are more than 3 digits at the end of file
//that is probably not a key number.
keynumFromFileName = nil}
}
{//had only one digits at end
j=i;
while ({i>=0 && "AaBbCcDdEeFfGg#Ss".includes(str.at(i))},{i=i-1});
if (i<j) { // had note and accidentals, i now before letter
i=i+1;
c=str.at(i);
case
{"Cc".includes(c)} {i=i+1; deg=0;}
{"Dd".includes(c)} {i=i+1; deg=2;}
{"Ee".includes(c)} {i=i+1; deg=4;}
{"Ff".includes(c)} {i=i+1; deg=5;}
{"Gg".includes(c)} {i=i+1; deg=7;}
{"Aa".includes(c)} {i=i+1; deg=9;}
{"Bb".includes(c)} {i=i+1; deg=11;}
{true} {keynumFromFileName = nil};
case
{"Ss#".includes(str.at(i))} {i=i+1; acc=1;}
{"Ffb".includes(str.at(i))} {i=i+1; acc= -1;};
c=str.at(i);
case
{($0==c) && (i==(l-1)) && ($0==str.at(i+1))} {oct=0;}
{($0==c) && (i==l)} {oct=1;}
{($1==c) && (i==l)} {oct=2;}
{($2==c) && (i==l)} {oct=3;}
{($3==c) && (i==l)} {oct=4;}
{($4==c) && (i==l)} {oct=5;}
{($5==c) && (i==l)} {oct=6;}
{($6==c) && (i==l)} {oct=7;}
{($7==c) && (i==l)} {oct=8;}
{($8==c) && (i==l)} {oct=9;}
{($9==c) && (i==l)} {oct=10;}
{true} {keynumFromFileName = nil};
keynumFromFileName = (deg+acc+(oct*12));
}
// else
{keynumFromFileName = nil};
}}
// else, no digit at end of file name, use pitch material for keynumber
{keynumFromFileName = nil};
//get keynum from pitch material, keynum will not necessarily be an integer.
//The pitch data is gathered by the pitch data after the peak frame, if it has pitch.
//Return nil if there is no pitch after 20 frames until the end of the section.
peakIndex.do({|thisPeakIndex, sectionIndex|
var pitch, hasPitch;
var pitchCollection = [];
thisPeakIndex = thisPeakIndex.asInteger;
if(endTime[sectionIndex]-peakTime[sectionIndex] <= (hoptime * 20))
{
var pitchArray = pitchData[thisPeakIndex..endIndex[sectionIndex]].flop;
pitch = pitchArray[0];
hasPitch = pitchArray[1];
}
{
pitch = pitchData[thisPeakIndex..thisPeakIndex + 20].flop[0];
hasPitch = pitchData[thisPeakIndex..thisPeakIndex + 20].flop[1];
};
//find collections of data with pitch
pitch.do{|thisPitch, index|
if(hasPitch[index] >= pitchThresh)
{
pitchCollection = pitchCollection.add(thisPitch)
};
};
//if no picth data is collected, find pitch before peak time
if(pitchCollection.size == 0)
{
if(peakTime[sectionIndex]-startTime[sectionIndex] <= (hoptime * 20))
{
var pitchArray = pitchData[startIndex[sectionIndex]..thisPeakIndex[sectionIndex]].flop;
pitch = pitchArray[0];
hasPitch = pitchArray[1];
}
{
var pitchArray = pitchData[(thisPeakIndex - 20)..thisPeakIndex].flop;
pitch = pitchArray[0];
hasPitch = pitchArray[1];
};
pitch.do{|thisPitch, index|
if(hasPitch[index] >= pitchThresh)
{
pitchCollection = pitchCollection.add(thisPitch)
};
};
};
//if no pitch data is collected, than use centroid data for pitch
//if there are pitch data collected, get the most occurred data for keynum
if((pitchCollection.size == 0) || (pitchCollection.occurrencesArray(0.5).maxItem == 1))
// use an octave lower to map to fit the range of my keyboard :p
{keynumFromPitchFound = keynumFromPitchFound.add((centroidData[thisPeakIndex] ? centroidData[centroidData.size-1]).explin(20, 20000, 28, 103) - 12);
"no pitch detected, using centorid".postln;}
{keynumFromPitchFound = keynumFromPitchFound.add(pitchCollection.mostOccurredItems(0.5).mean)};
});
//Now determine which answer to use
if(filenameAsNote && keynumFromFileName.isNil.not)
{keynum = Array.fill(peakIndex.size, keynumFromFileName) + pitchShift;}
{keynum = keynumFromPitchFound + pitchShift;}
}//end of getKeyNum
//********************************************************************************
//Separate sound file into recognizable sound sections if exist.
//Here is how it works:
//1. Find out onsets in this sound file using SCMIR onset data.
//2. Find section breaking point based on the RMS nadir point inbetween each onsets.
//3. Find peak point inbetween each sections.
//4. Find attact and ending point from the peak point using fixed threshold method.
//get onset time, if an onset is too close to previous one, it will be abandoned
getOnsetTime {|groupingThresh = 0.32|
var onsets = [file.onsetdata[0]];
file.onsetdata.doAdjacentPairs({|thisOnset, nextOnset|
//filter onsets if they are too close
if((nextOnset - thisOnset) > groupingThresh, {onsets = onsets.add(nextOnset)});
});
if(onsets[0].isNil){onsets[0] = 0};
onsetTime = onsets;
}
//get frame index at the onset time
getOnsetIndex {
onsetIndex = (onsetTime / hoptime).asInteger;
}
// Find breakpoint of a sample file by onsets.
// using lowest point in between onsets to be section breakpoints.
findBreakPointByOnsets {
var nadirArray = [];
sectionBreakPoint = [];
onsetIndex.do{|thisOnset, index|
var previousOnset = onsetIndex[index - 1];
nadirArray = nadirArray.add(rmsData[previousOnset..thisOnset].lastMinIndex + (previousOnset ?? 0));
};
sectionBreakPoint = nadirArray;
}
// Remove breakpoints if the peak amplitude inside the section does not reach the threshold
// preset to 0.2 of global peak amplitude
cleanBreakPointByOnsets{|thresh = 0.2|
var newSectionBreakPoint = [], newOnsetIndex = [], newOnsetTime = [];
var sectionSampleIndex = (sectionBreakPoint * hoptime * sampleRate * numChannels).asInteger;
sectionBreakPoint.do({|thisSection, index|
var thisSectionSampleIndex = sectionSampleIndex[index];
var nextSectionSampleIndex = sectionSampleIndex[index+1];
var thisSectionPeak = soundFileArray[thisSectionSampleIndex..nextSectionSampleIndex].abs.maxItem;
if(thisSectionPeak > (globalPeakAmp * thresh))
{
newSectionBreakPoint = newSectionBreakPoint.add(sectionBreakPoint[index]);
newOnsetIndex = newOnsetIndex.add(onsetIndex[index]);
newOnsetTime = newOnsetTime.add(onsetTime[index]);
}; // end of if(thisSectionPeak > (globalPeakAmp * thresh))
}); // end of sectionBreakPoint.do
sectionBreakPoint = newSectionBreakPoint;
onsetIndex = newOnsetIndex;
onsetTime = newOnsetTime;
}
//Separate rmsData into subsections by breakpoints.
sectionRmsDataByBreakPoint {
var output = [];
rmsDataBySection = [];
sectionBreakPoint.do{|thisSection, index|
var nextSection = sectionBreakPoint[index + 1];
output = output.add(rmsData[thisSection..nextSection]);
};
rmsDataBySection = output;
}
//find local peaks in the sectionBreakPoint
findPeaksByOnsets {
var peakArray = [], peakAmpArray = [];
var soundArrayByChannels;
peakIndex = [];
peakTime = [];
peakAmp = [];
soundArrayByChannels = soundFileArray.clump(numChannels).flop; //[[channel 1], [channel 2],....]
sectionBreakPoint.do{|thisSection, index|
var nextSection = sectionBreakPoint[index + 1];
var peakhop = rmsDataBySection[index].maxIndex + thisSection; //find the biggest rms session
var peaksInTheHop = [];
//find detailed peak time and level
numChannels.do{|channel|
var thisframe = peakhop*framehop;
var nextframe = (peakhop+1)*framehop;
var channelPeakPoint = (soundArrayByChannels[channel].abs[thisframe..nextframe].maxIndex)/framehop;
var channelPeakLevel = soundArrayByChannels[channel].abs[thisframe..nextframe].maxItem;
peaksInTheHop = peaksInTheHop.add([channelPeakPoint, channelPeakLevel]);
};
peaksInTheHop = peaksInTheHop.flop; //[peakPoints, peakLevels]
//select item with max amplitude in between channels.
peakAmpArray = peakAmpArray.add(peaksInTheHop[1].maxItem); // Peak Amps
peaksInTheHop = peaksInTheHop[0][peaksInTheHop[1].maxIndex]; // Peak Sample
peakArray = peakArray.add(peakhop + peaksInTheHop);
};
peakIndex = peakArray;
peakAmp = peakAmpArray;
globalPeakIndex = peakIndex[peakAmpArray.maxIndex];
peakTime = peakIndex * hoptime;
globalPeakTime = peakTime[peakAmp.maxIndex];
}
//Dictate attact/release time
arEnv {|startThresh, endThresh|
var startAmp;
var endAmp;
var thisSectionGlobalIndex = 0;
startIndex = [];
endIndex = [];
startTime = [];
endTime = [];
attackDur = [];
releaseDur = [];
activeDuration = [];
//for each onset section, find peaks
rmsDataBySection.do{|thisSection, sectionIndex|
var thisSectionStartIndex = sectionBreakPoint[sectionIndex];
var startAmp = startThresh * peakAmp[sectionIndex];
var endAmp = endThresh * peakAmp[sectionIndex];
//startTime:
//search for the first point pass above threshold.
//MAYBE: Use adaptative threshold method (weakest effort method) for start time detection.
//Peeters, Geoffroy. “A Large Set of Audio Features for Sound Description (Similarity and Description) in the Cuidado Project.” IRCAM, Paris, France (2004).
case
/*
//for percussive sounds, peak earlier than onset
{peakTime[sectionIndex] < onsetTime[sectionIndex]}
{
"Percussive sound, start at peak!".postln;
startTime = startTime.add(peakTime[sectionIndex]);
startIndex = startIndex.add(peakIndex[sectionIndex].asInteger);
}
{(peakTime[sectionIndex] - onsetTime[sectionIndex]) <= (framehop * 2)}
{
"Percussive sound, start at onset!".postln;
startTime = startTime.add(onsetTime[sectionIndex]);
startIndex = startIndex.add(onsetIndex[sectionIndex].asInteger);
peakTime[sectionIndex] = onsetTime[sectionIndex];
}
*/
// for less percussive sounds
{true}
{
block{|break|
thisSection.do({|rmsenergy, index|
if(rmsenergy >= startAmp)
{
var stime;
startIndex = startIndex.add(thisSectionStartIndex + index);
stime = startIndex.last * hoptime;
startTime = startTime.add(stime);
break.value(0);
};
})
};
};
//search for the last point pass below threshold.
block{|break|
var reversePeak = thisSection.reverse.maxIndex;
thisSection.reverseDo({|rmsenergy, index|
if(rmsenergy >= endAmp)
{
endIndex = endIndex.add(sectionBreakPoint[sectionIndex] + thisSection.size - index);
endTime = endTime.add(endIndex.last * hoptime);
break.value(0);
};
})
};
};
attackDur = peakTime - startTime;
releaseDur = endTime - peakTime;
activeDuration = endTime - startTime;
}
//Separate Datas into subsections by active envelopes.
//includes:
//activeRMSData
//activeCentroid
//activeDissonanace
//temporalCentorid
getActiveData {
//Datas to retrive
activeCentroidData = [];
activeNoiseData = [];
temporalCentroid = [];
activeRMSData = [];
startIndex.do({|thisStartIndex, sectionIndex|
var thisRMSData = rmsData[thisStartIndex..endIndex[sectionIndex]];
var activeTime = frameTimes[thisStartIndex..endIndex[sectionIndex]] - frameTimes[thisStartIndex];
activeRMSData=activeRMSData.add(thisRMSData);
activeCentroidData = activeCentroidData.add(centroidData[thisStartIndex..endIndex[sectionIndex]]);
activeNoiseData = activeNoiseData.add(noiseData[thisStartIndex..endIndex[sectionIndex]]);
temporalCentroid = temporalCentroid.add(
(thisRMSData*activeTime).sum / thisRMSData.sum;
)
})
}
//MFCC of a sound is decided arbitrarily to be the average MFCC data of 20 frames around the peak point
getMFCC {
var start, end;
mfcc = [];
peakIndex.do({|thisPeakIndex, sectionIndex|
thisPeakIndex = thisPeakIndex.asInteger;
if(thisPeakIndex-startIndex[sectionIndex] <= 10){start = startIndex[sectionIndex]}{start = thisPeakIndex - 10};
if(endIndex[sectionIndex]-thisPeakIndex <= 10){end = endIndex[sectionIndex]}{end = thisPeakIndex - 10};
mfcc = mfcc.add(mfccData[start..end].flop.collect({|data| data.sum/data.size}));
});
^mfcc;
}
//play the sound file, using(at) to play each onset. If (at) is larger than the last onset index, it plays a random onset.
play {arg at = nil, out = 0, server, detune = 0, pan = 0, level = 1;
var buf, rate, cond = Condition.new(false);
server = server ? Server.default;
rate = 2 ** (detune / 12);
if(buffer == [])
{server.waitForBoot{this.loadToBuffer(server, action: {cond.test = true; cond.signal;})}}
{cond.test = true; cond.signal;};
Routine.run{
cond.wait;
if(at==nil)
{buf = buffer}
{//if the index number is not in the range of section numbers, play a random section
if(at.asInteger >= 0 && at.asInteger < activeBuffer.size)
{buf = activeBuffer[at.asInteger]}
{buf = activeBuffer[activeBuffer.size.rand]}
};
if(buf.size == 1)
{{Pan2.ar(PlayBuf.ar(1, buf[0], doneAction: 2, rate: BufRateScale.ir(buf[0]) * rate), pan, level)}.play(outbus: out)}
{{Balance2.ar(PlayBuf.ar(1, buf[0], doneAction: 2, rate: BufRateScale.ir(buf[0]) * rate), PlayBuf.ar(1, buf[1], doneAction: 2, rate: BufRateScale.kr(buf[1]) * rate), pan, level)}.play(outbus: out)};
}
}
//return an envelope to represent the whole sound file
env {
var frametimes;
frametimes = Array.series(rmsData.size, 0, SS_SCMIR.framehop / sampleRate);
^Env.pairs([frametimes, rmsData].flop, \lin).duration_(duration); // Why is duration adjustment necessary?
//^Env.pairs([frameTimes, rmsData].flop, \lin);
}
//return an array of envelopes to represent each onsets.
getActiveEnv {
var envArray = [];
rmsDataBySection.do{|thisSection, sectionIndex|
var activeFrameTimes;
var activeRmsData;
activeFrameTimes = frameTimes[startIndex[sectionIndex]..endIndex[sectionIndex]] - frameTimes[startIndex[sectionIndex]];
activeRmsData = rmsData[startIndex[sectionIndex]..endIndex[sectionIndex]];
envArray = envArray.add(Env.pairs([activeFrameTimes, activeRmsData].flop, \lin));
};
activeEnv = envArray;
}
plot {
this.buffer.plot;
}
}