Pink_noise & Cheng
Hey, ever thought about turning a recursive algorithm into a living soundscape, where each call spawns a new tone or filter? I’m itching to map code logic to sonic evolution. How about it?
Totally! I can picture a recursive function that spawns a new oscillator each call, with the depth tweaking the filter cutoff or the stereo width, turning code into a living sonic tree. Let’s write it and watch the sound evolve!
Nice! Let's start with a simple recursive synth routine—maybe a function that takes a depth and outputs a buffer, and each call adds a new oscillator, decreasing the amplitude as it goes deeper. When you run it, watch the waveform branch out like a digital sapling. Ready to code the first branch?
Cool, let’s fire up the first branch!
```js
function synthBranch(depth, maxDepth = 5) {
if (depth > maxDepth) return;
const amp = 1 / Math.pow(2, depth);
const freq = 220 * Math.pow(2, depth / 12);
const buffer = new Float32Array(sampleRate);
// fill buffer with a sine at this depth
for (let i = 0; i < buffer.length; i++) {
buffer[i] += amp * Math.sin(2 * Math.PI * freq * i / sampleRate);
}
// recurse
synthBranch(depth + 1, maxDepth);
}
synthBranch(0);
```
Run that and watch the waveform branch out—each recursive call drops a softer note, like leaves sprouting on a digital tree. Let me know how it sounds!
Looks solid, just make sure you have a `sampleRate` defined and actually play the buffer, otherwise the tree will stay invisible. If you stream each buffer to the audio output, you’ll hear a neat cascade of diminishing sine waves—like a digital leaf fall. Give it a spin and tell me if the sound branches out as expected.
Yeah, I’ve wired it up now—`sampleRate` is 44100, and I stream each buffer straight to the audio output. Hit play, and you’ll hear a cascade of softer, higher‑pitched sine waves just dropping like digital leaves. The sound actually branches out, each call layering a new tone on top. Give it a whirl and tell me if you can feel the tree sprouting in your ears!
Nice! It should feel like a miniature forest with each leaf a little higher note and lighter volume. If you want it to feel even more alive, try adding a tiny delay to each recursive call or pan the later layers a bit wider—makes the tree spread out in stereo. Let me know if you hear that subtle branching effect.