mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-22 02:58:32 +00:00
lazy init + add livecode.org
This commit is contained in:
parent
9052ff71e6
commit
d78da55f54
@ -1,13 +1,16 @@
|
|||||||
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 { getAudioContext } from '@strudel.cycles/webaudio';
|
import { getAudioContext } from '@strudel.cycles/webaudio';
|
||||||
import csd from './sounds.csd?raw';
|
import csd from './project.csd?raw';
|
||||||
|
import orc from './livecode.orc?raw';
|
||||||
|
// import orc from './sounds.orc?raw';
|
||||||
|
|
||||||
let csoundLoader, _csound;
|
let csoundLoader, _csound;
|
||||||
|
|
||||||
// triggers given instrument name using csound. expects csound function to be called in advance `await csound()`
|
// triggers given instrument name using csound. expects csound function to be called in advance `await csound()`
|
||||||
Pattern.prototype._csound = function (instrument) {
|
Pattern.prototype._csound = function (instrument) {
|
||||||
instrument = instrument || 'triangle';
|
instrument = instrument || 'triangle';
|
||||||
|
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)
|
||||||
return this.onTrigger((time, hap, currentTime) => {
|
return this.onTrigger((time, hap, currentTime) => {
|
||||||
if (!_csound) {
|
if (!_csound) {
|
||||||
logger('[csound] not loaded yet', 'warning');
|
logger('[csound] not loaded yet', 'warning');
|
||||||
@ -36,8 +39,7 @@ Pattern.prototype._csound = function (instrument) {
|
|||||||
|
|
||||||
// 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 = '') {
|
||||||
csoundLoader = csoundLoader || init();
|
await init();
|
||||||
await csoundLoader;
|
|
||||||
code && (await _csound?.evalCode(`${code}`));
|
code && (await _csound?.evalCode(`${code}`));
|
||||||
// ^ ^
|
// ^ ^
|
||||||
// wrapping in backticks makes sure it works when calling as templated function
|
// wrapping in backticks makes sure it works when calling as templated function
|
||||||
@ -68,17 +70,24 @@ function eventLogger(type, args) {
|
|||||||
logger(`[csound] ${msg || ''}`, logType);
|
logger(`[csound] ${msg || ''}`, logType);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function init() {
|
async function load() {
|
||||||
|
const { Csound } = await import('@csound/browser');
|
||||||
_csound = await Csound({ audioContext: getAudioContext() });
|
_csound = await Csound({ audioContext: getAudioContext() });
|
||||||
_csound.removeAllListeners('message');
|
_csound.removeAllListeners('message');
|
||||||
['message'].forEach((k) => _csound.on(k, (...args) => eventLogger(k, args)));
|
['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('-m0'); // see -m flag https://csound.com/docs/manual/CommandFlags.html
|
||||||
await _csound.setOption('--sample-accurate');
|
await _csound.setOption('--sample-accurate');
|
||||||
await _csound.compileCsdText(csd);
|
await _csound.compileCsdText(csd);
|
||||||
|
await _csound.compileOrc(orc);
|
||||||
await _csound.start();
|
await _csound.start();
|
||||||
return _csound;
|
return _csound;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function init() {
|
||||||
|
csoundLoader = csoundLoader || load();
|
||||||
|
return csoundLoader;
|
||||||
|
}
|
||||||
|
|
||||||
// experimental: allows using jsx to eval csound
|
// experimental: allows using jsx to eval csound
|
||||||
window.jsxPragma = function (fn, args, text) {
|
window.jsxPragma = function (fn, args, text) {
|
||||||
return fn(text);
|
return fn(text);
|
||||||
|
|||||||
2226
packages/csound/livecode.orc
Normal file
2226
packages/csound/livecode.orc
Normal file
File diff suppressed because it is too large
Load Diff
10
packages/csound/project.csd
Normal file
10
packages/csound/project.csd
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<CsoundSynthesizer>
|
||||||
|
<CsInstruments>
|
||||||
|
|
||||||
|
sr=48000
|
||||||
|
ksmps=64
|
||||||
|
nchnls=2
|
||||||
|
0dbfs=1
|
||||||
|
|
||||||
|
</CsInstruments>
|
||||||
|
</CsoundSynthesizer>
|
||||||
@ -1,10 +1,3 @@
|
|||||||
<CsoundSynthesizer>
|
|
||||||
<CsInstruments>
|
|
||||||
sr=48000
|
|
||||||
ksmps=64
|
|
||||||
nchnls=2
|
|
||||||
0dbfs=1
|
|
||||||
|
|
||||||
instr organ
|
instr organ
|
||||||
iduration = p3
|
iduration = p3
|
||||||
ifreq = p4
|
ifreq = p4
|
||||||
@ -45,5 +38,3 @@ instr triangle
|
|||||||
|
|
||||||
out(asig, asig)
|
out(asig, asig)
|
||||||
endin
|
endin
|
||||||
</CsInstruments>
|
|
||||||
</CsoundSynthesizer>
|
|
||||||
Loading…
x
Reference in New Issue
Block a user