Guest User

Untitled

a guest
Jan 30th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. double fa=220, fb=0;   
  2. SourceDataLine speaker;
  3. AudioFormat af = new AudioFormat(44100f, 16, 1, true, false);
  4. DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
  5. speaker = (SourceDataLine) AudioSystem.getLine(info);
  6. speaker.open(af, 4096);
  7. speaker.start();
  8. double t = 0;
  9. short[] buff = new short[256];
  10. byte[] toSoundCard = new byte[buff.length*2];
  11. for (;;) {
  12.         if(t>=1) break;
  13.             System.out.println(t+" "+e.getValueAt(t));
  14.             for (int i = 0; i < buff.length; i++) {
  15.         double f=lerp(fa,fb,t);
  16.         //System.out.println(f);
  17.                 buff[i] = (short) (Short.MAX_VALUE * 0.5 * Math.sin(t * Math.PI * 2 * f));
  18.                 toSoundCard[2 * i] = (byte) buff[i];
  19.                 toSoundCard[2 * i + 1] = (byte) (buff[i] >> 8);   //(i know i could avoid doing this)
  20.                 t += 1.0 / 44100.0;
  21.             }
  22.             speaker.write(toSoundCard, 0, toSoundCard.length);
  23.         }`
  24.  
  25. where lerp is this function:  
  26.  public static final double lerp(double a, double b, double f) {
  27.         double fn = f > 1 ? 1 : f < 0 ? 0 : f;
  28.         return a * (1 - fn) + b * fn;
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment