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
|
<!DOCTYPE html>
<html>
<!--
csound.js canvas 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;
}
H1 {
font-size: 36px;
}
#cnvs {
touch-action: none;
background-color: #bf94f7;
}
#messages {
width: 800px;
display: flex;
align-items: center;
justify-content: center;
font-family: Monospace;
}
</style>
<script type="text/javascript" src="js/csound.js"></script>
<script type="text/javascript">
function moduleDidLoad() {
csound.Play();
csound.CompileOrc(
"instr 1 \n" +
"kfr chnget \"freq\" \n" +
"kamp chnget \"amp\" \n" +
"kcf chnget \"cf\" \n" +
"ka linenr (800-kamp)/800,.01,0.1,0.01 \n" +
"a1 vco2 ka,440+kfr \n" +
"kcf port kcf,0.01 \n" +
"a2 moogvcf a1, kcf*(1+ka), 0.8 \n" +
"al,ar freeverb a2,a2, 0.3, 0.8 \n" +
"outs al,ar \n" +
"endin");
csound.SetChannel("cf", 1000);
}
function handleMessage(message) {
var cons = document.getElementById("messages");
cons.innerText = message;
}
function main() {
var cnvs = document.getElementById("cnvs");
var cons = document.getElementById("messages");
cons.innerText = "Loading Csound...";
// called by csound.js
var playing = false;
function cnvs_getCoordinates(e) {
if (playing) {
var ctx = cnvs.getContext("2d");
var rect = cnvs.getBoundingClientRect();
x = e.clientX - rect.left
y = e.clientY - rect.top
csound.SetChannel('amp', y);
csound.SetChannel('freq', x);
ctx.clearRect(0, 0, cnvs.width, cnvs.height);
ctx.fillStyle = "#000055";
ctx.fillRect(x, y, 15, 15);
}
}
var started = false;
function cnvs_play(e) {
if (started == false) {
CsoundObj.CSOUND_AUDIO_CONTEXT.resume();
started = true;
}
csound.Event("i1 0 -1");
csound.SetChannel('cf', 3000);
var ctx = cnvs.getContext("2d");
var rect = cnvs.getBoundingClientRect();
x = e.clientX - rect.left
y = e.clientY - rect.top
csound.SetChannel('amp', y);
csound.SetChannel('freq', x);
ctx.fillStyle = "#000055";
ctx.fillRect(x, y, 15, 15);
playing = true;
}
function cnvs_stop(e) {
csound.Event("i-1 0 -1");
csound.SetChannel('cf', 1000);
var ctx = cnvs.getContext("2d");
ctx.clearRect(0, 0, cnvs.width, cnvs.height);
playing = false;
}
function cnvs_out() {}
function cnvs_getTCoordinates(e) {
e.preventDefault();
if (playing) {
var ctx = cnvs.getContext("2d");
var rect = cnvs.getBoundingClientRect();
touches = e.touches;
t = touches[0];
x = t.clientX - rect.left
y = t.clientY - rect.top
csound.SetChannel('amp', y);
csound.SetChannel('freq', x);
ctx.clearRect(0, 0, cnvs.width, cnvs.height);
ctx.fillStyle = "#000055";
ctx.fillRect(x, y, 50, 50);
}
}
var started = false;
function cnvs_tplay(e) {
e.preventDefault();
if (started == false) {
CsoundObj.CSOUND_AUDIO_CONTEXT.resume();
started = true;
}
csound.Event("i1 0 -1");
csound.SetChannel('cf', 3000);
var ctx = cnvs.getContext("2d");
var rect = cnvs.getBoundingClientRect();
touches = e.touches;
t = touches[0];
x = t.clientX - rect.left
y = t.clientY - rect.top
csound.SetChannel('amp', y);
csound.SetChannel('freq', x);
ctx.fillStyle = "#000055";
ctx.fillRect(x, y, 50, 50);
playing = true;
}
function cnvs_tstop(e) {
e.preventDefault();
csound.Event("i-1 0 -1");
csound.SetChannel('cf', 1000);
var ctx = cnvs.getContext("2d");
ctx.clearRect(0, 0, cnvs.width, cnvs.height);
playing = false;
}
cnvs.addEventListener("touchstart", cnvs_tplay, false);
cnvs.addEventListener("touchmove", cnvs_getTCoordinates, false);
cnvs.addEventListener("touchend", cnvs_tstop, false);
cnvs.addEventListener("mousedown", cnvs_play, false);
cnvs.addEventListener("mouseup", cnvs_stop, false);
cnvs.addEventListener("mousemove", cnvs_getCoordinates, false);
}
window.oncontextmenu = function(event) {
event.preventDefault();
event.stopPropagation();
return false;
};
</script>
</head>
<body onload="main()">
<H1 id='tit'>Canvas Synth</H1>
<p>
<div id="messages"> </div>
</p>
<p>
<canvas class="cnvs " id="cnvs" width=800 height=800></canvas>
</p>
</body>
</html>
|