2 * DemosceneMusic - AudioCodecFilter.java - Copyright © 2012 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.demoscenemusic.utils;
20 import java.util.HashMap;
24 * Helper for audio codec descriptions.
26 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28 public class AudioCodecs {
30 /** Descriptions for audio codecs. */
31 public static final Map<String, AudioCodecDescription> codecDescriptions = new HashMap<String, AudioCodecDescription>();
34 codecDescriptions.put("mp3", new AudioCodecDescription("mp3", "MPEG 1 Layer 3", "MP3", "audio/mpeg", true, false));
35 codecDescriptions.put("vorbis", new AudioCodecDescription("vorbis", "Ogg Vorbis", "Vorbis", "audio/vorbis", true, false));
36 codecDescriptions.put("aac", new AudioCodecDescription("aac", "Advanced Audio Coding", "AAC", "audio/x-aac", true, false));
37 codecDescriptions.put("flac", new AudioCodecDescription("flac", "Free Lossless Audio Codec", "FLAC", "audio/ogg", true, true));
38 codecDescriptions.put("wav", new AudioCodecDescription("wav", "Waveform Audio", "WAV", "audio/vnc.wave", true, true));
39 codecDescriptions.put("mod", new AudioCodecDescription("mod", "Module", "MOD", "audio/mod", false, true));
40 codecDescriptions.put("ft2", new AudioCodecDescription("ft2", "FastTracker II Module", "XM", "audio/xm", false, true));
44 * Tries to automatically detect the audio codec from the filename. In most
45 * cases this will match the {@link AudioCodecDescription#code} of the codec
46 * but in some cases other extensions are possible, too. Also, on Amiga
47 * filenames usually start with the extension (mod files are often named
48 * “mod.somethingsomethingsomethingdarkside”).
51 * The name of the file
52 * @return The audio codec of the file, or {@code null} if no codec could be
55 public static AudioCodecDescription detect(String filename) {
56 String extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
57 if (codecDescriptions.containsKey(extension)) {
58 return codecDescriptions.get(extension);
60 if (extension.equals("ogg")) {
61 return codecDescriptions.get("vorbis");
63 if (extension.equals("mp4")) {
64 return codecDescriptions.get("aac");
66 if (extension.equals("xm")) {
67 return codecDescriptions.get("ft2");
73 * Bundles descriptions for various audio codecs. For simplicity reasons,
74 * file formats such as MOD, IT, or SID are also treated as audio codecs
75 * even though they are not.
77 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
79 public final static class AudioCodecDescription {
81 /** The code of this codec. */
82 public final String code;
84 /** The full name of the codec. */
85 public final String name;
87 /** The short name of the codec. */
88 public final String shortName;
91 public final String mimeType;
93 /** Whether this is a streaming codec. */
94 public final boolean streaming;
96 /** Whether this codec is lossless. */
97 public final boolean lossless;
100 * Creates a new audio codec description.
103 * The code of the codec
105 * The full name of the codec
107 * The short name of the codec
109 * The MIME type of the audio codec
111 * {@code true} if the codec is a streaming audio codec,
112 * {@code false} otherwise
114 * {@code true} if the codec is a lossless audio codec,
115 * {@code false} otherwise
117 AudioCodecDescription(String code, String name, String shortName, String mimeType, boolean streaming, boolean lossless) {
120 this.shortName = shortName;
121 this.mimeType = mimeType;
122 this.streaming = streaming;
123 this.lossless = lossless;
134 public String toString() {
135 return String.format("AudioCodec[%s,%s,%s,%s,%s,%s]", code, name, shortName, mimeType, streaming, lossless);