Page 1 of 1

making triangle/ramp switch

Posted: Mon Sep 16, 2013 3:00 pm
by tester
I can't find an easy way to do that.

I have flat-ramp generator (with phase offset) to drive something, and I'd like to add some quick switch to the code to flip from ramp to triangle.

One of my guesses is, that some sort of triangle formula could be added to the code, but this would produce double calculations or would involve additional selectors on stream routing. I'd like to avoid that.

Other of my guesses is, that there is some sort of threshold based formula, that would generate flat ramp or flat triangle (in 0-1 range), depending on flip, value that can be provided via separate input.

How to make it?

Re: making triangle/ramp switch

Posted: Mon Sep 16, 2013 3:37 pm
by tester
Hmm... Found and adapted something like this?

Re: making triangle/ramp switch

Posted: Mon Sep 16, 2013 9:38 pm
by trogluddite
Pretty cool - PWM for triangle waves!

If you're only ever switching in green, you could calculate 1/sw and 1/(1-sw) in a hop - should save a fair few CPU cycles replacing the divides for multiplys in the main equation.

Might also be worth restricting the range of sw by a tiny amount - there's a corner case at each end of the wave where 'zero divides zero' is possible - which would give an indeterminate answer (-1.#IND). If sw = 0, you will always get that on the first sample when ph has just initialised.
Depends what you're going to do with it, but that gives potential for a 'lock-up' situation if feeding into e.g. biquad filters etc. where the #INF can feed back on itself indefinitely. Probably rare that 'ph' would exactly be zero to 32bit precision, but it only needs to happen once in that kind of setup - needing an audio reset to recover!

Re: making triangle/ramp switch

Posted: Mon Sep 16, 2013 9:58 pm
by tester
I borrowed this one from SM and cleaned up and slightly modified. The original idea there was to smoothly change/morph from ramp through traigne to saw.

As for hoping - generally this one will be modulated (ramp is needed to get full rotation, triangle is needed to get angle swing back and forth), so not sure which part / how could be hoped.

Yes, I noticed a glitch on initlalizing. By limiting you mean like 1e-36 instead of 0 for phase offset or something in code?

Re: making triangle/ramp switch

Posted: Mon Sep 16, 2013 11:24 pm
by tester
Trog - you mean something like this?

Question: Ramp/triangle is feeding/modulating the rotation of local soundspace. Will not the sw limit produce audio glitch, at least from time to time?

Re: making triangle/ramp switch

Posted: Thu Dec 19, 2013 8:45 pm
by DjMmix
hi tester,

I took your algo in asm as impossible to understand the conditions of your code (it's not fault of trying to have)

if you could explain your algorithm simply I include in the DLL in C + + code directly thank you :)

and very interesting passage: p

Re: making triangle/ramp switch

Posted: Thu Dec 19, 2013 9:41 pm
by Nubeat7
i remember some further discussion about this code here:
viewtopic.php?f=2&t=1688&start=10

variables are renamed here, maybe its better to understand
here is the fixed schematic with different variablenames, hope this helps
triramp_fix.fsm
(29.49 KiB) Downloaded 1050 times


if you re not familiar with FS DSP code, the & acts like "if - then"

so:

Code: Select all

ramp>=1&1

would mean "if ramp >= 1 then 1"

Re: making triangle/ramp switch

Posted: Thu Dec 19, 2013 10:59 pm
by DjMmix
thank's nubeat :)

good explain

Re: making triangle/ramp switch

Posted: Thu Dec 19, 2013 11:43 pm
by tester
Gosh... I don't remember the context! :mrgreen: :-) I only remember, that it is already implemented somewhere (modified or as it is - I don't remember) and probably works fine. Otherwise I would remember it among things to do.

But as I am going to revisit some projects soon (within few months), switching from 'clone' philosophy into 'multifunction/array' based - if there is something to find out - it will come out. Thanks for pointing.