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
|
/*
* AuAudioFileReader.java
*
* This file is part of Tritonus: http://www.tritonus.org/
*/
/*
* Copyright (c) 1999,2000,2001 by Florian Bomers <http://www.bomers.de>
* Copyright (c) 1999 by Matthias Pfisterer
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
|<--- this code is formatted to fit into 80 columns --->|
*/
package org.tritonus.sampled.file;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.AudioFileReader;
import org.tritonus.share.TDebug;
import org.tritonus.share.sampled.file.TAudioFileFormat;
import org.tritonus.share.sampled.file.TAudioFileReader;
/** Class for reading Sun/Next AU files.
*
* @author Florian Bomers
* @author Matthias Pfisterer
*/
public class AuAudioFileReader extends TAudioFileReader
{
private static final int READ_LIMIT = 1000;
public AuAudioFileReader()
{
super(READ_LIMIT);
}
private static String readDescription(DataInputStream dis, int len) throws IOException {
byte c=-1;
String ret="";
while (len>0 && (c=dis.readByte())!=0) {
ret=ret+(char) c;
len--;
}
if (len>1 && c==0) {
dis.skip(len-1);
}
return ret;
}
protected AudioFileFormat getAudioFileFormat(InputStream inputStream, long lFileSizeInBytes)
throws UnsupportedAudioFileException, IOException
{
if (TDebug.TraceAudioFileReader) {TDebug.out("AuAudioFileReader.getAudioFileFormat(InputStream, long): begin"); }
DataInputStream dataInputStream = new DataInputStream(inputStream);
int nMagic = dataInputStream.readInt();
if (nMagic != AuTool.AU_HEADER_MAGIC) {
throw new UnsupportedAudioFileException(
"not an AU file: wrong header magic");
}
int nDataOffset = dataInputStream.readInt();
if (TDebug.TraceAudioFileReader) {
TDebug.out("AuAudioFileReader.getAudioFileFormat(): data offset: " + nDataOffset);
}
if (nDataOffset < AuTool.DATA_OFFSET) {
throw new UnsupportedAudioFileException(
"not an AU file: data offset must be 24 or greater");
}
int nDataLength = dataInputStream.readInt();
if (TDebug.TraceAudioFileReader) {
TDebug.out("AuAudioFileReader.getAudioFileFormat(): data length: " + nDataLength);
}
if (nDataLength < 0 && nDataLength!=AuTool.AUDIO_UNKNOWN_SIZE) {
throw new UnsupportedAudioFileException(
"not an AU file: data length must be positive, 0 or -1 for unknown");
}
AudioFormat.Encoding encoding = null;
int nSampleSize = 0;
int nEncoding = dataInputStream.readInt();
switch (nEncoding) {
case AuTool.SND_FORMAT_MULAW_8: // 8-bit uLaw G.711
encoding = AudioFormat.Encoding.ULAW;
nSampleSize = 8;
break;
case AuTool.SND_FORMAT_LINEAR_8:
encoding = AudioFormat.Encoding.PCM_SIGNED;
nSampleSize = 8;
break;
case AuTool.SND_FORMAT_LINEAR_16:
encoding = AudioFormat.Encoding.PCM_SIGNED;
nSampleSize = 16;
break;
case AuTool.SND_FORMAT_LINEAR_24:
encoding = AudioFormat.Encoding.PCM_SIGNED;
nSampleSize = 24;
break;
case AuTool.SND_FORMAT_LINEAR_32:
encoding = AudioFormat.Encoding.PCM_SIGNED;
nSampleSize = 32;
break;
case AuTool.SND_FORMAT_ALAW_8: // 8-bit aLaw G.711
encoding = AudioFormat.Encoding.ALAW;
nSampleSize = 8;
break;
}
if (nSampleSize == 0) {
throw new UnsupportedAudioFileException(
"unsupported AU file: unknown encoding " + nEncoding);
}
int nSampleRate = dataInputStream.readInt();
if (nSampleRate <= 0) {
throw new UnsupportedAudioFileException(
"corrupt AU file: sample rate must be positive");
}
int nNumChannels = dataInputStream.readInt();
if (nNumChannels <= 0) {
throw new UnsupportedAudioFileException(
"corrupt AU file: number of channels must be positive");
}
// skip header information field
inputStream.skip(nDataOffset - AuTool.DATA_OFFSET);
// read header info field
//String desc=readDescription(dataInputStream, nDataOffset - AuTool.DATA_OFFSET);
AudioFormat format = new AudioFormat(encoding,
(float) nSampleRate,
nSampleSize,
nNumChannels,
calculateFrameSize(nSampleSize, nNumChannels),
(float) nSampleRate,
true);
AudioFileFormat audioFileFormat = new TAudioFileFormat(
AudioFileFormat.Type.AU,
format,
(nDataLength==AuTool.AUDIO_UNKNOWN_SIZE)?
AudioSystem.NOT_SPECIFIED:(nDataLength / format.getFrameSize()),
(nDataLength==AuTool.AUDIO_UNKNOWN_SIZE)?
AudioSystem.NOT_SPECIFIED:(nDataLength + nDataOffset));
if (TDebug.TraceAudioFileReader) { TDebug.out("AuAudioFileReader.getAudioFileFormat(InputStream, long): begin"); }
return audioFileFormat;
}
}
/*** AuAudioFileReader.java ***/
|