summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/file/AuAudioFileReader.java
diff options
context:
space:
mode:
authorMichiel Van Der Kolk <not.valid@email.address>2005-07-11 15:42:37 +0000
committerMichiel Van Der Kolk <not.valid@email.address>2005-07-11 15:42:37 +0000
commit9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e (patch)
tree4c304cd4151020bd5494d279ee68a105ae3a5a3a /songdbj/org/tritonus/file/AuAudioFileReader.java
parentdfa8ecbe609ca8ea194d08560a44fb9a92e94b4b (diff)
downloadrockbox-9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e.tar.gz
rockbox-9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e.zip
Songdb java version, source. only 1.5 compatible
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7101 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'songdbj/org/tritonus/file/AuAudioFileReader.java')
-rw-r--r--songdbj/org/tritonus/file/AuAudioFileReader.java185
1 files changed, 185 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/file/AuAudioFileReader.java b/songdbj/org/tritonus/file/AuAudioFileReader.java
new file mode 100644
index 0000000000..b527920118
--- /dev/null
+++ b/songdbj/org/tritonus/file/AuAudioFileReader.java
@@ -0,0 +1,185 @@
1/*
2 * AuAudioFileReader.java
3 *
4 * This file is part of Tritonus: http://www.tritonus.org/
5 */
6
7/*
8 * Copyright (c) 1999,2000,2001 by Florian Bomers <http://www.bomers.de>
9 * Copyright (c) 1999 by Matthias Pfisterer
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as published
14 * by the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 */
27
28/*
29|<--- this code is formatted to fit into 80 columns --->|
30*/
31
32package org.tritonus.sampled.file;
33
34import java.io.DataInputStream;
35import java.io.File;
36import java.io.InputStream;
37import java.io.IOException;
38
39import javax.sound.sampled.AudioFormat;
40import javax.sound.sampled.AudioFileFormat;
41import javax.sound.sampled.AudioInputStream;
42import javax.sound.sampled.AudioSystem;
43import javax.sound.sampled.UnsupportedAudioFileException;
44import javax.sound.sampled.spi.AudioFileReader;
45
46import org.tritonus.share.TDebug;
47import org.tritonus.share.sampled.file.TAudioFileFormat;
48import org.tritonus.share.sampled.file.TAudioFileReader;
49
50
51/** Class for reading Sun/Next AU files.
52 *
53 * @author Florian Bomers
54 * @author Matthias Pfisterer
55 */
56public class AuAudioFileReader extends TAudioFileReader
57{
58 private static final int READ_LIMIT = 1000;
59
60
61
62 public AuAudioFileReader()
63 {
64 super(READ_LIMIT);
65 }
66
67
68
69 private static String readDescription(DataInputStream dis, int len) throws IOException {
70 byte c=-1;
71 String ret="";
72 while (len>0 && (c=dis.readByte())!=0) {
73 ret=ret+(char) c;
74 len--;
75 }
76 if (len>1 && c==0) {
77 dis.skip(len-1);
78 }
79 return ret;
80 }
81
82
83
84 protected AudioFileFormat getAudioFileFormat(InputStream inputStream, long lFileSizeInBytes)
85 throws UnsupportedAudioFileException, IOException
86 {
87 if (TDebug.TraceAudioFileReader) {TDebug.out("AuAudioFileReader.getAudioFileFormat(InputStream, long): begin"); }
88 DataInputStream dataInputStream = new DataInputStream(inputStream);
89 int nMagic = dataInputStream.readInt();
90 if (nMagic != AuTool.AU_HEADER_MAGIC) {
91 throw new UnsupportedAudioFileException(
92 "not an AU file: wrong header magic");
93 }
94 int nDataOffset = dataInputStream.readInt();
95 if (TDebug.TraceAudioFileReader) {
96 TDebug.out("AuAudioFileReader.getAudioFileFormat(): data offset: " + nDataOffset);
97 }
98 if (nDataOffset < AuTool.DATA_OFFSET) {
99 throw new UnsupportedAudioFileException(
100 "not an AU file: data offset must be 24 or greater");
101 }
102 int nDataLength = dataInputStream.readInt();
103 if (TDebug.TraceAudioFileReader) {
104 TDebug.out("AuAudioFileReader.getAudioFileFormat(): data length: " + nDataLength);
105 }
106 if (nDataLength < 0 && nDataLength!=AuTool.AUDIO_UNKNOWN_SIZE) {
107 throw new UnsupportedAudioFileException(
108 "not an AU file: data length must be positive, 0 or -1 for unknown");
109 }
110 AudioFormat.Encoding encoding = null;
111 int nSampleSize = 0;
112 int nEncoding = dataInputStream.readInt();
113 switch (nEncoding) {
114 case AuTool.SND_FORMAT_MULAW_8: // 8-bit uLaw G.711
115 encoding = AudioFormat.Encoding.ULAW;
116 nSampleSize = 8;
117 break;
118
119 case AuTool.SND_FORMAT_LINEAR_8:
120 encoding = AudioFormat.Encoding.PCM_SIGNED;
121 nSampleSize = 8;
122 break;
123
124 case AuTool.SND_FORMAT_LINEAR_16:
125 encoding = AudioFormat.Encoding.PCM_SIGNED;
126 nSampleSize = 16;
127 break;
128
129 case AuTool.SND_FORMAT_LINEAR_24:
130 encoding = AudioFormat.Encoding.PCM_SIGNED;
131 nSampleSize = 24;
132 break;
133
134 case AuTool.SND_FORMAT_LINEAR_32:
135 encoding = AudioFormat.Encoding.PCM_SIGNED;
136 nSampleSize = 32;
137 break;
138
139 case AuTool.SND_FORMAT_ALAW_8: // 8-bit aLaw G.711
140 encoding = AudioFormat.Encoding.ALAW;
141 nSampleSize = 8;
142 break;
143 }
144 if (nSampleSize == 0) {
145 throw new UnsupportedAudioFileException(
146 "unsupported AU file: unknown encoding " + nEncoding);
147 }
148 int nSampleRate = dataInputStream.readInt();
149 if (nSampleRate <= 0) {
150 throw new UnsupportedAudioFileException(
151 "corrupt AU file: sample rate must be positive");
152 }
153 int nNumChannels = dataInputStream.readInt();
154 if (nNumChannels <= 0) {
155 throw new UnsupportedAudioFileException(
156 "corrupt AU file: number of channels must be positive");
157 }
158 // skip header information field
159 inputStream.skip(nDataOffset - AuTool.DATA_OFFSET);
160 // read header info field
161 //String desc=readDescription(dataInputStream, nDataOffset - AuTool.DATA_OFFSET);
162
163 AudioFormat format = new AudioFormat(encoding,
164 (float) nSampleRate,
165 nSampleSize,
166 nNumChannels,
167 calculateFrameSize(nSampleSize, nNumChannels),
168 (float) nSampleRate,
169 true);
170 AudioFileFormat audioFileFormat = new TAudioFileFormat(
171 AudioFileFormat.Type.AU,
172 format,
173 (nDataLength==AuTool.AUDIO_UNKNOWN_SIZE)?
174 AudioSystem.NOT_SPECIFIED:(nDataLength / format.getFrameSize()),
175 (nDataLength==AuTool.AUDIO_UNKNOWN_SIZE)?
176 AudioSystem.NOT_SPECIFIED:(nDataLength + nDataOffset));
177 if (TDebug.TraceAudioFileReader) { TDebug.out("AuAudioFileReader.getAudioFileFormat(InputStream, long): begin"); }
178 return audioFileFormat;
179 }
180}
181
182
183
184/*** AuAudioFileReader.java ***/
185