annotate src/com/aurellem/capture/audio/WaveFileWriter.java @ 35:c969b04fa0dc

working on divergence
author Robert McIntyre <rlm@mit.edu>
date Mon, 31 Oct 2011 00:14:46 -0700
parents c4bfbf5d090e
children 388f9d062012
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@31 20 private TDataOutputStream tos;
rlm@31 21 private boolean initialized = false;
rlm@31 22
rlm@31 23 public WaveFileWriter(File targetFile) throws FileNotFoundException{
rlm@31 24 tos = new TNonSeekableDataOutputStream(
rlm@31 25 new FileOutputStream(targetFile));
rlm@31 26
rlm@9 27
rlm@31 28 }
rlm@31 29
rlm@31 30 public void init(AudioFormat format){
rlm@31 31 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
rlm@31 32 }
rlm@31 33
rlm@31 34 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
rlm@33 35 audioSamples.clear();
rlm@31 36 if (!initialized){
rlm@31 37 init(format);
rlm@31 38 initialized = true;
rlm@31 39 }
rlm@31 40
rlm@31 41 byte[] data = new byte[numSamples];
rlm@31 42 audioSamples.get(data, 0, numSamples);
rlm@31 43 try {wao.write(data, 0, numSamples);}
rlm@31 44 catch (IOException e) {e.printStackTrace();}
rlm@9 45 }
rlm@9 46
rlm@24 47 public void cleanup() {
rlm@24 48 try {wao.close();}
rlm@24 49 catch (IOException e) {e.printStackTrace();}
rlm@9 50 }
rlm@9 51
rlm@24 52
rlm@9 53 }