oscillator test

by jmchen

HTML

<script src='https://jyunming-chen.github.io/WebAudio/js/shared.js'></script>
<button id='play'>Play/pause</button>
<button id='loC'>low-C</button>
<button id='loA'>low-A</button>
<div>
    <input type="radio" name="ir" value="0" class="effect" onclick="sample.changeType('sine')">Sine</input>
    <input type="radio" name="ir" value="1" class="effect" checked onclick="sample.changeType('square')">Square</input>
    <input type="radio" name="ir" value="2" class="effect" onclick="sample.changeType('sawtooth')">Sawtooth</input>
    <input type="radio" name="ir" value="3" class="effect" onclick="sample.changeType('triangle')">Triangle</input>
</div>

JavaScript

$("#play").click(function () {
    sample.toggle();
});
$("#loC").click(function () {
    //    sample.oscillator.frequency.value = 440/Math.pow (semitone, 9);
    frequency = 440 / Math.pow(semitone, 9);
    //   sample.stop(); sample.play();
});

$("#loA").click(function () {
    //    sample.oscillator.frequency.value = 440;
    frequency = 440;
    //    sample.stop(); sample.play();
});
///////////////////////////////////////////////////////////////////////////

var semitone = Math.pow(2, 1 / 12);
var frequency = 440;

var sample = new OscillatorSample();

///////////////////////////////////////////////////////////////////////////
function OscillatorSample() {
    this.isPlaying = false;
}

OscillatorSample.prototype.play = function () {
    // Create some sweet sweet nodes.
    this.oscillator = context.createOscillator();
    this.analyser = context.createAnalyser();

    this.oscillator.frequency.value = frequency;

    // Setup the graph.
    this.oscillator.connect(this.analyser);
    this.analyser.connect(context.destination);

   // this.oscillator[this.oscillator.start ? 'start' : 'noteOn'](0);
    this.oscillator.start(1);
    //    this.isPlaying = true;
};

OscillatorSample.prototype.stop = function () {
    this.oscillator.stop(0);
    //   this.isPlaying = false;
};

OscillatorSample.prototype.toggle = function () {
    (this.isPlaying ? this.stop() : this.play());
    this.isPlaying = !this.isPlaying;
};

///////////////////////////////////////////////////////////////////////////
OscillatorSample.prototype.changeFrequency = function (val) {
    this.oscillator.frequency.value = val;
};

OscillatorSample.prototype.changeDetune = function (val) {
    this.oscillator.detune.value = val;
};

OscillatorSample.prototype.changeType = function (type) {
    this.oscillator.type = type;
};