From ef9ef67fea32ff908c370d7db5a09fb10c8c87cf Mon Sep 17 00:00:00 2001 From: joachimheintz Date: Fri, 27 Dec 2024 20:31:13 +0100 Subject: [PATCH] first working example --- _layouts/examples.html | 12 +++-- _layouts/examples2.html | 36 -------------- assets/css/main/csound-site.css | 8 ++++ learn/examples.md | 21 ++++++++- learn/examples/examples.js | 83 +++++++++++++++++++++++++-------- learn/examples/examples2.js | 81 -------------------------------- learn/examples/pling.csd | 6 +-- 7 files changed, 102 insertions(+), 145 deletions(-) delete mode 100644 _layouts/examples2.html delete mode 100644 learn/examples/examples2.js diff --git a/_layouts/examples.html b/_layouts/examples.html index 5e1478c2e..ad82eb481 100644 --- a/_layouts/examples.html +++ b/_layouts/examples.html @@ -3,6 +3,7 @@ --- +
@@ -17,10 +18,13 @@

{{ page.title }}

{{ content }} -
-

[First Test while developing this page:

-

Click in this area to hear a sound.]

-
+ +   + +

+ +

diff --git a/_layouts/examples2.html b/_layouts/examples2.html deleted file mode 100644 index e50a24f4a..000000000 --- a/_layouts/examples2.html +++ /dev/null @@ -1,36 +0,0 @@ ---- -layout: default ---- - - - - -
-
-

{{ page.title }}

-
-
- -
-
-
-
-
- {{ content }} - - - - - - -

-

- -

- -

