Question about EXTRACT-ABS and RMS

The following code in Nyquist Prompt…

(setf snd (noise 1234)) ; "random" length
(extract-abs 0 1 (rms snd (/ *sound-srate* 10)))

… generates 0.1sec long “sound”, not 1 sec long (as I expected).
It is as if the product of RMS function is passing to EXTRACT-ABS its Hz parameter as it’s new sound-srate.
The same way
(extract-abs 0 1 (rms snd (/ *sound-srate* 100)))
produces 0.01sec,
(extract-abs 0 1 (rms snd (/ *sound-srate* 1000)))
produces 0.001sec, etc.

What would be the best way to get 1 sec out of RMS-ed “sound”?

You are already getting 1 second.
Try running this code with the Debug button:

(setf snd (noise 1234)) ; "random" length
(setf mysound (extract-abs 0 1 (rms snd (/ *sound-srate* 10))))

(setf srate (snd-srate mysound))
(format t "Sample rate of mysound: ~a~%" srate)

(setf duration (/ (snd-length mysound  ny:all) srate))
(format t "Length of mysound is: ~a seconds" duration)

The reason that it is only 0.1 seconds when returned to the Audacity track is because the sample rate has been reduced to 1/10th of the track’s sample rate. The reason for the reduced sample rate is because the RMS function is calculating the RMS of each block of 10 samples, and returning one sample per block.

If you want to stretch the sound so that it fills one second of track time, then you need to resample it back to the sample rate of the track:

(setf snd (noise 1234)) ; "random" length
(setf mysound (extract-abs 0 1 (rms snd (/ *sound-srate* 10))))

(force-srate *sound-srate* mysound)

OK, thank you, Steve! That was also what I tried to do as a workaround. But it actually does not fix the problem.

If I run the parts of my sample code separately, I get different results – I get 1sec, as expected.

(setf snd (noise 1234))
(rms snd (/ *sound-srate* 10))


(extract-abs 0 1 *track*)

result: 1sec

Your solution with force-srate would not work because it would up-sample (linear interpolation) the 0.1 sec long signal. But I need the actual 1st second of the RMS-ed 123.4sec long (if Hz=sound-srate/10) signal.

I still have some workarounds in mind but just wanted to find out if there is a proper solution

To illustrate the problem, lets take a 20 sec faded white noise:

If I do it in two steps (first RMS, then in another prompt, EXTRACT-ABS), I get first RMS-ed 2 sec signal:
2sec RMS-ed
then 1sec with EXTRACT-ABS (the expected result):
1st sec of 2sec RMS-ed

If I would just stretch the 0.1 sec (produced by (extract-abs 0 1 (rms snd (/ sound-srate 10)))) with the force-srate (as Steve suggested), I would get a different (incorrect) result:
0.1 sec stretched to 1st sec of 2sec RMS-ed

As Steve has explained above, the RMS function passes to the following functions the new sample rate of the signal (as I suspected in my first post). Therefore, it seems, the only way to fix it would be to take into consideration this behavior when EXTRACT-ABS comes after the RMS:

Because I need the actual first second (based on the project sample rate, not based on RMS-ed signal sample rate) of the RMS-ed signal, one option would be to write it in this way:

(setf snd (noise 1234)) ; "random" length
(setf rmsRatio 10)
(setf actualSecondsNeeded 1)
(setf secondsWeNeedToPassToExtractAbs (* rmsRatio actualSecondsNeeded ))
(extract-abs 0 secondsWeNeedToPassToExtractAbs (rms snd (/ *sound-srate* rmsRatio )))

The reason that works is because:

  1. (setf snd (noise 1234)) generates 1234 seconds of audio. Assuming the track sample rate is 44100, then that is 5441940 samples.
  2. (rms snd (/ *sound-srate* 10)) returns 1 sample for each 10 samples processed. You now have 1234 seconds of audio with a sample rate of 4410 samples per second, totalling 544194 samples.
  3. The audio is returned to the track. The track has a sample rate of 44100, so the 1234 seconds of audio is squashed into 123.4 seconds. You now have 123.4 seconds of audio with a sample rate of 44100.

If, at this point, you change the track sample rate to 4410, to match the RMS sound, then the samples will be spaced out correctly and you will have the full 1234 seconds.

If you don’t change the sample rate of the track, then you have in effect increased the speed by a factor of 10.

Thank you for your help, Steve!