1 /** 2 * Wave form generators 3 * @param {Number} sample 4 * @constructor 5 */ 6 function AP_Oscillator(sample) { 7 this.sample = sample; 8 this.random = this.rnd(); 9 this.flop = 0; 10 } 11 12 /** 13 * Random noise value 14 * @returns {Number} 15 */ 16 AP_Oscillator.prototype.rnd = function() { 17 return Math.random() * 2 -1; 18 }; 19 20 /** 21 * Get oscillator value by index 22 * @param {Number} freq 23 * @param {Number} index 24 * @param {String} type 25 * @returns {Number} 26 */ 27 AP_Oscillator.prototype.get = function(freq, index, type) { 28 var flip, 29 lambda = this.sample / freq; 30 switch (type) { 31 case 'sin': 32 return -Math.sin((index % lambda) / lambda * Math.PI * 2); 33 case 'sqr': 34 return Math.round(index / lambda * 2) % 2 ? 1 : -1; 35 case 'saw': 36 return (index % lambda) / lambda * 2 - 1; 37 case 'noi': 38 flip = Math.round(index / lambda) % 2; 39 if (this.flop != flip) { 40 this.flop = flip; 41 this.random = this.rnd(); 42 } 43 return this.random; 44 } 45 return 0; 46 }; 47