-
-
-
-
diff --git a/assets/css/main/csound-site.css b/assets/css/main/csound-site.css index 04f0f5989..e5c17c3ba 100644 --- a/assets/css/main/csound-site.css +++ b/assets/css/main/csound-site.css @@ -304,3 +304,11 @@ footer a:hover { .team-caption img { height: 5rem; } + + +/* Projects */ + +.console { + font-size: 0.75rem; + font-family: monospace; +} diff --git a/learn/examples.md b/learn/examples.md index 0ef07b943..57cc5ef20 100644 --- a/learn/examples.md +++ b/learn/examples.md @@ -1,7 +1,24 @@ --- -layout: examples2 +layout: examples title: Examples permalink: /examples.html --- -coming soon ... +This is a small collection of modern coding practice in Csound. + +## Pling + +Three lines of code generating a structure of short events. + +```csound +instr Pling + // generate one tone in a wide range between short/noisy and long/pitched + aMode = mode(mpulse(ampdb(random:i(-22,0)),p3),mtof:i(random:i(80,100)),10^(p3-1)) + // distribute anywhere in the stereo field and output + aL,aR pan2 aMode,random:i(0,1) + out(aL,aR) + // call the next instance of this instrument + schedule("Pling",rnd:i(1),random:i(1,4)) +endin +schedule("Pling",0,3) +``` diff --git a/learn/examples/examples.js b/learn/examples/examples.js index 34bbe89f8..f44e48023 100644 --- a/learn/examples/examples.js +++ b/learn/examples/examples.js @@ -1,31 +1,76 @@ +// play "pling.csd" +// csound.js is the Csound WASM module +const csoundjs = "https://www.unpkg.com/@csound/browser@6.18.7/dist/csound.js"; // csound is the Csound engine object (null as we start) let csound = null; -// csound synthesis code -const code = ` -instr 1 - out linenr(oscili(0dbfs*p4,p5),0.01,0.5,0.01) -endin -schedule(1,0,1,0.2,A4) -`; - -// this is the JS function to run Csound -async function play() { +// instrument on/off state +let isOn = false; +// CSD file name +const csd = '/learn/examples/pling.csd' +// this is the JS function to start Csound +// and resume performance if needed +async function start() { // if the Csound object is not initialised if(csound == null) { // import the Csound method from csound.js -const { Csound } = await import('https://www.unpkg.com/@csound/browser@6.18.7/dist/csound.js'); -// create a Csound engine object +const { Csound } = await import(csoundjs); +// create a Csound engine object inside the context actx csound = await Csound(); -// set realtime audio (dac) output -await csound.setOption("-odac"); -// compile csound code -await csound.compileOrc(code); +// copy the CSD file to the Csound local filesystem +await copyUrlToLocal(csd,csd) +// compile the code in the CSD file +await csound.compileCsd(csd) +// handle Csound messages +await csound.on("message", handleMessage); // start the engine await csound.start(); -} else -// if not just send an event to play a sound -await csound.inputMessage('i1 0 1 0.2 440'); +isOn = true; +} +// start performance if paused +if(!isOn) { + await csound.resume(); + isOn = true; +} +} + +let count = 0; +function handleMessage(message) { +// get the display element (called console in the page) +let element = document.getElementById('console'); +// add the message to HTML content (plus a newline) +element.innerHTML += message + '\n'; +// focus on bottom, new messages make the display scroll down +element.scrollTop = 99999; +// clear display every 1000 lines +if(count == 1000) { +count = 0; +element.innerHTML == ""; +} +count += 1; +}; + +// Copy file to local filesystem +async function copyUrlToLocal(src, dest) { +// fetch the file +let srcfile = await fetch(src) +// get the file data as an array +let dat = await srcfile.arrayBuffer(); +// write the data as a new file in the filesystem +await csound.fs.writeFile(dest, new Uint8Array(dat)); +} + +// toggle performance on/off +async function pause() { +if(csound != null) { +if(isOn) { + await csound.pause(); + isOn = false; +} else { + await csound.resume(); + isOn = true; +} +} } diff --git a/learn/examples/examples2.js b/learn/examples/examples2.js deleted file mode 100644 index b2cfc6486..000000000 --- a/learn/examples/examples2.js +++ /dev/null @@ -1,81 +0,0 @@ -// play "pling.csd" - -// csound.js is the Csound WASM module -const csoundjs = "https://www.unpkg.com/@csound/browser@6.18.7/dist/csound.js"; -// csound is the Csound engine object (null as we start) -let csound = null; - -// instrument on/off state -let isOn = false; -// CSD file name -const csd = '/learn/examples/pling.csd' -// this is the JS function to start Csound -// and resume performance if needed -async function start() { -// if the Csound object is not initialised -if(csound == null) { -// import the Csound method from csound.js -const { Csound } = await import(csoundjs); -// create a Csound engine object inside the context actx -csound = await Csound(); -// copy the CSD file to the Csound local filesystem -await copyUrlToLocal(csd,csd) -// compile the code in the CSD file -await csound.compileCsd(csd) -// handle Csound messages -await csound.on("message", handleMessage); -// start the engine -await csound.start(); -isOn = true; -} -// start performance if paused -if(!isOn) { - await csound.resume(); - isOn = true;stria -} -} - -let count = 0; -function handleMessage(message) { -// get the display element (called console in the page) -let element = document.getElementById('console'); -// add the message to HTML content (plus a newline) -element.innerHTML += message + '\n'; -// focus on bottom, new messages make the display scroll down -element.scrollTop = 99999; -// clear display every 1000 lines -if(count == 1000) { -count = 0; -element.innerHTML == ""; -} -count += 1; -}; - -// Copy file to local filesystem -async function copyUrlToLocal(src, dest) { -// fetch the file -let srcfile = await fetch(src) -// get the file data as an array -let dat = await srcfile.arrayBuffer(); -// write the data as a new file in the filesystem -await csound.fs.writeFile(dest, new Uint8Array(dat)); -} - -// toggle performance on/off -async function pause() { -if(csound != null) { -if(isOn) { - await csound.pause(); - isOn = false; -} else { - await csound.resume(); - isOn = true; -} -} -} - -// rewind score -async function rewind() { -if(csound != null) -await csound.rewindScore(); -} \ No newline at end of file diff --git a/learn/examples/pling.csd b/learn/examples/pling.csd index 6112e33ac..39a7624de 100644 --- a/learn/examples/pling.csd +++ b/learn/examples/pling.csd @@ -11,16 +11,16 @@ seed 0 // by joachim heintz -instr PlingStruct +instr Pling // generate one tone in a wide range between short/noisy and long/pitched aMode = mode(mpulse(ampdb(random:i(-22,0)),p3),mtof:i(random:i(80,100)),10^(p3-1)) // distribute anywhere in the stereo field and output aL,aR pan2 aMode,random:i(0,1) out(aL,aR) // call the next instance of this instrument - schedule("PlingStruct",rnd:i(1),random:i(1,4)) + schedule("Pling",rnd:i(1),random:i(1,4)) endin -schedule("PlingStruct",0,3) +schedule("Pling",0,3)