1 /** 2 * Instrument 3 * @param {Number} sample 4 * @param {Number} form 5 * @param {Object} config 6 * @constructor 7 */ 8 function AP_Instrument(sample, form, config) { 9 this.oscillator = new AP_Oscillator(sample); 10 this.sample = sample; 11 this.form = form; 12 this.config = config; 13 }; 14 15 /** 16 * Generate volume values 17 * @param {Number} index 18 * @returns {Number} 19 */ 20 AP_Instrument.prototype.vol = function(index) { 21 if (this.form == 0) return 1; 22 var sec = index / this.sample; 23 return sec < this.form ? 1 - (sec / this.form) : 0; 24 }; 25 26 /** 27 * Mix the oscillators and the wave form 28 * @param {Number} freq 29 * @param {Number} index 30 * @returns {Number} 31 */ 32 AP_Instrument.prototype.get = function(freq, index) { 33 var value = 0; 34 for (var type in this.config) { 35 value += this.oscillator.get(freq, index, type) * this.config[type]; 36 } 37 if (value > 1) value = 1; 38 if (value < -1) value = -1; 39 return value * this.vol(index); 40 }; 41