For short audio selections (the limit is a bit more than 1 million samples), you can read the audio into an array, reverse the data, then convert back to a sound and return the sound to Audacity.
(defun reverse-sound (sig ln)
(let* ((srate (snd-srate sig))
(ar (snd-fetch-array sig ln ln))
(ra (make-array ln)))
(do ((i 0 (1+ i))
(j (1- ln) (1- j)))
((= i ln))
(setf (aref ra j) (aref ar i)))
(snd-from-array 0 srate ra)))
; Limit to max 1 million samples
(setf snd-length (min (truncate len) 1000000))
(multichan-expand #'reverse-sound *track* snd-length)
For longer selections you would have to use Audacity’s “Reverse” effect. This can be called from Nyquist using AUD-DO or AUD-DO-COMMAND, but only in a Nyquist Macro:
Finally, I was able to make the same operation on a small selection, using SAL syntax.
Here is the code below, I hope it will be useful to other developers who wanted to do the same operation.
Thanks again Steve, because your LISP code helped me a lot.
define function reverse-sound(sig, ln)
begin
set srate = snd-srate(sig);
set ar = snd-fetch-array(sig, ln, ln);
set ra = make-array(ln);
set j = ln;
loop
for i from 0 below ln by 1
set j = j - 1;
set ra[j] = ar[i];
end;
return snd-from-array(0, srate, ra);
end;
; Limit to max 1 million samples
set snd_length = min(truncate(len), 1000000);
return reverse-sound(*track*, snd_length);
If you want to support stereo tracks, you will need to adjust your SAL code to process each channel separately. This is accomplished in the LISP syntax version using multichan-expand.
I forgot to use Multichan-Expand for stereo channels.
Here is the corrected code, always in sal:
define function reverse-sound(sig, ln)
begin
set srate = snd-srate(sig);
set ar = snd-fetch-array(sig, ln, ln);
set ra = make-array(ln);
set j = ln;
loop
for i from 0 below ln by 1
set j = j - 1;
set ra[j] = ar[i];
end;
return snd-from-array(0, srate, ra);
end;
; Limit to max 1 million samples
set snd_length = min(truncate(len), 1000000);
return multichan-expand(quote(reverse-sound), *track*, snd_length);