maximecb / noisecraft

Browser-based visual programming language and platform for sound synthesis.

Home Page:https://noisecraft.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wavefolding

raisch opened this issue · comments

I have a PR I'd like to submit that provides a simple wavefolding module; it's patterned after the slide module, with additions to model.js, compiler.js, and audiograph.js and does not require any other modules/libraries.

Fold takes two inputs (in and rate), one output (out), and implements a pretty simple but effective algorithm, i.e.:

// assumes sine input
f(x) = sin(x) * a               // where a is amplitude of the modulation

g(x) = 4(abs(0.25x + 0.25 - round(0.25x + 0.25)) - 0.25

g(f(x)) => signal output

The PR includes both a simple demo:

const(440)->sine:freq->fold1:in->audioout:left // to hear the fold in action
const(1)->sine:freq->fold2:in->scope           // to display the fold on a single wave period

knob->fold[1,2]:rate                           // connected to both folders.

See Simple Wavefolder Screenshot

as well as a more complicated example using a sine LFO to modulate fold:rate.

Hi @raisch. I like that you kept the node name short ;)

I don't know much about wavefolders, is this the "standard" way of implementing them, so to speak?

Feel free to open the PR and I will give you my feedback.

I believe it's the simplest, least mathematically complex, and so, most performant approach.

Performance measurement on my MacBook Pro with 2.4GHz 8-core i9, 64GB 2667Mhz DDR4:

Assuming average of 10 runs of 1M iterations each,

  • 3.5ms (0.00003s per iteration) using constant value for x
  • 13.9ms (0.00014s per iteration) using Math.random for x:

Since x will be supplied by the runtime environment (i.e. noisecraft), the first measurement should reflect real-world use.

I'll submit a PR later today.

Thanks.

/rob

You can see how the folding occurs here: wavefold example (Click the start arrow on row two of the left hand column to animate the display. Or just move the slider.)

To my (ancient) ears, values above 20 or so become somewhat indistinguishable except for harmonic crossover points (which occur every 2.0+ increments or so), so for me, the "goldilocks zone" for useful synthesis is from 1 to 15.

Lower values from 1 to 3 or so are also useful as modulation inputs.

You can get interesting harmonics using Tri and Saw, somewhat similar to playing harmonic notes on guitar. On a Saw VCO with rate=4, you get a pure Tri at about the 3rd harmonic, and with rate=6, you get a pure Tri at about the 5th harmonic.

And very odd things occur when you use a Pulse VCO, like at rate=2, the amplitude of the waveform renders it inaudible.

Looking forward to trying out your PR :)

It appears I do not have permission to create a new branch.

You can view my contribution on my fork at https://github.com/raisch/noisecraft
The branch is wavefolding-raisch

Can you create a pull request? You should be able to do it as your repository is a fork of this one.

https://www.freecodecamp.org/news/how-to-make-your-first-pull-request-on-github-3/