var filter = new dsp.Filter(); var osc1 = new dsp.Oscillator(); var osc2 = new dsp.Oscillator(); var lfo = new dsp.Oscillator(); var noise = new dsp.Noise(); var dist = new dsp.Distortion(); var env1 = new dsp.Envelope(); var env2 = new dsp.Envelope(); var delL = new dsp.Delay(); var delR = new dsp.Delay(); var patches; var playing = false; var pitch = 0; (function(){ var osc, dev, sampleRate, fx; function fillBuffer(buffer, channelCount){ var l = buffer.length,i, n; if(channelCount!=2) return; if(gui.values.wave1_shape==1) { osc1.setType(OSC_SQUARE_WARP); } else if(gui.values.wave1_shape==0) { osc1.setType(OSC_TRI_WARP); } if(gui.values.wave2_shape==1) { osc2.setType(OSC_SQUARE_WARP); } else if(gui.values.wave2_shape==0) { osc2.setType(OSC_TRI_WARP); } if(gui.values.lfo_shape==0) { lfo.setType(OSC_TRI); } else if(gui.values.lfo_shape==1) { lfo.setType(OSC_SQUARE); } else { lfo.setType(OSC_SAW); } lfo.setFrequency(gui.values.lfo_speed); env1.setParameters( gui.values.env1_attack, gui.values.env1_decay, gui.values.env1_sustain, gui.values.env1_release ); env2.setParameters( gui.values.env2_attack, gui.values.env2_decay, gui.values.env2_sustain, gui.values.env2_release ); dist.gain = gui.values.dist_gain; dist.volume = gui.values.dist_volume; var osc1freq = pitch; if(gui.values.wave1_octave==0) osc1freq *= 2; else if(gui.values.wave1_octave==2) osc1freq /= 2; osc1freq *= Math.pow(2, gui.values.wave1_pitch); var osc2freq = pitch; if(gui.values.wave2_octave==0) osc2freq *= 2; else if(gui.values.wave2_octave==2) osc2freq /= 2; osc2freq *= Math.pow(2, gui.values.wave2_pitch); noise.setColour(gui.values.noise_colour); for(i = 0; i < l; i+=channelCount) { var lfoOut = lfo.getSample(); var env2Out = env2.getSample(); osc1.setFrequency(osc1freq + lfoOut*gui.values.wave1_pitch_lfo*osc1freq*0.05 + env2Out*gui.values.wave1_pitch_env*osc1freq*0.05); osc1.setWarp(gui.values.wave1_warp + lfoOut*gui.values.wave1_warp_lfo); osc2.setFrequency(osc2freq + lfoOut*gui.values.wave2_pitch_lfo*osc2freq*0.05 + env2Out*gui.values.wave2_pitch_env*osc2freq*0.05); osc2.setWarp(gui.values.wave2_warp + lfoOut*gui.values.wave2_warp_lfo); filter.setCoeffs(gui.values.cutoff+lfoOut*gui.values.filter_lfo_mod*gui.values.cutoff*0.5+env2Out*gui.values.filter_env_mod*gui.values.cutoff, gui.values.resonance); var smpl = osc1.getSample() * gui.values.wave1_volume;//0.1; if(gui.values.ring) { smpl *= osc2.getSample() * gui.values.wave2_volume; } else { smpl += osc2.getSample() * gui.values.wave2_volume; } smpl += noise.getSample()* gui.values.noise_volume; smpl *= env1.getSample(); smpl = filter.process(smpl); smpl = smpl*(1.0-gui.values.dist_mix) + gui.values.dist_mix*dist.process(smpl); buffer[i] = smpl;//delL.process(smpl)*0.5;//buff[i]*0.1; buffer[i+1] = smpl;//delR.process(smpl)*0.5;//buff[i]*0.1; } } window.onload = function(){ setTimeout(function(){ dev = audioLib.AudioDevice(fillBuffer, 2, 1024); sampleRate = dev.sampleRate; }, 1000); }; }()); function notePressed(id) { // noteon(id); var note = parseInt(id); } function noteReleased(id) { // noteoff(id); } var bg = null; var keys = [65, 87, 83, 69, 68, 70, 84, 71, 89, 72, 85, 74, 75, 79, 76, 80, 186, 222]; $(document).ready(function() { graphics.init("#holder", draw, 911, 500); bg = new graphics.Image("bg.jpg"); var elements = [ { "type": "slider", "name": "wave1_volume", "x": 41, "y": 42, "height": 139, "width": 21, "min": 0, "max": 1, "defaultValue":0.5 }, { "type": "segmented", "name": "wave1_shape", "options": ["SQU", "TRI"], "x":78, "y":66, "optionImageUrls": ["tri.png", "square.png"], "imageOffset": {x: -7, y: -8}, "height": 100, "width": 35 }, { "type": "slider", "name": "wave1_warp", "x": 41, "y": 231, "height": 139, "width": 21, "min": 0, "max": 1, "defaultValue":0.5 }, { "type": "slider", "name": "wave1_warp_lfo", "x": 86 , "y": 232, "height": 139, "width": 21, "min": -0.5, "max": 0.5, "defaultValue":0 }, { "type": "segmented", "name": "wave1_octave", "options": ["+1", "0", "-1"], "x":124, "y": 42, "optionImageUrls": ["octaveUp.png", "octave0.png", "octaveDown.png"], "imageOffset": {x: -7, y: -8}, "width": 35, "height": 146 }, { "type": "slider", "name": "wave1_pitch", "x": 176 , "y": 42, "height": 139, "width": 21, "min": -1, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "wave1_pitch_lfo", "x": 131 , "y": 231, "height": 139, "width": 21, "min": -1, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "wave1_pitch_env", "x": 176 , "y": 232, "height": 139, "width": 21, "min": -1, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "wave2_volume", "x": 253, "y": 42, "height": 139, "width": 21, "min": 0, "max": 1, "defaultValue":0.0 }, { "type": "segmented", "name": "wave2_shape", "options": ["SQU", "TRI"], "x":290, "y":66, "optionImageUrls": ["tri.png", "square.png"], "imageOffset": {x: -7, y: -8}, "height": 100, "width": 35 }, { "type": "slider", "name": "wave2_warp", "x": 253, "y": 231, "height": 139, "width": 21, "min": 0, "max": 1, "defaultValue":0.5 }, { "type": "slider", "name": "wave2_warp_lfo", "x": 298, "y": 231, "height": 139, "width": 21, "min": -0.5, "max": 0.5, "defaultValue":0.0 }, { "type": "segmented", "name": "wave2_octave", "options": ["+1", "0", "-1"], "x": 336, "y":42, "optionImageUrls": ["octaveUp.png", "octave0.png", "octaveDown.png"], "imageOffset": {x: -7, y: -8}, "width": 35, "height": 145 }, { "type": "slider", "name": "wave2_pitch", "x": 388, "y": 42, "height": 139, "width": 21, "min": -1, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "wave2_pitch_lfo", "x": 343, "y": 231, "height": 139, "width": 21, "min": -1, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "wave2_pitch_env", "x": 388 , "y": 231, "height": 139, "width": 21, "min": -1, "max": 1, "defaultValue":0 }, { "type": "toggle", "name": "ring", "imageUrl": "ring.png", "x": 201, "y": 187, "width": 35, "height": 33, "imageOffset": {x: -7, y: -8} }, { "type": "slider", "name": "noise_volume", "x": 611, "y": 231, "height": 140, "width": 21, "min": 0, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "noise_colour", "x": 655, "y": 231, "height": 140, "width": 21, "min": 0, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "lfo_speed", "x": 611, "y": 56, "height": 124, "width": 21, "min": 0.1, "max": 10, "defaultValue":0 }, { "type": "segmented", "name": "lfo_shape", "x": 648, "y": 55, "optionImageUrls": ["tri.png", "square.png", "saw.png"], "imageOffset": {x: -8, y: -7}, "height": 135, "width": 35, "options": ["SQU", "TRI", "SAW"], "defaultValue":0 }, { "type": "slider", "name": "env1_attack", "x": 453, "y": 56, "height": 125, "width": 21, "min": 0, "max": 1000, "defaultValue":0 }, { "type": "slider", "name": "env1_decay", "x": 482, "y": 56, "height": 125, "width": 21, "min": 0, "max": 1000, "defaultValue":0 }, { "type": "slider", "name": "env1_sustain", "x": 511, "y": 56, "height": 125, "width": 21, "min": 0, "max": 1, "defaultValue":1 }, { "type": "slider", "name": "env1_release", "x": 540, "y": 56, "height": 125, "width": 21, "min": 0, "max": 1000, "defaultValue":0 }, { "type": "slider", "name": "env2_attack", "x": 453, "y": 246, "height": 125, "width": 21, "min": 0, "max": 1000, "defaultValue":0 }, { "type": "slider", "name": "env2_decay", "x": 482, "y": 246, "height": 125, "width": 21, "min": 0, "max": 1000, "defaultValue":0 }, { "type": "slider", "name": "env2_sustain", "x": 511, "y": 246, "height": 125, "width": 21, "min": 0, "max": 1, "defaultValue":1 }, { "type": "slider", "name": "env2_release", "x": 540, "y": 246, "height": 125 , "width": 21, "min": 0, "max": 1000, "defaultValue":0 }, { "type": "slider", "name": "cutoff", "x": 727, "y": 41, "height": 140, "width": 21, "min": 100, "max": 3000, "defaultValue":500 }, { "type": "slider", "name": "resonance", "x": 771, "y": 41, "height": 140, "width": 21, "min": 0, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "dist_gain", "x": 773, "y": 231, "height": 140, "width": 21, "min": 0, "max": 10, "defaultValue":0 }, { "type": "slider", "name": "dist_volume", "x": 817, "y": 231, "height": 140, "width": 21, "min": 0, "max": 1, "defaultValue":1 }, { "type": "slider", "name": "dist_mix", "x": 862, "y": 231, "height": 140, "width": 21, "min": 0, "max": 1, "defaultValue":0 }, { "type": "slider", "name": "filter_env_mod", "x": 816, "y": 41, "height": 140, "width": 21, "min": 0, "max": 4, "defaultValue":0 }, { "type": "slider", "name": "filter_lfo_mod", "x": 861, "y": 41, "height": 140, "width": 21, "min": 0, "max": 4, "defaultValue":0 }, { "type": "keyboard", "name":"keyboard", "x": 5, "y": 400, "width": 900, "height":100, "noteonCallback": noteon, "noteoffCallback": noteoff } ]; for(i = 0; i < elements.length; i++) { if(elements[i].type=="slider") { elements[i].y += 19; elements[i].height -= 41; } } function isBlack(i) { i = i%12; return (i==1 || i==3 || i==6 || i==8 || i==10); } function getChar(i) { var cc = ['a', 'w', 's', 'e', 'd', 'f', 't', 'g', 'y', 'h', 'u', 'j', 'k', 'o', 'l', 'p', ';', '\'']; if(i<0 || i>=cc.length) return ''; return cc[i]; } gui.init(elements); $(document).keydown(function(e) { var note = keyToNote(e.which); if(note!=-1) { //alert(note); playing = true; noteon(note); //pitch = mtof(note+48); // alert(pitch); } }); $(document).keyup(function(e) { playing = false; var note = keyToNote(e.which); noteoff(note); } ); //alert(''); var defaultPatches = ''; patches = new PatchStorage("patches", "patchHolder", getPatchData, loadCallback, defaultPatches); bg = new graphics.Image("bg.jpg"); gui.sliderHandle = new graphics.Image("sliderHandle.png"); $(window).resize(function() { var pos = $("#mygraphicscanvas").offset(); graphics.x = pos.left; graphics.y = pos.top + 22; }); }); function loadCallback(patch) { gui.values = patch; } function getPatchData() { return gui.values; } // these are all the notes that are on. var notes = []; function inArray(arr, val) { for(i = 0; i < arr.length; i++) { if(arr[i]==val) { return i; } } return -1; } function noteon(note) { //alert(note); //return; if(inArray(notes, note)==-1) { notes.push(note); env1.noteon(); env2.noteon(); pitch = mtof(note+48); playing = true; // $("body").append("noteon"); } } function noteoff(note) { var ind = inArray(notes, note); if(ind!=-1) { // remove it notes.splice(ind, 1); } if(notes.length==0) { env1.noteoff(); env2.noteoff(); playing = false; } } function keyToNote(key) { for(i = 0; i < keys.length; i++) { if(keys[i]==key) return i; } return -1; } function draw() { //background(0,0,0); bg.draw(); gui.draw(); }