;control norm-level "Maximum output volume [percent]" int "[Normalization level]" 95 20 99 ; function to get peak amplitude for normalization (defun my-peak (signal) (if (arrayp signal) (max (peak (aref signal 0) ny:all) (peak (aref signal 1) ny:all)) (peak signal ny:all) ) ; end if ) ; end defun my-peak ; function to 'squish' the signal to 1 million samples ; if sample number is greater (defun squish (signal sound-samples) (if (arrayp signal) (vector ; left channel (force-srate *sound-srate* (stretch-abs (float (/ 1000000.0 sound-samples)) (sound (aref signal 0)))) ; right channel (force-srate *sound-srate* (stretch-abs (float (/ 1000000.0 sound-samples)) (sound (aref signal 1))))) ; mono (force-srate *sound-srate* (stretch-abs (float (/ 1000000.0 sound-samples)) (sound signal))))) ; function to normalize the signal, ; whether it's been 'squished' or otherwise. ; there's an assumption here that the 'squished' signal ; retains enough of peaks so that clipping will not occur. (defun squish-normalize (norm-level signal) ; determine number of samples in generated signal (setf sound-samples (if (arrayp signal) (snd-length (aref signal 0) ny:all) (snd-length signal ny:all) ) ; end if ) ; end setf sound-samples ; convert norm-level from integer to real value (setf norm-level (* norm-level 0.01)) ; (cond ((<= sound-samples 1000000) ; <= 1 million samples in signal (scale (/ norm-level (my-peak signal)) signal) ) ; end <= 1 million samples ; (t ; > 1 million samples (let ((pk (my-peak (squish signal sound-samples) ; force signal to be condensed to 1 million samples ) ; end my-peak )) ; end pk definition (setf amp (/ norm-level pk)) ) ; end let (scale amp signal) ) ; end t ) ; end cond ) ; end defun #| comment - use this way: (squish-normalize norm-level {Nyquist effect or generate code output} ) end of comment |#