User Tools

Site Tools


javascript:electronic-music-tools:4.17_programming_exercise
4.14 FX

https://live.codecircle.com/d/aAfgQMCcPCb2JSJj7

<body>
    <div
        tabindex="0"
        style="background:black; height:100%;"
    >
    <input style="background:black; color:white; font-size:25pt" type="text" value="" id="freq_box">
    <br>
    <input style="background:black; color:white; font-size:25pt" type="text" value="" id="freq_box_2">
    <br>
    <input style="background:black; color:white; font-size:25pt" type="text" value="" id="seq_box">
    <br>

    </div>

</body>

<script>
/*

function Sequence ( contexte, osc_type, start_freq, freq_changes, interval, box_id ) {
    this.start_freq = start_freq;
    this.freq_changes = freq_changes;
    this.interval = interval;
    this.box = document.getElementById( box_id );
    this.contexte = contexte;

    this.step = 0;
    this.current_freq = this.start_freq;

    this.osc = this.contexte.createOscillator();
    this.osc.type = osc_type;
    this.osc.frequency.value = this.start_freq;
    this.osc_amp = this.contexte.createGain();
    this.osc_amp.gain.value = 0.125;
    this.osc.connect(osc_amp);
}

Sequence.prototype.change_note = function ( when ) {
    // get the next frequency change from the
    // freq_change array
    var freq_change = this.freq_changes[this.step % this.freq_changes.length];
    // move along the step, ready for the next time we get called
    this.step ++;

    // update the frequency
    this.current_freq = this.current_freq + freq_change;

    // check if it went too high or too low
    if (this.current_freq > 1500 || this.current_freq < 100){
        this.current_freq = this.start_freq;
    }
    // print the chosen frequncy into the input tag so we can see it
    this.box.value = this.current_freq;
    // finally, change the frequency of the oscillator
    osc.frequency.setValueAtTime(this.current_freq, when);
};

var seq_1 = new Sequence ('triangle', 200, [200], 0.5, 'freq_box');

*/

var start_freq = 200;

var current_freq = start_freq;

var freq_changes = [200];

var step = 0;

var interval = 0.5; // speed up here

function changeNote(when){
    // get the next frequency change from the
    // freq_change array
    var freq_change = freq_changes[step % freq_changes.length];
    // move along the step, ready for the next time we get called
    step ++;

    // update the frequency
    current_freq = current_freq + freq_change;

    // check if it went too high or too low
    if (current_freq > 1500 || current_freq < 100){
        current_freq = start_freq;
    }
    // print the chosen frequncy into the input tag so we can see it
    document.getElementById('freq_box').value = current_freq;
    // finally, change the frequency of the oscillator
    seq_selection ();
    document.getElementById('freq_box_2').value = seq(current_freq);
    document.getElementById('seq_box').value = current_seq;
    osc.frequency.setValueAtTime(current_freq, when);
    osc2.frequency.setValueAtTime( seq(current_freq) , when);

}

function seq ( freq ) {
    switch ( current_seq ) {
      case 0:
        return (freq * 0.075) * (freq * 0.075);
      case 1:
        return 13000 - (freq * 0.075) * (freq * 0.075);
      case 2:
        return 1600 - freq;
     /* case 3:
        return
        break;
      case 4:

        break;
      case 5:

        break;*/
    }
}

var current_seq_since = 0;
var current_seq = 0;

function seq_selection () {
    current_seq_since++;
    if (current_seq_since > 6) {
        current_seq_since = 0;
        current_seq = Math.floor( Math.random() * 3);
    }
}

var audio_context = window.AudioContext || window.webkitAudioContext;
var con = new audio_context();


var osc = con.createOscillator();
osc.type = 'triangle';
osc.frequency.value = start_freq;
var osc_amp = con.createGain();
osc_amp.gain.value = 0.125;
osc.connect(osc_amp);

var osc2 = con.createOscillator();
osc2.type = 'sawtooth';
osc2.frequency.value = start_freq;
var osc2_amp = con.createGain();
osc2_amp.gain.value = 0.125;
osc2.connect(osc2_amp);



var del = con.createDelay();
osc_amp.connect(del);
osc2_amp.connect(del);

var fb = con.createGain();
del.connect(fb);
fb.connect(del);
del.delayTime.value = 0.25;
fb.gain.value = 0.75;


del.connect(con.destination);

//osc_amp.connect(con.destination);

osc.start();
osc2.start();




// this code will wake up every (wait_time) ms
// and schedule a load of drum triggers on the clock
// each time, remembering where it scheduled to in the future
// so it does not repeat anything
var wait_time = 0.5;
var got_up_to;

setInterval(function(){
    var now = con.currentTime;
    // how far into the future will we schedule?
    // we schedule beyond the next wait time as we cannot
    // rely on it being exactly 'wait_time' ms before
    // we get woken up again, therefore put in a few
    // extra events on the scheduler to cover any delays
    var max_future_time = now + (wait_time  * 1.5);
    if (got_up_to > now) {// already scheduled up to this point
        now = got_up_to;
    }

    while (now <= max_future_time){
        changeNote(now);
        now += interval;
    }
    got_up_to = now;

}, wait_time*1000);


</script>
javascript/electronic-music-tools/4.17_programming_exercise.txt · Last modified: 2017/07/19 23:38 by leo