Mercurial > hg > ishara
diff audio/java/TestLine.java @ 0:672052bd81f8
Initial partial import.
author | samer |
---|---|
date | Wed, 19 Dec 2012 22:38:28 +0000 |
parents | |
children | 63cefb01cbab |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/audio/java/TestLine.java Wed Dec 19 22:38:28 2012 +0000 @@ -0,0 +1,63 @@ +package samer.audio.alt; +import javax.sound.sampled.*; + + +public class TestLine { + // Args: <dur> <freq in Hz> <bufsize> <framesize> + public static double TwoPi=2*Math.PI; + + public static class SineSrc { + double ph,freq; + + public SineSrc(double f) { freq=f; ph=0; } + public void next(double [] buf, int l) { + for (int i=0; i<l; i++) { + buf[i]=Math.sin(ph); + ph+=freq; + } + ph -= TwoPi*Math.floor(ph/TwoPi); + } + }; + + public static void main(String[] args) { + int bufsize, N, numframes; + double [] buffer; + double rate, dur, freq; + + if (args.length<4) { + System.out.println("TestLine <dur> <freq in Hz> <bufsize> <framesize>"); + return; + } + + try { + dur=Double.parseDouble(args[0]); + freq=Double.parseDouble(args[1]); + bufsize=Integer.parseInt(args[2]); + N=Integer.parseInt(args[3]); + rate=22050; + + System.out.println("Playing test signal: "+dur+" s, "+freq+"% Hz"); + System.out.println("frame size="+N+" buffer size="+bufsize); + + buffer = new double[N]; + SineSrc src=new SineSrc(2*Math.PI*freq/rate); + LineSink sink=new LineSink(new AudioFormat((float)rate,16,1,true,false),bufsize); + try { + AudioSink.Writer w=sink.writer(N); + numframes = (int)(dur*rate/N); + + sink.start(); + for (int i=0; i<numframes; i++) { + src.next(buffer,N); + w.write(buffer,0,N); + } + sink.getLine().drain(); + sink.stop(); + } finally { + sink.dispose(); + } + } catch(Exception ex) { + } + } +} +