view src/com/aurellem/capture/audio/WaveFileWriter.java @ 38:adeb88787645

fixed all problems for 16 bit mono output
author Robert McIntyre <rlm@mit.edu>
date Mon, 31 Oct 2011 07:43:44 -0700
parents c4bfbf5d090e
children 388f9d062012
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));
28 }
30 public void init(AudioFormat format){
31 wao = new WaveAudioOutputStream(format,AudioSystem.NOT_SPECIFIED, tos);
32 }
34 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
35 audioSamples.clear();
36 if (!initialized){
37 init(format);
38 initialized = true;
39 }
41 byte[] data = new byte[numSamples];
42 audioSamples.get(data, 0, numSamples);
43 try {wao.write(data, 0, numSamples);}
44 catch (IOException e) {e.printStackTrace();}
45 }
47 public void cleanup() {
48 try {wao.close();}
49 catch (IOException e) {e.printStackTrace();}
50 }
53 }