| Author |
Message |
sebpiq
Joined: Apr 02, 2011 Posts: 4 Location: finland
|
Posted: Sat Aug 27, 2011 1:15 am Post subject:
Normalizing the volume of short samples |
 |
|
Hi !
I am trying to normalize several samples that have different volume levels ...
For this, I am trying to calculate RMS on those samples, but even that, I don't manage to do
Here is my code :
| Code: |
SndBuf b => FFT fft =^ RMS rms => blackhole;
"sons/1.wav" => b.read;
0 => b.pos;
// set parameters
b.samples() => fft.size;
// set hann window
Windowing.hann(b.samples()) => fft.window;
// upchuck: take fft then rms
rms.upchuck() @=> UAnaBlob blob;
// advance time
fft.size()::samp => now;
// print out RMS
<<< blob.fval(0) >>>;
|
First problem, is that it always outputs "0.0" ... so what am I doing wrong ?
Second problem, I would like to do this analysis "offline", i.e., I don't want to wait for the whole file to play in real-time. So is there a way to read all samples from the file and send them to the UAna somehow ?
Third problem (yep ... that's a lot of problems), is that the correct way to perform a rms normalization (compute rms, then somehow compute an appropriate gain for normalizing the file) ? I didn't find any algorithm that I could copy ... any link would be really welcome !!!
Cheers !
Sébastien |
|
|
Back to top
|
|
 |
sebpiq
Joined: Apr 02, 2011 Posts: 4 Location: finland
|
Posted: Tue Aug 30, 2011 9:26 am Post subject:
Peak normalization |
 |
|
My samples being percussion samples, I chose a peak normalization, and it seems to work quite well. In case it interests someone, here is the code I came up with :
| Code: |
//Volume normalization functions
fun float find_peak(SndBuf buf) {
0.0 => float max;
for (0 => int i; i<buf.samples(); i++) {
if (buf.valueAt(i) > max){
buf.valueAt(i) => max;
}
}
return max;
}
fun UGen peak_normalization (float level, SndBuf buf) {
find_peak(buf) => float peak;
buf => Gain g;
level/peak => g.gain;
return g;
}
// TESTS
// init
SndBuf b => dac;
"low_volume_beat.wav" => b.read;
SndBuf b2 => dac;
"loud_beat.wav" => b2.read;
<<< "before peak normalization" >>>;
0 => b.pos;
b2.samples() => b2.pos;
b.samples()::samp + 0.5::second => now;
0 => b2.pos;
b2.samples()::samp + 0.5::second => now;
0 => b.pos;
b.samples()::samp + 0.5::second => now;
0 => b2.pos;
b2.samples()::samp + 0.5::second => now;
<<< "after peak normalization" >>>;
2::second => now;
b =< dac;
b2 =< dac;
peak_normalization(1, b) => dac;
peak_normalization(1, b2) => dac;
0 => b.pos;
b.samples()::samp => now;
0 => b2.pos;
b2.samples()::samp => now;
0 => b.pos;
b.samples()::samp => now;
0 => b2.pos;
b2.samples()::samp => now;
2::second => now;
|
Cheers,
Sébastien |
|
|
Back to top
|
|
 |
|