| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>CsoundQt and WebAudio Csound html template</title> |
| 5 | </head> |
| 6 | <script type="text/javascript" src="js/csound.js"></script> |
| 7 | <script type="text/javascript"> |
| 8 | |
| 9 | |
| 10 | function moduleDidLoad() { // necessary for WebAudio Csound |
| 11 | console.log("Module did load. Press Start button"); |
| 12 | // do not put any csound calls here, otherwise it is missed in CsoundQt |
| 13 | } |
| 14 | |
| 15 | var handleMessage = function(message) { |
| 16 | var messages_textarea = document.getElementById("messages"); |
| 17 | var existing = messages_textarea.value; |
| 18 | messages_textarea.value = existing + message; |
| 19 | messages_textarea.scrollTop = messages_textarea.scrollHeight; |
| 20 | } |
| 21 | |
| 22 | function start() { |
| 23 | var orc = document.getElementById('orc').value; |
| 24 | csound.CompileOrc(orc); |
| 25 | csound.Start(); |
| 26 | } |
| 27 | |
| 28 | function stop() { |
| 29 | if (typeof(qt) === "object") { |
| 30 | csound.Stop(); // for CsoundQt-html |
| 31 | } else if ( typeof(csound.Csound)==="object" ) { // WebaudioCsound Csound() object |
| 32 | csound.Pause(); //otherwise resets Csound and audio context |
| 33 | } else { |
| 34 | csound.stop(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function getHarmonics() { |
| 39 | var value; |
| 40 | if (typeof(qt) === "object") { |
| 41 | // get values from CsoundQt Webchannel are always returned via callbacks |
| 42 | csound.getControlChannel("harmonics", function(value) { |
| 43 | document.getElementById("harmonics").innerHTML = value; |
| 44 | }); |
| 45 | } else if ( typeof(csound.Csound)==="object" ) { // WebaudioCsound Csound() object |
| 46 | var value = csound.RequestChannel("harmonics"); |
| 47 | document.getElementById("harmonics").innerHTML = value; |
| 48 | } else { |
| 49 | var value = csound.getControlChannel("harmonics") // should work in csound.node |
| 50 | document.getElementById("harmonics").innerHTML = value; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | function makeSound() { |
| 55 | csound.Event("i 1 0 1") |
| 56 | setTimeout(getHarmonics , 50); // give a little bit of time for the value to be set in Csound |
| 57 | } |
| 58 | |
| 59 | |
| 60 | </script> |
| 61 | <body> |
| 62 | <h1>Html template for CsoundQt and WebAudio Csound</h1> |
| 63 | <br> |
| 64 | Press "Strart" to compile the orchestra, "Event" to make the sound and display number of harmonics used.<br> |
| 65 | |
| 66 | <br> |
| 67 | <input type="button" value="Start" onclick="start()"/> |
| 68 | <input type="button" value="Stop" onclick="stop()"/><br> |
| 69 | |
| 70 | <button onclick='makeSound()'>Event</button> |
| 71 | Number of harmonics: <label id="harmonics">?</label><br> |
| 72 | Volume: <input type="range" id="slider" oninput='csound.SetChannel("volume",this.value/100.0); '></input><br> |
| 73 | <h4>Csound Code</h4> |
| 74 | <textarea id="orc" rows=20 cols=80> |
| 75 | |
| 76 | sr = 44100 |
| 77 | ksmps = 32 |
| 78 | nchnls = 2 |
| 79 | 0dbfs = 1 |
| 80 | |
| 81 | chn_k "volume", 1 // the channel must be declared to work in CsoundQt |
| 82 | chnset 0.5, "volume" |
| 83 | |
| 84 | instr 1 |
| 85 | ifreq random 200, 400 |
| 86 | iharm = int(random:i(1,20)) |
| 87 | chnset iharm, "harmonics" |
| 88 | print ifreq, iharm |
| 89 | kvolume chnget "volume" |
| 90 | asig buzz adsr(0.1,0.1,0.5, 0.5) *chnget:k("volume"), ifreq, iharm, -1 |
| 91 | outs asig, asig |
| 92 | endin |
| 93 | |
| 94 | </textarea> |
| 95 | <h4>Csound Messages</h4> |
| 96 | <textarea id="messages" rows=10 cols=80> |
| 97 | In CsoundQt the messages are directed to console tab. |
| 98 | </textarea> |
| 99 | </body> |
| 100 | </html> |