4ec19e854f0ce5f53d6a57c394505a2c9214ed61
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / utils / AudioCodecs.java
1 /*
2  * DemosceneMusic - AudioCodecFilter.java - Copyright © 2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.utils;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
23 /**
24  * Helper for audio codec descriptions.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class AudioCodecs {
29
30         /** Descriptions for audio codecs. */
31         public static final Map<String, AudioCodecDescription> codecDescriptions = new HashMap<String, AudioCodecDescription>();
32
33         static {
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));
41         }
42
43         /**
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”).
49          *
50          * @param filename
51          *            The name of the file
52          * @return The audio codec of the file, or {@code null} if no codec could be
53          *         detected
54          */
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);
59                 }
60                 if (extension.equals("ogg")) {
61                         return codecDescriptions.get("vorbis");
62                 }
63                 if (extension.equals("mp4")) {
64                         return codecDescriptions.get("aac");
65                 }
66                 if (extension.equals("xm")) {
67                         return codecDescriptions.get("ft2");
68                 }
69                 return null;
70         }
71
72         /**
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.
76          *
77          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
78          */
79         public final static class AudioCodecDescription {
80
81                 /** The code of this codec. */
82                 public final String code;
83
84                 /** The full name of the codec. */
85                 public final String name;
86
87                 /** The short name of the codec. */
88                 public final String shortName;
89
90                 /** The MIME type. */
91                 public final String mimeType;
92
93                 /** Whether this is a streaming codec. */
94                 public final boolean streaming;
95
96                 /** Whether this codec is lossless. */
97                 public final boolean lossless;
98
99                 /**
100                  * Creates a new audio codec description.
101                  *
102                  * @param code
103                  *            The code of the codec
104                  * @param name
105                  *            The full name of the codec
106                  * @param shortName
107                  *            The short name of the codec
108                  * @param mimeType
109                  *            The MIME type of the audio codec
110                  * @param streaming
111                  *            {@code true} if the codec is a streaming audio codec,
112                  *            {@code false} otherwise
113                  * @param lossless
114                  *            {@code true} if the codec is a lossless audio codec,
115                  *            {@code false} otherwise
116                  */
117                 AudioCodecDescription(String code, String name, String shortName, String mimeType, boolean streaming, boolean lossless) {
118                         this.code = code;
119                         this.name = name;
120                         this.shortName = shortName;
121                         this.mimeType = mimeType;
122                         this.streaming = streaming;
123                         this.lossless = lossless;
124                 }
125
126         }
127
128 }