Advertisement
Guest User

Untitled

a guest
Jun 18th, 2014
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dsp(t) {
  2.  
  3.   var tmod = t - Math.floor( t ); // basically, remove the whole-number portion of t, so 3.3 becomes 0.3, 7.87 becomes 0.87, etc.
  4.  
  5.   /**
  6.    * Sine pattern:
  7.    * - 220 Hz for a quarter-second
  8.    * - silence for a quarter-second
  9.    * - 254 Hz for a quarter-second
  10.    * - silence for a quarter-second
  11.    */
  12.  
  13.   var melody = 0;
  14.  
  15.   if ( tmod < 0.25 ) {
  16.     melody = sinewave( t, 220 );
  17.   } else if ( tmod >= 0.5 && tmod < 0.75 ) {
  18.     melody = sinewave( t, 254 );
  19.   }
  20.  
  21.   /**
  22.    * Noise pattern:
  23.    * - on for an eighth-second
  24.    * - off for an eighth-second
  25.    * - repeat
  26.    */
  27.  
  28.   var noise = 0;
  29.  
  30.   if (
  31.     tmod < 1/8 ||
  32.     ( tmod >= 2/8 && tmod < 3/8 ) ||
  33.     ( tmod >= 4/8 && tmod < 5/8 ) ||
  34.     ( tmod > 6/8 && tmod < 7/8 )
  35.   ) {
  36.     noise = Noise();
  37.   }
  38.  
  39.   return 0.5 * melody + 0.2 * noise;
  40.  
  41. }
  42.  
  43. function sinewave( t, freq ) {
  44.   return Math.sin(2 * Math.PI * t * freq );
  45. }
  46.  
  47. function Noise() {
  48.   return Math.random() * 3 - 1; // return a random number between -1 and 1
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement