comparison src/com/aurellem/capture/audio/WaveFileWriter.java @ 24:5f616cc420dd

improved WaveFileWriter using tritonus
author Robert McIntyre <rlm@mit.edu>
date Sun, 30 Oct 2011 04:42:40 -0700
parents b643413c3aba
children 25fcf5fda505
comparison
equal deleted inserted replaced
23:b643413c3aba 24:5f616cc420dd
1 package com.aurellem.capture.audio; 1 package com.aurellem.capture.audio;
2 2
3 import java.io.ByteArrayInputStream;
4 import java.io.File; 3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
5 import java.io.IOException; 6 import java.io.IOException;
6 import java.nio.ByteBuffer; 7 import java.nio.ByteBuffer;
7 import java.util.Vector; 8 import java.util.Vector;
8 9
9 import javax.sound.sampled.AudioFileFormat;
10 import javax.sound.sampled.AudioFormat; 10 import javax.sound.sampled.AudioFormat;
11 import javax.sound.sampled.AudioInputStream;
12 import javax.sound.sampled.AudioSystem; 11 import javax.sound.sampled.AudioSystem;
13 12
14 import org.tritonus.sampled.file.WaveAudioOutputStream; 13 import org.tritonus.sampled.file.WaveAudioOutputStream;
14 import org.tritonus.share.sampled.file.TDataOutputStream;
15 import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream;
15 16
16 public class WaveFileWriter implements SoundProcessor { 17 public class WaveFileWriter implements SoundProcessor {
17 18
18 public Vector<Byte> fullWaveData = new Vector<Byte>();
19 public File targetFile; 19 public File targetFile;
20 20 private WaveAudioOutputStream wao;
21 public WaveFileWriter(File targetFile){
22 this.targetFile = targetFile;
23 }
24
25
26 WaveAudioOutputStream wao;
27
28
29 public void cleanup() {
30 byte[] data = new byte[this.fullWaveData.size()];
31
32 for (int i = 0; i < this.fullWaveData.size(); i++){
33 data[i] = this.fullWaveData.get(i);}
34
35 ByteArrayInputStream input = new ByteArrayInputStream(data);
36 AudioFormat format = new AudioFormat(44100.0f, 32, 1, true, false);
37 AudioInputStream audioInput = new AudioInputStream(input, format, data.length / 4 );
38 try {AudioSystem.write(audioInput, AudioFileFormat.Type.WAVE, targetFile);}
39 catch (IOException e) {e.printStackTrace();}
40 21
22 public WaveFileWriter(File targetFile) throws FileNotFoundException{
23 wao = new WaveAudioOutputStream(new AudioFormat(44100.0f, 32, 1, true, false),
24 AudioSystem.NOT_SPECIFIED,
25 (TDataOutputStream) new TNonSeekableDataOutputStream(new FileOutputStream(targetFile)));
41 } 26 }
42 27
43 28 public void cleanup() {
44 public void process(ByteBuffer audioSamples, int numSamples) { 29 try {wao.close();}
45 for (int i = 0; i<numSamples; i++){ 30 catch (IOException e) {e.printStackTrace();}
46 Byte b = audioSamples.get(i);
47 fullWaveData.add(b);
48 }
49 } 31 }
50 32
33
34 public void process(ByteBuffer audioSamples, int numSamples) {
35 byte[] data = new byte[numSamples];
36 audioSamples.get(data, 0, numSamples);
37
38 try {wao.write(data, 0, numSamples);}
39 catch (IOException e) {e.printStackTrace();}
40 }
51 } 41 }