From: David ‘Bombe’ Roden Date: Fri, 15 Mar 2013 05:36:48 +0000 (+0100) Subject: Add MP3 identifier. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c6e8341e830e6a8360a1d7279487f7ea497a3512;p=sonitus.git Add MP3 identifier. --- diff --git a/pom.xml b/pom.xml index 37f979e..1121e62 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,11 @@ jorbis 0.0.17 + + javazoom + jlayer + 1.0.1 + diff --git a/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java b/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java new file mode 100644 index 0000000..b4001bb --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java @@ -0,0 +1,60 @@ +/* + * Sonitus - Mp3Identifier.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.google.common.base.Optional; +import javazoom.jl.decoder.Bitstream; +import javazoom.jl.decoder.BitstreamException; +import javazoom.jl.decoder.Header; + +/** + * Identifies MP3 files. + * + * @author David ‘Bombe’ Roden + */ +public class Mp3Identifier { + + /** + * Tries to identify the MP3 file contained in the given stream. + * + * @param inputStream + * The input stream + * @return The identified format, or {@link com.google.common.base.Optional#absent()} + * if the format can not be identified + * @throws IOException + * if an I/O error occurs + */ + public static Optional identify(InputStream inputStream) throws IOException { + Bitstream bitstream = new Bitstream(inputStream); + try { + Header frame = bitstream.readFrame(); + if (frame == null) { + return Optional.absent(); + } + return Optional.of(new Format(frame.mode() == Header.SINGLE_CHANNEL ? 1 : 2, frame.frequency(), "MP3")); + } catch (BitstreamException be1) { + return Optional.absent(); + } + } + +}