Inverse the audio selection from the Nyquist console

Hello,

I’m new in the forum, and I try to learn nyquist language.

I use Audacity version 3.7.1 and I’m on Windows.

I would just like to reverse the selected audio from the Nyquist console.

Could someone give me the code I need to use?

Thanks in advance for your help.

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:

(aud-do-command "Reverse")
""
1 Like

Hi Steve,

Thanks a lot, your function works like a charm.

Kind regards.

Hi Steve,

Could I have the equivalent of your previous LISP code in SAL if possible?

Thanks in advance.

I don’t do SAL.

1 Like

Hi Steve,

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);

Kind regards.

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.

1 Like

Hi Steve,

Thanks a lot, you’re perfectly right.

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);

Kind regards.