2 * Sonitus - OggVorbisIdentifier.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.io;
20 import java.io.IOException;
21 import java.io.InputStream;
23 import net.pterodactylus.sonitus.data.Format;
25 import com.jcraft.jogg.Packet;
26 import com.jcraft.jogg.Page;
27 import com.jcraft.jogg.StreamState;
28 import com.jcraft.jogg.SyncState;
29 import com.jcraft.jorbis.Comment;
30 import com.jcraft.jorbis.Info;
33 * Identifies Ogg Vorbis files.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class OggVorbisIdentifier {
39 /** The default size of the read buffer. */
40 private static final int BUFFER_SIZE = 4096;
42 /** Suppress default constructor. */
43 private OggVorbisIdentifier() {
48 * Tries to parse the given stream as Ogg Vorbis file and returns a {@link
49 * Format} describing the stream.
55 public static Format identify(InputStream inputStream) throws IOException {
57 /* stuff needed to decode Ogg. */
58 Packet packet = new Packet();
59 Page page = new Page();
60 StreamState streamState = new StreamState();
61 SyncState syncState = new SyncState();
63 /* stuff needed to decode Vorbis. */
64 Comment comment = new Comment();
65 Info info = new Info();
67 /* initialize jorbis. */
69 int bufferSize = BUFFER_SIZE;
70 int index = syncState.buffer(bufferSize);
71 byte[] buffer = syncState.data;
73 boolean streamStateInitialized = false;
76 /* read until we have read the three packets required to decode the header. */
77 while (packetsRead < 3) {
78 int read = inputStream.read(buffer, index, bufferSize);
79 syncState.wrote(read);
80 switch (syncState.pageout(page)) {
82 throw new IdentifierException("Hole in Ogg data!");
84 if (!streamStateInitialized) {
85 /* init stream state. */
86 streamState.init(page.serialno());
90 streamStateInitialized = true;
92 if (streamState.pagein(page) == -1) {
93 throw new IdentifierException("Error parsing Ogg data!");
95 switch (streamState.packetout(packet)) {
97 throw new IdentifierException("Error parsing Ogg data!");
99 info.synthesis_headerin(comment, packet);
108 index = syncState.buffer(bufferSize);
109 buffer = syncState.data;
112 return new Format(info.channels, info.rate, "Vorbis");