rlm@9: package com.aurellem.capture.audio; rlm@9: rlm@9: import java.io.File; rlm@24: import java.io.FileNotFoundException; rlm@24: import java.io.FileOutputStream; rlm@9: import java.io.IOException; rlm@9: import java.nio.ByteBuffer; rlm@9: rlm@9: import javax.sound.sampled.AudioFormat; rlm@9: import javax.sound.sampled.AudioSystem; rlm@9: rlm@23: import org.tritonus.sampled.file.WaveAudioOutputStream; rlm@24: import org.tritonus.share.sampled.file.TDataOutputStream; rlm@24: import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream; rlm@23: rlm@9: public class WaveFileWriter implements SoundProcessor { rlm@9: rlm@9: public File targetFile; rlm@24: private WaveAudioOutputStream wao; rlm@31: private TDataOutputStream tos; rlm@31: private boolean initialized = false; rlm@31: rlm@31: public WaveFileWriter(File targetFile) throws FileNotFoundException{ rlm@31: tos = new TNonSeekableDataOutputStream( rlm@31: new FileOutputStream(targetFile)); rlm@31: rlm@9: rlm@31: } rlm@31: rlm@31: public void init(AudioFormat format){ rlm@31: wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos); rlm@31: } rlm@31: rlm@31: public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) { rlm@33: audioSamples.clear(); rlm@31: if (!initialized){ rlm@31: init(format); rlm@31: initialized = true; rlm@31: } rlm@31: rlm@31: byte[] data = new byte[numSamples]; rlm@31: audioSamples.get(data, 0, numSamples); rlm@31: try {wao.write(data, 0, numSamples);} rlm@31: catch (IOException e) {e.printStackTrace();} rlm@9: } rlm@9: rlm@24: public void cleanup() { rlm@24: try {wao.close();} rlm@24: catch (IOException e) {e.printStackTrace();} rlm@9: } rlm@9: rlm@24: rlm@9: }