annotate 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
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 import java.util.Vector;
rlm@9 9
rlm@9 10 import javax.sound.sampled.AudioFormat;
rlm@9 11 import javax.sound.sampled.AudioSystem;
rlm@9 12
rlm@23 13 import org.tritonus.sampled.file.WaveAudioOutputStream;
rlm@24 14 import org.tritonus.share.sampled.file.TDataOutputStream;
rlm@24 15 import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream;
rlm@23 16
rlm@9 17 public class WaveFileWriter implements SoundProcessor {
rlm@9 18
rlm@9 19 public File targetFile;
rlm@24 20 private WaveAudioOutputStream wao;
rlm@9 21
rlm@24 22 public WaveFileWriter(File targetFile) throws FileNotFoundException{
rlm@24 23 wao = new WaveAudioOutputStream(new AudioFormat(44100.0f, 32, 1, true, false),
rlm@24 24 AudioSystem.NOT_SPECIFIED,
rlm@24 25 (TDataOutputStream) new TNonSeekableDataOutputStream(new FileOutputStream(targetFile)));
rlm@9 26 }
rlm@9 27
rlm@24 28 public void cleanup() {
rlm@24 29 try {wao.close();}
rlm@24 30 catch (IOException e) {e.printStackTrace();}
rlm@9 31 }
rlm@9 32
rlm@24 33
rlm@24 34 public void process(ByteBuffer audioSamples, int numSamples) {
rlm@24 35 byte[] data = new byte[numSamples];
rlm@24 36 audioSamples.get(data, 0, numSamples);
rlm@24 37
rlm@24 38 try {wao.write(data, 0, numSamples);}
rlm@24 39 catch (IOException e) {e.printStackTrace();}
rlm@24 40 }
rlm@9 41 }