changeset 24:5f616cc420dd

improved WaveFileWriter using tritonus
author Robert McIntyre <rlm@mit.edu>
date Sun, 30 Oct 2011 04:42:40 -0700
parents b643413c3aba
children 25fcf5fda505
files build.xml src/com/aurellem/capture/Capture.java src/com/aurellem/capture/audio/WaveFileWriter.java src/com/aurellem/capture/examples/AdvancedAudio.java src/com/aurellem/capture/examples/Basic.java
diffstat 5 files changed, 28 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/build.xml	Sun Oct 30 04:22:14 2011 -0700
     1.2 +++ b/build.xml	Sun Oct 30 04:42:40 2011 -0700
     1.3 @@ -19,6 +19,8 @@
     1.4      <pathelement path="${lib}/slf4j-api.jar"/>
     1.5      <pathelement path="${lib}/xuggle-xuggler.jar"/>
     1.6      <pathelement path="${lib}/xuggle-xuggler-test.jar"/>
     1.7 +    <pathelement path="${lib}/tritonus_aos-0.3.6.jar"/>
     1.8 +    <pathelement path="${lib}/tritonus_share.jar"/>
     1.9    </path>
    1.10  
    1.11    <target name="prepare">
     2.1 --- a/src/com/aurellem/capture/Capture.java	Sun Oct 30 04:22:14 2011 -0700
     2.2 +++ b/src/com/aurellem/capture/Capture.java	Sun Oct 30 04:42:40 2011 -0700
     2.3 @@ -56,13 +56,14 @@
     2.4  		AppSettings settings = new AppSettings(true);
     2.5  		settings.setAudioRenderer("Send");
     2.6  		app.setSettings(settings);
     2.7 +		final WaveFileWriter writer = new WaveFileWriter(file);
     2.8  
     2.9  		Callable<Object> thunk = new Callable<Object>(){
    2.10  			public Object call(){
    2.11  				AudioRenderer ar = app.getAudioRenderer();
    2.12  				if (ar instanceof MultiListener){
    2.13  					MultiListener ml = (MultiListener)ar;
    2.14 -					ml.registerSoundProcessor(new WaveFileWriter(file));
    2.15 +					ml.registerSoundProcessor(writer);
    2.16  				}
    2.17  				return null;
    2.18  			}
     3.1 --- a/src/com/aurellem/capture/audio/WaveFileWriter.java	Sun Oct 30 04:22:14 2011 -0700
     3.2 +++ b/src/com/aurellem/capture/audio/WaveFileWriter.java	Sun Oct 30 04:42:40 2011 -0700
     3.3 @@ -1,51 +1,41 @@
     3.4  package com.aurellem.capture.audio;
     3.5  
     3.6 -import java.io.ByteArrayInputStream;
     3.7  import java.io.File;
     3.8 +import java.io.FileNotFoundException;
     3.9 +import java.io.FileOutputStream;
    3.10  import java.io.IOException;
    3.11  import java.nio.ByteBuffer;
    3.12  import java.util.Vector;
    3.13  
    3.14 -import javax.sound.sampled.AudioFileFormat;
    3.15  import javax.sound.sampled.AudioFormat;
    3.16 -import javax.sound.sampled.AudioInputStream;
    3.17  import javax.sound.sampled.AudioSystem;
    3.18  
    3.19  import org.tritonus.sampled.file.WaveAudioOutputStream;
    3.20 +import org.tritonus.share.sampled.file.TDataOutputStream;
    3.21 +import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream;
    3.22  
    3.23  public class WaveFileWriter implements SoundProcessor {
    3.24  
    3.25 -	public Vector<Byte> fullWaveData = new Vector<Byte>();
    3.26  	public File targetFile;
    3.27 -	
    3.28 -	public WaveFileWriter(File targetFile){
    3.29 -		this.targetFile = targetFile;
    3.30 -	}
    3.31 -	
    3.32 -	
    3.33 -	WaveAudioOutputStream wao;
    3.34 -	
    3.35 -	
    3.36 -	public void cleanup() {
    3.37 -		byte[] data = new byte[this.fullWaveData.size()];
    3.38 -		
    3.39 -		for (int i = 0; i < this.fullWaveData.size(); i++){
    3.40 -			data[i] = this.fullWaveData.get(i);}
    3.41 -		
    3.42 -		ByteArrayInputStream input = new ByteArrayInputStream(data);
    3.43 -		AudioFormat format = new AudioFormat(44100.0f, 32, 1, true, false); 
    3.44 -		AudioInputStream audioInput = new AudioInputStream(input, format, data.length / 4 );
    3.45 -		try {AudioSystem.write(audioInput, AudioFileFormat.Type.WAVE, targetFile);} 
    3.46 -		catch (IOException e) {e.printStackTrace();}
    3.47 +	private WaveAudioOutputStream wao;
    3.48  
    3.49 +	public WaveFileWriter(File targetFile) throws FileNotFoundException{
    3.50 +		wao = new WaveAudioOutputStream(new AudioFormat(44100.0f, 32, 1, true, false),
    3.51 +				AudioSystem.NOT_SPECIFIED, 
    3.52 +				(TDataOutputStream) new TNonSeekableDataOutputStream(new FileOutputStream(targetFile)));
    3.53  	}
    3.54  
    3.55 -	
    3.56 -	public void process(ByteBuffer audioSamples, int numSamples) {
    3.57 -		for (int i = 0; i<numSamples; i++){
    3.58 -			Byte b = audioSamples.get(i);
    3.59 -			fullWaveData.add(b);
    3.60 -		}
    3.61 +	public void cleanup() {
    3.62 +		try {wao.close();}
    3.63 +		catch (IOException e) {e.printStackTrace();}
    3.64  	}
    3.65  
    3.66 +
    3.67 +	public void process(ByteBuffer audioSamples, int numSamples) {
    3.68 +		byte[] data = new byte[numSamples];
    3.69 +		audioSamples.get(data, 0, numSamples);
    3.70 +		
    3.71 +		try {wao.write(data, 0, numSamples);}
    3.72 +		catch (IOException e) {e.printStackTrace();}
    3.73 +	}
    3.74  }
     4.1 --- a/src/com/aurellem/capture/examples/AdvancedAudio.java	Sun Oct 30 04:22:14 2011 -0700
     4.2 +++ b/src/com/aurellem/capture/examples/AdvancedAudio.java	Sun Oct 30 04:42:40 2011 -0700
     4.3 @@ -260,7 +260,7 @@
     4.4  		
     4.5  		rf.addListener(auxListener);
     4.6  		
     4.7 -		rf.registerSoundProcessor(new WaveFileWriter(data1));
     4.8 +		//rf.registerSoundProcessor(new WaveFileWriter(data1));
     4.9  		rf.registerSoundProcessor(auxListener, new Dancer(ear1));
    4.10  		
    4.11  	}   
     5.1 --- a/src/com/aurellem/capture/examples/Basic.java	Sun Oct 30 04:22:14 2011 -0700
     5.2 +++ b/src/com/aurellem/capture/examples/Basic.java	Sun Oct 30 04:42:40 2011 -0700
     5.3 @@ -26,9 +26,6 @@
     5.4  		File video = File.createTempFile("JME-water-video", ".avi");
     5.5  		File audio = File.createTempFile("JME-water-audio", ".wav");
     5.6  		
     5.7 -		System.out.println(video.getCanonicalPath());
     5.8 -		System.out.println(audio.getCanonicalPath());
     5.9 -		
    5.10  		SimpleApplication app = new TestPostWater();
    5.11  		app.setTimer(new IsoTimer(60));
    5.12  		app.setShowSettings(false);
    5.13 @@ -37,5 +34,8 @@
    5.14  		Capture.captureAudio(app, audio);
    5.15  		
    5.16  		app.start();
    5.17 +		
    5.18 +		System.out.println(video.getCanonicalPath());
    5.19 +		System.out.println(audio.getCanonicalPath());
    5.20  	}
    5.21  }