00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package com.meapsoft;
00024
00025 import java.io.File;
00026 import java.io.IOException;
00027 import java.io.PipedInputStream;
00028 import java.io.PipedOutputStream;
00029
00030 import javax.sound.sampled.AudioFileFormat;
00031 import javax.sound.sampled.AudioFormat;
00032 import javax.sound.sampled.AudioInputStream;
00033 import javax.sound.sampled.AudioSystem;
00034 import javax.sound.sampled.SourceDataLine;
00035
00044 public class AudioWriter implements Runnable {
00045 AudioFormat format;
00046 File file;
00047 AudioFileFormat.Type targetType;
00048
00049 SourceDataLine sdl;
00050 PipedOutputStream pos;
00051 PipedInputStream pis;
00052 AudioInputStream ais;
00053 byte[] bytes;
00054
00055
00056
00057 public AudioWriter(SourceDataLine sdl) {
00058 this.sdl = sdl;
00059 format = sdl.getFormat();
00060 sdl.start();
00061 }
00062
00063
00064 public AudioWriter(File file, AudioFormat format,
00065 AudioFileFormat.Type targetType) throws IOException {
00066
00067 this.format = format;
00068 this.targetType = targetType;
00069 this.file = file;
00070
00071
00072 pos = new PipedOutputStream();
00073
00074
00075 pis = new PipedInputStream(pos);
00076 ais = new AudioInputStream(pis, format, AudioSystem.NOT_SPECIFIED);
00077
00078 new Thread(this).start();
00079 }
00080
00081 public void run() {
00082 try {
00083 AudioSystem.write(ais, targetType, file);
00084 } catch(Exception e) {
00085 e.printStackTrace();
00086 }
00087 }
00088
00089 public void write(double[] data) throws IOException {
00090 write(data, data.length);
00091 }
00092
00093 public void write(double[] data, int length) throws IOException {
00094
00095
00096 int numBytes = length * format.getFrameSize();
00097 if(bytes == null || numBytes > bytes.length)
00098 bytes = new byte[numBytes];
00099
00100
00101 limit(data);
00102
00103
00104 doubles2bytes(data, bytes, length);
00105
00106
00107 if(pos != null)
00108 pos.write(bytes, 0, numBytes);
00109 if(sdl != null)
00110 sdl.write(bytes, 0, numBytes);
00111 }
00112
00113
00114
00115 public static void limit(double[] data) {
00116 double t = 0.8;
00117 double c = 2*(1-t)/Math.PI;
00118
00119 for(int i=0; i<data.length; i++) {
00120 if(data[i] > t) {
00121 data[i] = c*Math.atan((data[i]-t)/c)+t;
00122 } else if(data[i] < -t) {
00123 data[i] = c*Math.atan((data[i]+t)/c)-t;
00124 }
00125 }
00126 }
00127
00128 public void write(byte[] bytes) throws IOException {
00129 if(pos != null)
00130 pos.write(bytes, 0, bytes.length);
00131 if(sdl != null)
00132 sdl.write(bytes, 0, bytes.length);
00133 }
00134
00135 public void close() throws IOException {
00136 if(pos != null) {
00137 ais.close();
00138 pis.close();
00139 pos.close();
00140 }
00141 if(sdl != null)
00142 sdl.close();
00143 }
00144
00145 public AudioFormat getFormat() { return format; }
00146
00147
00148 public void doubles2bytes(double[] audioData, byte[] audioBytes) {
00149 doubles2bytes(audioData, audioBytes, audioData.length);
00150 }
00151
00152 public void doubles2bytes(double[] audioData, byte[] audioBytes, int length) {
00153 int in;
00154 if (format.getSampleSizeInBits() == 16) {
00155 if (format.isBigEndian()) {
00156 for (int i = 0; i < length; i++) {
00157 in = (int)(audioData[i]*32767);
00158
00159 audioBytes[2*i] = (byte)(in >> 8);
00160
00161 audioBytes[2*i+1] = (byte)(in & 255);
00162 }
00163 } else {
00164 for (int i = 0; i < length; i++) {
00165 in = (int)(audioData[i]*32767);
00166
00167 audioBytes[2*i] = (byte)(in & 255);
00168
00169 audioBytes[2*i+1] = (byte)(in >> 8);
00170 }
00171 }
00172 } else if (format.getSampleSizeInBits() == 8) {
00173 if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
00174 for (int i = 0; i < length; i++) {
00175 audioBytes[i] = (byte)(audioData[i]*127);
00176 }
00177 } else {
00178 for (int i = 0; i < length; i++) {
00179 audioBytes[i] = (byte)(audioData[i]*127 + 127);
00180 }
00181 }
00182 }
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 }