annotate src/com/aurellem/capture/audio/WaveFileWriter.java @ 63:df4a2d12ea13

added new timer for debugging purposes
author Robert McIntyre <rlm@mit.edu>
date Sun, 15 Jan 2012 04:10:37 -0700
parents d799a0278cc9
children 302d5e9ad120
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@52 16 /**
rlm@52 17 * A SoundProcessor that sends all sound data it receives to a wav file.
rlm@52 18 *
rlm@52 19 * @author Robert McIntyre
rlm@52 20 */
rlm@52 21
rlm@9 22 public class WaveFileWriter implements SoundProcessor {
rlm@9 23
rlm@9 24 public File targetFile;
rlm@24 25 private WaveAudioOutputStream wao;
rlm@31 26 private TDataOutputStream tos;
rlm@31 27 private boolean initialized = false;
rlm@31 28
rlm@31 29 public WaveFileWriter(File targetFile) throws FileNotFoundException{
rlm@31 30 tos = new TNonSeekableDataOutputStream(
rlm@31 31 new FileOutputStream(targetFile));
rlm@31 32 }
rlm@31 33
rlm@31 34 public void init(AudioFormat format){
rlm@31 35 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
rlm@31 36 }
rlm@31 37
rlm@31 38 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
rlm@33 39 audioSamples.clear();
rlm@31 40 if (!initialized){
rlm@31 41 init(format);
rlm@31 42 initialized = true;
rlm@31 43 }
rlm@31 44
rlm@31 45 byte[] data = new byte[numSamples];
rlm@31 46 audioSamples.get(data, 0, numSamples);
rlm@31 47 try {wao.write(data, 0, numSamples);}
rlm@31 48 catch (IOException e) {e.printStackTrace();}
rlm@9 49 }
rlm@9 50
rlm@24 51 public void cleanup() {
rlm@24 52 try {wao.close();}
rlm@24 53 catch (IOException e) {e.printStackTrace();}
rlm@9 54 }
rlm@9 55
rlm@24 56
rlm@9 57 }