blob: 31a68d4893a504a1c7a88f6af633b7c5d0e91360 [file] [log] [blame]
Bill Cox60eeb062015-02-27 10:17:45 -08001/* This file was written by Bill Cox in 2011, and is licensed under the Apache
2 2.0 license. */
3
Bill Cox2e48c222011-07-16 12:00:46 -04004package sonic;
5
6import java.io.File;
7import java.io.IOException;
8
9import javax.sound.sampled.AudioFormat;
10import javax.sound.sampled.AudioInputStream;
11import javax.sound.sampled.AudioSystem;
12import javax.sound.sampled.DataLine;
13import javax.sound.sampled.LineUnavailableException;
14import javax.sound.sampled.SourceDataLine;
15import javax.sound.sampled.UnsupportedAudioFileException;
16
17public class Main {
18
19 // Run sonic.
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
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}