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