annotate src/com/aurellem/capture/audio/WaveFileWriter.java @ 73:877ae4b2993c tip

merge laptop changes.
author Robert McIntyre <rlm@mit.edu>
date Mon, 10 Mar 2014 18:58:08 -0400
parents 302d5e9ad120
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@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@68 24 public File targetFile;
rlm@68 25 private WaveAudioOutputStream wao;
rlm@68 26 private TDataOutputStream tos;
rlm@68 27 private boolean initialized = false;
rlm@31 28
rlm@68 29 public WaveFileWriter(File targetFile)
rlm@68 30 throws FileNotFoundException{
rlm@68 31 tos = new TNonSeekableDataOutputStream
rlm@68 32 (new FileOutputStream(targetFile));
rlm@68 33 }
rlm@31 34
rlm@68 35 public void init(AudioFormat format){
rlm@68 36 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
rlm@68 37 }
rlm@68 38
rlm@68 39 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
rlm@68 40 audioSamples.clear();
rlm@68 41 if (!initialized){
rlm@68 42 init(format);
rlm@68 43 initialized = true;
rlm@31 44 }
rlm@31 45
rlm@68 46 byte[] data = new byte[numSamples];
rlm@68 47 audioSamples.get(data, 0, numSamples);
rlm@68 48 try {wao.write(data, 0, numSamples);}
rlm@68 49 catch (IOException e) {e.printStackTrace();}
rlm@68 50 }
rlm@9 51
rlm@68 52 public void cleanup() {
rlm@68 53 try {wao.close();}
rlm@68 54 catch (IOException e) {e.printStackTrace();}
rlm@68 55 }
rlm@9 56
rlm@24 57
rlm@9 58 }