<!DOCTYPE html> <head> <title>button1</title> </head> <html> <body> <!-- this makes an "alert" window when the 'fun fun fun!' button is clicked --> <button type="button" onclick="alert('and a WHOLE LOTTA fun!')">fun fun fun!</button> </body> </html>
<!DOCTYPE html> <head> <title>button1b</title> </head> <html> <body> <!-- the buttonfunction() is a javascript function that will print to the "innerHTML" part of the "output" tag on your main web page --> <button onclick="buttonfunction()">hey hey hey!</button> <br> <br> <!-- this is where it will print (id="output") -- getElementById will find this on your web page --> <output id="output"></output> <!-- <script> and </script> define where your javascript code lives --> <script> function buttonfunction() { document.getElementById('output').innerHTML = "I was CLICKED!" } </script> </body> </html>
<!DOCTYPE html> <head> <title>buttonjs</title> </head> <html> <body> <button onclick="buttonfunction()">count them clicks</button> <br> <br> <output id="output"></output> <script> // declare the variable outside the function so it will be 'scoped' throh // the entire script (and not just 'inside' the function) var clicktimes = 0; function buttonfunction() { clicktimes++; // same as clicktimes = clicktimes + 1; documentetElementById('output').innerHTML = "I was CLICKED this many times: " + clicktimes; } </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <titletosc1</title> <script src="../Tone.js"</script> <!-- ok, this is tricky. the "src=" has to designate where, relative to this web page, the Tone.js file is located. The "../" means it is one directory *above* the directory where this file is located. "../../Tone.js" would put it TWO directories above. Just plain src="Tone.js" means it is in the same directory as this file. Why not just do that always? Because you may want to reference it from many pages, and you would want to locate it in a place reasonably accessible from those other directories --> </head> <body> <script> var osc = new Tone.Oscillator(); // create a new Oscillator 'object' osc.frequency.value = 478.0; // set this way because it is a signal // you can't say osc.frequency = 478.0; osc.toMaster(); // connect it to the main audio output osc.start(); // START ME UP! </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <title>osc2</title> <script src="../Tone.js"></script> </head> <body> <!-- what these buttons do should be obvious --> <button onclick="startbuttonfunction()">start me up!</button> <br> <br> <button onclick="stopbuttonfunction()">STOPIT!</button> <script> var osc = new Tone.Oscillator(378); osc.toMaster(); function startbuttonfunction() { osc.start(); } function stopbuttonfunction() { osc.stop(); } </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <title>osc3</title> <script src="../Tone.js"></script> </head> <body> <button onclick="startbuttonfunction()">start me up!</button> <br> <br> <button onclick="stopbuttonfunction()">STOPIT!</button> <br> <br> <br> <!-- call the "set_freq()" function with "this.value" when the slide is moved --> <input type="range" oninput="set_freq(this.value)"> <output id="output"></output> <script> var osc = new Tone.Oscillator(378); osc.toMaster(); function startbuttonfunction() { osc.start(); } function stopbuttonfunction() { osc.stop(); } function set_freq(val) { // multiply the value by 10 because the range slider goes from 0-100 document.getElementById('output').innerHTML = val*10; osc.frequency.value = val*10; } </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <title>osc3a</title> <script src="../Tone.js"></script> </head> <body> <button onclick="startbuttonfunction()">start me up!</button> <br> <br> <button onclick="stopbuttonfunction()">STOPIT!</button> <br> <br> <br> <input type="range" oninput="set_freq(this.value)"> <output id="output"></output> <br> <br> <!-- here's where we set the max and min of the range slider --> <input type="range" oninput="set_amp(this.value)" max="0" min="-100"> <output id="output1"></output> <!-- note that we label this with id 'output1, so we can print to it from the set_amp() function --> <script> var osc = new Tone.Oscillator(378); osc.toMaster(); function startbuttonfunction() { osc.start(); } function stopbuttonfunction() { osc.stop(); } function set_freq(val) { document.getElementById('output').innerHTML = val*10; osc.frequency.value = val*10; } function set_amp(val) { // don't need to multiply val by anything becuase the range slider // was already set with the range from -100 to 0 document.getElementById('output1').innerHTML = val; osc.volume.value = val; } </script> </body> </html>
Alternatively, we could multiply the output of our oscillator by one of the Tone.Envelope or Tone.ScaledEnvelope components (the second way to do amplitude enveloping). However, there is a highly suggestively-named component in Tone.js called Tone.AmplitudeEnvelope. This is the third method for doing amplitude control, and it is the preferred method. Tone.AmplitudeEnvelope is set up to accept a signal input (like from our oscillator) and it will apply the envelope to that signal given an appropriate trigger (the triggerAttackRelease() method, for example). This is the code to do this:
<!DOCTYPE HTML> <html> <head> <title>osc4</title> <script src="../Tone.js"></script> </head> <body> <button onclick="startbuttonfunction()">start me up!</button> <br> <br> <button onclick="stopbuttonfunction()">STOPIT!</button> <br> <br> <br> <input type="range" oninput="set_freq(this.value)"> <output id="output"></output> <br> <br> <!-- we can still set the amp of the basic oscillator; the AmplitudeEnvelope will work upon that base amp --> <input type="range" oninput="set_amp(this.value)" max="0" min="-100"> <output id="output1"></output> <br> <br> <br> <br> <!-- this will trigger the AmplitudeEnevelope --> <button onclick="triggerbuttonfunction()">GO!</button> <script> var osc = new Tone.Oscillator(378); // the params are attack time, initial decay time, sustain level (0-1), release time var ampenv = new Tone.AmplitudeEnvelope(0.01, 0.0, 1.0, 0.1); // connect the oscillator to the input of the ampenv osc.connect(ampenv) // and then the ampenv goes out to the Master ampenv.toMaster(); function startbuttonfunction() { osc.start(); } function stopbuttonfunction() { osc.stop(); } function set_freq(val) { document.getElementById('output').innerHTML = val*10; osc.frequency.value = val*10; } function set_amp(val) { document.getElementById('output1').innerHTML = val; osc.volume.value = val; } function triggerbuttonfunction() { // 0.5 is how long the note will last (at the sustain level) ampenv.triggerAttackRelease(0.5); } </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <title>osc4a</title> <script src="../Tone.js"></script> </head> <body> <br> <input type="range" oninput="set_freq(this.value)"> <output id="output"></output> <br> <br> <input type="range" oninput="set_amp(this.value)" max="0" min="-100"> <output id="output1"></output> <br> <br> <br> <br> <button onclick="triggerbuttonfunction()">GO!</button> <script> var osc = new Tone.Oscillator(378); var ampenv = new Tone.AmplitudeEnvelope(0.01, 0.0, 1.0, 0.1); osc.connect(ampenv) ampenv.toMaster(); // start the oscillator immediately osc.start(); function set_freq(val) { document.getElementById('output').innerHTML = val*10; osc.frequency.value = val*10; } function set_amp(val) { document.getElementById('output1').innerHTML = val; osc.volume.value = val; } function triggerbuttonfunction() { // shorter duration just for fun ampenv.triggerAttackRelease(0.1); } </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <title>osc5</title> <script src="../Tone.js"></script> </head> <body> <br> <input type="range" oninput="set_freq(this.value)"> <output id="output"></output> <br> <br> <input type="range" oninput="set_amp(this.value)" max="0" min="-100"> <output id="output1"></output> <br> <br> <br> <br> <button onclick="triggerbuttonfunction()">GO!</button> <script> var osc = new Tone.Oscillator(378); // set our LFO to 3.5 Hz, with a range from 0 to 30 (this will be added // to the frequency set by the range slider) var LFO = new Tone.LFO(3.4, 0, 30); // connect the LFO to the oscillator frequency input and start it up LFO.connect(osc.frequency); LFO.start(); var ampenv = new Tone.AmplitudeEnvelope(0.01, 0.0, 1.0, 0.1); osc.connect(ampenv) ampenv.toMaster(); osc.start(); function set_freq(val) { document.getElementById('output').innerHTML = val*10; osc.frequency.value = val*10; } function set_amp(val) { document.getElementById('output1').innerHTML = val; osc.volume.value = val; } function triggerbuttonfunction() { ampenv.triggerAttackRelease(3.5); } </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <title>oscwaves</title> <script src="../Tone.js"></script> </head> <body> <button onclick="startbuttonfunction()">start me up!</button> <br> <br> <button onclick="stopbuttonfunction()">STOPIT!</button> <script> // this will use a square wave. See the Tone.js documentation for others var osc = new Tone.Oscillator(378, "square"); osc.toMaster(); function startbuttonfunction() { osc.start(); } function stopbuttonfunction() { osc.stop(); } </script> </body> </html>
<!DOCTYPE HTML> <html> <head> <title>fmosc1</title> <script src="../Tone.js"></script> </head> <body> <button onclick="startbuttonfunction()">ho ho!</button> <br> <br> <button onclick="stopbuttonfunction()">ha ha!</button> <br> <br> <br> <!-- we're using a range of 100 for the slider to give us finer 'gradations' in the index value. We scale this to 0-10 by divding by 10 in the set_index() function below --> <input type="range" oninput="set_index(this.value)" min="0" max="100"> <output id="output"></output> <br> <br> <!-- we're doing the same kind of scaling here for the harmonicity. the harmonicity is a multipler of the base (carrier) frequency to get the modulation frequency. Non-integer multiples will produce non-harmonic sidebands --> <input type="range" oninput="set_harmonicity(this.value)" min="1" max="50"> <output id="output1"></output> <script> // here is the 'object-oriented' way of initializing values when // we create the FMOscillator var fmosc = new Tone.FMOscillator ( { frequency : 314, modulationIndex : 2, modulationType : "sine", harmonicity : 1 } ); fmosc.toMaster(); function startbuttonfunction() { fmosc.start(); } function stopbuttonfunction() { fmosc.stop(); } function set_index(val) { document.getElementById('output').innerHTML = val/10; fmosc.modulationIndex.value = val/10; } function set_harmonicity(val) { document.getElementById('output1').innerHTML = val/10; fmosc.harmonicity.value = val/10; } </script> </body> </html>