comparison audio/java/TestLine.java @ 0:672052bd81f8

Initial partial import.
author samer
date Wed, 19 Dec 2012 22:38:28 +0000
parents
children 63cefb01cbab
comparison
equal deleted inserted replaced
-1:000000000000 0:672052bd81f8
1 package samer.audio.alt;
2 import javax.sound.sampled.*;
3
4
5 public class TestLine {
6 // Args: <dur> <freq in Hz> <bufsize> <framesize>
7 public static double TwoPi=2*Math.PI;
8
9 public static class SineSrc {
10 double ph,freq;
11
12 public SineSrc(double f) { freq=f; ph=0; }
13 public void next(double [] buf, int l) {
14 for (int i=0; i<l; i++) {
15 buf[i]=Math.sin(ph);
16 ph+=freq;
17 }
18 ph -= TwoPi*Math.floor(ph/TwoPi);
19 }
20 };
21
22 public static void main(String[] args) {
23 int bufsize, N, numframes;
24 double [] buffer;
25 double rate, dur, freq;
26
27 if (args.length<4) {
28 System.out.println("TestLine <dur> <freq in Hz> <bufsize> <framesize>");
29 return;
30 }
31
32 try {
33 dur=Double.parseDouble(args[0]);
34 freq=Double.parseDouble(args[1]);
35 bufsize=Integer.parseInt(args[2]);
36 N=Integer.parseInt(args[3]);
37 rate=22050;
38
39 System.out.println("Playing test signal: "+dur+" s, "+freq+"% Hz");
40 System.out.println("frame size="+N+" buffer size="+bufsize);
41
42 buffer = new double[N];
43 SineSrc src=new SineSrc(2*Math.PI*freq/rate);
44 LineSink sink=new LineSink(new AudioFormat((float)rate,16,1,true,false),bufsize);
45 try {
46 AudioSink.Writer w=sink.writer(N);
47 numframes = (int)(dur*rate/N);
48
49 sink.start();
50 for (int i=0; i<numframes; i++) {
51 src.next(buffer,N);
52 w.write(buffer,0,N);
53 }
54 sink.getLine().drain();
55 sink.stop();
56 } finally {
57 sink.dispose();
58 }
59 } catch(Exception ex) {
60 }
61 }
62 }
63