# Inverse the audio selection from the Nyquist console

**URL:** <https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096>\
**Category:** Nyquist\
**Created:** [March 4, 2025, 11:54am UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096 "2025-03-04T11:54:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![felico](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/f/cdc98d/32.png) [@felico](https://forum.audacityteam.org/u/felico)\
**Post date:** [March 4, 2025, 11:54am UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/1 "2025-03-04T11:54:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steve](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://forum.audacityteam.org/u/steve)\
**Post date:** [March 4, 2025, 1:03pm UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/2 "2025-03-04T13:03:38Z")

</div>

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.

```auto
(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](https://manual.audacityteam.org/man/nyquist_macros.html):

```auto
(aud-do-command "Reverse")
""

```

---

<div class="post-metadata">

**Author:** ![felico](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/f/cdc98d/32.png) [@felico](https://forum.audacityteam.org/u/felico)\
**Post date:** [March 4, 2025, 3:41pm UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/3 "2025-03-04T15:41:05Z")

</div>

Hi Steve,

Thanks a lot, your function works like a charm.

Kind regards.

---

<div class="post-metadata">

**Author:** ![felico](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/f/cdc98d/32.png) [@felico](https://forum.audacityteam.org/u/felico)\
**Post date:** [March 5, 2025, 7:29am UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/4 "2025-03-05T07:29:19Z")

</div>

Hi Steve,

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

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![steve](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://forum.audacityteam.org/u/steve)\
**Post date:** [March 5, 2025, 11:33am UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/5 "2025-03-05T11:33:25Z")

</div>

I don’t do SAL.

---

<div class="post-metadata">

**Author:** ![felico](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/f/cdc98d/32.png) [@felico](https://forum.audacityteam.org/u/felico)\
**Post date:** [March 6, 2025, 10:05pm UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/6 "2025-03-06T22:05:38Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![steve](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://forum.audacityteam.org/u/steve)\
**Post date:** [March 6, 2025, 10:10pm UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/7 "2025-03-06T22:10:36Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![felico](https://forum.audacityteam.org/letter_avatar_proxy/v4/letter/f/cdc98d/32.png) [@felico](https://forum.audacityteam.org/u/felico)\
**Post date:** [March 7, 2025, 7:13am UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/8 "2025-03-07T07:13:38Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.audacityteam.org/uploads/default/original/1X/81a69b20bcd548215067cfaceccc8a10bfa2d176.png) [@system](https://forum.audacityteam.org/u/system)\
**Post date:** [April 3, 2025, 11:55am UTC](https://forum.audacityteam.org/t/inverse-the-audio-selection-from-the-nyquist-console/133096/9 "2025-04-03T11:55:07Z")

</div>

Closed on April 3, 2025, 11:55am UTC

This topic was automatically closed after 30 days. New replies are no longer allowed.
