view src/com/aurellem/capture/audio/WaveFileWriter.java @ 27:5249c8a9603c

changed SoundProcessors to use AudioFormat
author Robert McIntyre <rlm@mit.edu>
date Sun, 30 Oct 2011 08:10:26 -0700
parents 95648b0c12bc
children
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 public class WaveFileWriter implements SoundProcessor {
18 public File targetFile;
19 private WaveAudioOutputStream wao;
20 private boolean initialized = false;
21 private FileOutputStream fos;
23 public WaveFileWriter(File targetFile) throws FileNotFoundException{
24 this.fos = new FileOutputStream(targetFile);
25 }
27 public void cleanup() {
28 try {wao.close();}
29 catch (IOException e) {e.printStackTrace();}
30 }
32 private void initiaize(AudioFormat format){
33 wao = new WaveAudioOutputStream(format,
34 (long)AudioSystem.NOT_SPECIFIED,
35 (TDataOutputStream) new TNonSeekableDataOutputStream(
36 fos));
37 }
39 public void process(ByteBuffer audioSamples, AudioFormat format, int numSamples) {
41 if (!initialized){initiaize(format);}
43 byte[] data = new byte[numSamples];
44 audioSamples.get(data, 0, numSamples);
45 try {wao.write(data, 0, numSamples);}
46 catch (IOException e) {e.printStackTrace();}
47 }
48 }