Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getFrequency(n) {
  2.   return Math.round(440 * Math.pow(2, (piano.length - piano.indexOf(n) - 1 + 3 - 36) / 12));
  3. }
  4.  
  5. var synthTypes = ['sine', 'square', 'sawtooth', 'triangle'];
  6.  
  7. var poly = 64;
  8. var oscillators = [{},{},{},{}];
  9. var synthTime = 0;
  10.  
  11. function initSynth(context, id) {
  12.   for (var i = 0; i < piano.length; i++) {
  13.     var n = piano[i];
  14.     oscillators[id][n] = context.createOscillator();
  15.     var o = oscillators[id][n];
  16.     o.type = synthTypes[id];
  17.     o.frequency.value = getFrequency(n);
  18.  
  19.     var gain = context.createGain();
  20.     gain.gain.value = 0;
  21.     o.connect(gain);
  22.     gain.connect(audioSystem.gainNodes[id + 13]);
  23.  
  24.     o.gain = gain;
  25.     o.inUse = false;
  26.     o.inUseTime = 0;
  27.  
  28.     o.start ? o.start(0) : o.noteOn(0);
  29.   }
  30. }
  31.  
  32. function playSynthNote(context, id, note, time) {
  33.   synthTime++;
  34.   var oscillator = oscillators[id][note];
  35.   if (oscillator) {
  36.     (function (synthTime) {
  37.       oscillator.inUseTime = synthTime;
  38.       oscillator.inUse = true;
  39.       oscillator.gain.gain.value = 0.1 * audioSystem.masterVolume;
  40.       window.setTimeout(function () {
  41.         if (oscillator.inUseTime == synthTime) {
  42.           oscillator.gain.gain.value = 0;
  43.           oscillator.inUse = false;
  44.         }
  45.       }, time);
  46.     })(synthTime);
  47.   }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement