summaryrefslogtreecommitdiff
path: root/songdbj/de/jarnbjo/vorbis/VorbisAudioFileReader.java
blob: b1bc99994778d59d9a5bc02d6587f84c09f7c337 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package de.jarnbjo.vorbis;

/*
 * $ProjectName$
 * $ProjectRevision$
 * -----------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------
 *
 * $Author$
 *
 * Description:
 *
 * Copyright 2002-2003 Tor-Einar Jarnbjo
 * -----------------------------------------------------------
 *
 * Change History
 * -----------------------------------------------------------
 * $Log$
 * Revision 1.1  2005/07/11 15:42:36  hcl
 * Songdb java version, source. only 1.5 compatible
 *
 * Revision 1.2  2004/09/21 06:39:06  shred
 * Importe reorganisiert, damit Eclipse Ruhe gibt. ;-)
 *
 * Revision 1.1.1.1  2004/04/04 22:09:12  shred
 * First Import
 *
 *
 */
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.Collection;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.AudioFileReader;

import de.jarnbjo.ogg.BasicStream;
import de.jarnbjo.ogg.EndOfOggStreamException;
import de.jarnbjo.ogg.FileStream;
import de.jarnbjo.ogg.LogicalOggStream;
import de.jarnbjo.ogg.OggFormatException;
import de.jarnbjo.ogg.PhysicalOggStream;
import de.jarnbjo.ogg.UncachedUrlStream;

public class VorbisAudioFileReader extends AudioFileReader {
	
	public VorbisAudioFileReader() {
	}
	
	public AudioFileFormat getAudioFileFormat(File file) throws IOException, UnsupportedAudioFileException {
		try {
			return getAudioFileFormat(new FileStream(new RandomAccessFile(file, "r")));	
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());	
		}
	}
	
	public AudioFileFormat getAudioFileFormat(InputStream stream) throws IOException, UnsupportedAudioFileException {
		try {
			return getAudioFileFormat(new BasicStream(stream));	
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());	
		}		
	}
	
	public AudioFileFormat getAudioFileFormat(URL url) throws IOException, UnsupportedAudioFileException {
		try {
			return getAudioFileFormat(new UncachedUrlStream(url));	
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());	
		}		
	}
	
	private AudioFileFormat getAudioFileFormat(PhysicalOggStream oggStream)  throws IOException, UnsupportedAudioFileException {
		try {
			Collection streams=oggStream.getLogicalStreams();
			if(streams.size()!=1) {
				throw new UnsupportedAudioFileException("Only Ogg files with one logical Vorbis stream are supported.");	
			}
			
			LogicalOggStream los=(LogicalOggStream)streams.iterator().next();
			if(los.getFormat()!=LogicalOggStream.FORMAT_VORBIS) {
				throw new UnsupportedAudioFileException("Only Ogg files with one logical Vorbis stream are supported.");	
			}

			VorbisStream vs=new VorbisStream(los);
			
	        AudioFormat audioFormat=new AudioFormat(
            	(float)vs.getIdentificationHeader().getSampleRate(),
            	16,
            	vs.getIdentificationHeader().getChannels(),
            	true, true);
			
			return new AudioFileFormat(VorbisFormatType.getInstance(), audioFormat, AudioSystem.NOT_SPECIFIED);
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());
		}
		catch(VorbisFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());
		}
	}
	
	
	
	public AudioInputStream getAudioInputStream(File file) throws IOException, UnsupportedAudioFileException {
		try {
			return getAudioInputStream(new FileStream(new RandomAccessFile(file, "r")));	
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());	
		}
	}
	
	public AudioInputStream getAudioInputStream(InputStream stream) throws IOException, UnsupportedAudioFileException {
		try {
			return getAudioInputStream(new BasicStream(stream));	
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());	
		}		
	}
	
	public AudioInputStream getAudioInputStream(URL url) throws IOException, UnsupportedAudioFileException {
		try {
			return getAudioInputStream(new UncachedUrlStream(url));	
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());	
		}		
	}
	
	private AudioInputStream getAudioInputStream(PhysicalOggStream oggStream)  throws IOException, UnsupportedAudioFileException {
		try {
			Collection streams=oggStream.getLogicalStreams();
			if(streams.size()!=1) {
				throw new UnsupportedAudioFileException("Only Ogg files with one logical Vorbis stream are supported.");	
			}
			
			LogicalOggStream los=(LogicalOggStream)streams.iterator().next();
			if(los.getFormat()!=LogicalOggStream.FORMAT_VORBIS) {
				throw new UnsupportedAudioFileException("Only Ogg files with one logical Vorbis stream are supported.");	
			}

			VorbisStream vs=new VorbisStream(los);

	        AudioFormat audioFormat=new AudioFormat(
            	(float)vs.getIdentificationHeader().getSampleRate(),
            	16,
            	vs.getIdentificationHeader().getChannels(),
            	true, true);
			
         	return new AudioInputStream(new VorbisInputStream(vs), audioFormat, -1);
		}
		catch(OggFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());
		}
		catch(VorbisFormatException e) {
			throw new UnsupportedAudioFileException(e.getMessage());
		}
	}
	
	
	public static class VorbisFormatType extends AudioFileFormat.Type {
		
		private static final VorbisFormatType instance=new VorbisFormatType();
		
		private VorbisFormatType() {
			super("VORBIS", "ogg");
		}
		
		public static AudioFileFormat.Type getInstance() {
			return instance;
		}
	}
	
   	public static class VorbisInputStream extends InputStream {

      	private VorbisStream source;
      	private byte[] buffer=new byte[8192];

      	public VorbisInputStream(VorbisStream source) {
         	this.source=source;
      	}

      	public int read() throws IOException {
         	return 0;
      	}

      	public int read(byte[] buffer) throws IOException {
         	return read(buffer, 0, buffer.length);
      	}

      	public int read(byte[] buffer, int offset, int length) throws IOException {
         	try {
            	return source.readPcm(buffer, offset, length);
         	}
         	catch(EndOfOggStreamException e) {
            	return -1;
         	}
      	}
   	}

	
}