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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
<!DOCTYPE html>
<html>
<!--
WebAudio Csound example
Copyright (C) 2017 V Lazzarini
-->
<head>
<title>WebAudio Csound</title>
<style type="text/css">
html, body {
font-family: Monospace;
color: #bf94f7;
background-color: #000055;
}
#console {
font-family: Monospace;
color: #b5b6ff;
background-color: #000000;
font-size: 16px;
width: 805px;
display: flex;
align-items: center;
justify-content: center;
border-style: solid;
padding: 20px 0px;
}
H1 {
font-size: 36px;
}
#playButton {
margin: auto;
font-family: Monospace;
width: 810px;
padding: 10px 0px;
font-size: 24px;
background-color: #b5b6ff;
}
</style>
<script src="js/CsoundObj.js"></script>
<script type="text/javascript">
var random_colour;
// fancy colours
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// message printout
function display_msg(message) {
var messField = document.getElementById("console")
messField.innerText = message;
random_colour = messField.style.color = getRandomColor();
}
function print_msg(message) {
csound.requestControlChannel("freq", function() {
freq = Math.round(csound.getControlChannel("freq"));
mess = message + " :: " + freq + " Hz";
display_msg(mess);
});
}
function main() {
CsoundObj.initialize().then(() => {
console.log = print_msg;
console.warn = print_msg;
document.getElementById('playButton').addEventListener('click',
click_this);
document.getElementById('playButton').disabled = false;
CsoundObj.CSOUND_AUDIO_CONTEXT.suspend();
csound = new CsoundObj();
csound.compileOrc(document.getElementById('instruments').value);
csound.start();
window.addEventListener("unload", function(e) {
if (csound != null)
csound.destroy();
}, false);
scopeNode = CsoundObj.CSOUND_AUDIO_CONTEXT.createAnalyser();
csound.getNode().connect(scopeNode);
scope = function() {
let ctx = document.getElementById('scope').getContext('2d');
let width = ctx.canvas.width;
let height = ctx.canvas.height;
let timeData = new Uint8Array(scopeNode.frequencyBinCount);
let scaling = height / 256;
let risingEdge = 0;
let edgeThreshold = 5;
scopeNode.getByteTimeDomainData(timeData);
ctx.fillStyle = 'rgba(0, 20, 0, 0.1)';
ctx.fillRect(0, 0, width, height);
ctx.lineWidth = 2;
ctx.strokeStyle = random_colour;
ctx.beginPath();
while (timeData[risingEdge++] - 128 > 0 && risingEdge <= width);
if (risingEdge >= width) risingEdge = 0;
while (timeData[risingEdge++] - 128 < edgeThreshold &&
risingEdge <= width);
if (risingEdge >= width) risingEdge = 0;
for (var x = risingEdge; x < timeData.length &&
x - risingEdge < width; x++)
ctx.lineTo(x - risingEdge, height - timeData[x] * scaling);
ctx.stroke();
requestAnimationFrame(scope);
}
scope();
mags = function() {
let ctx = document.getElementById('mags').getContext('2d');
let width = ctx.canvas.width;
let height = ctx.canvas.height;
let freqData = new Uint8Array(scopeNode.frequencyBinCount);
let scaling = height / 256;
scopeNode.getByteFrequencyData(freqData);
ctx.fillStyle = 'rgba(0, 20, 0, 0.1)';
ctx.fillRect(0, 0, width, height);
ctx.lineWidth = 2;
ctx.strokeStyle = random_colour;
ctx.beginPath();
for (var x = 0; x < width; x++)
ctx.lineTo(x, height - freqData[x] * scaling);
ctx.stroke();
requestAnimationFrame(mags);
}
mags();
});
}
var playing = false;
function click_this() {
if(playing == false) {
CsoundObj.CSOUND_AUDIO_CONTEXT.resume();
document.getElementById('playButton').innerText = "pause";
playing = true;
} else {
CsoundObj.CSOUND_AUDIO_CONTEXT.suspend();
document.getElementById('playButton').innerText = "play";
playing = false;
}
}
</script>
</head>
<body onload="main()">
<h1> Random Generator</h1>
<div id="console">...loading...
</div>
<canvas id='scope' width=400 height=200 style="border:1px solid#000000;">
</canvas>
<canvas id='mags' width=400 height=200 style="border:1px solid#000000;">
</canvas>
<p>
<button id="playButton" disabled>play</button>
</p>
<textarea class="code" id="instruments" hidden>
/* this is the synthesis code
for this example
*/
nchnls=2
0dbfs=1
instr 1
k1 expon p4,p3,p4*0.001
a1 oscili k1,p5
chnset p5, "freq"
outs a1,a1
schedule 1,0.25,0.5,0.3+rnd(0.2),1000+birnd(500)
endin
schedule 1,0,0.5,0.1,500
</textarea>
</body>
</html>
|