FluidNormalize: review example

nix
Gerard 6 years ago
parent 26b8fcdbe1
commit b2338ed9d9

@ -68,66 +68,59 @@ s.boot;
~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav"; ~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav";
~raw = FluidDataSet(s,\norm_help_raw); ~raw = FluidDataSet(s,\norm_help_raw);
~norm = FluidDataSet(s,\norm_help_normd); ~norm = FluidDataSet(s,\norm_help_normd);
~audio = Buffer.read(s,~audiofile);
~pitch_feature = Buffer.new(s); ~pitch_feature = Buffer.new(s);
~stats = Buffer.new(s); ~stats = Buffer.alloc(s, 7, 2);
~datapoint = Buffer.alloc(s,2);
~normalizer = FluidNormalize(s); ~normalizer = FluidNormalize(s);
) )
// Do a pitch analysis on the audio, which gives us pitch and pitch confidence (so a 2D datum) // Load audio and run a pitch analysis, which gives us pitch and pitch confidence (so a 2D datum)
(
~audio = Buffer.read(s,~audiofile);
FluidBufPitch.process(s,~audio, features: ~pitch_feature);
)
// Divide the time series in to 10, and take the mean of each segment and add this as a point to // Divide the time series in to 10, and take the mean of each segment and add this as a point to
// the 'raw' FluidDataSet // the 'raw' FluidDataSet
( (
~raw.clear; {
~norm.clear; var trig = LocalIn.kr(1, 1);
FluidBufPitch.process(s,~audio,features:~pitch_feature,action:{ var buf = LocalBuf(2, 1);
"Pitch analysis.complete. Doing stats".postln; var count = PulseCount.kr(trig) - 1;
fork{
var chunkLen = (~pitch_feature.numFrames / 10).asInteger; var chunkLen = (~pitch_feature.numFrames / 10).asInteger;
10.do{ |i| var stats = FluidBufStats.kr(
s.sync; FluidBufStats.process(s,~pitch_feature,startFrame:i*chunkLen,numFrames:chunkLen,stats:~stats, action:{ source: ~pitch_feature, startFrame: count * chunkLen,
~stats.loadToFloatArray(action:{ |statsdata| numFrames: chunkLen, stats: ~stats, trig: trig
[statsdata[0],statsdata[1]].postln; );
~datapoint.setn(0,[statsdata[0],statsdata[1]]); var rd = BufRd.kr(2, ~stats, DC.kr(0), 0, 1);// pick only mean pitch and confidence
s.sync; var wr1 = BufWr.kr(rd[0], buf, DC.kr(0));
("Adding point" ++ i).postln; var wr2 = BufWr.kr(rd[1], buf, DC.kr(1));
~raw.addPoint(i,~datapoint); var dsWr = FluidDataSetWr.kr(\norm_help_raw, buf: buf, trig: Done.kr(stats));
}) LocalOut.kr( Done.kr(dsWr));
}); FreeSelf.kr(count - 9);
if(i == 9) {"Analysis done, dataset ready".postln} }.play;
}
}
});
) )
//Fit the FluidNormalizer to the raw data, and then apply the scaling out of place into
//our second FluidDataSet, so we can compare. // Normalize and load to language-side array
//Download the dataset contents into arrays for plotting
( (
~normalizer.fit(~raw);
~normalizer.transform(~raw,~norm);
~rawarray = Array.new(10); ~rawarray = Array.new(10);
~normedarray= Array.new(10); ~normedarray= Array.new(10);
fork{ ~normalizer.fitTransform(~raw,~norm, {
10.do{|i| ~raw.dump{|x| 10.do{|i|
~raw.getPoint(i,~datapoint,{ ~rawarray.add(x["data"][i.asString])
~datapoint.loadToFloatArray(action:{|a| ~rawarray.add(Array.newFrom(a))}) }};
}); ~norm.dump{|x| 10.do{|i|
s.sync; ~normedarray.add(x["data"][i.asString])
~norm.getPoint(i,~datapoint,{ }};
});
~datapoint.loadToFloatArray(action:{|a| ~normedarray.add(Array.newFrom(a))})
});
s.sync;
if(i==9){"Data downloaded".postln};
}
}
) )
//Plot side by side. Before normalization the two dimensions have radically different scales //Plot side by side. Before normalization the two dimensions have radically different scales
//which can be unhelpful in many cases //which can be unhelpful in many cases
( (
~rawarray.flatten(1).unlace.plot("Unnormalized",Rect(0,0,400,400),minval:0,maxval:[5000,1]).plotMode=\bars; ~rawarray.flatten(1).unlace.plot("Unnormalized",Rect(0,0,400,400),minval:0,maxval:[5000,1]).plotMode=\bars;
~plot2 = ~normedarray.flatten(1).unlace.plot("Normalized",Rect(410,0,400,400)).plotMode=\bars; ~plot2 = ~normedarray.flatten(1).unlace.plot("Normalized",Rect(410,0,400,400)).plotMode=\bars;
) )
:: ::

Loading…
Cancel
Save