1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55: public class AUReader extends AudioFileReader
56: {
57: private static class AUHeader
58: {
59:
60: private static final int MAGIC = 0x2e736e64;
61:
62: public static final int SIZE = 24;
63:
64:
65: public static final int ULAW = 1;
66: public static final int PCM8 = 2;
67: public static final int PCM16 = 3;
68: public static final int PCM24 = 4;
69: public static final int PCM32 = 5;
70: public static final int IEEE32 = 6;
71: public static final int IEEE64 = 7;
72: public static final int G721 = 23;
73: public static final int G722 = 24;
74: public static final int G723 = 25;
75: public static final int G723_5BIT = 26;
76: public static final int ALAW = 27;
77:
78:
79: public int headerSize;
80: public int fileSize;
81: public int encoding;
82: public int sampleRate;
83: public int channels;
84: public int sampleSizeInBits;
85:
86: public AUHeader(InputStream stream)
87: throws IOException, UnsupportedAudioFileException
88: {
89: byte[] hdr = new byte[24];
90: stream.read( hdr );
91: ByteBuffer buf = ByteBuffer.wrap(hdr);
92:
93: if( buf.getInt() != MAGIC )
94: throw new UnsupportedAudioFileException("Not an AU format audio file.");
95: headerSize = buf.getInt();
96: fileSize = buf.getInt();
97: encoding = buf.getInt();
98: sampleRate = buf.getInt();
99: channels = buf.getInt();
100:
101: switch(encoding)
102: {
103: case ULAW:
104: case PCM8:
105: case ALAW:
106: sampleSizeInBits = 8;
107: break;
108: case PCM16:
109: sampleSizeInBits = 16;
110: break;
111: case PCM24:
112: sampleSizeInBits = 24;
113: break;
114: case PCM32:
115: sampleSizeInBits = 32;
116: break;
117: default:
118: throw new UnsupportedAudioFileException("Unsupported encoding.");
119: }
120: }
121:
122: public AudioFormat getAudioFormat()
123: {
124: AudioFormat.Encoding encType = AudioFormat.Encoding.PCM_SIGNED;
125: if(encoding == 1)
126: encType = AudioFormat.Encoding.ULAW;
127: if(encoding == 27)
128: encType = AudioFormat.Encoding.ALAW;
129:
130: return new AudioFormat(encType,
131: (float)sampleRate,
132: sampleSizeInBits,
133: channels,
134: (sampleSizeInBits >> 3) * channels,
135: (float)sampleRate,
136: true);
137: }
138:
139: public AudioFileFormat getAudioFileFormat()
140: {
141: return new AudioFileFormat(new AUFormatType(),
142: getAudioFormat(),
143: AudioSystem.NOT_SPECIFIED);
144: }
145: }
146:
147: public static class AUFormatType extends AudioFileFormat.Type
148: {
149: public AUFormatType()
150: {
151: super("AU", ".au");
152: }
153: }
154:
155: public AudioFileFormat getAudioFileFormat(File file)
156: throws IOException, UnsupportedAudioFileException
157: {
158: return getAudioFileFormat(new FileInputStream(file));
159: }
160:
161: public AudioFileFormat getAudioFileFormat(InputStream stream)
162: throws IOException, UnsupportedAudioFileException
163: {
164: if(!stream.markSupported())
165: throw new IOException("Stream must support marking.");
166:
167: stream.mark(25);
168: AUHeader header = new AUHeader(stream);
169: stream.reset();
170:
171: return header.getAudioFileFormat();
172: }
173:
174: public AudioFileFormat getAudioFileFormat(URL url)
175: throws IOException, UnsupportedAudioFileException
176: {
177: return getAudioFileFormat(new BufferedInputStream(url.openStream()));
178: }
179:
180: public AudioInputStream getAudioInputStream(File file)
181: throws IOException, UnsupportedAudioFileException
182: {
183: InputStream stream = new FileInputStream(file);
184: long length = file.length();
185:
186: AUHeader header = new AUHeader( stream );
187: if( header.headerSize > AUHeader.SIZE )
188: stream.skip(header.headerSize - AUHeader.SIZE);
189:
190: length -= header.headerSize;
191:
192: return new AudioInputStream(stream, header.getAudioFormat(), length);
193: }
194:
195: public AudioInputStream getAudioInputStream(InputStream stream)
196: throws IOException, UnsupportedAudioFileException
197: {
198: AUHeader header = new AUHeader( stream );
199: if( header.headerSize > AUHeader.SIZE )
200: stream.skip(header.headerSize - AUHeader.SIZE);
201:
202: return new AudioInputStream(stream, header.getAudioFormat(),
203: AudioSystem.NOT_SPECIFIED);
204: }
205:
206: public AudioInputStream getAudioInputStream(URL url)
207: throws IOException, UnsupportedAudioFileException
208: {
209: return getAudioInputStream(new BufferedInputStream(url.openStream()));
210: }
211: }