refactor csound
This commit is contained in:
parent
27a6ac0c0b
commit
2f4da3ed52
2 changed files with 18 additions and 19 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { getFrequency, logger, Pattern } from '@strudel.cycles/core';
|
import { getFrequency, logger, register } from '@strudel.cycles/core';
|
||||||
import { getAudioContext } from '@strudel.cycles/webaudio';
|
import { getAudioContext } from '@strudel.cycles/webaudio';
|
||||||
import csd from './project.csd?raw';
|
import csd from './project.csd?raw';
|
||||||
// import livecodeOrc from './livecode.orc?raw';
|
// import livecodeOrc from './livecode.orc?raw';
|
||||||
|
|
@ -6,12 +6,24 @@ import presetsOrc from './presets.orc?raw';
|
||||||
|
|
||||||
let csoundLoader, _csound;
|
let csoundLoader, _csound;
|
||||||
|
|
||||||
// triggers given instrument name using csound.
|
// initializes csound + can be used to reevaluate given instrument code
|
||||||
Pattern.prototype._csound = function (instrument) {
|
export async function loadCSound(code = '') {
|
||||||
|
await init();
|
||||||
|
if (code) {
|
||||||
|
code = `${code}`;
|
||||||
|
// ^ ^
|
||||||
|
// wrapping in backticks makes sure it works when calling as templated function
|
||||||
|
await _csound?.evalCode(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const loadcsound = loadCSound;
|
||||||
|
export const loadCsound = loadCSound;
|
||||||
|
|
||||||
|
export const csound = register('csound', (instrument, pat) => {
|
||||||
instrument = instrument || 'triangle';
|
instrument = instrument || 'triangle';
|
||||||
init(); // not async to support csound inside other patterns + to be able to call pattern methods after it
|
init(); // not async to support csound inside other patterns + to be able to call pattern methods after it
|
||||||
// TODO: find a alternative way to wait for csound to load (to wait with first time playback)
|
// TODO: find a alternative way to wait for csound to load (to wait with first time playback)
|
||||||
return this.onTrigger((time, hap) => {
|
return pat.onTrigger((time, hap) => {
|
||||||
if (!_csound) {
|
if (!_csound) {
|
||||||
logger('[csound] not loaded yet', 'warning');
|
logger('[csound] not loaded yet', 'warning');
|
||||||
return;
|
return;
|
||||||
|
|
@ -40,20 +52,7 @@ Pattern.prototype._csound = function (instrument) {
|
||||||
const msg = `i ${params.join(' ')}`;
|
const msg = `i ${params.join(' ')}`;
|
||||||
_csound.inputMessage(msg);
|
_csound.inputMessage(msg);
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
|
|
||||||
// initializes csound + can be used to reevaluate given instrument code
|
|
||||||
export async function csound(code = '') {
|
|
||||||
await init();
|
|
||||||
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 });
|
|
||||||
|
|
||||||
function eventLogger(type, args) {
|
function eventLogger(type, args) {
|
||||||
const [msg] = args;
|
const [msg] = args;
|
||||||
|
|
|
||||||
|
|
@ -769,7 +769,7 @@ note("c3 eb3 g3 bb3").palindrome()
|
||||||
|
|
||||||
export const csoundDemo = `// licensed with CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/
|
export const csoundDemo = `// licensed with CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||||
// by Felix Roos
|
// by Felix Roos
|
||||||
await csound\`
|
await loadCsound\`
|
||||||
instr CoolSynth
|
instr CoolSynth
|
||||||
iduration = p3
|
iduration = p3
|
||||||
ifreq = p4
|
ifreq = p4
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue