Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getFrequency(n) {
- return Math.round(440 * Math.pow(2, (piano.length - piano.indexOf(n) - 1 + 3 - 36) / 12));
- }
- var synthTypes = ['sine', 'square', 'sawtooth', 'triangle'];
- var poly = 64;
- var oscillators = [{},{},{},{}];
- var synthTime = 0;
- function initSynth(context, id) {
- for (var i = 0; i < piano.length; i++) {
- var n = piano[i];
- oscillators[id][n] = context.createOscillator();
- var o = oscillators[id][n];
- o.type = synthTypes[id];
- o.frequency.value = getFrequency(n);
- var gain = context.createGain();
- gain.gain.value = 0;
- o.connect(gain);
- gain.connect(audioSystem.gainNodes[id + 13]);
- o.gain = gain;
- o.inUse = false;
- o.inUseTime = 0;
- o.start ? o.start(0) : o.noteOn(0);
- }
- }
- function playSynthNote(context, id, note, time) {
- synthTime++;
- var oscillator = oscillators[id][note];
- if (oscillator) {
- (function (synthTime) {
- oscillator.inUseTime = synthTime;
- oscillator.inUse = true;
- oscillator.gain.gain.value = 0.1 * audioSystem.masterVolume;
- window.setTimeout(function () {
- if (oscillator.inUseTime == synthTime) {
- oscillator.gain.gain.value = 0;
- oscillator.inUse = false;
- }
- }, time);
- })(synthTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement