Merge pull request 'Fix sounds example to work in the REPL' (#1851) from JesCoding/strudel:jescoding-patch-2 into main

Reviewed-on: https://codeberg.org/uzu/strudel/pulls/1851
This commit is contained in:
froos 2026-01-11 19:00:24 +01:00
commit 5ee5d4f1b1

View file

@ -13,7 +13,7 @@ Let's take a closer look about how sounds are implemented in the webaudio output
All sounds are registered in the sound map, using the the `registerSound` function: All sounds are registered in the sound map, using the the `registerSound` function:
```ts ```js
function registerSound( function registerSound(
name: string, // The name of the sound that should be given to `s`, e.g. `mysaw` name: string, // The name of the sound that should be given to `s`, e.g. `mysaw`
// The function called by the scheduler to trigger the sound: // The function called by the scheduler to trigger the sound:
@ -35,34 +35,36 @@ When `registerSound` is called, it registers `{ onTrigger, data }` under the giv
This might be a bit abstract, so here is a minimal example: This might be a bit abstract, so here is a minimal example:
```js <MiniRepl
registerSound( client:idle
'mysaw', tune={`
(time, value, onended) => { registerSound(
let { freq } = value; // destructure control params 'mysaw',
const ctx = getAudioContext(); (time, value, onended) => {
// create oscillator let { freq } = value; // destructure control params
const o = new OscillatorNode(ctx, { type: 'sawtooth', frequency: Number(freq) }); const ctx = getAudioContext();
o.start(time); // create oscillator
// add gain node to level down osc const o = new OscillatorNode(ctx, { type: 'sawtooth', frequency: Number(freq) });
const g = new GainNode(ctx, { gain: 0.3 }); o.start(time);
// connect osc to gain // add gain node to level down osc
const node = o.connect(g); const g = new GainNode(ctx, { gain: 0.3 });
// this function can be called from outside to stop the sound // connect osc to gain
const stop = (time) => o.stop(time); const node = o.connect(g);
// ended will be fired when stop has been fired // this function can be called from outside to stop the sound
o.addEventListener('ended', () => { const stop = (time) => o.stop(time);
o.disconnect(); // ended will be fired when stop has been fired
g.disconnect(); o.addEventListener('ended', () => {
onended(); o.disconnect();
}); g.disconnect();
return { node, stop }; onended();
}, });
{ type: 'synth' }, return { node, stop };
); },
// use the sound { type: 'synth' },
freq(220, 440, 330).s('mysaw'); );
``` // use the sound
freq("220 440 330").s('mysaw');`}
/>
You can actually use this code in the [REPL](https://strudel.cc/) and it'll work. You can actually use this code in the [REPL](https://strudel.cc/) and it'll work.
After evaluating the code, you should see `mysaw` in listed in the sounds tab. After evaluating the code, you should see `mysaw` in listed in the sounds tab.