view src/com/aurellem/capture/audio/WaveFileWriter.java @ 23:b643413c3aba

going to improve WaveFileWriter
author Robert McIntyre <rlm@mit.edu>
date Sun, 30 Oct 2011 04:22:14 -0700
parents 8a6b1684f536
children 5f616cc420dd
line wrap: on
line source
1 package com.aurellem.capture.audio;
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.IOException;
6 import java.nio.ByteBuffer;
7 import java.util.Vector;
9 import javax.sound.sampled.AudioFileFormat;
10 import javax.sound.sampled.AudioFormat;
11 import javax.sound.sampled.AudioInputStream;
12 import javax.sound.sampled.AudioSystem;
14 import org.tritonus.sampled.file.WaveAudioOutputStream;
16 public class WaveFileWriter implements SoundProcessor {
18 public Vector<Byte> fullWaveData = new Vector<Byte>();
19 public File targetFile;
21 public WaveFileWriter(File targetFile){
22 this.targetFile = targetFile;
23 }
26 WaveAudioOutputStream wao;
29 public void cleanup() {
30 byte[] data = new byte[this.fullWaveData.size()];
32 for (int i = 0; i < this.fullWaveData.size(); i++){
33 data[i] = this.fullWaveData.get(i);}
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();}
41 }
44 public void process(ByteBuffer audioSamples, int numSamples) {
45 for (int i = 0; i<numSamples; i++){
46 Byte b = audioSamples.get(i);
47 fullWaveData.add(b);
48 }
49 }
51 }