annotate src/com/aurellem/capture/audio/WaveFileWriter.java @ 11:8a6b1684f536

refactored.
author Robert McIntyre <rlm@mit.edu>
date Thu, 27 Oct 2011 02:27:02 -0700
parents 5dfc9e768816
children b643413c3aba
rev   line source
rlm@9 1 package com.aurellem.capture.audio;
rlm@9 2
rlm@9 3 import java.io.ByteArrayInputStream;
rlm@9 4 import java.io.File;
rlm@9 5 import java.io.IOException;
rlm@9 6 import java.nio.ByteBuffer;
rlm@9 7 import java.util.Vector;
rlm@9 8
rlm@9 9 import javax.sound.sampled.AudioFileFormat;
rlm@9 10 import javax.sound.sampled.AudioFormat;
rlm@9 11 import javax.sound.sampled.AudioInputStream;
rlm@9 12 import javax.sound.sampled.AudioSystem;
rlm@9 13
rlm@9 14 public class WaveFileWriter implements SoundProcessor {
rlm@9 15
rlm@9 16 public Vector<Byte> fullWaveData = new Vector<Byte>();
rlm@9 17 public File targetFile;
rlm@9 18
rlm@9 19 public WaveFileWriter(File targetFile){
rlm@9 20 this.targetFile = targetFile;
rlm@9 21 }
rlm@9 22
rlm@9 23 public void cleanup() {
rlm@9 24 byte[] data = new byte[this.fullWaveData.size()];
rlm@9 25
rlm@9 26 for (int i = 0; i < this.fullWaveData.size(); i++){
rlm@9 27 data[i] = this.fullWaveData.get(i);}
rlm@9 28
rlm@9 29 ByteArrayInputStream input = new ByteArrayInputStream(data);
rlm@9 30 AudioFormat format = new AudioFormat(44100.0f, 32, 1, true, false);
rlm@9 31 AudioInputStream audioInput = new AudioInputStream(input, format, data.length / 4 );
rlm@9 32 try {AudioSystem.write(audioInput, AudioFileFormat.Type.WAVE, targetFile);}
rlm@9 33 catch (IOException e) {e.printStackTrace();}
rlm@9 34
rlm@9 35 }
rlm@9 36
rlm@9 37
rlm@9 38 public void process(ByteBuffer audioSamples, int numSamples) {
rlm@9 39 for (int i = 0; i<numSamples; i++){
rlm@9 40 Byte b = audioSamples.get(i);
rlm@9 41 fullWaveData.add(b);
rlm@9 42 }
rlm@9 43 }
rlm@9 44
rlm@9 45 }