view src/com/aurellem/capture/audio/WaveFileWriter.java @ 49:121b6d7e4d3f

more friendly AppSettings handling
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Dec 2011 13:22:27 -0600
parents 388f9d062012
children d799a0278cc9
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 TDataOutputStream tos;
21 private boolean initialized = false;
23 public WaveFileWriter(File targetFile) throws FileNotFoundException{
24 tos = new TNonSeekableDataOutputStream(
25 new FileOutputStream(targetFile));
26 }
28 public void init(AudioFormat format){
29 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
30 }
32 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
33 audioSamples.clear();
34 if (!initialized){
35 init(format);
36 initialized = true;
37 }
39 byte[] data = new byte[numSamples];
40 audioSamples.get(data, 0, numSamples);
41 try {wao.write(data, 0, numSamples);}
42 catch (IOException e) {e.printStackTrace();}
43 }
45 public void cleanup() {
46 try {wao.close();}
47 catch (IOException e) {e.printStackTrace();}
48 }
51 }