## Cubic Bezier Envelopes

widdershins
KVRist

97 posts since 22 Sep, 2008, from UK

Hello all. There's an old thread on this topic but it didn't come to much. I'm interested in drawing envelope shapes with cubic bezier curves. This is done in Logic's Ultrabeat (above, bottom right) and it works great for percussion synths. Now, I could do this by implementing the equation for bezier curves, but I'm sure that would be pretty expensive.

So, I'm wondering, does anyone have an idea of how this could be done? Ultrabeat seems to be pretty frugal with CPU, so it must be possible. Would the best approach be to approximate the curve with a series of straight line segments? Any ideas / brainstorming much appreciated.
widdershins
KVRist

97 posts since 22 Sep, 2008, from UK
One reason that I rarely post questions to forums is that I inevitably find a suitable answer very soon afterwards. After searching the web for formulae to approximate bezier curves with straight lines, I found that JUCE, which I am building my interface with, has handy methods for finding points on a Bezier curve. So now I can easily divide a curve into as many linear segments as I want.

This definitely isn't the optimum solution, because it simply divides the curve into equal-sized segments, instead of taking account of where the curves change most. This means there are some inaccuracies:

(The user won't see the red lines, those are for your benefit).

I think this should be sufficient, however. I know there are mixed feelings about JUCE on this board, but the more I use it, the more it impresses me.
Music Engineer
KVRAF

3750 posts since 8 Mar, 2004, from Berlin, Germany
are you sure that ultrabeat's envelopes are actually proper bezier-splines or is that just an assumption based on the interface? i could imagine, that they might be 5th order polynomials instead, where the values, slopes and curvatures are given at the endpoints. the slopes could be set equal to the slopes of straight lines between the endpoints and the inner control-points and the curvatures could be inversely related to the distances of the endpoints to the inner control points (such that the endpoint-slope "prolongs" longer, when the inner control-point is farther away from the respective endpoint). ...may be. but may be nonsense as well. i'm just speculating.

the problem with bezier splines was pointed out in the other thread - they give x- and y-values based on a parameter t, but we actually need a functional form where y is computed from x. to do this properly, we would have to invert x(t) to get t(x), and then use y(x) = y(t(x)). inverting the function x(t) will be troublesome since x(t) is a 3rd order polynomial, so t(x) would have to be a 3rd order polynomial root-finding problem for any given x.
widdershins
KVRist

97 posts since 22 Sep, 2008, from UK
Hmm, well that mathematics is mostly above my head, but I will say that Ultrabeat's envelopes certainly seem to work in the way they are displayed. That much is clear when you do pitch modulation (as I often do when synthesising drums). And it's a really fun, responsive way to set envelopes, which is why I'm interested in it. It allows you to easily set exponential or s-curve shapes of exactly the shape you like.

But you could right that it has nothing to do with Beziers, I just assumed that because that's what the interface resembles to me.
Music Engineer
KVRAF

3750 posts since 8 Mar, 2004, from Berlin, Germany
widdershins wrote:Hmm, well that mathematics is mostly above my head, but I will say that Ultrabeat's envelopes certainly seem to work in the way they are displayed.

yeah - i actually didn't question that. what i did question is whether or not this shape is actually a proper bezier spline or something else.

But you could right that it has nothing to do with Beziers, I just assumed that because that's what the interface resembles to me.

yes - as i described above, other functional forms besides bezier splines might be possible which could give rise to a similar control-point based interface (and similar shapes). the idea with 5th order polynomial is just my 1st shot. in reality it might something else altogether.
widdershins
KVRist

97 posts since 22 Sep, 2008, from UK
Well I've implemented an envelope gui of my own using JUCE's Bezier curves and it seems to work in almost exactly the same way visually (see above). Which leads me to believe that Ultrabeat may be approximating the Beziers as I'm now doing. I should have it working shortly, I'll let you know whether it sounds any good.
Last edited by widdershins on Sat Feb 16, 2013 7:14 pm, edited 1 time in total.
Z1202
KVRian

913 posts since 11 Apr, 2002
Personally, I have been always questioning the use of Bezier curves and other polynomial functions for audio control signals. Natural processes tend to have exponential behavior (polynomials occur rather as a particular case thereof). I think you can achieve similar to Bezier functionality using exponentials. I also believe, that Logic's automation curves are exponential based. Don't know about Ultrabeat.

Regards,
{Z}
widdershins
KVRist

97 posts since 22 Sep, 2008, from UK
Z1202 wrote:Personally, I have been always questioning the use of Bezier curves and other polynomial functions for audio control signals. Natural processes tend to have exponential behavior (polynomials occur rather as a particular case thereof). I think you can achieve similar to Bezier functionality using exponentials. I also believe, that Logic's automation curves are exponential based. Don't know about Ultrabeat.

Well, for me it's as much about the interface as the sound. Beziers offer an intuitive way for a user to get any shape of exponential or s-shaped curve without knowing or understanding either of those words. That is the appeal. I also find it much easier to internalize what's happening in my sound with a visual envelope over a series of sliders, which is in reality a very indirect way to think about a sound and its control mechanisms.

Once you have decided that the Bezier curve is a good interface, it only remains to implement it in such a way that the sound reflects the visual display to the user, and such that it isn't too costly in CPU. Hence my question
Z1202
KVRian

913 posts since 11 Apr, 2002
widdershins wrote:
Z1202 wrote:Personally, I have been always questioning the use of Bezier curves and other polynomial functions for audio control signals. Natural processes tend to have exponential behavior (polynomials occur rather as a particular case thereof). I think you can achieve similar to Bezier functionality using exponentials. I also believe, that Logic's automation curves are exponential based. Don't know about Ultrabeat.

Well, for me it's as much about the interface as the sound. Beziers offer an intuitive way for a user to get any shape of exponential or s-shaped curve without knowing or understanding either of those words. That is the appeal. I also find it much easier to internalize what's happening in my sound with a visual envelope over a series of sliders, which is in reality a very indirect way to think about a sound and its control mechanisms.

Once you have decided that the Bezier curve is a good interface, it only remains to implement it in such a way that the sound reflects the visual display to the user, and such that it isn't too costly in CPU. Hence my question
I guess then we have some kind of terminology discrepancy here IIRC, Beziers refer specifically to cubic splines as the underlying curve math. As such they can only approximate the exponentials, and my guess is that they won't do it too well at the more extreme settings. OTOH, I fully agree about the interface point. That is one could think of having exponential curves with a similar interface.

Oh, and BTW, exponentials can be implemented in the incremental fashion. So neither should be too CPU costly.
widdershins
KVRist

97 posts since 22 Sep, 2008, from UK
yes - as i described above, other functional forms besides bezier splines might be possible which could give rise to a similar control-point based interface (and similar shapes). the idea with 5th order polynomial is just my 1st shot. in reality it might something else altogether.

Sorry, misread your post last night. So you're saying that polynomials can be used to make a graphical representation similar to Beziers with control points etc? I wasn't aware of that. When I said "an intuitive way for a user to get any shape of exponential or s-shaped curve" I was referring to my own mathematical ineptitude

Oh, and BTW, exponentials can be implemented in the incremental fashion. So neither should be too CPU costly.

I will have a look into exponentials and how they might work with such an interface, thanks.
Music Engineer
KVRAF

3750 posts since 8 Mar, 2004, from Berlin, Germany
So you're saying that polynomials can be used to make a graphical representation similar to Beziers with control points etc?

i suppose so. let me flesh out my idea a bit more: let's assume, we use control-points (x1,y1), (x2,y2), (x3,y3), (x4,y4) to define an envelope segment. (x1,y1) is the startpoint, (x4,y4) is the endpoint and (x2,y2), (x3,y3) are the inner control points that determine the shape. we should assume that x1 < x2,x3 < x4 such that the term "inner" is justified. let's use a 5th order polynomial such that:

y(x) = a5*x^5 + a4*x^4 + a3*x^3 + a2*x^2 + a1*x + a0

we now want to determine the coefficients a0,a1,...,a5 such that our polynomial y(x) satisfies some constraints. we have 6 coefficients, so we need 6 constraints. the first 2 constraints are obviously that y(x) should pass through (x1,y1) and (x4,y4), so we get 2 equations:

y1 = a5*x1^5 + a4*x1^4 + a3*x1^3 + a2*x1^2 + a1*x1 + a0
y4 = a5*x4^5 + a4*x4^4 + a3*x4^3 + a2*x4^2 + a1*x4 + a0

now, let's use the slope of a straight line between (x1,y1) and (x2,y2) to determine the slope of our polynomial at (x1,y1) - such that the straight line between (x1,y1) and (x2,y2) becomes tangent to our polynomial at the startpoint. likewise, the line between (x3,y3) and (x4,y4) should be tangent to our curve at the endpoint. the slopes of the lines are given by:

s1 = (y2-y1) / (x2-x1)
s4 = (y4-y3) / (x4-x3)

the slope of our polynomial is its derivative y'(x) and given by:

y'(x) = 5*a5*x^4 + 4*a4*x^3 + 3*a3*x^2 + 2*a2*x + a1

using our desired slopes s1 and s4, we obtain 2 further constraints:

s1 = 5*a5*x1^4 + 4*a4*x1^3 + 3*a3*x1^2 + 2*a2*x1 + a1
s4 = 5*a5*x4^4 + 4*a4*x4^3 + 3*a3*x4^2 + 2*a2*x4 + a1

4 contstraints for 6 coefficients - we need yet 2 more constraints, so my idea would now be to use the distance between (x1,y1) and (x2,y2) to determine the 2nd derivative at x1 and likewise the distance between (x3,y3) and (x4,y4) to determine the 2nd derivative at x4. the 2nd derivative is the rate of change of the 1st derivative and it would be intuitive that a larger distance forces the 1st derivative to change less quickly, thereby somehow putting more "weight" or "stiffness" to the respective endpoint's slope. these distances are given by:

d1 = sqrt((x2-x1)^2 + (y2-y1)^2)
d4 = sqrt((x4-x3)^2 + (y4-y3)^2)

i would now propose to use the reciprocal of the distance as 2nd derivative:

c1 = 1 / d1
c4 = 1 / d4

this is just a first idea - the exact form of this function could be something else, as long as it is a function that decreases towards zero. the 2nd derivative of our polynomial is:

y''(x) = 20*a5*x^3 + 12*a4*x^2 + 6*a3*x + 2*a2

using our desired 2nd derivatives at the start and endpoint gives the final 2 constraints:

c1 = 20*a5*x1^3 + 12*a4*x1^2 + 6*a3*x1 + 2*a2
c4 = 20*a5*x4^3 + 12*a4*x4^2 + 6*a3*x4 + 2*a2

we now have 6 equations for our 6 polynomial coefficients. incidently (or maybe not), i once worked out a couple of formulas to solve such kinds of systems and writte a little note about it:

www.rs-met.com/documents/dsp/TwoPointHe ... lation.pdf
(edit: to use the formulas there, we identify (x1,y1) here with (x0,y0) there and (x4,y4) here with (x1,y1) there - and there i have picked the particular values x0=0 and x1=1 for convenience - you will have to transform the input argument as explained in the 1st section if your x1 and x4 are not zero and one respectively)

...take all of this with a grain of salt - it's just an idea (as in "brainstorming") and i don't know how well that approach will work in practice.
helium
KVRist

327 posts since 13 Nov, 2002, from Germany, Darmstadt
As an aside:
Z1202 wrote:IIRC, Beziers refer specifically to cubic splines as the underlying curve math.

While the cubic form seems to be particularly popular you can have Bézier curves of any degree.
widdershins
KVRist

97 posts since 22 Sep, 2008, from UK
Robin from www.rs-met.com wrote:
So you're saying that polynomials can be used to make a graphical representation similar to Beziers with control points etc?

...take all of this with a grain of salt - it's just an idea (as in "brainstorming") and i don't know how well that approach will work in practice.

Very interesting, thanks. I'll have a look at working through that.

Moderator: Moderators (Main)