2 * Sonitus - Mp3Identifier.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.google.common.base.Optional;
26 import javazoom.jl.decoder.Bitstream;
27 import javazoom.jl.decoder.BitstreamException;
28 import javazoom.jl.decoder.Header;
31 * Identifies MP3 files.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class Mp3Identifier {
38 * Tries to identify the MP3 file contained in the given stream.
42 * @return The identified format, or {@link com.google.common.base.Optional#absent()}
43 * if the format can not be identified
45 * if an I/O error occurs
47 public static Optional<Format> identify(InputStream inputStream) throws IOException {
48 Bitstream bitstream = new Bitstream(inputStream);
50 Header frame = bitstream.readFrame();
52 return Optional.absent();
54 return Optional.of(new Format(frame.mode() == Header.SINGLE_CHANNEL ? 1 : 2, frame.frequency(), "MP3"));
55 } catch (BitstreamException be1) {
56 return Optional.absent();