Bill Cox | 60eeb06 | 2015-02-27 10:17:45 -0800 | [diff] [blame] | 1 | /* This file was written by Bill Cox in 2011, and is licensed under the Apache |
| 2 | 2.0 license. */ |
| 3 | |
Bill Cox | 2e48c22 | 2011-07-16 12:00:46 -0400 | [diff] [blame] | 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. |
| 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 | } |