Add MP3 identifier.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 15 Mar 2013 05:36:48 +0000 (06:36 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 15 Mar 2013 05:36:48 +0000 (06:36 +0100)
pom.xml
src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 37f979e..1121e62 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <artifactId>jorbis</artifactId>
                        <version>0.0.17</version>
                </dependency>
+               <dependency>
+                       <groupId>javazoom</groupId>
+                       <artifactId>jlayer</artifactId>
+                       <version>1.0.1</version>
+               </dependency>
        </dependencies>
 
        <properties>
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 (file)
index 0000000..b4001bb
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<Format> 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();
+               }
+       }
+
+}