| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Simple Csound HTML5 Example</title> |
| 5 | </head> |
| 6 | <script type="text/javascript"> |
| 7 | var handleMessage = function(message) { |
| 8 | var messages_textarea = document.getElementById("messages"); |
| 9 | var existing = messages_textarea.value; |
| 10 | messages_textarea.value = existing + message; |
| 11 | messages_textarea.scrollTop = messages_textarea.scrollHeight; |
| 12 | } |
| 13 | var start_onclick = function() { |
| 14 | csound.stop(); |
| 15 | var csd = document.getElementById('csd').value; |
| 16 | csound.compileCsdText(csd); |
| 17 | csound.start(); |
| 18 | csound.perform(); |
| 19 | } |
| 20 | var stop_onclick = function() { |
| 21 | csound.stop(); |
| 22 | } |
| 23 | var gk_FM_index_oninput = function(event) { |
| 24 | var slider_value = parseFloat(event.target.value); |
| 25 | csound.setControlChannel(event.target.id, slider_value); |
| 26 | } |
| 27 | </script> |
| 28 | <body> |
| 29 | <h1>Simple Csound HTML5 Example</h1> |
| 30 | <h4>By Michael Gogins</h3> |
| 31 | <h4>2016</h4> |
| 32 | <p>The purpose of this example is to provide the simplest possible <i>useful</i> example |
| 33 | of running a Csound piece in a Web page. This example has been tested |
| 34 | in csound.node, CsoundQt, and Csound for Android. The following features are demonstrated:</p> |
| 35 | <ul> |
| 36 | <li>Instantiating Csound in csound.node, with fallback for other environments. |
| 37 | <li>Running Csound code from an HTML text area. |
| 38 | <li>Using HTML buttons to start and stop Csound. |
| 39 | <li>Displaying Csound's runtime and diagnostic messages, in real time, in an HTML text area. |
| 40 | <li>Real-time control of a Csound instrument using an HTML input element (slider). |
| 41 | </ul> |
| 42 | <p>Just examine the code of this page to see how these things are done, it is all quite simple. You can edit the |
| 43 | Csound code and run the piece again to hear your changes.</p> |
| 44 | <label for=gk_FM_index>FM index</label> |
| 45 | <input type=range min=0 max=4 value=1 id=gk_FM_index step=.001 oninput="gk_FM_index_oninput(event)"/> |
| 46 | <br><br> |
| 47 | <input type="button" value="Play" onclick="start_onclick()"/> |
| 48 | <input type="button" value="Stop" onclick="stop_onclick()"/> |
| 49 | <h4>Csound Code</h4> |
| 50 | <textarea id="csd" rows=20 cols=80> |
| 51 | <CsoundSynthesizer> |
| 52 | <CsOptions> |
| 53 | csound -dodac |
| 54 | </CsOptions> |
| 55 | <CsInstruments> |
| 56 | sr = 44100 |
| 57 | ksmps = 32 |
| 58 | nchnls = 2 |
| 59 | ;-------------------------------------------------------- |
| 60 | ;Instrument 1 : plucked strings chorused left/right and |
| 61 | ; pitch-shifted and delayed taps thru exponential |
| 62 | ; functions, and delayed. |
| 63 | ;-------------------------------------------------------- |
| 64 | |
| 65 | instr 1 |
| 66 | ishift = .00666667 ;shift it 8/1200. |
| 67 | ipch = cpspch(p5) ;convert parameter 5 to cps. |
| 68 | ioct = octpch(p5) ;convert parameter 5 to oct. |
| 69 | kvib poscil 1/120, ipch/50, 1 ;vibrato |
| 70 | ag pluck 2000, cpsoct(ioct+kvib), 1000, 1, 1 |
| 71 | agleft pluck 2000, cpsoct(ioct+ishift), 1000, 1, 1 |
| 72 | agright pluck 2000, cpsoct(ioct-ishift), 1000, 1, 1 |
| 73 | adamping linsegr 0.0, 0.006, 1.0, p3 - 0.066, 1.0, 0.06, 0.0 |
| 74 | ag = adamping * ag |
| 75 | agleft = adamping * agleft |
| 76 | agright = adamping * agright |
| 77 | af1 expon .1, p3, 1.0 ;exponential from 0.1 to 1.0 |
| 78 | af2 expon 1.0, p3, .1 ;exponential from 1.0 to 0.1 |
| 79 | adump delayr 2.0 ;set delay line of 2.0 sec |
| 80 | atap1 deltap3 af1 ;tap delay line with kf1 func. |
| 81 | atap2 deltap3 af2 ;tap delay line with kf2 func. |
| 82 | ad1 deltap3 2.0 ;delay 2 sec. |
| 83 | ad2 deltap3 1.1 ;delay 1.1 sec. |
| 84 | delayw ag ;put ag signal into delay line. |
| 85 | outs agleft+atap1+ad1, agright+atap2+ad2 |
| 86 | endin |
| 87 | ;------------------------------------------------------------- |
| 88 | ;Instrument 2 : plucked strings chorused left/right and |
| 89 | ; pitch-shifted with fixed delayed taps. |
| 90 | ;------------------------------------------------------------ |
| 91 | |
| 92 | instr 2 |
| 93 | ishift = .00666667 ;shift it 8/1200. |
| 94 | ipch = cpspch(p5) ;convert parameter 5 to cps. |
| 95 | ioct = octpch(p5) ;convert parameter 5 to oct. |
| 96 | kvib poscil 1/120, ipch/50, 1 ;vibrato |
| 97 | ag pluck 1000, cpsoct(ioct+kvib), 1000, 1, 1 |
| 98 | agleft pluck 1000, cpsoct(ioct+ishift), 1000, 1, 1 |
| 99 | agright pluck 1000, cpsoct(ioct-ishift), 1000, 1, 1 |
| 100 | adamping linsegr 0.0, 0.006, 1.0, p3 - 0.066, 1.0, 0.06, 0.0 |
| 101 | ag = adamping * ag |
| 102 | agleft = adamping * agleft |
| 103 | agright = adamping * agright |
| 104 | adump delayr 0.3 ;set delay line of 0.3 sec |
| 105 | ad1 deltap3 0.1 ;delay 100 msec. |
| 106 | ad2 deltap3 0.2 ;delay 200 msec. |
| 107 | delayw ag ;put ag sign into del line. |
| 108 | outs agleft+ad1, agright+ad2 |
| 109 | endin |
| 110 | ;----------------------------------------------------------- |
| 111 | ;Instrument 3 : New FM algorithm, modified to produce large timbre |
| 112 | ; shifts using modulation of I and r. Detuned chorusing employed. |
| 113 | ;----------------------------------------------------------- |
| 114 | instr 3 |
| 115 | ishift = .00666667 ;shift it 8/1200. |
| 116 | ipch = cpspch(p5) ;convert parameter 5 to cps. |
| 117 | ioct = octpch(p5) ;convert parameter 5 to oct. |
| 118 | aadsr linsegr 0, p3/3, 1.0, p3/3, 1.0, p3/3, 0 ;ADSR envelope |
| 119 | amodi linseg 0, p3/3, 5, p3/3, 3, p3/3, 0 ;ADSR envelope for I |
| 120 | gk_FM_index chnget "gk_FM_index" |
| 121 | amodi = amodi * gk_FM_index |
| 122 | amodr linseg p6, p3, p7 ;r moves from p6->p7 in p3 sec. |
| 123 | a1 = amodi*(amodr-1/amodr)/2 |
| 124 | a1ndx = abs(a1*2/20) ;a1*2 is normalized from 0-1. |
| 125 | a2 = amodi*(amodr+1/amodr)/2 |
| 126 | a3 tablei a1ndx, 3, 1 ;lookup tbl in f3, normal index |
| 127 | ao1 poscil a1, ipch, 2 ;cosine |
| 128 | a4 = exp(-0.5*a3+ao1) |
| 129 | ao2 poscil a2*ipch, ipch, 2 ;cosine |
| 130 | aoutl poscil 1000*aadsr*a4, ao2+cpsoct(ioct+ishift), 1 ;fnl outleft |
| 131 | aoutr poscil 1000*aadsr*a4, ao2+cpsoct(ioct-ishift), 1 ;fnl outright |
| 132 | outs aoutl, aoutr |
| 133 | endin |
| 134 | |
| 135 | </CsInstruments> |
| 136 | <CsScore> |
| 137 | ; Score for final project in Digital Audio Processing |
| 138 | ; --------------------------------------------------- |
| 139 | |
| 140 | ; Piece entitled : X A N A D U (short version) |
| 141 | ; Joseph T. Kung, 12/12/88 |
| 142 | ; Instruments modified for higher precision |
| 143 | ; Michael Gogins, 07/22/2006 |
| 144 | |
| 145 | ; The first part of the score will specify all function |
| 146 | ; tables used in the piece. The second part specifies |
| 147 | ; the instruments and notes. The latter is divided into |
| 148 | ; 7 sections, each playing a chord on a different |
| 149 | ; instrument. |
| 150 | ; The chords are uncommon guitar chords that use the open |
| 151 | ; B and E strings often. These will be transposed by |
| 152 | ; octaves on some chords. |
| 153 | |
| 154 | ; Each instrument will play a chord for 15 seconds. The |
| 155 | ; timbre |
| 156 | ; of the instrument will change in that interval and join |
| 157 | ; with the next instrument/chord sequence. Instrument 3 |
| 158 | ; uses a modified FM synthesis technique. This is joined |
| 159 | ; by an additional plucked-string instrument |
| 160 | ; (instruments 1 and 2). |
| 161 | |
| 162 | ; The Function Tables |
| 163 | ; ------------------- |
| 164 | ;All functions are post-normalized (max value is 1) if p4 is |
| 165 | ;POSITIVE. |
| 166 | |
| 167 | f1 0 65537 10 1 ;sine wave |
| 168 | f2 0 65537 11 1 ;cosine wave |
| 169 | f3 0 65537 -12 20.0 ;unscaled ln(I(x)) from 0 to 20.0 |
| 170 | |
| 171 | ;----------------------------------------------------------- |
| 172 | |
| 173 | ;----- This section comprises all the new FM sounds ----------- |
| 174 | |
| 175 | ;F#7addB chord on a guitar |
| 176 | i3 0 15 0 7.06 2.0 0.2 ;F# |
| 177 | i3 . . . 8.01 . . ;C# above |
| 178 | i3 . . . 8.06 . . ;F# octave above 1st one |
| 179 | i3 . . . 8.10 . . ;Bb next one up |
| 180 | i3 . . . 8.11 . . ;B |
| 181 | i3 . . . 9.04 . . ;E |
| 182 | |
| 183 | ;D6add9 chord on a guitar |
| 184 | i3 7.5 15 0 6.02 1.7 0.5 ;D |
| 185 | i3 . . . 6.09 . . ;A above |
| 186 | i3 . . . 7.02 . . ;D octave above 1st one |
| 187 | i3 . . . 7.06 . . ;F# next one up |
| 188 | i3 . . . 6.11 . . ;B |
| 189 | i3 . . . 7.04 . . ;E |
| 190 | |
| 191 | ;Bmajadd11 chord on a guitar |
| 192 | i3 15 15 0 7.11 1.4 0.8 ;B |
| 193 | i3 . . . 8.06 . . ;F# above |
| 194 | i3 . . . 8.11 . . ;B octave above 1st one |
| 195 | i3 . . . 9.03 . . ;D# next one up |
| 196 | i3 . . . 8.11 . . ;B |
| 197 | i3 . . . 9.04 . . ;E; |
| 198 | |
| 199 | ;Amajadd9 chord on a guitar |
| 200 | i3 22.5 15 0 6.09 1.1 1.1 ;A |
| 201 | i3 . . . 7.04 . . ;E above |
| 202 | i3 . . . 8.09 . . ;A octave above 1st one |
| 203 | i3 . . . 8.01 . . ;C# next one up |
| 204 | i3 . . . 7.11 . . ;B |
| 205 | i3 . . . 8.04 . . ;E |
| 206 | |
| 207 | ;Bmajadd11 chord on a guitar |
| 208 | i3 30 15 0 6.11 0.8 1.4 ;B |
| 209 | i3 . . . 7.06 . . ;F# above |
| 210 | i3 . . . 7.11 . . ;B octave above 1st one |
| 211 | i3 . . . 8.03 . . ;D# next one up |
| 212 | i3 . . . 7.11 . . ;B |
| 213 | i3 . . . 8.04 . . ;E; |
| 214 | |
| 215 | ;Gmaj6 chord on a guitar |
| 216 | i3 37.5 15 0 5.07 0.5 1.7 ;G |
| 217 | i3 . . . 6.02 . . ;D above |
| 218 | i3 . . . 6.07 . . ;G octave above 1st one |
| 219 | i3 . . . 6.11 . . ;B on G string |
| 220 | i3 . . . 6.11 . . ;B |
| 221 | i3 . . . 7.04 . . ;E |
| 222 | |
| 223 | ;F#7addB chord on a guitar |
| 224 | i3 45 15 0 7.06 0.2 2.0 ;F# |
| 225 | i3 . . . 8.01 . . ;C# above |
| 226 | i3 . . . 8.06 . . ;F# octave above 1st one |
| 227 | i3 . . . 8.10 . . ;Bb next one up |
| 228 | i3 . . . 8.11 . . ;B |
| 229 | i3 . . . 9.04 . . ;E |
| 230 | |
| 231 | ; This section adds the plucked chords to the beginning of each |
| 232 | ; section. |
| 233 | |
| 234 | ;F#7addB chord on a guitar |
| 235 | i1 0 10 0 8.06 ;F# |
| 236 | i1 0.1 . . 9.01 ;C# above |
| 237 | i1 0.2 . . 9.06 ;F# octave above 1st one |
| 238 | i1 0.3 . . 9.10 ;Bb next one up |
| 239 | i1 0.4 . . 9.11 ;B |
| 240 | i1 0.5 . . 10.04 ;E |
| 241 | |
| 242 | ;D6add9 chord on a guitar |
| 243 | i2 7.5 10 0 8.02 ;D |
| 244 | i2 7.6 . . 8.09 ;A above |
| 245 | i2 7.7 . . 9.02 ;D octave above 1st one |
| 246 | i2 7.8 . . 9.06 ;F# next one up |
| 247 | i2 7.9 . . 9.11 ;B |
| 248 | i2 8.0 . . 10.04 ;E |
| 249 | |
| 250 | ;Bmajadd11 chord on a guitar |
| 251 | i2 15 10 0 8.11 ;B |
| 252 | i2 15.1 . . 9.06 ;F# above |
| 253 | i2 15.2 . . 9.11 ;B octave above 1st one |
| 254 | i2 15.3 . . 10.03 ;D# next one up |
| 255 | i2 15.4 . . 9.11 ;B |
| 256 | i2 15.5 . . 10.04 ;E; |
| 257 | |
| 258 | ;Amajadd9 chord on a guitar |
| 259 | i2 22.5 10 0 8.09 ;A |
| 260 | i2 22.6 . . 9.04 ;E above |
| 261 | i2 22.7 . . 10.09 ;A octave above 1st one |
| 262 | i2 22.8 . . 10.01 ;C# next one up |
| 263 | i2 22.9 . . 9.11 ;B |
| 264 | i2 23.0 . . 10.04 ;E |
| 265 | |
| 266 | ;Bmajadd11 chord on a guitar |
| 267 | i2 30 10 0 8.11 ;B |
| 268 | i2 30.1 . . 9.06 ;F# above |
| 269 | i2 30.2 . . 9.11 ;B octave above 1st one |
| 270 | i2 30.3 . . 10.03 ;D# next one up |
| 271 | i2 30.4 . . 9.11 ;B |
| 272 | i2 30.5 . . 10.04 ;E; |
| 273 | |
| 274 | ;Gmaj6 chord on a guitar |
| 275 | i2 37.5 10 0 8.07 ;G |
| 276 | i2 37.6 . . 9.02 ;D above |
| 277 | i2 37.7 . . 9.07 ;G octave above 1st one |
| 278 | i2 37.8 . . 9.11 ;B on G string |
| 279 | i2 37.9 . . 9.11 ;B |
| 280 | i2 38.0 . . 10.04 ;E |
| 281 | |
| 282 | ;F#7addB chord on a guitar |
| 283 | i1 45 10 0 9.06 ;F# |
| 284 | i1 45.1 . . 10.01 ;C# above |
| 285 | i1 45.2 . . 10.06 ;F# octave above 1st one |
| 286 | i1 45.3 . . 10.10 ;Bb next one up |
| 287 | i1 45.4 . . 10.11 ;B |
| 288 | i1 45.5 . . 11.04 ;E |
| 289 | e |
| 290 | |
| 291 | </CsScore> |
| 292 | </CsoundSynthesizer> |
| 293 | </textarea> |
| 294 | <h4>Csound Messages</h4> |
| 295 | <textarea id="messages" rows=20 cols=80> |
| 296 | </textarea> |
| 297 | </body> |
| 298 | </html> |