get rid of csound warning on reevaluation:
- only compile on init, use evalCode after - pull presets into csd file - use custom logging
This commit is contained in:
parent
713da887ca
commit
9052ff71e6
2 changed files with 55 additions and 45 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import { getFrequency, logger, Pattern } from '@strudel.cycles/core';
|
import { getFrequency, logger, Pattern } from '@strudel.cycles/core';
|
||||||
import { Csound } from '@csound/browser'; // TODO: use dynamic import for code splitting..
|
import { Csound } from '@csound/browser'; // TODO: use dynamic import for code splitting..
|
||||||
import { csd } from './csd.mjs';
|
|
||||||
import { getAudioContext } from '@strudel.cycles/webaudio';
|
import { getAudioContext } from '@strudel.cycles/webaudio';
|
||||||
|
import csd from './sounds.csd?raw';
|
||||||
|
|
||||||
let csoundLoader, _csound;
|
let csoundLoader, _csound;
|
||||||
|
|
||||||
|
|
@ -30,30 +30,53 @@ Pattern.prototype._csound = function (instrument) {
|
||||||
gain, // p5: gain
|
gain, // p5: gain
|
||||||
];
|
];
|
||||||
const msg = `i ${params.join(' ')}`;
|
const msg = `i ${params.join(' ')}`;
|
||||||
// console.log('msg', msg);
|
|
||||||
_csound.inputMessage(msg);
|
_csound.inputMessage(msg);
|
||||||
// _csound.readScore(msg); // slower alternative
|
|
||||||
// even slower alternative:
|
|
||||||
/* const code = `schedule(${params.join(', ')})`;
|
|
||||||
_csound.evalCode(code); */
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// initializes csound + can be used to reevaluate given instrument code
|
// initializes csound + can be used to reevaluate given instrument code
|
||||||
export async function csound(code = '') {
|
export async function csound(code = '') {
|
||||||
code = csd(code);
|
csoundLoader = csoundLoader || init();
|
||||||
let isInit = false;
|
|
||||||
if (!csoundLoader) {
|
|
||||||
isInit = true;
|
|
||||||
csoundLoader = (async () => {
|
|
||||||
_csound = await Csound({ audioContext: getAudioContext() });
|
|
||||||
await _csound.setOption('-m0');
|
|
||||||
await _csound.compileCsdText(code);
|
|
||||||
await _csound.start();
|
|
||||||
})();
|
|
||||||
}
|
|
||||||
await csoundLoader;
|
await csoundLoader;
|
||||||
!isInit && (await _csound?.compileCsdText(code));
|
code && (await _csound?.evalCode(`${code}`));
|
||||||
|
// ^ ^
|
||||||
|
// wrapping in backticks makes sure it works when calling as templated function
|
||||||
|
}
|
||||||
|
|
||||||
|
Pattern.prototype.define('csound', (a, pat) => pat.csound(a), { composable: false, patternified: true });
|
||||||
|
|
||||||
|
function eventLogger(type, args) {
|
||||||
|
const [msg] = args;
|
||||||
|
if (
|
||||||
|
type === 'message' &&
|
||||||
|
(['[commit: HEAD]'].includes(msg) ||
|
||||||
|
msg.startsWith('--Csound version') ||
|
||||||
|
msg.startsWith('libsndfile') ||
|
||||||
|
msg.startsWith('sr =') ||
|
||||||
|
msg.startsWith('0dBFS') ||
|
||||||
|
msg.startsWith('audio buffered') ||
|
||||||
|
msg.startsWith('writing') ||
|
||||||
|
msg.startsWith('SECTION 1:'))
|
||||||
|
) {
|
||||||
|
// ignore
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let logType = 'info';
|
||||||
|
if (msg.startsWith('error:')) {
|
||||||
|
logType = 'error';
|
||||||
|
}
|
||||||
|
logger(`[csound] ${msg || ''}`, logType);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function init() {
|
||||||
|
_csound = await Csound({ audioContext: getAudioContext() });
|
||||||
|
_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('--sample-accurate');
|
||||||
|
await _csound.compileCsdText(csd);
|
||||||
|
await _csound.start();
|
||||||
|
return _csound;
|
||||||
}
|
}
|
||||||
|
|
||||||
// experimental: allows using jsx to eval csound
|
// experimental: allows using jsx to eval csound
|
||||||
|
|
@ -68,5 +91,3 @@ export function CsInstruments(text) {
|
||||||
}
|
}
|
||||||
return csound(text);
|
return csound(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pattern.prototype.define('csound', (a, pat) => pat.csound(a), { composable: false, patternified: true });
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,4 @@
|
||||||
export function csd(code = '') {
|
|
||||||
return `
|
|
||||||
<CsoundSynthesizer>
|
<CsoundSynthesizer>
|
||||||
<CsOptions>
|
|
||||||
-o dac --port=10000 --sample-accurate
|
|
||||||
</CsOptions>
|
|
||||||
<CsInstruments>
|
<CsInstruments>
|
||||||
sr=48000
|
sr=48000
|
||||||
ksmps=64
|
ksmps=64
|
||||||
|
|
@ -36,25 +31,19 @@ instr organ
|
||||||
endin
|
endin
|
||||||
|
|
||||||
instr triangle
|
instr triangle
|
||||||
iduration = p3
|
iduration = p3
|
||||||
ifreq = p4
|
ifreq = p4
|
||||||
igain = p5
|
igain = p5
|
||||||
ioct = octcps(ifreq)
|
ioct = octcps(ifreq)
|
||||||
|
|
||||||
asig = vco2(igain, ifreq, 12, .5)
|
asig = vco2(igain, ifreq, 12, .5)
|
||||||
|
|
||||||
; amp envelope
|
; amp envelope
|
||||||
iattack = .001
|
iattack = .001
|
||||||
irelease = .05
|
irelease = .05
|
||||||
asig *= linsegr:a(0, iattack, 1, iduration, 1, irelease, 0)
|
asig *= linsegr:a(0, iattack, 1, iduration, 1, irelease, 0)
|
||||||
|
|
||||||
out(asig, asig)
|
out(asig, asig)
|
||||||
|
|
||||||
endin
|
endin
|
||||||
|
|
||||||
${code}
|
|
||||||
|
|
||||||
</CsInstruments>
|
</CsInstruments>
|
||||||
</CsoundSynthesizer>
|
</CsoundSynthesizer>
|
||||||
`;
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue