-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
104 lines (96 loc) · 2.63 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>Plucks</title>
</head>
<script type="text/javascript">
// csound is the Csound engine object (null as we start)
let csound = null;
// header code
const head = `
0dbfs = 1
seed 0
giNotes[] fillarray 45,48,52,55,60,62,64,67,69,72,74,76,79
giAmps[] fillarray 0.05,0.1,0.2,0.3,0.4,0.6
giDurs[] fillarray 0.3,0.075,0.1,0.2,0.15,0.4
`;
// recursion code
const recurse = `
instr 1
prints "freq:%dHz",p5
out linenr(pluck(p4,p5,p5,0,1),0,0.1,0.01)
icps = cpsmidinn(giNotes[gauss(6)+6])
iamp = giAmps[gauss(3)+3]
schedule(1,giDurs[gauss(3)+3],800*iamp/icps,iamp,icps)
endin
schedule(1,0,0.01,0.001,500)
`;
// stop code
const stop = `
instr 1
out linenr(pluck(p4,p5,p5,0,1),0,0.1,0.01)
endin
`;
// instrument on/off state
let isOn = false;
// message handling
function handleMessage(msg){
// if running, display messages
if (isOn) document.getElementById('mess').value = msg;
// else just fill with blanks
else document.getElementById('mess').value = " ";
}
// this is the JS function to start Csound
// and toggle recursion on/off
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/[email protected]/dist/csound.js');
// create a Csound engine object
csound = await Csound();
// set realtime audio (dac) output
await csound.setOption("-odac");
// compile csound code
await csound.compileOrc(head + recurse);
// start the engine
await csound.start();
// add a listener for Csound messages
await csound.on("message", handleMessage);
// change text to ON
document.getElementById("start button").value = "ON";
// change colour to RED
document.getElementById("start button").style.color = "red";
}
// if csound engine is running
else {
// check if instrument is running
if(isOn) {
// turn it off
await csound.evalCode(stop)
// change text to OFF
document.getElementById("start button").value = "OFF";
// change colour to black
document.getElementById("start button").style.color = "black";
} else {
// turn it on
await csound.evalCode(recurse)
// change text to ON
document.getElementById("start button").value = "ON";
// change colour to red
document.getElementById("start button").style.color = "red";
}
}
// toggle instrument state
isOn = !isOn;
}
</script>
<body>
<h1>Plucks</h1>
<p>
<input type="button" id="start button" onclick="start()" value="OFF"> </input>
</p>
<textarea cols="80" rows="1" id="mess"></textarea>
<hr>
<p><a href="./readme.html">README</a></p>
<!-- hhmts start -->Last modified: Sat Sep 28 18:22:55 IST 2024 <!-- hhmts end -->
</body> </html>