# HG changeset patch # User Robert McIntyre # Date 1319987426 25200 # Node ID 5249c8a9603ce6dbbc66e83c6580a06c68235d54 # Parent 95648b0c12bc87a132f19d989bf60e37e7f58ae7 changed SoundProcessors to use AudioFormat diff -r 95648b0c12bc -r 5249c8a9603c src/com/aurellem/capture/audio/AudioSendRenderer.java --- a/src/com/aurellem/capture/audio/AudioSendRenderer.java Sun Oct 30 04:43:39 2011 -0700 +++ b/src/com/aurellem/capture/audio/AudioSendRenderer.java Sun Oct 30 08:10:26 2011 -0700 @@ -8,6 +8,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import javax.sound.sampled.AudioFormat; + import org.lwjgl.LWJGLException; import org.lwjgl.openal.AL; import org.lwjgl.openal.AL10; @@ -25,6 +27,7 @@ extends LwjglAudioRenderer implements MultiListener { private AudioSend audioSend; + public static final AudioFormat outputFormat = new AudioFormat(44100.0f, 32, 1, true, false); /** * Keeps track of all the listeners which have been registered so far. @@ -102,8 +105,7 @@ public void initInThread(){ try{ if (!AL.isCreated()){ - AL.create("Multiple Audio Send", 44100, 60, false); - } + AL.create("Multiple Audio Send", (int)outputFormat.getSampleRate(), 60, false); } }catch (OpenALException ex){ logger.log(Level.SEVERE, "Failed to load audio library", ex); System.exit(1); @@ -166,11 +168,11 @@ } - public final static int BYTES_PER_SAMPLE = 4; + private ByteBuffer buffer = BufferUtils.createByteBuffer(4096); public void dispatchAudio(float tpf){ - int samplesToGet = (int) (tpf * 44100); + int samplesToGet = (int) (tpf * outputFormat.getSampleRate()); try {latch.await();} catch (InterruptedException e) {e.printStackTrace();} audioSend.step(samplesToGet); @@ -181,7 +183,8 @@ audioSend.getSamples(buffer, samplesToGet, i); SoundProcessor sp = this.soundProcessorMap.get(this.listeners.get(i)); - if (null != sp){sp.process(buffer, samplesToGet*BYTES_PER_SAMPLE);} + if (null != sp){sp.process(buffer, outputFormat, + samplesToGet*outputFormat.getSampleSizeInBits()/8);} } } diff -r 95648b0c12bc -r 5249c8a9603c src/com/aurellem/capture/audio/CompositeSoundProcessor.java --- a/src/com/aurellem/capture/audio/CompositeSoundProcessor.java Sun Oct 30 04:43:39 2011 -0700 +++ b/src/com/aurellem/capture/audio/CompositeSoundProcessor.java Sun Oct 30 08:10:26 2011 -0700 @@ -2,6 +2,8 @@ import java.nio.ByteBuffer; +import javax.sound.sampled.AudioFormat; + /** * Method of Combination for sound processors. This SoundProcessor will * run the methods of each of its constituent SoundProcessors in the order @@ -18,15 +20,16 @@ this.processors = processors; } - public void process(ByteBuffer audioSamples, int numSamples) { + public void process(ByteBuffer audioSamples, AudioFormat format, int numSamples) { for (SoundProcessor sp : processors){ - sp.process(audioSamples, numSamples); + sp.process(audioSamples, format, numSamples); } } public void cleanup() { for (SoundProcessor sp : processors){ - sp.cleanup(); + sp.cleanup(); } } + } diff -r 95648b0c12bc -r 5249c8a9603c src/com/aurellem/capture/audio/SoundProcessor.java --- a/src/com/aurellem/capture/audio/SoundProcessor.java Sun Oct 30 04:43:39 2011 -0700 +++ b/src/com/aurellem/capture/audio/SoundProcessor.java Sun Oct 30 08:10:26 2011 -0700 @@ -2,10 +2,12 @@ import java.nio.ByteBuffer; +import javax.sound.sampled.AudioFormat; + public interface SoundProcessor { void cleanup(); - void process(ByteBuffer audioSamples, int numSamples); + void process(ByteBuffer audioSamples, AudioFormat format, int numSamples); } diff -r 95648b0c12bc -r 5249c8a9603c src/com/aurellem/capture/audio/WaveFileWriter.java --- a/src/com/aurellem/capture/audio/WaveFileWriter.java Sun Oct 30 04:43:39 2011 -0700 +++ b/src/com/aurellem/capture/audio/WaveFileWriter.java Sun Oct 30 08:10:26 2011 -0700 @@ -17,12 +17,11 @@ public File targetFile; private WaveAudioOutputStream wao; - + private boolean initialized = false; + private FileOutputStream fos; + public WaveFileWriter(File targetFile) throws FileNotFoundException{ - wao = new WaveAudioOutputStream(new AudioFormat(44100.0f, 32, 1, true, false), - AudioSystem.NOT_SPECIFIED, - (TDataOutputStream) new TNonSeekableDataOutputStream( - new FileOutputStream(targetFile))); + this.fos = new FileOutputStream(targetFile); } public void cleanup() { @@ -30,8 +29,17 @@ catch (IOException e) {e.printStackTrace();} } - - public void process(ByteBuffer audioSamples, int numSamples) { + private void initiaize(AudioFormat format){ + wao = new WaveAudioOutputStream(format, + (long)AudioSystem.NOT_SPECIFIED, + (TDataOutputStream) new TNonSeekableDataOutputStream( + fos)); + } + + public void process(ByteBuffer audioSamples, AudioFormat format, int numSamples) { + + if (!initialized){initiaize(format);} + byte[] data = new byte[numSamples]; audioSamples.get(data, 0, numSamples); try {wao.write(data, 0, numSamples);} diff -r 95648b0c12bc -r 5249c8a9603c src/com/aurellem/capture/examples/AdvancedAudio.java --- a/src/com/aurellem/capture/examples/AdvancedAudio.java Sun Oct 30 04:43:39 2011 -0700 +++ b/src/com/aurellem/capture/examples/AdvancedAudio.java Sun Oct 30 08:10:26 2011 -0700 @@ -3,6 +3,8 @@ import java.io.File; import java.nio.ByteBuffer; +import javax.sound.sampled.AudioFormat; + import com.aurellem.capture.IsoTimer; import com.aurellem.capture.audio.MultiListener; import com.aurellem.capture.audio.SoundProcessor; @@ -235,7 +237,7 @@ * Dance to the beat! This is the brain of an AI entity that * hears it's surroundings and reacts to them. */ - public void process(ByteBuffer audioSamples, int numSamples) { + public void process(ByteBuffer audioSamples, AudioFormat format, int numSamples) { //System.out.println("I'm DANCING <3"); entity.scale(this.scale); if (this.scale == 2f){this.scale = 0.5f;}