Skip to content

Commit

Permalink
fix getKeynum bug in SampleDescript
Browse files Browse the repository at this point in the history
  • Loading branch information
allenloves committed Feb 7, 2018
1 parent 891334a commit 9ea8bdc
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 30 deletions.
112 changes: 108 additions & 4 deletions GUI.sc
Expand Up @@ -251,23 +251,127 @@



//********************
// GUI for Sampler
// Provides loading and assinging pitches to samples
//********************
+ Sampler {

gui {
var win;
var win, column;
var sampleItem = [];
var colorSet1 = [Color(0.639,0.537,0.463), Color(0.953,0.894,0.847),Color(0.78,0.694,0.627),Color(0.498,0.4,0.325),Color(0.38,0.267,0.184)];
var colorSet2 = [Color(0.639,0.576,0.463), Color(0.953,0.918,0.847),Color(0.78,0.725,0.627),Color(0.498,0.435,0.325),Color(0.38,0.31,0.184)];
var colorSet3 = [Color(0.627,0.455,0.463), Color(0.937,0.831,0.835),Color(0.765,0.616,0.624),Color(0.49,0.318,0.325),Color(0.373,0.18,0.188)];
var dragHandler = {this.load(View.currentDrag.value.pathMatch); win.refresh; this.gui; win.close;};

win = Window.new("Sound Descripter", Rect(140, 800, 1100, 200))


win = Window.new(("Sampler__" ++ this.name).asString, Rect(140, 800, 1100, 900), scroll: true)
.front.alwaysOnTop_(true)
.background_(colorSet1[2]);
.background_(colorSet1[4])
;


DragSink(win, win.view.bounds)
.background_(Color(alpha: 0))
.resize_(5)
.receiveDragHandler_(dragHandler)
;

win.view.decorator = FlowLayout( win.view.bounds, 10@10, 5@5 );


// For Column Texts
column = FlowView.new(win.view, (win.view.bounds.width - 20)@50);
StaticText(column.view, 750@50).string_("Sound Sample ___ Section").align_(\center).font_(Font(size: 20, bold: true)).stringColor_(colorSet1[1]);
StaticText(column.view, 100@50).string_("Sample Pitch").align_(\center).font_(Font(size: 20, bold: true)).stringColor_(colorSet1[1]);
StaticText(column.view, 100@50).string_("Respond Low").align_(\center).font_(Font(size: 20, bold: true)).stringColor_(colorSet1[1]);
StaticText(column.view, 100@50).string_("Respond High").align_(\center).font_(Font(size: 20, bold: true)).stringColor_(colorSet1[1]);


//For each samples, there is a button indicating filename,
//followed by anchored picth and responsing key ranges.
samples.do{|thisSample, index| // index is the index of samples
thisSample.keynum.do{|thisKeynum, idx| //idx is the sections in a sample
var thisLine;
var rangeBox = [];
var file = PathName(thisSample.filename);
var fileText = file.folderName ++ "/" ++ file.fileName ++ "___" ++ (idx+1).asString;

thisLine = FlowView.new(win.view, (win.view.bounds.width - 20)@50).background_(Color.rand);

//for filename
Button(thisLine.view, 750@50)
.states_([[fileText, colorSet1[4], colorSet1[1]]])
.action_({thisSample.play(idx)})
.receiveDragHandler_(dragHandler)
.font_(Font(size: 14, bold: true))
;

//for anchored keynumber
NumberBox(thisLine.view, 100@50)
.value_(thisKeynum)
.align_(\center)
.background_(colorSet1[1])
.font_(Font(size: 14, bold: true))
.stringColor_(colorSet1[4])
.receiveDragHandler_(dragHandler)
.maxDecimals_(2)
.action_({|input|
var key = input.value;
if(key.isNumber){
thisSample.keynum.put(idx, key);
}
}
;
);

//for key ganges
2.do{|ix|
NumberBox(thisLine.view, 100@50)
.value_(keyRanges.at(thisSample)[idx][ix])
.align_(\center)
.background_(colorSet1[1])
.font_(Font(size: 14, bold: true))
.stringColor_(colorSet1[4])
.receiveDragHandler_(dragHandler)
.maxDecimals_(2)
.action_({|input|
var key = input.value;
if(key.isNumber)
{
keyRanges.at(thisSample)[idx].put(ix, key);
}
});
}

}//End of thisSample.keynum.do
};//End of samples.do

}
}




