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

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
effect.tiff (66.7 KB)

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

Sure, here it is again:
effect.jpg

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

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

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? :slight_smile:
My “dream patch” is to make a selection and let the plugin draw a single cycle of perfect sine wave.

Best,
Nils

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.

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.

(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
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
and so does increasing the number of cycles by 1 (setq cycles 1.5)
firsttrack006.png
I determined the parameters by trial and error, which is not a very good solution.

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.

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). …

I’m not sure that I understand your use case, but the requested modification is very simple.
Just change (setq amp 0.5) to (setq amp (snd-fetch s))

So then you have:

(setq amp (snd-fetch s))  ; set amplitude = first sample
(setq offset 0.)          ; DC offset
(setq phase -270)         ; start phase in degrees
(setq cycles 1.0)         ; number of cycles

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

(Reminder for other readers: this is for short mono tracks only)

thank you Steve, very, very small request : )
Is it possible to do the same for the other basic waveforms? (triangular, square,…)
Thanks again for your nice work!

This is what I’m trying to do:
I have made some samples which need to be looped. Unfortunately for very complex reasons,
all my recordings need to have the exact same length. Therefor I had to stretch them a bit to
make them all end with a full cycle. Unfortunately the stretching messed up the first and last
wave. So now I have to draw them manually, I can’t copy/paste because the length of the “glitch”
is not exactly the same as a cycle. Because some of the samples are very sine-like I get clicks and
pops very easily if the glitch isn’t “filled in” correctly.

There are two additional wave forms that can be integrated into Steve’s code:
Just replace sine-table in the last line with either tri-table or saw-table.

I’ve still the impression that your procedure is unnecessarily complicated.
How do you accomplish this audio stretching?

The procedure is complicated because I’m trying to make virtual oscillators
out of a series of chromatic samples in the max msp language. The original
oscillators are too complex to mock starting from a digital oscillator. I ended
up using the 2d.wave object should you be familiar with it by any chance.

I used the flex speed stretch in logic X. It did a perfect job except for the fact
that my samples are not longer loopable. Well, most still are, but the sine-like
tones are giving pops.

All oscillators are round off a bit towards the top (filtered) so the higher notes are
the most troublesome.

Each of my recordings exists of exactly 300 cycles, 299 are ok, I just need to fix one :slight_smile:
Unfortunately there are over 500 recordings…which is why I’m searching for a practical plugin.

There’s probably/hopefully an easier way to do this. Could you post one of the problematic examples for us to have a look at?
Assuming that they are under 1 MB you should be able to upload one or two of them using the “upload attachment” feature below the message composing box.

I’ll upload some samples as soon as I get home. Thanks again for your help :slight_smile:
This is by far the most enthousiastic forum I participated in so far!

the files are both around 1.5MB…but you can download them here:

http://we.tl/RNICGCPJps
http://we.tl/rujmHAeBPl

the first one is a kind of rectangular wave
the second one is a sine wave.

I have cut them so that the glitch is now in the middle and they can be looped correctly.
thanks again,
Nils

It would probably be have been easier if you’d made the samples a bit longer than necessary so that you could have just trimmed the ends to get the right length. However, why not just trim the deformed cycle off, then copy and paste a good one in its place? If you need to tweak the overall length but want to keep the same number of cycles, use the Sliding Time Scale / Pitch Shift effect and enter the same % for each of start/final for Pitch and Tempo.

Hi steve, thanks again for your reply,
Unfortunately your ideas won’t work. I can’t change the length of the recording, they all
need to have the same length up to the sample to work well in my max msp program. The
size of the glitch is not equal to the length of a cycle. The smallest imperfection results immediately in
clicks/pops. Your sine wave patch works well, I just need to change the parameters for each sample.