-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathAudioFileWriter.java
More file actions
394 lines (368 loc) · 15 KB
/
Copy pathAudioFileWriter.java
File metadata and controls
394 lines (368 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/******************************************************************************
* *
* Copyright (c) 1999-2004 Wimba S.A., All Rights Reserved. *
* *
* COPYRIGHT: *
* This software is the property of Wimba S.A. *
* This software is redistributed under the Xiph.org variant of *
* the BSD license. *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* - Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* - Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* - Neither the name of Wimba, the Xiph.org Foundation nor the names of *
* its contributors may be used to endorse or promote products derived *
* from this software without specific prior written permission. *
* *
* WARRANTIES: *
* This software is made available by the authors in the hope *
* that it will be useful, but without any warranty. *
* Wimba S.A. is not liable for any consequence related to the *
* use of the provided software. *
* *
* Class: RawWriter.java *
* *
* Author: Marc GIMPEL *
* *
* Date: 6th January 2004 *
* *
******************************************************************************/
/* $Id$ */
package javaforce.codec.speex;
import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/**
* Abstract Class that defines an Audio File Writer.
*
* @author Marc Gimpel, Wimba S.A. ([email protected])
* @version $Revision$
*/
public abstract class AudioFileWriter {
/**
* Closes the output file.
*
* @throws IOException if there was an exception closing the Audio Writer.
*/
public abstract void close()
throws IOException;
/**
* Open the output file.
*
* @param file - file to open.
* @throws IOException if there was an exception opening the Audio Writer.
*/
public abstract void open(File file)
throws IOException;
/**
* Open the output file.
*
* @param filename - file to open.
* @throws IOException if there was an exception opening the Audio Writer.
*/
public abstract void open(String filename)
throws IOException;
/**
* Writes the header pages that start the Ogg Speex file.
* Prepares file for data to be written.
*
* @param comment description to be included in the header.
* @throws IOException
*/
public abstract void writeHeader(String comment)
throws IOException;
/**
* Writes a packet of audio.
*
* @param data audio data
* @param offset the offset from which to start reading the data.
* @param len the length of data to read.
* @throws IOException
*/
public abstract void writePacket(byte[] data, int offset, int len)
throws IOException;
/**
* Writes an Ogg Page Header to the given byte array.
* <p/>
* Ogg Page Header structure:
* <pre>
* 0 - 3: capture_pattern
* 4: stream_structure_version
* 5: header_type_flag
* 6 - 13: absolute granule position
* 14 - 17: stream serial number
* 18 - 21: page sequence no
* 22 - 25: page checksum
* 26: page_segments
* 27 - x: segment_table
* </pre>
*
* @param buf the buffer to write to.
* @param offset the from which to start writing.
* @param headerType the header type flag
* (0=normal, 2=bos: beginning of stream, 4=eos: end of stream).
* @param granulepos the absolute granule position.
* @param streamSerialNumber
* @param pageCount
* @param packetCount
* @param packetSizes
* @return the amount of data written to the buffer.
*/
public static int writeOggPageHeader(byte[] buf, int offset, int headerType,
long granulepos, int streamSerialNumber,
int pageCount, int packetCount,
byte[] packetSizes) {
writeString(buf, offset, "OggS"); // 0 - 3: capture_pattern
buf[offset + 4] = 0; // 4: stream_structure_version
buf[offset + 5] = (byte) headerType; // 5: header_type_flag
writeLong(buf, offset + 6, granulepos); // 6 - 13: absolute granule position
writeInt(buf, offset + 14, streamSerialNumber); // 14 - 17: stream serial number
writeInt(buf, offset + 18, pageCount); // 18 - 21: page sequence no
writeInt(buf, offset + 22, 0); // 22 - 25: page checksum
buf[offset + 26] = (byte) packetCount; // 26: page_segments
System.arraycopy(packetSizes, 0, // 27 - x: segment_table
buf, offset + 27, packetCount);
return packetCount + 27;
}
/**
* Builds and returns an Ogg Page Header.
*
* @param headerType the header type flag
* (0=normal, 2=bos: beginning of stream, 4=eos: end of stream).
* @param granulepos the absolute granule position.
* @param streamSerialNumber
* @param pageCount
* @param packetCount
* @param packetSizes
* @return an Ogg Page Header.
*/
public static byte[] buildOggPageHeader(int headerType, long granulepos,
int streamSerialNumber, int pageCount,
int packetCount, byte[] packetSizes) {
byte[] data = new byte[packetCount + 27];
writeOggPageHeader(data, 0, headerType, granulepos, streamSerialNumber,
pageCount, packetCount, packetSizes);
return data;
}
/**
* Writes a Speex Header to the given byte array.
* <p/>
* Speex Header structure:
* <pre>
* 0 - 7: speex_string
* 8 - 27: speex_version
* 28 - 31: speex_version_id
* 32 - 35: header_size
* 36 - 39: rate
* 40 - 43: mode (0=NB, 1=WB, 2=UWB)
* 44 - 47: mode_bitstream_version
* 48 - 51: nb_channels
* 52 - 55: bitrate
* 56 - 59: frame_size (NB=160, WB=320, UWB=640)
* 60 - 63: vbr
* 64 - 67: frames_per_packet
* 68 - 71: extra_headers
* 72 - 75: reserved1
* 76 - 79: reserved2
* </pre>
*
* @param buf the buffer to write to.
* @param offset the from which to start writing.
* @param sampleRate
* @param mode
* @param channels
* @param vbr
* @param nframes
* @return the amount of data written to the buffer.
*/
public static int writeSpeexHeader( byte[] buf, int offset, int sampleRate,
int mode, int channels, boolean vbr,
int nframes) {
writeString(buf, offset, "Speex "); // 0 - 7: speex_string
writeString(buf, offset + 8, "speex-1.0"); // 8 - 27: speex_version
System.arraycopy(new byte[11], 0, buf, offset + 17, 11); // : speex_version (fill in up to 20 bytes)
writeInt(buf, offset + 28, 1); // 28 - 31: speex_version_id
writeInt(buf, offset + 32, 80); // 32 - 35: header_size
writeInt(buf, offset + 36, sampleRate); // 36 - 39: rate
writeInt(buf, offset + 40, mode); // 40 - 43: mode (0=NB, 1=WB, 2=UWB)
writeInt(buf, offset + 44, 4); // 44 - 47: mode_bitstream_version
writeInt(buf, offset + 48, channels); // 48 - 51: nb_channels
writeInt(buf, offset + 52, -1); // 52 - 55: bitrate
writeInt(buf, offset + 56, 160 << mode); // 56 - 59: frame_size (NB=160, WB=320, UWB=640)
writeInt(buf, offset + 60, vbr ? 1 : 0); // 60 - 63: vbr
writeInt(buf, offset + 64, nframes); // 64 - 67: frames_per_packet
writeInt(buf, offset + 68, 0); // 68 - 71: extra_headers
writeInt(buf, offset + 72, 0); // 72 - 75: reserved1
writeInt(buf, offset + 76, 0); // 76 - 79: reserved2
return 80;
}
/**
* Builds a Speex Header.
*
* @param sampleRate
* @param mode
* @param channels
* @param vbr
* @param nframes
* @return a Speex Header.
*/
public static byte[] buildSpeexHeader(int sampleRate, int mode, int channels,
boolean vbr, int nframes) {
byte[] data = new byte[80];
writeSpeexHeader(data, 0, sampleRate, mode, channels, vbr, nframes);
return data;
}
/**
* Writes a Speex Comment to the given byte array.
*
* @param buf the buffer to write to.
* @param offset the from which to start writing.
* @param comment the comment.
* @return the amount of data written to the buffer.
*/
public static int writeSpeexComment( byte[] buf, int offset, String comment) {
int length = comment.length();
writeInt(buf, offset, length); // vendor comment size
writeString(buf, offset + 4, comment); // vendor comment
writeInt(buf, offset + length + 4, 0); // user comment list length
return length + 8;
}
/**
* Builds and returns a Speex Comment.
*
* @param comment the comment.
* @return a Speex Comment.
*/
public static byte[] buildSpeexComment( String comment) {
byte[] data = new byte[comment.length() + 8];
writeSpeexComment(data, 0, comment);
return data;
}
/**
* Writes a Little-endian short.
*
* @param out the data output to write to.
* @param v value to write.
* @throws IOException
*/
public static void writeShort( DataOutput out, short v)
throws IOException {
out.writeByte((0xff & v));
out.writeByte((0xff & (v >>> 8)));
}
/**
* Writes a Little-endian int.
*
* @param out the data output to write to.
* @param v value to write.
* @throws IOException
*/
public static void writeInt( DataOutput out, int v)
throws IOException {
out.writeByte(0xff & v);
out.writeByte(0xff & (v >>> 8));
out.writeByte(0xff & (v >>> 16));
out.writeByte(0xff & (v >>> 24));
}
/**
* Writes a Little-endian short.
*
* @param os - the output stream to write to.
* @param v - the value to write.
* @throws IOException
*/
public static void writeShort( OutputStream os, short v)
throws IOException {
os.write((0xff & v));
os.write((0xff & (v >>> 8)));
}
/**
* Writes a Little-endian int.
*
* @param os - the output stream to write to.
* @param v - the value to write.
* @throws IOException
*/
public static void writeInt( OutputStream os, int v)
throws IOException {
os.write(0xff & v);
os.write(0xff & (v >>> 8));
os.write(0xff & (v >>> 16));
os.write(0xff & (v >>> 24));
}
/**
* Writes a Little-endian long.
*
* @param os - the output stream to write to.
* @param v - the value to write.
* @throws IOException
*/
public static void writeLong( OutputStream os, long v)
throws IOException {
os.write((int) (0xff & v));
os.write((int) (0xff & (v >>> 8)));
os.write((int) (0xff & (v >>> 16)));
os.write((int) (0xff & (v >>> 24)));
os.write((int) (0xff & (v >>> 32)));
os.write((int) (0xff & (v >>> 40)));
os.write((int) (0xff & (v >>> 48)));
os.write((int) (0xff & (v >>> 56)));
}
/**
* Writes a Little-endian short.
*
* @param data the array into which the data should be written.
* @param offset the offset from which to start writing in the array.
* @param v the value to write.
*/
public static void writeShort(byte[] data, int offset, int v) {
data[offset] = (byte) (0xff & v);
data[offset + 1] = (byte) (0xff & (v >>> 8));
}
/**
* Writes a Little-endian int.
*
* @param data the array into which the data should be written.
* @param offset the offset from which to start writing in the array.
* @param v the value to write.
*/
public static void writeInt(byte[] data, int offset, int v) {
data[offset] = (byte) (0xff & v);
data[offset + 1] = (byte) (0xff & (v >>> 8));
data[offset + 2] = (byte) (0xff & (v >>> 16));
data[offset + 3] = (byte) (0xff & (v >>> 24));
}
/**
* Writes a Little-endian long.
*
* @param data the array into which the data should be written.
* @param offset the offset from which to start writing in the array.
* @param v the value to write.
*/
public static void writeLong(byte[] data, int offset, long v) {
data[offset] = (byte) (0xff & v);
data[offset + 1] = (byte) (0xff & (v >>> 8));
data[offset + 2] = (byte) (0xff & (v >>> 16));
data[offset + 3] = (byte) (0xff & (v >>> 24));
data[offset + 4] = (byte) (0xff & (v >>> 32));
data[offset + 5] = (byte) (0xff & (v >>> 40));
data[offset + 6] = (byte) (0xff & (v >>> 48));
data[offset + 7] = (byte) (0xff & (v >>> 56));
}
/**
* Writes a String.
*
* @param data the array into which the data should be written.
* @param offset the offset from which to start writing in the array.
* @param v the value to write.
*/
public static void writeString( byte[] data, int offset, String v) {
byte[] str = v.getBytes();
System.arraycopy(str, 0, data, offset, str.length);
}
}