# [SOLVED] Irregular Sine Waves

Discussion and feedback on Construct 2

### » Tue Nov 03, 2015 12:36 am

@tulamide
@QuaziGNRLnose
I had a go at quazi's idea here:
https://dl.dropboxusercontent.com/u/542 ... ordan.capx

It was getting to tedious to get a nice formula from the equations, so I ended up putting all the formulas into a matrix and used guass-jordan to solve as a system of equations. Every time I change "m" I use that to recalculate the coefficients.

Using an x^3 polynomial wasn't working right. It looked good when "m" was centered, but the amplitude would get crazy otherwise and the shape would lean. It ended up needing a x^4 polynomial, or 10 coefficients. For 10 unknowns I needed 10 equations: Three equations per piece (S0(x) and S1(x)) for the points. Two for the single derivatives between the pieces and two more for the double derivatives. That mess of formulas is in the bottom of js.txt.

Anyways hat's off to quazi for the idea. Testing it it seems the limit for m is 0.5 to 1.5, beyond that the shape gets crazy.
B
100
S
38
G
134
Posts: 5,552
Reputation: 85,317

### » Tue Nov 03, 2015 7:55 am

@R0J0hound
@QuaziGNRLnose

ROJO you nailed it! The wave behaves exactly as it should inbetween the limits. I will now take a closer look at js.txt
Thank you both, ROJO and Quazi, for your effort and a solution to a more complex problem than I thought.

I did a Spline class once for Ruby (an interpreter oop language), and I can replicate the behaviour only to a degree with it (unless I change the controls). Unfortunetly I did it with cubic interpolation from spot to spot instead of polynomials (which I never really got used to), so this is really, really helping me a lot!
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Mon Nov 09, 2015 7:17 pm

QuaziGNRLnose wrote:To be honest i have no idea what it'll sound like so if you work it out i'd be interested to hear I assume it'll just start to exemplify strange harmonics as you shift the center, since this is what it'll be doing to the Fourier series

@QuaziGNRLnose and @R0J0hound

It was such a joy when I heard the oscillator playing for the first time! I made a quick mp3. No pre-editing, no post-editing, no mastering. What you hear is just one SWM-Sine-Osc, whose "center" is modulated by hand as well as by two serial LFOs. No Filters, no effects, nothing but pure Osc-Sound. The modulation does exactly what Rojo demonstrated in his capx (although the underlying dsp-code is somewhat different).

So much smoother and musically way better controllable than, for example, FM, and on my 7 year old pc the cpu load was between 0.3 and 0.7% during the sequence you hear in the mp3. Awesome! I'm in love with that oscillator.

Currently started working on a VSTi to see what else I can get out of those OSCs...

First impression.mp3

btw, SWM stands for Slope Width Modulation. But that doesn't perfectly describe it. If anyone has a better name for it, please tell!

(And sorry that this has gone beyond C2)
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Mon Nov 09, 2015 8:50 pm

tulamide wrote:
QuaziGNRLnose wrote:To be honest i have no idea what it'll sound like so if you work it out i'd be interested to hear I assume it'll just start to exemplify strange harmonics as you shift the center, since this is what it'll be doing to the Fourier series

@QuaziGNRLnose and @R0J0hound

It was such a joy when I heard the oscillator playing for the first time! I made a quick mp3. No pre-editing, no post-editing, no mastering. What you hear is just one SWM-Sine-Osc, whose "center" is modulated by hand as well as by two serial LFOs. No Filters, no effects, nothing but pure Osc-Sound. The modulation does exactly what Rojo demonstrated in his capx (although the underlying dsp-code is somewhat different).

So much smoother and musically way better controllable than, for example, FM, and on my 7 year old pc the cpu load was between 0.3 and 0.7% during the sequence you hear in the mp3. Awesome! I'm in love with that oscillator.

Currently started working on a VSTi to see what else I can get out of those OSCs...

First impression.mp3

btw, SWM stands for Slope Width Modulation. But that doesn't perfectly describe it. If anyone has a better name for it, please tell!

(And sorry that this has gone beyond C2)

Yo,

I
m bit confused now: do you mean you've created synth inside c2?
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
93
S
30
G
22
Posts: 1,987
Reputation: 20,203

### » Mon Nov 09, 2015 9:04 pm

@tulamide
Hey that sounds pretty cool.

@megatronx
I guess he could by generating it on the fly with webauido, but from the sounds of it he's using something else to program directly with a dsp chip on the soundcard.
B
100
S
38
G
134
Posts: 5,552
Reputation: 85,317

### » Tue Nov 10, 2015 2:35 pm

@megatronx
What ROJO said. Last time I asked about webaudio, Ashley stated that he will only use the parts that are compatible with all supported platforms. That also means it isn't possible to build a synth in C2 (yet?). Those with javascript knowledge might be able to, but I'm not one of those.
The dsp was done in another program, using SSE and a special coding that allows to do calculations per sample at samplerate.

If any of the fellow musicians is interested in the VSTi, when it is done, just drop a line. I'll pm you then when ready (may take months/half a year/a year).
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Tue Nov 10, 2015 5:15 pm

