| 1 | <!doctype html> |
| 2 | <html lang="en-us"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 | <title>ChucK + JS => ChucKScript</title> |
| 7 | <link rel="stylesheet" href="../../css/editor.css"> |
| 8 | </head> |
| 9 | <body onresize="chuckEditor.resize();"> |
| 10 | <div> |
| 11 | <div id="program">// HID 2: Mouse |
| 12 | // name: mouse-fm.ck |
| 13 | // desc: uses first X/Y axes of a mouse to control mf and index for FM |
| 14 | // author: Spencer Salazar |
| 15 | |
| 16 | global Event mouseMoved, mouseDown, mouseUp; |
| 17 | global int mouseDeltaX, mouseDeltaY; |
| 18 | // modulator to carrier |
| 19 | SinOsc m => SinOsc c => Envelope e => dac; |
| 20 | |
| 21 | // carrier frequency |
| 22 | 220 => c.freq; |
| 23 | // modulator frequency |
| 24 | 550 => m.freq; |
| 25 | // index of modulation |
| 26 | 1000 => m.gain; |
| 27 | |
| 28 | // phase modulation is FM synthesis (sync is 2) |
| 29 | 2 => c.sync; |
| 30 | |
| 31 | // attack |
| 32 | 10::ms => e.duration; |
| 33 | .5 => e.gain; |
| 34 | // variables |
| 35 | int base; |
| 36 | float a0; |
| 37 | float a1; |
| 38 | float a2; |
| 39 | int count; |
| 40 | |
| 41 | // start things |
| 42 | set( base, a0, a1, a2 ); |
| 43 | |
| 44 | |
| 45 | fun void RespondToMouseMovement() |
| 46 | { |
| 47 | while( true ) |
| 48 | { |
| 49 | mouseMoved => now; |
| 50 | mouseDeltaX * .001 + a0 => a0; |
| 51 | mouseDeltaY * .001 + a1 => a1; |
| 52 | set( base, a0, a1, a2 ); |
| 53 | } |
| 54 | } |
| 55 | spork ~ RespondToMouseMovement(); |
| 56 | |
| 57 | fun void RespondToMouseClickDown() |
| 58 | { |
| 59 | while( true ) |
| 60 | { |
| 61 | mouseDown => now; |
| 62 | count++; |
| 63 | if( count == 1 ) e.keyOn(); |
| 64 | set( base, a0, a1, a2 ); |
| 65 | } |
| 66 | } |
| 67 | spork ~ RespondToMouseClickDown(); |
| 68 | |
| 69 | fun void RespondToMouseClickUp() |
| 70 | { |
| 71 | while( true ) |
| 72 | { |
| 73 | mouseUp => now; |
| 74 | count--; |
| 75 | if( count <= 0 ) |
| 76 | { |
| 77 | e.keyOff(); |
| 78 | 0 => count; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | spork ~ RespondToMouseClickUp(); |
| 83 | |
| 84 | // infinite time loop |
| 85 | while( true ) |
| 86 | { |
| 87 | 0.1::second => now; |
| 88 | <<< "carrier:", c.freq(), "modulator:", m.freq(), "index:", m.gain() >>>; |
| 89 | } |
| 90 | // mapping function |
| 91 | fun void set( int base, float v0, float v1, float v2 ) |
| 92 | { |
| 93 | // modulator frequency |
| 94 | ( 500 + 5*base + ( 500 * v0) ) => m.freq; |
| 95 | // carrier frequency |
| 96 | ( 220 + (220 * v2) ) => c.freq; |
| 97 | // index of modulation |
| 98 | ( 1000 * (v1+1) ) => m.gain; |
| 99 | //<<< "carrier:", c.freq(), "modulator:", m.freq(), "index:", m.gain() >>>; |
| 100 | } |
| 101 | </div> |
| 102 | </div> |
| 103 | <div class="buttons"> |
| 104 | <button class="chuckButton" id="startChuck" disabled="disabled">Start ChucK</button> |
| 105 | <button class="chuckButton" id="compileButton" disabled="disabled">Compile and Run</button> |
| 106 | <button class="chuckButton" id="replaceButton" disabled="disabled">Replace</button> |
| 107 | <button class="chuckButton" id="removeButton" disabled="disabled">Remove</button> |
| 108 | <button class="chuckButton" id="clearButton" disabled="disabled">Clear VM and Globals</button> |
| 109 | <button class="chuckButton" id="micButton" disabled="disabled">Enable Microphone</button> |
| 110 | <button class="chuckButton" id="fileFormButton">Show File Uploader</button> |
| 111 | </div> |
| 112 | <div id="fileForm" class="hidden"> |
| 113 | <p>File name to use in ChucK: <input type="text" id="uploadFilename"></p> |
| 114 | <p>Local file to upload: <input type="file" id="uploadFile" name="uploadFile"></p> |
| 115 | <p><button class="chuckButton" id="fileButton" disabled="disabled">Upload File to Virtual File System</button></p> |
| 116 | </div> |
| 117 | <div> |
| 118 | <textarea id="output"></textarea> |
| 119 | <div id="shreds"> |
| 120 | <table id="shredstable"> |
| 121 | <col width="60px"> |
| 122 | <col width="1*"> |
| 123 | <col width="50px"> |
| 124 | <col width="60px"> |
| 125 | <tr><th>shred</th><th>code</th><th>time</th><th>remove</th> |
| 126 | </table> |
| 127 | </div> |
| 128 | <div class="clearfix"></div> |
| 129 | </div> |
| 130 | <script type="text/javascript" src="../../js/ace-min-noconflict/ace.js" charset="utf-8"></script> |
| 131 | <script type="text/javascript" src="../../js/editor.js"></script> |
| 132 | <script type="text/javascript" src="../../js/defer.js"></script> |
| 133 | <script> |
| 134 | // override |
| 135 | var whereIsChuck = "../../js"; |
| 136 | </script> |
| 137 | <script type="text/javascript" src="../../js/webchuck_host.js"></script> |
| 138 | <script type="text/javascript" src="../../js/host.js"></script> |
| 139 | <script> |
| 140 | theChuckReady.then( function() |
| 141 | { |
| 142 | var prevX, prevY; |
| 143 | window.addEventListener( "mousemove", function( e ) |
| 144 | { |
| 145 | var x = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); |
| 146 | var y = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); |
| 147 | theChuck.setInt( "mouseDeltaX", x - prevX ); |
| 148 | theChuck.setInt( "mouseDeltaY", y - prevY ); |
| 149 | theChuck.broadcastEvent( "mouseMoved" ); |
| 150 | prevX = x; |
| 151 | prevY = y; |
| 152 | }); |
| 153 | window.addEventListener( "mousedown", function( e ) |
| 154 | { |
| 155 | theChuck.broadcastEvent( "mouseDown" ); |
| 156 | }); |
| 157 | window.addEventListener( "mouseup", function( e ) |
| 158 | { |
| 159 | theChuck.broadcastEvent( "mouseUp" ); |
| 160 | }); |
| 161 | }); |
| 162 | </script> |
| 163 | </body> |
| 164 | </html> |
| 165 | |
| 166 |