Little buffer query
-
I'm running a loop over a mono sample, loaded as a buffer, and I'm outputting the amplitude every 5000 samples.
But I'm getting unexpected values as I get towards the end of the sample.
Here is the waveform
And here is an example of some of the final values (in dB) I'm getting in the output.
Interface: 21201.8ms : -2.999999518003015 Interface: 21315.2ms : -2.999999518003015 Interface: 21428.6ms : -2.999999518003015 Interface: 21542ms : -2.999999518003015 Interface: 21655.3ms : -2.999999518003015 Interface: 21768.7ms : -2.999999518003015 Interface: 21882.1ms : -2.999999518003015 Interface: 21995.5ms : -2.999999518003015 Interface: 22108.8ms : -2.999999518003015
Anyone know what's going on?
local selection = Sampler1.createSelection(""); local stepSize = 5000; for (x in selection) { local buf = x.loadIntoBufferArray(); for (i = 0; i < buf.length; i++) { for (j = stepSize; j < buf[i].length; j += stepSize) { Console.print(Engine.getMilliSecondsForSamples(j) + " : " + Engine.getDecibelsForGainFactor(buf[i].getMagnitude(j - stepSize, j))); } } }
-
@d-healey Are you reading past the array? what if
buf[i].length - j < 5000
? -
@Christoph-Hart I thought that too so tested with
Console.print(j + " : " + buf[i].length);
The last line output is:
Interface: 975000 : 976752
And since I'm never reading beyond
j
I think that should be good. Or perhaps I'm misunderstanding the question. -
@d-healey this seems wrong, the second parameter
numSamples
should be yourstepSize
(protected), not the positionj
Also you need to protect it when reaching the end as Chris mentioned
for (j = stepSize; j < buf[i].length; j += stepSize) { local position = j - stepSize; local numSamples = Math.min(buf[i].length - position, stepSize); // protect buf[i].getMagnitude(position, numSamples); }
And why not starting the loop at 0 to save some calculations
for (j = 0; j < buf[i].length; j += stepSize) { local numSamples = Math.min(buf[i].length - j, stepSize); // protect buf[i].getMagnitude(j, numSamples); }
-
@ustk Oh, you're right, I was thinking it was start samples and end samples, but it's start samples and num samples. Now I get it, thanks!
-
-