|
1 /* |
|
2 * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.media.sound; |
|
27 |
|
28 import java.util.Vector; |
|
29 import java.io.File; |
|
30 import java.io.InputStream; |
|
31 import java.io.OutputStream; |
|
32 import java.io.IOException; |
|
33 import java.lang.IllegalArgumentException; |
|
34 |
|
35 import java.io.BufferedOutputStream; |
|
36 import java.io.DataOutputStream; |
|
37 import java.io.FileOutputStream; |
|
38 import java.io.ByteArrayInputStream; |
|
39 import java.io.ByteArrayOutputStream; |
|
40 import java.io.SequenceInputStream; |
|
41 |
|
42 import javax.sound.sampled.AudioFileFormat; |
|
43 import javax.sound.sampled.AudioInputStream; |
|
44 import javax.sound.sampled.AudioFormat; |
|
45 import javax.sound.sampled.AudioSystem; |
|
46 |
|
47 |
|
48 /** |
|
49 * WAVE file format class. |
|
50 * |
|
51 * @author Jan Borgersen |
|
52 */ |
|
53 |
|
54 class WaveFileFormat extends AudioFileFormat { |
|
55 |
|
56 /** |
|
57 * Wave format type. |
|
58 */ |
|
59 private int waveType; |
|
60 |
|
61 //$$fb 2001-07-13: added management of header size in this class |
|
62 //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant |
|
63 private static final int STANDARD_HEADER_SIZE = 28; |
|
64 |
|
65 //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant |
|
66 /** |
|
67 * fmt_ chunk size in bytes |
|
68 */ |
|
69 private static final int STANDARD_FMT_CHUNK_SIZE = 16; |
|
70 |
|
71 // magic numbers |
|
72 static final int RIFF_MAGIC = 1380533830; |
|
73 static final int WAVE_MAGIC = 1463899717; |
|
74 static final int FMT_MAGIC = 0x666d7420; // "fmt " |
|
75 static final int DATA_MAGIC = 0x64617461; // "data" |
|
76 |
|
77 // encodings |
|
78 static final int WAVE_FORMAT_UNKNOWN = 0x0000; |
|
79 static final int WAVE_FORMAT_PCM = 0x0001; |
|
80 static final int WAVE_FORMAT_ADPCM = 0x0002; |
|
81 static final int WAVE_FORMAT_ALAW = 0x0006; |
|
82 static final int WAVE_FORMAT_MULAW = 0x0007; |
|
83 static final int WAVE_FORMAT_OKI_ADPCM = 0x0010; |
|
84 static final int WAVE_FORMAT_DIGISTD = 0x0015; |
|
85 static final int WAVE_FORMAT_DIGIFIX = 0x0016; |
|
86 static final int WAVE_IBM_FORMAT_MULAW = 0x0101; |
|
87 static final int WAVE_IBM_FORMAT_ALAW = 0x0102; |
|
88 static final int WAVE_IBM_FORMAT_ADPCM = 0x0103; |
|
89 static final int WAVE_FORMAT_DVI_ADPCM = 0x0011; |
|
90 static final int WAVE_FORMAT_SX7383 = 0x1C07; |
|
91 |
|
92 |
|
93 WaveFileFormat( AudioFileFormat aff ) { |
|
94 |
|
95 this( aff.getType(), aff.getByteLength(), aff.getFormat(), aff.getFrameLength() ); |
|
96 } |
|
97 |
|
98 WaveFileFormat(AudioFileFormat.Type type, int lengthInBytes, AudioFormat format, int lengthInFrames) { |
|
99 |
|
100 super(type,lengthInBytes,format,lengthInFrames); |
|
101 |
|
102 AudioFormat.Encoding encoding = format.getEncoding(); |
|
103 |
|
104 if( encoding.equals(AudioFormat.Encoding.ALAW) ) { |
|
105 waveType = WAVE_FORMAT_ALAW; |
|
106 } else if( encoding.equals(AudioFormat.Encoding.ULAW) ) { |
|
107 waveType = WAVE_FORMAT_MULAW; |
|
108 } else if( encoding.equals(AudioFormat.Encoding.PCM_SIGNED) || |
|
109 encoding.equals(AudioFormat.Encoding.PCM_UNSIGNED) ) { |
|
110 waveType = WAVE_FORMAT_PCM; |
|
111 } else { |
|
112 waveType = WAVE_FORMAT_UNKNOWN; |
|
113 } |
|
114 } |
|
115 |
|
116 int getWaveType() { |
|
117 |
|
118 return waveType; |
|
119 } |
|
120 |
|
121 int getHeaderSize() { |
|
122 return getHeaderSize(getWaveType()); |
|
123 } |
|
124 |
|
125 static int getHeaderSize(int waveType) { |
|
126 //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant |
|
127 // use dynamic format chunk size |
|
128 return STANDARD_HEADER_SIZE + getFmtChunkSize(waveType); |
|
129 } |
|
130 |
|
131 static int getFmtChunkSize(int waveType) { |
|
132 //$$fb 2002-04-16: Fix for 4636355: RIFF audio headers could be _more_ spec compliant |
|
133 // add 2 bytes for "codec specific data length" for non-PCM codecs |
|
134 int result = STANDARD_FMT_CHUNK_SIZE; |
|
135 if (waveType != WAVE_FORMAT_PCM) { |
|
136 result += 2; // WORD for "codec specific data length" |
|
137 } |
|
138 return result; |
|
139 } |
|
140 } |