From e8fb7ee683cc089f474a7d88bf7ba82cba8610b6 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Fri, 2 Dec 2022 11:38:47 +0100 Subject: [PATCH] now passing controls as string keymap --- packages/csound/csound.mjs | 20 +++++++++++++++----- packages/csound/presets.orc | 35 +++++++++++++++++++++++++++++++++++ repl/src/tunes.mjs | 16 ++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/packages/csound/csound.mjs b/packages/csound/csound.mjs index 4e5ec370..3df64189 100644 --- a/packages/csound/csound.mjs +++ b/packages/csound/csound.mjs @@ -16,10 +16,16 @@ Pattern.prototype._csound = function (instrument) { logger('[csound] not loaded yet', 'warning'); return; } + if (typeof hap.value !== 'object') { + throw new Error('csound only support objects as hap values'); + } let { gain = 0.8 } = hap.value; gain *= 0.2; - const freq = getFrequency(hap); + const freq = Math.round(getFrequency(hap)); + const controls = Object.entries({ ...hap.value, freq }) + .flat() + .join('/'); // TODO: find out how to send a precise ctx based time // http://www.csounds.com/manual/html/i.html const params = [ @@ -29,6 +35,7 @@ Pattern.prototype._csound = function (instrument) { // instrument specific params: freq, //.toFixed(precision), // p4: frequency gain, // p5: gain + `"${controls}"`, // p6 controls as string (like superdirt osc message) ]; const msg = `i ${params.join(' ')}`; _csound.inputMessage(msg); @@ -38,9 +45,12 @@ Pattern.prototype._csound = function (instrument) { // initializes csound + can be used to reevaluate given instrument code export async function csound(code = '') { await init(); - code && (await _csound?.evalCode(`${code}`)); - // ^ ^ - // wrapping in backticks makes sure it works when calling as templated function + if (code) { + code = `${code}`; + // ^ ^ + // wrapping in backticks makes sure it works when calling as templated function + await _csound?.evalCode(code); + } } Pattern.prototype.define('csound', (a, pat) => pat.csound(a), { composable: false, patternified: true }); @@ -78,7 +88,7 @@ async function load() { } _csound.removeAllListeners('message'); ['message'].forEach((k) => _csound.on(k, (...args) => eventLogger(k, args))); - await _csound.setOption('-m0'); // see -m flag https://csound.com/docs/manual/CommandFlags.html + await _csound.setOption('-m0d'); // see -m flag https://csound.com/docs/manual/CommandFlags.html await _csound.setOption('--sample-accurate'); await _csound.compileCsdText(csd); await _csound.compileOrc(livecodeOrc); diff --git a/packages/csound/presets.orc b/packages/csound/presets.orc index b75122d5..a6e2b442 100644 --- a/packages/csound/presets.orc +++ b/packages/csound/presets.orc @@ -1,3 +1,15 @@ +; returns value of given key in given "string map" +; keymap("freq", "note/c3/freq/220/gain/0.5") +; yields "220" +opcode keymap, S, SS + Skey, Smap xin + idelimiter = strindex(Smap, strcat(Skey, "/")) + ifrom = idelimiter + strlen(Skey) + 1 + Svalue = strsub(Smap, ifrom, strlen(Smap)) + Svalue = strsub(Svalue, 0, strindex(Svalue, "/")) + xout Svalue +endop + ; TODO add incredibly dope synths instr organ iduration = p3 @@ -58,4 +70,27 @@ instr pad asig = zdf_2pole(asig, 1000, 2) out(asig, asig) +endin + + +gisine ftgen 0, 0, 4096, 10, 1 + +instr bow + kpres = 2 + krat = 0.16 + kvibf = 6.12723 + + kvib linseg 0, 0.5, 0, 1, 1, p3-0.5, 1 + kvamp = kvib * 0.01 + asig wgbow .7, p4, kpres, krat, kvibf, kvamp, gisine + asig = asig*p5 + outs asig, asig +endin + + +instr Meta + Smap = strget(p6) + Sinstrument = keymap("s", Smap) + schedule(Sinstrument, 0, p3, p4, p5) + ; TODO find a way to pipe Sinstrument through effects endin \ No newline at end of file diff --git a/repl/src/tunes.mjs b/repl/src/tunes.mjs index cf839ead..ba7811e2 100644 --- a/repl/src/tunes.mjs +++ b/repl/src/tunes.mjs @@ -1023,3 +1023,19 @@ endin\` .note() //.pianoroll() .csound("/4")`; + +export const loungeSponge = `await csound() + +stack( + note("/2".voicings()) + .cutoff(sine.range(500,2000).round().slow(16)) + .euclidLegato(3,8).csound('FM1') + , + note("/2").ply(8).csound('Bass').gain("1 4 1 4") + , + note("0 7 [4 3] 2".fast(2/3).off(".25 .125",add("<2 4 -3 -1>")) + .slow(2).scale('A4 minor')) + .legato(.25).csound('SynHarp') + , + s("bd*2,[~ hh]*2,~ cp").bank('RolandTR909') + )`;