annotate src/com/aurellem/capture/audio/WaveFileWriter.java @ 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
rev   line source
rlm@9 1 package com.aurellem.capture.audio;
rlm@9 2
rlm@9 3 import java.io.File;
rlm@24 4 import java.io.FileNotFoundException;
rlm@24 5 import java.io.FileOutputStream;
rlm@9 6 import java.io.IOException;
rlm@9 7 import java.nio.ByteBuffer;
rlm@9 8
rlm@9 9 import javax.sound.sampled.AudioFormat;
rlm@9 10 import javax.sound.sampled.AudioSystem;
rlm@9 11
rlm@23 12 import org.tritonus.sampled.file.WaveAudioOutputStream;
rlm@24 13 import org.tritonus.share.sampled.file.TDataOutputStream;
rlm@24 14 import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream;
rlm@23 15
rlm@9 16 public class WaveFileWriter implements SoundProcessor {
rlm@9 17
rlm@9 18 public File targetFile;
rlm@24 19 private WaveAudioOutputStream wao;
rlm@27 20 private boolean initialized = false;
rlm@27 21 private FileOutputStream fos;
rlm@27 22
rlm@24 23 public WaveFileWriter(File targetFile) throws FileNotFoundException{
rlm@27 24 this.fos = new FileOutputStream(targetFile);
rlm@9 25 }
rlm@9 26
rlm@24 27 public void cleanup() {
rlm@24 28 try {wao.close();}
rlm@24 29 catch (IOException e) {e.printStackTrace();}
rlm@9 30 }
rlm@9 31
rlm@27 32 private void initiaize(AudioFormat format){
rlm@27 33 wao = new WaveAudioOutputStream(format,
rlm@27 34 (long)AudioSystem.NOT_SPECIFIED,
rlm@27 35 (TDataOutputStream) new TNonSeekableDataOutputStream(
rlm@27 36 fos));
rlm@27 37 }
rlm@27 38
rlm@27 39 public void process(ByteBuffer audioSamples, AudioFormat format, int numSamples) {
rlm@27 40
rlm@27 41 if (!initialized){initiaize(format);}
rlm@27 42
rlm@24 43 byte[] data = new byte[numSamples];
rlm@24 44 audioSamples.get(data, 0, numSamples);
rlm@24 45 try {wao.write(data, 0, numSamples);}
rlm@24 46 catch (IOException e) {e.printStackTrace();}
rlm@24 47 }
rlm@9 48 }