Skip to content

Commit

Permalink
Merge pull request #27 from ahihi/dirt-compressor-needs
Browse files Browse the repository at this point in the history
nodes for making Dirt compressor work
  • Loading branch information
felixroos authored Sep 17, 2024
2 parents 22bf887 + e2a76f2 commit f8b74c6
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
29 changes: 28 additions & 1 deletion packages/cli/src/clib/ugens.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ double lerp(double x, double y0, double y1)
return y0 + x * (y1 - y0);
}

// a pair of values
// used to implement e.g. argmin and argmax
typedef struct pair {
double a;
double b;
} pair;

// SineOsc

typedef struct SineOsc
Expand Down Expand Up @@ -831,6 +838,26 @@ void *Clock_create()
return (void *)node;
}

// Pick

typedef struct Pick {} Pick;

void Pick_init(Pick *self)
{
}

double Pick_update(Sequence *self, float index, int len, float *inputs)
{
return inputs[((int) floor(index)) % len];
}

void *Pick_create()
{
Pick *node = (Pick *)malloc(sizeof(Pick));
Pick_init(node);
return (void *)node;
}

#endif // UGENS_H

/*
Expand Down Expand Up @@ -890,4 +917,4 @@ void *Template_create()
return (void *)node;
}
*/
*/
10 changes: 9 additions & 1 deletion packages/lib/src/lang/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export let def = (name, value, comment) =>

export let defUgen = (meta, ...args) => {
args.unshift(`nodes[${meta.ugenIndex}]`);
if (meta.ugen === "Sequence") {
if (meta.ugen === "Sequence" || meta.ugen === "Pick") {
const len = args.length - 2;
const seq = `(float[${len}]){${args.slice(2).join(",")}}`;
return def(
Expand All @@ -28,3 +28,11 @@ export let pow = (a, b) => `pow(${a}, ${b})`;
export let exp = (a) => `exp(${a})`;
export let log = (input) => `log(${input})`;
export let mod = (a, b) => `${a}>=${b}?${a}-${b}:${a}`;
export let abs = (input) => `fabs(${input})`;
export let min = (a, b) => `fmin(${a}, ${b})`;
export let max = (a, b) => `fmax(${a}, ${b})`;
export let pair_make = (value, i) => `((pair) {${value}, ${i}})`;
export let pair_a = (p) => `${p}.a`;
export let pair_b = (p) => `${p}.b`;
export let pair_a_min = (p, q) => `(${pair_a(p)} < ${pair_a(q)} ? ${p} : ${q})`;
export let pair_a_max = (p, q) => `(${pair_a(p)} > ${pair_a(q)} ? ${p} : ${q})`;
8 changes: 8 additions & 0 deletions packages/lib/src/lang/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ export let pow = (a, b) => `${a} ** ${b}`;
export let exp = (input) => `Math.exp(${input})`;
export let log = (input) => `Math.log(${input})`;
export let mod = (a, b) => `${a}%${b}`;
export let abs = (input) => `Math.abs(${input})`;
export let min = (a, b) => `Math.min(${a}, ${b})`;
export let max = (a, b) => `Math.max(${a}, ${b})`;
export let pair_make = (a, b) => `[${a}, ${b}]`;
export let pair_a = (p) => `${p}[0]`;
export let pair_b = (p) => `${p}[1]`;
export let pair_a_min = (p, q) => `(${pair_a(p)} < ${pair_a(q)} ? ${p} : ${q})`;
export let pair_a_max = (p, q) => `(${pair_a(p)} > ${pair_a(q)} ? ${p} : ${q})`;
83 changes: 83 additions & 0 deletions packages/lib/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,64 @@ export let mod = registerNode("mod", {
compile: ({ vars, name, lang }) =>
langs[lang].def(name, langs[lang].mod(...vars) || 0),
});
export let abs = registerNode("abs", {
tags: ["math"],
description: "returns the absolute value of the signal",
ins: [{ name: "in" }],
examples: [`sine(440).abs().out()`],
compile: ({ vars: [input = 0], name, lang }) =>
langs[lang].def(name, langs[lang].abs(input)),
});
export let min = registerNode("min", {
tags: ["math"],
description: "returns the minimum of the given signals",
examples: [
`impulse(4).apply(x => min(x.seq(0,3,2), x.seq(0,7,0,5,0)).add(48).midinote().sine()).out()`,
],
ins: [{ name: "in", dynamic: true }],
compile: ({ vars, name, lang }) =>
langs[lang].def(name, vars.reduce(langs[lang].min) || 0),
});
export let max = registerNode("max", {
tags: ["math"],
description: "returns the maximum of the given signals",
examples: [
`impulse(4).apply(x => max(x.seq(0,3,2), x.seq(0,7,0,5,0)).add(48).midinote().sine()).out()`,
],
ins: [{ name: "in", dynamic: true }],
compile: ({ vars, name, lang }) =>
langs[lang].def(name, vars.reduce(langs[lang].max) || 0),
});
export let argmin = registerNode("argmin", {
tags: ["math"],
description: "returns the index of the minimum of the given signals",
examples: [
`argmin(saw(1), saw(3), saw(5)).mul(12).add(48).midinote().sine().out()`,
],
ins: [{ name: "in", dynamic: true }],
compile: ({ vars, name, lang }) =>
langs[lang].def(
name,
langs[lang].pair_b(
vars.map(langs[lang].pair_make).reduce(langs[lang].pair_a_min)
) || 0
),
});
export let argmax = registerNode("argmax", {
tags: ["math"],
description: "returns the index of the maximum of the given signals",
examples: [
`argmax(saw(1), saw(3), saw(5)).mul(12).add(48).midinote().sine().out()`,
],
ins: [{ name: "in", dynamic: true }],
compile: ({ vars, name, lang }) =>
langs[lang].def(
name,
langs[lang].pair_b(
vars.map(langs[lang].pair_make).reduce(langs[lang].pair_a_max)
) || 0
),
});
export let greater = registerNode("greater", {
tags: ["logic"],
description: "returns 1 if input is greater then threshold",
Expand Down Expand Up @@ -785,6 +843,31 @@ export let pan = module(
}
);

export let pick = registerNode("pick", {
tags: ["multi-channel"],
ugen: "Pick",
description: "Pick",
ins: [{ name: "index" }, { name: "inputs", dynamic: true }],
compile: ({ vars, ...meta }) => langs[meta.lang].defUgen(meta, ...vars),
});

export let split = register(
"split",
(input, fn) => {
if (input.type !== "poly") {
return fn([input]);
}
return fn(input.ins);
},
{
ins: [{ name: "input" }, { name: "fn" }],
tags: ["multi-channel"],
description:
"apply fn to an array of signals, one for each channel in input",
examples: [`sine([220,330,550]).split(chs => add(...chs)).out()`],
}
);

export let mix = register(
"mix",
(input, channels = 1) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/lib/src/ugens.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,9 @@ export class Sequence extends AudioNode {
return ins[this.step];
}
}

export class Pick extends AudioNode {
update(index, ...inputs) {
return inputs[Math.floor(index) % inputs.length];
}
}

0 comments on commit f8b74c6

Please sign in to comment.