view 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
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)
30 throws FileNotFoundException{
31 tos = new TNonSeekableDataOutputStream
32 (new FileOutputStream(targetFile));
33 }
35 public void init(AudioFormat format){
36 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
37 }
39 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
40 audioSamples.clear();
41 if (!initialized){
42 init(format);
43 initialized = true;
44 }
46 byte[] data = new byte[numSamples];
47 audioSamples.get(data, 0, numSamples);
48 try {wao.write(data, 0, numSamples);}
49 catch (IOException e) {e.printStackTrace();}
50 }
52 public void cleanup() {
53 try {wao.close();}
54 catch (IOException e) {e.printStackTrace();}
55 }
58 }