01: /*
02: * @(#)NativeAudioStream.java 1.11 06/10/10
03: *
04: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: *
26: */
27:
28: package sun.audio;
29:
30: import java.io.InputStream;
31: import java.io.DataInputStream;
32: import java.io.FilterInputStream;
33: import java.io.IOException;
34:
35: /**
36: * A Sun-specific AudioStream that supports the .au file format.
37: *
38: * @version 1.7, 08/19/02
39: */
40: public class NativeAudioStream extends FilterInputStream {
41: private final int SUN_MAGIC = 0x2e736e64;
42: private final int DEC_MAGIC = 0x2e736400;
43: private final int MINHDRSIZE = 24;
44: private final int TYPE_ULAW = 1;
45: private int length = 0;
46:
47: /**
48: * Read header, only sun 8 bit, ulaw encoded, single channel,
49: * 8000hz is supported
50: */
51: public NativeAudioStream(InputStream in) throws IOException {
52: super (in);
53: DataInputStream data = new DataInputStream(in);
54: int magic = data.readInt();
55: if (magic != SUN_MAGIC && magic != DEC_MAGIC) {
56: System.out.println("NativeAudioStream: invalid file type.");
57: throw new InvalidAudioFormatException();
58: }
59: int hdr_size = data.readInt(); // header size
60: if (hdr_size < MINHDRSIZE) {
61: System.out
62: .println("NativeAudioStream: wrong header size of "
63: + hdr_size + ".");
64: throw new InvalidAudioFormatException();
65: }
66: length = data.readInt();
67: int encoding = data.readInt();
68: if (encoding != TYPE_ULAW) {
69: System.out
70: .println("NativeAudioStream: invalid audio encoding.");
71: throw new InvalidAudioFormatException();
72: }
73: int sample_rate = data.readInt();
74: if ((sample_rate / 1000) != 8) { // allow some slop
75: System.out
76: .println("NativeAudioStream: invalid sample rate of "
77: + sample_rate + ".");
78: throw new InvalidAudioFormatException();
79: }
80: int channels = data.readInt();
81: if (channels != 1) {
82: System.out
83: .println("NativeAudioStream: wrong number of channels. "
84: + "(wanted 1, actual " + channels + ")");
85: throw new InvalidAudioFormatException();
86: }
87: in.skip(hdr_size - MINHDRSIZE);
88: }
89:
90: public int getLength() {
91: return length;
92: }
93: }
|