possible to make this plugin? ("auto draw wave")

Using Nyquist scripts in Audacity.
Post and download new plug-ins.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
N871
Posts: 96
Joined: Sat Mar 29, 2014 9:40 pm
Operating System: Please select

possible to make this plugin? ("auto draw wave")

Post by N871 » Sat Mar 29, 2014 9:55 pm

Hello,

I'm new to this forum and hoping someone can help me out with the following:
Is it possible to make a plugin in Nyquist that connects the first and last sample of a selection with a straight (or cosine-like) line (see picture).
The existence of such an effect would really make my day..I really could use a tool like this.

thank you for your time,

Nils
Attachments

[The extension tiff has been deactivated and can no longer be displayed.]


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

Re: possible to make this plugin? ("auto draw wave")

Post by kozikowski » Sun Mar 30, 2014 4:04 am

I don't think the forum likes TIFFs. Can you send a JPEG or GIF?
Koz

N871
Posts: 96
Joined: Sat Mar 29, 2014 9:40 pm
Operating System: Please select

Re: possible to make this plugin? ("auto draw wave")

Post by N871 » Sun Mar 30, 2014 9:17 am

Sure, here it is again:
Attachments
effect.jpg
effect.jpg (32.2 KiB) Viewed 4877 times

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

Re: possible to make this plugin? ("auto draw wave")

Post by steve » Mon Mar 31, 2014 12:20 am

Assuming that you are talking about very short selections, joining with a straight line is not too difficult.

Try running the following code in the "Nyquist Prompt" effect (Effect menu):

Code: Select all

(let* ((one (/ *sound-srate*))                  ; duration of one sample in seconds.
       (end-time (- (/ len *sound-srate*) one)) ; time of last sample
       (start-amp (sref s 0))                   ; 1st sample value
       (end-amp (abs-env (sref s end-time))))   ; last sample value
  (control-srate-abs *sound-srate*
    (pwlv start-amp 1 end-amp)))                ; create line.
Note that this is only for a mono track.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

N871
Posts: 96
Joined: Sat Mar 29, 2014 9:40 pm
Operating System: Please select

Re: possible to make this plugin? ("auto draw wave")

Post by N871 » Mon Mar 31, 2014 12:32 am

Steve, thank you so much. Your kind solution will certainly help me out, mono is just fine for me.
Is it possible to expand your patch to have a curve instead of a straight line? maybe by entering an equation somewhere? :)
My "dream patch" is to make a selection and let the plugin draw a single cycle of perfect sine wave.

Best,
Nils

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

Re: possible to make this plugin? ("auto draw wave")

Post by steve » Mon Mar 31, 2014 12:39 am

N871 wrote:Is it possible to expand your patch to have a curve instead of a straight line?
That would be possible, but the hard part is that if you want a smooth transition between the original waveform and the new "line", you need to be able to calculate the curve. For example, if you were to use a sine curve, then you need to calculate the initial phase, the frequency and the amplitude that will fit between the start and end points. If you know the maths and can tell me how to calculate that, then I can probably show you how to code it.
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: possible to make this plugin? ("auto draw wave")

Post by Robert J. H. » Mon Mar 31, 2014 6:04 am

steve wrote:
N871 wrote:Is it possible to expand your patch to have a curve instead of a straight line?
That would be possible, but the hard part is that if you want a smooth transition between the original waveform and the new "line", you need to be able to calculate the curve. For example, if you were to use a sine curve, then you need to calculate the initial phase, the frequency and the amplitude that will fit between the start and end points. If you know the maths and can tell me how to calculate that, then I can probably show you how to code it.
I've certainly missed something.
Could one explain what is actually needed?
I mean, a cycle is just one cycle, i.e. 2 Pi.
Thus, the sample after the selection would be the same as the one at the start of the selection.
The questions are therefore:
- must the amplitude be interpolated to match the first and last value.
- is the highest magnitude of those the max (=cosine) or...
- ...we aim for a peak value of one (with adapted phase)
- is a DC offset allowed
- is a fractional cycle allowed in the range Pi/4 <> 2 Pi (or multiples of it).

In fact, some points require at least 3 samples instead of only 2.

Let's say that we have the values 0.5 and 0 from a 10 sample selection.
This could mean
a -6 dB cosine that lasts 3/4 Pi;
a cosine with start amplitude 0.5 and end amplitude 0;
a 30° sinusoid with amplitude 1 and partial cycle;
a sine wave with 0.5 initial DC offset, amplitude arbitrary;
and so on.

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

Re: possible to make this plugin? ("auto draw wave")

Post by steve » Mon Mar 31, 2014 12:19 pm

This code allows you to generate short sections of a s sin curve within the selected region.
The code lines that start with "(setq" set the parameters for generating the curve.
If you want the curve to automatically fit to the start and end points, then you need to know how to calculate those parameters.

Code: Select all

(setq amp 0.5)      ; +/- amplitude of sine
(setq offset 0.3)   ; DC offset
(setq phase 45)     ; start phase in degrees
(setq cycles 0.5)   ; number of cycles

(sum offset
  (mult amp
    (osc (hz-to-step (/ cycles (get-duration 1)))
         1 *sine-table* phase)))


For example, for this selection:
firsttrack004.png
firsttrack004.png (7.8 KiB) Viewed 4823 times
These parameters produce a reasonably good fit:

(setq amp 0.3) ; +/- amplitude of sine
(setq offset -0.06) ; DC offset
(setq phase -85) ; start phase in degrees
(setq cycles 0.5) ; number of cycles

firsttrack005.png
firsttrack005.png (7.57 KiB) Viewed 4823 times
and so does increasing the number of cycles by 1 (setq cycles 1.5)
firsttrack006.png
firsttrack006.png (7.83 KiB) Viewed 4823 times
I determined the parameters by trial and error, which is not a very good solution.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

N871
Posts: 96
Joined: Sat Mar 29, 2014 9:40 pm
Operating System: Please select

Re: possible to make this plugin? ("auto draw wave")

Post by N871 » Mon Mar 31, 2014 1:54 pm

I must say this forum is just great, thanks again Steve, this seems to be exactly what I wanted, give me a moment to try it out and I will return with some final thoughts and a better explanation about what I'm trying to do.
Thank you Robert for your thoughts, will reply in my next message.

N871
Posts: 96
Joined: Sat Mar 29, 2014 9:40 pm
Operating System: Please select

Re: possible to make this plugin? ("auto draw wave")

Post by N871 » Mon Mar 31, 2014 2:33 pm

Steve, great stuff, before explaining why this plugin is of great use to me, could you possibly add one feature: make the "setq amp" automatically the amplitude of the first sample in the selection.
(I will always use 1 cycle, with start phase 270 degrees with DC offset 0.0)

This plugin works great for what I'm doing now (working with sine waves). In general this is what I would like to be able to in audacity (hope it's clear):

make a selection of a wave (for example a selection with a length of 11123 samples) and copy, make a new selection somewhere (for example with a length of 11248 samples), and paste the wave (overwrite! instead of insert) with an auto-stretch so the wave fits the new length without any visible changes in the drawing of the wave. In fact this is a speed change but with a very accurate starting and ending point and without much of a hassle.
I have to be honest in saying I'm not sure such a thing can be done. Stretching always seems to involve some change in wave shape. On the other hand I'm working with very high sample rates: 192kHz so the resolution is high enough, and the waveform are not that complex). But is has to be perfect (for looping reasons). ...

Post Reply