From 3b99111c3d082841f0a7f5819103fb3db6ba8447 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Mar 2013 22:08:00 +0100 Subject: [PATCH] Add Ogg Vorbis identifier. --- .../sonitus/io/OggVorbisIdentifier.java | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java diff --git a/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java new file mode 100644 index 0000000..64ad2dd --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java @@ -0,0 +1,115 @@ +/* + * Sonitus - OggVorbisIdentifier.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.io; + +import java.io.IOException; +import java.io.InputStream; + +import net.pterodactylus.sonitus.data.Format; + +import com.jcraft.jogg.Packet; +import com.jcraft.jogg.Page; +import com.jcraft.jogg.StreamState; +import com.jcraft.jogg.SyncState; +import com.jcraft.jorbis.Comment; +import com.jcraft.jorbis.Info; + +/** + * Identifies Ogg Vorbis files. + * + * @author David ‘Bombe’ Roden + */ +public class OggVorbisIdentifier { + + /** The default size of the read buffer. */ + private static final int BUFFER_SIZE = 4096; + + /** Suppress default constructor. */ + private OggVorbisIdentifier() { + /* nothing here. */ + } + + /** + * Tries to parse the given stream as Ogg Vorbis file and returns a {@link + * Format} describing the stream. + * + * @param inputStream + * @return + * @throws IOException + */ + public static Format identify(InputStream inputStream) throws IOException { + + /* stuff needed to decode Ogg. */ + Packet packet = new Packet(); + Page page = new Page(); + StreamState streamState = new StreamState(); + SyncState syncState = new SyncState(); + + /* stuff needed to decode Vorbis. */ + Comment comment = new Comment(); + Info info = new Info(); + + /* initialize jorbis. */ + syncState.init(); + int bufferSize = BUFFER_SIZE; + int index = syncState.buffer(bufferSize); + byte[] buffer = syncState.data; + + boolean streamStateInitialized = false; + int packetsRead = 0; + + /* read until we have read the three packets required to decode the header. */ + while (packetsRead < 3) { + int read = inputStream.read(buffer, index, bufferSize); + syncState.wrote(read); + switch (syncState.pageout(page)) { + case -1: + throw new IOException("Hole in Ogg data!"); + case 1: + if (!streamStateInitialized) { + /* init stream state. */ + streamState.init(page.serialno()); + streamState.reset(); + info.init(); + comment.init(); + streamStateInitialized = true; + } + if (streamState.pagein(page) == -1) { + throw new IOException("Error parsing Ogg data!"); + } + switch (streamState.packetout(packet)) { + case -1: + throw new IOException("Error parsing Ogg data!"); + case 1: + info.synthesis_headerin(comment, packet); + packetsRead++; + default: + /* continue. */ + } + + default: + /* continue. */ + } + index = syncState.buffer(bufferSize); + buffer = syncState.data; + } + + return new Format(info.channels, info.rate, "Vorbis"); + } + +} -- 2.7.4