How to Generate Arbitrary Waveforms as Envelopes

Help for Audacity on Windows.
Forum rules
ImageThis forum is for Audacity on Windows.
Please state which version of Windows you are using,
and the exact three-section version number of Audacity from "Help menu > About Audacity".


Audacity 1.2.x and 1.3.x are obsolete and no longer supported. If you still have those versions, please upgrade at https://www.audacityteam.org/download/.
The old forums for those versions are now closed, but you can still read the archives of the 1.2.x and 1.3.x forums.
Post Reply
pnielsen
Posts: 12
Joined: Fri Apr 26, 2013 9:08 am
Operating System: Please select

How to Generate Arbitrary Waveforms as Envelopes

Post by pnielsen » Wed May 22, 2013 11:26 pm

I am looking for the capability to apply an arbitrary waveform envelope to an audio signal. Something more complex than what can be implemented with ADSR. Is there any way this can be accomplished in Audacity?

CoolEdit/Audition has a drag-and-drop graphical editor for this purpose, but I wouldn't mind typing in a sequence of X values. Maybe some rounding bewtween points would be useful, as well as fields to set the number of X data points, and the repetition rate of the envelope.

Any suggestions along these lines would be greatly appreciated.

Peter Nielsen

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by steve » Wed May 22, 2013 11:39 pm

If it is a relatively simple envelope, you can use the Envelope Tool: http://manual.audacityteam.org/o/man/envelope_tool.html

