Skip to content

Commit ef9ef67

Browse files
author
joachimheintz
committed
first working example
1 parent 3566c0a commit ef9ef67

File tree

7 files changed

+102
-145
lines changed

7 files changed

+102
-145
lines changed

_layouts/examples.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
---
44

55
<script type="text/javascript" src="/learn/examples/examples.js"></script>
6+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
67

78

89
<div class="container-fluid page-banner-background">
@@ -17,10 +18,13 @@ <h1 class="display-7">{{ page.title }}</h1>
1718
<div class="row">
1819
<div class="col-xl-10 col-lg-10 col-md-12 col-sm-12">
1920
{{ content }}
20-
<div id="click area" onclick="play()" style="margin-top:30px;">
21-
<p>[First Test while developing this page:</p>
22-
<p> Click in this area to hear a sound.]</p>
23-
</div>
21+
<button id="play" onclick="start()"><i class="fa fa-play"></i></button>
22+
<button id="pause" onclick="pause()"><i class="fa fa-pause"></i></button>&nbsp;
23+
<a href="/learn/examples/pling.csd"><i class="fa fa-download"></i></button>
24+
<p>
25+
<textarea class="console" cols="80" rows="5" id="console">
26+
</textarea>
27+
<p>
2428

2529
</div>
2630
</div>

_layouts/examples2.html

Lines changed: 0 additions & 36 deletions
This file was deleted.

assets/css/main/csound-site.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,11 @@ footer a:hover {
304304
.team-caption img {
305305
height: 5rem;
306306
}
307+
308+
309+
/* Projects */
310+
311+
.console {
312+
font-size: 0.75rem;
313+
font-family: monospace;
314+
}

learn/examples.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
---
2-
layout: examples2
2+
layout: examples
33
title: Examples
44
permalink: /examples.html
55
---
66

7-
coming soon ...
7+
This is a small collection of modern coding practice in Csound.
8+
9+
## Pling
10+
11+
Three lines of code generating a structure of short events.
12+
13+
```csound
14+
instr Pling
15+
// generate one tone in a wide range between short/noisy and long/pitched
16+
aMode = mode(mpulse(ampdb(random:i(-22,0)),p3),mtof:i(random:i(80,100)),10^(p3-1))
17+
// distribute anywhere in the stereo field and output
18+
aL,aR pan2 aMode,random:i(0,1)
19+
out(aL,aR)
20+
// call the next instance of this instrument
21+
schedule("Pling",rnd:i(1),random:i(1,4))
22+
endin
23+
schedule("Pling",0,3)
24+
```

learn/examples/examples.js

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,76 @@
1+
// play "pling.csd"
12

3+
// csound.js is the Csound WASM module
4+
const csoundjs = "https://www.unpkg.com/@csound/browser@6.18.7/dist/csound.js";
25
// csound is the Csound engine object (null as we start)
36
let csound = null;
47

5-
// csound synthesis code
6-
const code = `
7-
instr 1
8-
out linenr(oscili(0dbfs*p4,p5),0.01,0.5,0.01)
9-
endin
10-
schedule(1,0,1,0.2,A4)
11-
`;
12-
13-
// this is the JS function to run Csound
14-
async function play() {
8+
// instrument on/off state
9+
let isOn = false;
10+
// CSD file name
11+
const csd = '/learn/examples/pling.csd'
12+
// this is the JS function to start Csound
13+
// and resume performance if needed
14+
async function start() {
1515
// if the Csound object is not initialised
1616
if(csound == null) {
1717
// import the Csound method from csound.js
18-
const { Csound } = await import('https://www.unpkg.com/@csound/browser@6.18.7/dist/csound.js');
19-
// create a Csound engine object
18+
const { Csound } = await import(csoundjs);
19+
// create a Csound engine object inside the context actx
2020
csound = await Csound();
21-
// set realtime audio (dac) output
22-
await csound.setOption("-odac");
23-
// compile csound code
24-
await csound.compileOrc(code);
21+
// copy the CSD file to the Csound local filesystem
22+
await copyUrlToLocal(csd,csd)
23+
// compile the code in the CSD file
24+
await csound.compileCsd(csd)
25+
// handle Csound messages
26+
await csound.on("message", handleMessage);
2527
// start the engine
2628
await csound.start();
27-
} else
28-
// if not just send an event to play a sound
29-
await csound.inputMessage('i1 0 1 0.2 440');
29+
isOn = true;
30+
}
31+
// start performance if paused
32+
if(!isOn) {
33+
await csound.resume();
34+
isOn = true;
35+
}
36+
}
37+
38+
let count = 0;
39+
function handleMessage(message) {
40+
// get the display element (called console in the page)
41+
let element = document.getElementById('console');
42+
// add the message to HTML content (plus a newline)
43+
element.innerHTML += message + '\n';
44+
// focus on bottom, new messages make the display scroll down
45+
element.scrollTop = 99999;
46+
// clear display every 1000 lines
47+
if(count == 1000) {
48+
count = 0;
49+
element.innerHTML == "";
50+
}
51+
count += 1;
52+
};
53+
54+
// Copy file to local filesystem
55+
async function copyUrlToLocal(src, dest) {
56+
// fetch the file
57+
let srcfile = await fetch(src)
58+
// get the file data as an array
59+
let dat = await srcfile.arrayBuffer();
60+
// write the data as a new file in the filesystem
61+
await csound.fs.writeFile(dest, new Uint8Array(dat));
62+
}
63+
64+
// toggle performance on/off
65+
async function pause() {
66+
if(csound != null) {
67+
if(isOn) {
68+
await csound.pause();
69+
isOn = false;
70+
} else {
71+
await csound.resume();
72+
isOn = true;
73+
}
74+
}
3075
}
3176

learn/examples/examples2.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

learn/examples/pling.csd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ seed 0
1111
1212
// by joachim heintz
1313
14-
instr PlingStruct
14+
instr Pling
1515
// generate one tone in a wide range between short/noisy and long/pitched
1616
aMode = mode(mpulse(ampdb(random:i(-22,0)),p3),mtof:i(random:i(80,100)),10^(p3-1))
1717
// distribute anywhere in the stereo field and output
1818
aL,aR pan2 aMode,random:i(0,1)
1919
out(aL,aR)
2020
// call the next instance of this instrument
21-
schedule("PlingStruct",rnd:i(1),random:i(1,4))
21+
schedule("Pling",rnd:i(1),random:i(1,4))
2222
endin
23-
schedule("PlingStruct",0,3)
23+
schedule("Pling",0,3)
2424
2525
</CsInstruments>
2626
<CsScore>

0 commit comments

Comments
 (0)