@tulamide
plugin-jsfxr_t127653
But I don't know if it has the level of features you're looking for.
B
100
S
38
G
134
Posts: 5,552
Reputation: 85,317

### » Fri Nov 13, 2015 1:14 am

I know you have already solved it, but here's my take on it:

https://dl.dropboxusercontent.com/u/7871870/construct/prr-sine-distortion-A03.capx
(uses Rojo's Canvas plugin)

The solution of the first layout distorts a sine wave, offsetting each X proportionally to an interpolated linear function that goes from 0 on the edges to 1 at the center. The interpolation power can be controlled by the bottom slider. It can easily be transformed into a single function but it would get long and hard to read. It also gives a seamless tilling.

In the second layout I tested just interpolating a "sine wave skeleton" to see if it could work. It doesn't tile so well when distorted, but it's interesting that you can adjust the interpolation on the fly to go from a triangle wave to a rounded square, passing through a sine. The downfall is that with too much center offset it kinda breaks the smaller spike, but maybe if a damping is added to not allow the smaller spike to approximate too much from the edge it could give better results. It also uses conditionals to generate the base skeleton, so it's not easily transformed into a single function, but it could be done.

*Just to clarify that I was lazy and the graph is upside down, with top-left being (0,0) and bottom right (1,1).
Scirra Employee
B
173
S
55
G
17
Posts: 711
Reputation: 18,527

### » Sun Nov 15, 2015 6:56 pm

@ROJOhound
Thanks a lot for the link. Very cool approach there. But it is basically prerendering sound for an octave and then triggers the according note-sound. A runtime calculation of the sound (to be able to modulate it), is not possible with it. Still, it is a cool addition to C2!

@Animmaniac
I'm impressed! You are more lightweight in terms of processing, but generating good results. You're right with breaking the smaller spike, but considering the smaller cpu footprint it is probably acceptable.

The calculations used in the wavecycle have something special. After that talented guy made a first impressive demo, I insisted (yes, I can be that annoying) that all spikes in either direction should always behave convex (as in "bend to the outer sides"). And he did it. Even at extreme offsets it stays intact. Here are some screens taken from a scope while playing the osc (you see approx. 2 cycles):

wave at offset 0.0

wave at offset -0.5

wave at offset +0.5

wave at offset +0.95

This behaviour is the reason for that warm sound even when the harmonics kick in. It is done in two parts. The second part is called "sine approximation", but it is pure assembler code, so I can't tell how it is approximated:
Code: Select all
`streamin x;   // -1...+1streamout cos1x;/* minimum harmonic distortion */float c1=-6.280099538;float c2=41.10687865;float c3=-74.00457656;float FMP25=-0.25;float FP25=.25;int abs=2147483647;movaps xmm0,x;mulps xmm0,FP25;subps xmm0,FP25;andps xmm0,abs;addps xmm0,FMP25;movaps xmm1,xmm0;mulps xmm1,xmm1;movaps xmm2,c3;mulps xmm2,xmm1;addps xmm2,c2;mulps xmm2,xmm1;addps xmm2,c1;mulps xmm2,xmm0;movaps cos1x,xmm2;`

The first part, however, is in a C-like DSP code. These calculations are done for each sample point at sample rate (the rate can vary depending on the DAW, but most will use 44.1kHz):

Code: Select all
`streamin f;streamin m;streamout wav;float ramp;float ma,mb,a,b,f2,mm1;float abs;   // bitmaskstage(0){  abs  = 3.4e38|0.999999|0.1;  ramp = 1 - m - 2*f;}stage(2){  hop(32){      // some abreviations to speed up the iteration    f2  = 2*f;   mm1 = m - 1;   ma  = 1/(1 + m);   mb  = 1/(1 - m);   b   = (1 - (abs&m))/(1 + (abs&m));   a   = 1 - b;  }  ramp = ramp + f2;  ramp = ramp - 4&(ramp>2);                           // ramp (-2...+2)  wav  = abs&ramp + mm1;                           // biased triangle (m-1...m+1)  wav  = wav*ma&(wav>0) + wav*mb&(wav<0);                // "PWM"-triangle (-1...+1)  wav  = wav + (wav/(a*(abs&wav) + b) - wav)&((m*wav)>0);   // rational mapping of long halfwave}`

stage(0) is like an initialize-procedure. It is only executed once per new note. stage(2) is executed on each sample point. hop(32) means that all enclosed code is only calculated every 32 sample points. The streamin f is an input which is a mixture of frequencies for each note playing. The streamin m is the input for the current modulation value. Streamout wav is the mixture of amplitude values for each note playing. Each ampersand (&) is a bitmask. It works a bit like conditionals, but with all bits set to 0 when false and all bits set to 1 when true. The result is then bitwise and'ed with the preceding variable.

At least at this point you probably see that I wouldn't have done this on my own...
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Fri Nov 20, 2015 1:23 am

@tulamide

for such a "clean" waveform I love how it gets those nice full sounding harmonics. You can hear gentle beating changes that make it so warm! It's impressive that the osc. alone achieved such a wide range of effects!

Davio would probably be interested in the VST when it's done, so I'd love to have it!
B
79
S
13
G
8
Posts: 1,977
Reputation: 9,947

PreviousNext