annotate src/com/aurellem/capture/audio/WaveFileWriter.java @ 47:74b53dfe369e

stressing similiarity between audio and video capture
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Dec 2011 12:48:40 -0600
parents 388f9d062012
children d799a0278cc9
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@31 27
rlm@31 28 public void init(AudioFormat format){
rlm@31 29 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
rlm@31 30 }
rlm@31 31
rlm@31 32 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
rlm@33 33 audioSamples.clear();
rlm@31 34 if (!initialized){
rlm@31 35 init(format);
rlm@31 36 initialized = true;
rlm@31 37 }
rlm@31 38
rlm@31 39 byte[] data = new byte[numSamples];
rlm@31 40 audioSamples.get(data, 0, numSamples);
rlm@31 41 try {wao.write(data, 0, numSamples);}
rlm@31 42 catch (IOException e) {e.printStackTrace();}
rlm@9 43 }
rlm@9 44
rlm@24 45 public void cleanup() {
rlm@24 46 try {wao.close();}
rlm@24 47 catch (IOException e) {e.printStackTrace();}
rlm@9 48 }
rlm@9 49
rlm@24 50
rlm@9 51 }