view src/com/aurellem/capture/audio/WaveFileWriter.java @ 52:d799a0278cc9

advanced documentation.
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Dec 2011 13:51:28 -0600
parents 388f9d062012
children 302d5e9ad120
line wrap: on
line source
1 package com.aurellem.capture.audio;
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.nio.ByteBuffer;
9 import javax.sound.sampled.AudioFormat;
10 import javax.sound.sampled.AudioSystem;
12 import org.tritonus.sampled.file.WaveAudioOutputStream;
13 import org.tritonus.share.sampled.file.TDataOutputStream;
14 import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream;
16 /**
17 * A SoundProcessor that sends all sound data it receives to a wav file.
18 *
19 * @author Robert McIntyre
20 */
22 public class WaveFileWriter implements SoundProcessor {
24 public File targetFile;
25 private WaveAudioOutputStream wao;
26 private TDataOutputStream tos;
27 private boolean initialized = false;
29 public WaveFileWriter(File targetFile) throws FileNotFoundException{
30 tos = new TNonSeekableDataOutputStream(
31 new FileOutputStream(targetFile));
32 }
34 public void init(AudioFormat format){
35 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
36 }
38 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
39 audioSamples.clear();
40 if (!initialized){
41 init(format);
42 initialized = true;
43 }
45 byte[] data = new byte[numSamples];
46 audioSamples.get(data, 0, numSamples);
47 try {wao.write(data, 0, numSamples);}
48 catch (IOException e) {e.printStackTrace();}
49 }
51 public void cleanup() {
52 try {wao.close();}
53 catch (IOException e) {e.printStackTrace();}
54 }
57 }