Page 1 of 5

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

Posted: Sat Mar 29, 2014 9:55 pm
by N871
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

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

Posted: Sun Mar 30, 2014 4:04 am
by kozikowski
I don't think the forum likes TIFFs. Can you send a JPEG or GIF?
Koz

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

Posted: Sun Mar 30, 2014 9:17 am
by N871
Sure, here it is again:

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

Posted: Mon Mar 31, 2014 12:20 am
by steve
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.

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

Posted: Mon Mar 31, 2014 12:32 am
by N871
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

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

Posted: Mon Mar 31, 2014 12:39 am
by steve
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.

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

Posted: Mon Mar 31, 2014 6:04 am
by Robert J. H.
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.

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

Posted: Mon Mar 31, 2014 12:19 pm
by steve
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 4824 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 4824 times
and so does increasing the number of cycles by 1 (setq cycles 1.5)
firsttrack006.png
firsttrack006.png (7.83 KiB) Viewed 4824 times
I determined the parameters by trial and error, which is not a very good solution.

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

Posted: Mon Mar 31, 2014 1:54 pm
by N871
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.

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

Posted: Mon Mar 31, 2014 2:33 pm
by N871
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). ...