If you want an envelope with dozens, or even thousands of points and have a formula to generate the points, it would be possible to create an envelope using a Nyquist Script (Nyquist is a simple programming language that is built into Audacity).
A Nyquist script can either be run in the Nyquist Prompt effect (http://manual.audacityteam.org/o/man/ny ... rompt.html)
or can be made into a plug-in (http://manual.audacityteam.org/o/man/ef ... st_Effects)

An example of a simple Nyquist script that will apply a 1.0 Hz sine wave envelope to the selected track (copy and paste into the Nyquist Prompt effect to apply this to a selected track) :

Code: Select all

(mult s 0.5 (sum 1 (hzosc 1)))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

kozikowski
Forum Staff
Posts: 68902
Joined: Thu Aug 02, 2007 5:57 pm
Operating System: macOS 10.13 High Sierra

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by kozikowski » Wed May 22, 2013 11:41 pm

Audacity only has the draw tool which only works on individual samples. I think it was intended as a repair tool rather than a production generation tool.

Koz

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by steve » Wed May 22, 2013 11:55 pm

A more complex example. Try applying this to a generated tone:

Code: Select all

(setf adsr (pwlv 0 0.05 1 0.3 0.4 0.5 0.4 1 0))
(setq wiggle 0.3)
(setq wigglerate 3)

(setf envelope
  (sim (mult adsr (- 1 (* 2 wiggle)))
    (mult adsr wiggle  (sum 1 (hzosc wigglerate)))))

(mult envelope s)
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

pnielsen
Posts: 12
Joined: Fri Apr 26, 2013 9:08 am
Operating System: Please select

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by pnielsen » Thu May 23, 2013 3:33 am

The envelope tool is very useful, as is the Nyquist "env" function.

But I was thinking of something more like a look-up table, where amplitude points (Y values) are entered at preset X intervals. In other words, a manual process that does not rely upon algebraic functions. The objective is to maximize flexibility. Even a 10x40 grid would provide a workable resolution for many applications.

Useful refinements might include an optional smoothing function and user-defined repetition rate for the generated envelope.

Is something like this beyond the scope of an Audacity plug-in?

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by steve » Thu May 23, 2013 5:37 am

pnielsen wrote:Is something like this beyond the scope of an Audacity plug-in?
Designing a nice interface would be tricky, but the basics of entering a list of numbers and creating an envelope from them is pretty easy.
Nyquist provides a set of functions for creating "piecewise envelopes", which take lists of points and creates an envelope from those points. The lists are in the form of time/amplitude pairs.

As an example, there is the function PWLV which creates a "piece-wise linear envelope". "Linear" in the name indicates that the points are joined by straight lines (there is also an exponential version PWEV).
The time of the first point is assumed to be 0, but after that each point is defined as a pair of values (time, amplitude).
The following is a very simple example that rises from 0.1 at time 0, to an amplitude of 1 at time=0.5 and falls to silence at time=1

Code: Select all

(pwlv 0.1 0.5 1 1 0)
When used in an effect (or in the Nyquist Prompt effect), the times are, by default, relative to the length of the selection, so a time position of 1.0 refers to the end of the selection.

To apply the envelope, all that is required is to "multiply" the sound by the envelope.
Audacity passes the sound from the selection to Nyquist as a variable called "S".
So to apply the above envelope to a selected audio track, the following code can be used in the Nyquist Prompt effect:

Code: Select all

(mult s
  (pwlv 0.1 0.5 1 1 0))
There is also command that does the same thing, but takes a list of numbers as its input:

Code: Select all

(setf point-list (list 0.1 0.5 1 1 0))
(mult s
  (pwlv-list point-list))
The list of points (which have been given the name "point-list" in this example) could have hundreds, or even thousands of points defined.

If we just wanted to add a list of amplitudes that would then be evenly spaced along the sound that we are processing, something like this could be used:

Code: Select all

(setf amp-list (list 0.1 1 0))

(setq num (1- (length amp-list)))
(setf point-list (list (first amp-list)))
(dotimes (count num)
  (push (/ (+ 1.0 count) num) point-list)
  (push (nth (1+ count) amp-list) point-list))

(setf point-list (reverse point-list))

(print point-list)

(mult s
  (pwlv-list point-list))
where "amp-list" is out list of amplitudes (in order from left to right along the time line).
The line (print point-list) just prints out the list to the debug window so that we can check which points were used (as time/amplitude pairs). To see the debug output, click the debug button rather than the OK button.

If you were using a very long list (hundreds of points) you would probably want to disable or remove the "print" line. A code line can be disabled by putting a semicolon at the beginning of the line. This is the way that "comments" (to be ignored by the program) are added to a Nyquist script.

Code: Select all

; this line will be ignored
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Robert J. H.
Posts: 3633
Joined: Thu May 31, 2012 8:33 am
Operating System: Windows 10

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by Robert J. H. » Thu May 23, 2013 10:59 am

... and there's Steve's Text Envelope plug-in, where you can enter the values into a text field (I usually Import the values from Excel).
Smoothing and Looping are currently not supported (possible Features?).

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by steve » Thu May 23, 2013 12:33 pm

Robert J. H. wrote:... and there's Steve's Text Envelope plug-in, where you can enter the values into a text field (I usually Import the values from Excel).
Link: http://wiki.audacityteam.org/wiki/Nyqui ... t_Envelope
Robert J. H. wrote:Smoothing and Looping are currently not supported (possible Features?).
I doubt that there is enough user demand to make it a standard feature, but it would probably not be hard to add as a custom feature.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

steve
Site Admin
Posts: 80677
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: How to Generate Arbitrary Waveforms as Envelopes

Post by steve » Thu May 23, 2013 5:01 pm

Here's an idea that you may like:
  1. Create some silence in a mono track,
  2. Zoom in really close
  3. Switch to the Draw Tool and draw an envelope shape.
firsttrack000.png
firsttrack000.png (7.77 KiB) Viewed 2178 times
Select the drawn envelope and apply this code in the Nyquist Prompt to copy the points into memory:

Code: Select all

(let ((envlist '()))
  (do ((val (snd-fetch s)(snd-fetch s)))
      ((not val))
    (push val envlist))
  (putprop '*SCRATCH* envlist 'envlist))

(print "done")
4. Open, import or generate a track to apply the envelope to.
firsttrack001.png
firsttrack001.png (12 KiB) Viewed 2178 times
5. Select the track and run this code in the Nyquist prompt.

Code: Select all

;; The next line tells the code to reset (remove) the
;; envelope after processing.
;; Change to 0 if you don't want to reset.
(setq reset 1)

(setf amp-list (get '*SCRATCH* 'envlist))
(setf point-list (list (first amp-list)))

(let ((num (1- (length amp-list))))
  (dotimes (count num)
    (push (- 1.0 (/ count (float num))) point-list)
    (push (nth (1+ count) amp-list) point-list))
  (setf point-list point-list))
; (print point-list)
(when (= reset 1)
  (remprop '*SCRATCH* 'envlist))
(mult s
  (pwlv-list point-list))
firsttrack002.png
firsttrack002.png (11.15 KiB) Viewed 2178 times
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply