1 /* This file was written by Bill Cox in 2011, and is licensed under the Apache
2    2.0 license. */
3 
4 package sonic;
5 
6 import java.io.File;
7 import java.io.IOException;
8 
9 import javax.sound.sampled.AudioFormat;
10 import javax.sound.sampled.AudioInputStream;
11 import javax.sound.sampled.AudioSystem;
12 import javax.sound.sampled.DataLine;
13 import javax.sound.sampled.LineUnavailableException;
14 import javax.sound.sampled.SourceDataLine;
15 import javax.sound.sampled.UnsupportedAudioFileException;
16 
17 public class Main {
18 
19     // Run sonic.
runSonic( AudioInputStream audioStream, SourceDataLine line, float speed, float pitch, float rate, float volume, boolean emulateChordPitch, int quality, int sampleRate, int numChannels)20     private static void runSonic(
21         AudioInputStream audioStream,
22         SourceDataLine line,
23         float speed,
24         float pitch,
25         float rate,
26         float volume,
27         boolean emulateChordPitch,
28         int quality,
29         int sampleRate,
30         int numChannels) throws IOException
31     {
32         Sonic sonic = new Sonic(sampleRate, numChannels);
33         int bufferSize = line.getBufferSize();
34         byte inBuffer[] = new byte[bufferSize];
35         byte outBuffer[] = new byte[bufferSize];
36         int numRead, numWritten;
37 
38         sonic.setSpeed(speed);
39         sonic.setPitch(pitch);
40         sonic.setRate(rate);
41         sonic.setVolume(volume);
42         sonic.setChordPitch(emulateChordPitch);
43         sonic.setQuality(quality);
44         do {
45             numRead = audioStream.read(inBuffer, 0, bufferSize);
46             if(numRead <= 0) {
47                 sonic.flushStream();
48             } else {
49                 sonic.writeBytesToStream(inBuffer, numRead);
50             }
51             do {
52                 numWritten = sonic.readBytesFromStream(outBuffer, bufferSize);
53                 if(numWritten > 0) {
54                     line.write(outBuffer, 0, numWritten);
55                 }
56             } while(numWritten > 0);
57         } while(numRead > 0);
58     }
59 
main( String[] argv)60     public static void main(
61     	String[] argv) throws UnsupportedAudioFileException, IOException, LineUnavailableException
62     {
63         float speed = 2.0f;
64         float pitch = 1.0f;
65         float rate = 1.0f;
66         float volume = 1.0f;
67         boolean emulateChordPitch = false;
68         int quality = 0;
69 
70         AudioInputStream stream = AudioSystem.getAudioInputStream(new File("talking.wav"));
71         AudioFormat format = stream.getFormat();
72         int sampleRate = (int)format.getSampleRate();
73         int numChannels = format.getChannels();
74         SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format,
75         	((int)stream.getFrameLength()*format.getFrameSize()));
76         SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);
77         line.open(stream.getFormat());
78         line.start();
79         runSonic(stream, line, speed, pitch, rate, volume, emulateChordPitch, quality,
80             sampleRate, numChannels);
81         line.drain();
82         line.stop();
83     }
84 }
85