diff src/com/aurellem/capture/audio/WaveFileWriter.java @ 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
line wrap: on
line diff
     1.1 --- a/src/com/aurellem/capture/audio/WaveFileWriter.java	Sun Oct 30 04:22:14 2011 -0700
     1.2 +++ b/src/com/aurellem/capture/audio/WaveFileWriter.java	Sun Oct 30 04:42:40 2011 -0700
     1.3 @@ -1,51 +1,41 @@
     1.4  package com.aurellem.capture.audio;
     1.5  
     1.6 -import java.io.ByteArrayInputStream;
     1.7  import java.io.File;
     1.8 +import java.io.FileNotFoundException;
     1.9 +import java.io.FileOutputStream;
    1.10  import java.io.IOException;
    1.11  import java.nio.ByteBuffer;
    1.12  import java.util.Vector;
    1.13  
    1.14 -import javax.sound.sampled.AudioFileFormat;
    1.15  import javax.sound.sampled.AudioFormat;
    1.16 -import javax.sound.sampled.AudioInputStream;
    1.17  import javax.sound.sampled.AudioSystem;
    1.18  
    1.19  import org.tritonus.sampled.file.WaveAudioOutputStream;
    1.20 +import org.tritonus.share.sampled.file.TDataOutputStream;
    1.21 +import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream;
    1.22  
    1.23  public class WaveFileWriter implements SoundProcessor {
    1.24  
    1.25 -	public Vector<Byte> fullWaveData = new Vector<Byte>();
    1.26  	public File targetFile;
    1.27 -	
    1.28 -	public WaveFileWriter(File targetFile){
    1.29 -		this.targetFile = targetFile;
    1.30 -	}
    1.31 -	
    1.32 -	
    1.33 -	WaveAudioOutputStream wao;
    1.34 -	
    1.35 -	
    1.36 -	public void cleanup() {
    1.37 -		byte[] data = new byte[this.fullWaveData.size()];
    1.38 -		
    1.39 -		for (int i = 0; i < this.fullWaveData.size(); i++){
    1.40 -			data[i] = this.fullWaveData.get(i);}
    1.41 -		
    1.42 -		ByteArrayInputStream input = new ByteArrayInputStream(data);
    1.43 -		AudioFormat format = new AudioFormat(44100.0f, 32, 1, true, false); 
    1.44 -		AudioInputStream audioInput = new AudioInputStream(input, format, data.length / 4 );
    1.45 -		try {AudioSystem.write(audioInput, AudioFileFormat.Type.WAVE, targetFile);} 
    1.46 -		catch (IOException e) {e.printStackTrace();}
    1.47 +	private WaveAudioOutputStream wao;
    1.48  
    1.49 +	public WaveFileWriter(File targetFile) throws FileNotFoundException{
    1.50 +		wao = new WaveAudioOutputStream(new AudioFormat(44100.0f, 32, 1, true, false),
    1.51 +				AudioSystem.NOT_SPECIFIED, 
    1.52 +				(TDataOutputStream) new TNonSeekableDataOutputStream(new FileOutputStream(targetFile)));
    1.53  	}
    1.54  
    1.55 -	
    1.56 -	public void process(ByteBuffer audioSamples, int numSamples) {
    1.57 -		for (int i = 0; i<numSamples; i++){
    1.58 -			Byte b = audioSamples.get(i);
    1.59 -			fullWaveData.add(b);
    1.60 -		}
    1.61 +	public void cleanup() {
    1.62 +		try {wao.close();}
    1.63 +		catch (IOException e) {e.printStackTrace();}
    1.64  	}
    1.65  
    1.66 +
    1.67 +	public void process(ByteBuffer audioSamples, int numSamples) {
    1.68 +		byte[] data = new byte[numSamples];
    1.69 +		audioSamples.get(data, 0, numSamples);
    1.70 +		
    1.71 +		try {wao.write(data, 0, numSamples);}
    1.72 +		catch (IOException e) {e.printStackTrace();}
    1.73 +	}
    1.74  }