Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function dsp(t) {
- 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.
- /**
- * Sine pattern:
- * - 220 Hz for a quarter-second
- * - silence for a quarter-second
- * - 254 Hz for a quarter-second
- * - silence for a quarter-second
- */
- var melody = 0;
- if ( tmod < 0.25 ) {
- melody = sinewave( t, 220 );
- } else if ( tmod >= 0.5 && tmod < 0.75 ) {
- melody = sinewave( t, 254 );
- }
- /**
- * Noise pattern:
- * - on for an eighth-second
- * - off for an eighth-second
- * - repeat
- */
- var noise = 0;
- if (
- tmod < 1/8 ||
- ( tmod >= 2/8 && tmod < 3/8 ) ||
- ( tmod >= 4/8 && tmod < 5/8 ) ||
- ( tmod > 6/8 && tmod < 7/8 )
- ) {
- noise = Noise();
- }
- return 0.5 * melody + 0.2 * noise;
- }
- function sinewave( t, freq ) {
- return Math.sin(2 * Math.PI * t * freq );
- }
- function Noise() {
- return Math.random() * 3 - 1; // return a random number between -1 and 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement