changeset 27:5249c8a9603c

changed SoundProcessors to use AudioFormat
author Robert McIntyre <rlm@mit.edu>
date Sun, 30 Oct 2011 08:10:26 -0700
parents 95648b0c12bc
children 7184bc09a92e
files src/com/aurellem/capture/audio/AudioSendRenderer.java src/com/aurellem/capture/audio/CompositeSoundProcessor.java src/com/aurellem/capture/audio/SoundProcessor.java src/com/aurellem/capture/audio/WaveFileWriter.java src/com/aurellem/capture/examples/AdvancedAudio.java
diffstat 5 files changed, 35 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/src/com/aurellem/capture/audio/AudioSendRenderer.java	Sun Oct 30 04:43:39 2011 -0700
     1.2 +++ b/src/com/aurellem/capture/audio/AudioSendRenderer.java	Sun Oct 30 08:10:26 2011 -0700
     1.3 @@ -8,6 +8,8 @@
     1.4  import java.util.logging.Level;
     1.5  import java.util.logging.Logger;
     1.6  
     1.7 +import javax.sound.sampled.AudioFormat;
     1.8 +
     1.9  import org.lwjgl.LWJGLException;
    1.10  import org.lwjgl.openal.AL;
    1.11  import org.lwjgl.openal.AL10;
    1.12 @@ -25,6 +27,7 @@
    1.13  	extends LwjglAudioRenderer implements MultiListener {
    1.14  
    1.15  	private AudioSend audioSend;
    1.16 +	public static final AudioFormat outputFormat = new AudioFormat(44100.0f, 32, 1, true, false);
    1.17  	
    1.18  	/**
    1.19  	 * Keeps track of all the listeners which have been registered so far.
    1.20 @@ -102,8 +105,7 @@
    1.21  	public void initInThread(){
    1.22  		try{
    1.23              if (!AL.isCreated()){
    1.24 -                AL.create("Multiple Audio Send", 44100, 60, false);
    1.25 -            }
    1.26 +                AL.create("Multiple Audio Send", (int)outputFormat.getSampleRate(), 60, false);            }
    1.27          }catch (OpenALException ex){
    1.28              logger.log(Level.SEVERE, "Failed to load audio library", ex);
    1.29              System.exit(1);
    1.30 @@ -166,11 +168,11 @@
    1.31  	}
    1.32  	
    1.33  	
    1.34 -	public final static int BYTES_PER_SAMPLE = 4;
    1.35 +
    1.36  	private ByteBuffer buffer = BufferUtils.createByteBuffer(4096); 
    1.37  	
    1.38  	public void dispatchAudio(float tpf){
    1.39 -		int samplesToGet = (int) (tpf * 44100);
    1.40 +		int samplesToGet = (int) (tpf * outputFormat.getSampleRate());
    1.41  		try {latch.await();} 
    1.42  		catch (InterruptedException e) {e.printStackTrace();}
    1.43  		audioSend.step(samplesToGet);
    1.44 @@ -181,7 +183,8 @@
    1.45  			audioSend.getSamples(buffer, samplesToGet, i);
    1.46  			SoundProcessor sp = 
    1.47  			this.soundProcessorMap.get(this.listeners.get(i));
    1.48 -			if (null != sp){sp.process(buffer, samplesToGet*BYTES_PER_SAMPLE);}
    1.49 +			if (null != sp){sp.process(buffer, outputFormat, 
    1.50 +					samplesToGet*outputFormat.getSampleSizeInBits()/8);}
    1.51  		}
    1.52  		
    1.53  	}
     2.1 --- a/src/com/aurellem/capture/audio/CompositeSoundProcessor.java	Sun Oct 30 04:43:39 2011 -0700
     2.2 +++ b/src/com/aurellem/capture/audio/CompositeSoundProcessor.java	Sun Oct 30 08:10:26 2011 -0700
     2.3 @@ -2,6 +2,8 @@
     2.4  
     2.5  import java.nio.ByteBuffer;
     2.6  
     2.7 +import javax.sound.sampled.AudioFormat;
     2.8 +
     2.9  /**
    2.10   * Method of Combination for sound processors.  This SoundProcessor will 
    2.11   * run the methods of each of its constituent SoundProcessors in the order 
    2.12 @@ -18,15 +20,16 @@
    2.13  		this.processors = processors;
    2.14  	}
    2.15  	
    2.16 -	public void process(ByteBuffer audioSamples, int numSamples) {
    2.17 +	public void process(ByteBuffer audioSamples, AudioFormat format,  int numSamples) {
    2.18  		for (SoundProcessor sp : processors){
    2.19 -			sp.process(audioSamples, numSamples);
    2.20 +			sp.process(audioSamples, format, numSamples);
    2.21  		}
    2.22  	}
    2.23  	
    2.24  	public void cleanup() {
    2.25  		for (SoundProcessor sp : processors){
    2.26 -			sp.cleanup();
    2.27 +			sp.cleanup(); 
    2.28  		}
    2.29  	}
    2.30 +
    2.31  }
     3.1 --- a/src/com/aurellem/capture/audio/SoundProcessor.java	Sun Oct 30 04:43:39 2011 -0700
     3.2 +++ b/src/com/aurellem/capture/audio/SoundProcessor.java	Sun Oct 30 08:10:26 2011 -0700
     3.3 @@ -2,10 +2,12 @@
     3.4  
     3.5  import java.nio.ByteBuffer;
     3.6  
     3.7 +import javax.sound.sampled.AudioFormat;
     3.8 +
     3.9  public interface SoundProcessor {
    3.10  
    3.11  	void cleanup();
    3.12  	
    3.13 -	void process(ByteBuffer audioSamples, int numSamples);
    3.14 +	void process(ByteBuffer audioSamples, AudioFormat format, int numSamples);
    3.15  	
    3.16  }
     4.1 --- a/src/com/aurellem/capture/audio/WaveFileWriter.java	Sun Oct 30 04:43:39 2011 -0700
     4.2 +++ b/src/com/aurellem/capture/audio/WaveFileWriter.java	Sun Oct 30 08:10:26 2011 -0700
     4.3 @@ -17,12 +17,11 @@
     4.4  
     4.5  	public File targetFile;
     4.6  	private WaveAudioOutputStream wao;
     4.7 -
     4.8 +	private boolean initialized = false;
     4.9 +	private FileOutputStream fos;
    4.10 +	
    4.11  	public WaveFileWriter(File targetFile) throws FileNotFoundException{
    4.12 -		wao = new WaveAudioOutputStream(new AudioFormat(44100.0f, 32, 1, true, false),
    4.13 -				AudioSystem.NOT_SPECIFIED, 
    4.14 -				(TDataOutputStream) new TNonSeekableDataOutputStream(
    4.15 -						new FileOutputStream(targetFile)));
    4.16 +		this.fos = new FileOutputStream(targetFile);
    4.17  	}
    4.18  
    4.19  	public void cleanup() {
    4.20 @@ -30,8 +29,17 @@
    4.21  		catch (IOException e) {e.printStackTrace();}
    4.22  	}
    4.23  
    4.24 -
    4.25 -	public void process(ByteBuffer audioSamples, int numSamples) {
    4.26 +	private void initiaize(AudioFormat format){
    4.27 +		wao = new WaveAudioOutputStream(format,
    4.28 +				(long)AudioSystem.NOT_SPECIFIED, 
    4.29 +				(TDataOutputStream) new TNonSeekableDataOutputStream(
    4.30 +						fos));
    4.31 +	}
    4.32 +	
    4.33 +	public void process(ByteBuffer audioSamples, AudioFormat format, int numSamples) {
    4.34 +		
    4.35 +		if (!initialized){initiaize(format);}	
    4.36 +		
    4.37  		byte[] data = new byte[numSamples];
    4.38  		audioSamples.get(data, 0, numSamples);
    4.39  		try {wao.write(data, 0, numSamples);}
     5.1 --- a/src/com/aurellem/capture/examples/AdvancedAudio.java	Sun Oct 30 04:43:39 2011 -0700
     5.2 +++ b/src/com/aurellem/capture/examples/AdvancedAudio.java	Sun Oct 30 08:10:26 2011 -0700
     5.3 @@ -3,6 +3,8 @@
     5.4  import java.io.File;
     5.5  import java.nio.ByteBuffer;
     5.6  
     5.7 +import javax.sound.sampled.AudioFormat;
     5.8 +
     5.9  import com.aurellem.capture.IsoTimer;
    5.10  import com.aurellem.capture.audio.MultiListener;
    5.11  import com.aurellem.capture.audio.SoundProcessor;
    5.12 @@ -235,7 +237,7 @@
    5.13  	   * Dance to the beat!  This is the brain of an AI entity that 
    5.14  	   * hears it's surroundings and reacts to them.
    5.15  	   */
    5.16 -	  public void process(ByteBuffer audioSamples, int numSamples) {
    5.17 +	  public void process(ByteBuffer audioSamples, AudioFormat format, int numSamples) {
    5.18  		  //System.out.println("I'm DANCING <3");
    5.19  		  entity.scale(this.scale);
    5.20  		  if (this.scale == 2f){this.scale = 0.5f;}