/*
+ SamplerDB {
gui {
var win, column;
var sampleItem = [];
var colorSet1 = [Color(0.639,0.537,0.463), Color(0.953,0.894,0.847),Color(0.78,0.694,0.627),Color(0.498,0.4,0.325),Color(0.38,0.267,0.184)];
var colorSet2 = [Color(0.639,0.576,0.463), Color(0.953,0.918,0.847),Color(0.78,0.725,0.627),Color(0.498,0.435,0.325),Color(0.38,0.31,0.184)];
var colorSet3 = [Color(0.627,0.455,0.463), Color(0.937,0.831,0.835),Color(0.765,0.616,0.624),Color(0.49,0.318,0.325),Color(0.373,0.18,0.188)];
var dragHandler = {this.load(View.currentDrag.value.pathMatch); win.refresh; this.gui; win.close;};
win = Window.new(("Sampler__" ++ name).asString, Rect(140, 800, 900, 1200), scroll: true)
.front.alwaysOnTop_(true)
.background_(colorSet1[4])
;
}
}
}
*/
4 changes: 2 additions & 2 deletions SampleDescript.sc
Expand Up @@ -47,7 +47,7 @@ SampleDescript{

// **** local information by Onsets *****
// Temporal information
var <>keynum;
var <> keynum;
var <activeDuration;
var <mfcc;
var <onsetTime;
Expand Down Expand Up @@ -333,7 +333,7 @@ SampleDescript{
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;
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
Expand Down
39 changes: 20 additions & 19 deletions Sampler.sc
Expand Up @@ -10,15 +10,15 @@ Sampler {
classvar < allSampler;
classvar <> defaultTexture;
classvar <> defaultOutputBus = 0;
classvar <> defaultLoadingServer;

var <dbs; // an array of SamplerDB instances that this Sampler is registered to.
var <name; //Name of this sampler
var <filenames;
var <samples; // samples are SampleDescript instances
var <bufServer;
// |------ Each Sample -------------| |-----Each Sample---
// |--Section---| |--Section---| |--Section---|
var <keyRanges;// in format [[[upper, lower], [upper, lower]..], [[upper, lower], [upper, lower]...], ....]
// |- section -| |- section -|
var <keyRanges;// Is a dictionary in format (SampleDescrtipt -> [[lower, upper], [lower, upper],..], ..)

//Sampler metadata
var <numActiveBuffer;
Expand All @@ -29,8 +29,8 @@ Sampler {
var <kdTreeNode; // temporarily setting to be [averageDuration, averageTemporalCentroid, averageMFCC].flat

// Initialization in this class is in SamplerInstruments.sc
// *initClass {
// }
*initClass {
}

*new{arg samplerName, dbname = \default;

Expand All @@ -55,8 +55,8 @@ Sampler {
thisSample.wait.wait;
thisSample.play(args, synthID);

};
}
};
}
}


Expand Down Expand Up @@ -130,7 +130,7 @@ Sampler {

//============================
//load and analyze sound files
load {arg soundfiles, server = Server.default, filenameAsKeynum = false, normalize = false, startThresh=0.01, endThresh=0.01, action = nil;
load {arg soundfiles, server = this.class.defaultLoadingServer, filenameAsKeynum = false, normalize = false, startThresh=0.01, endThresh=0.01, action = nil;
averageMFCC = averageMFCC ? Array.fill(13, 0);
if(soundfiles.isArray.not){Error("Sound files has to be an array").throw};
bufServer = server;
Expand Down Expand Up @@ -162,6 +162,7 @@ Sampler {
dbs.do{|thisDB| thisDB.makeTree};

this.setKeyRanges;
//finalAction.value;
}
}

Expand Down Expand Up @@ -194,29 +195,29 @@ Sampler {
//=================================================================
//
setKeyRanges{arg strategy = \keynumRadious, infoArray = [5];
keyRanges = [];
keyRanges = keyRanges ? Dictionary.new;
switch(strategy.asSymbol,
\keynumRadious,{//given a range radious from the keynum of each sample sections.
samples.do{|thisSample, index|
var radious = infoArray.asArray.wrapAt(index);
keyRanges = keyRanges.add([(thisSample.keynum - radious).thresh(0), thisSample.keynum + radious].flop)
keyRanges = keyRanges.add(thisSample -> [(thisSample.keynum - radious).thresh(0), thisSample.keynum + radious].flop)
};
},
\fullRange,{//every sample is responded in full range of midi key number.
samples.do{|thisSample, index|
var sectionRanges = [];
thisSample.keynum.do{|thisSection, idx|
sectionRanges = sectionRanges.add([0, 127]);
var rangeArray = [];
thisSample.keynum.size.do{
rangeArray.add([0, 127]);
};
keyRanges = keyRanges.add(sectionRanges);
}
keyRanges = keyRanges.add(thisSample -> rangeArray);
};
},
\keynumOnly,{//only respond to the keynum
samples.do{|thisSample, index|
var thisKeynum = thisSample.keynum;
keyRanges = keyRanges.add([thisKeynum, thisKeynum].flop)
}
}
keyRanges = keyRanges.add(thisSample -> [thisKeynum, thisKeynum].flop)
};
};
)
}

Expand Down Expand Up @@ -283,7 +284,7 @@ Sampler {
var texture = env.at(elapsed).linlin(0, env.levels.maxItem, 1, maxtexture).asInteger;
//args.set(syncmode: \percussive, amp: env.at(elapsed), texture: texture);
//this.playArgs(args);
this.key(keynums.asArray.choose, \percussive, dur: dur, amp: env.at(elapsed) * amp, pan: pan, texture: texture, out: out, midiChannel: midiChannel);
this.key(keynums.asArray.choose, \percussive, dur: min((dur ? 1), 0.2), amp: env.at(elapsed) * amp, pan: pan, texture: texture, out: out, midiChannel: midiChannel);
elapsed = elapsed + delayTime;
delayTime.wait;
}
Expand Down
1 change: 1 addition & 0 deletions SamplerInstruments.sc
Expand Up @@ -3,6 +3,7 @@
*initClass{

allSampler = IdentityDictionary.new;
defaultLoadingServer = Server.default;

//from Halim Beere and Henrich Taube
StartUp.add({
Expand Down
9 changes: 5 additions & 4 deletions SamplerQuery.sc
Expand Up @@ -28,16 +28,17 @@ SamplerQuery {
keyNum = keyNum.abs;

//find keyNums in the keyRanges of each sample sections, send the sample section information
sampler.keyRanges.do{|thisSample, index|
thisSample.do{|thisSection, idx|
sampler.keyRanges.keysValuesDo{|thisSample, thisKeyRange|
// for each section in the sample
thisKeyRange.do{|thisSection, idx| //idx is the section indes within the sample
if((keyNum <= thisSection[1]) && (keyNum >= thisSection[0]))
{

samplePrep.sample = sampler.samples[index];
samplePrep.sample = thisSample;
samplePrep.samplerName = sampler.name;
samplePrep.duration = args.dur;

samplePrep.setRate(2**((keyNum - sampler.samples[index].keynum[idx])/12) * (keySign + 1 - keySign.abs));
samplePrep.setRate(2**((keyNum - thisSample.keynum[idx])/12) * (keySign + 1 - keySign.abs));
samplePrep.section = idx;
samplePrep.buffer = samplePrep.sample.activeBuffer[samplePrep.section];
samplePrep.midiChannel = args.midiChannel;
Expand Down
6 changes: 5 additions & 1 deletion Testcode/Samplertestcode.scd
Expand Up @@ -8,7 +8,10 @@ s.waitForBoot{
// play a note by midi key number
t.key(81);
// play random note
t.key;
t.key(60);
t.gui;
t.samples[1].keynum;
t.keyRanges;

t.key(0, amp: 2.5);

Expand Down Expand Up @@ -131,6 +134,7 @@ f = Sampler(\cage, \speak).load([Quarks.folder ++ "/SuperSampler/Testcode/Sounds

f.keynums;
f.setKeynums([[20, 30, 40, 50, 60, 70, 80, 90, 100]]);
f.gui;

~cageenv = f.samples[0].env.normalize(1);
~cageenv.plot;
Expand Down

0 comments on commit 9ea8bdc

Please sign in to comment.