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