04d23b8036576ec1149d485660aca706581322fd
[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                 codecDescriptions.put("it",  new AudioCodecDescription("it", "Impulse Tracker Module", "IT", "audio/it", false, true));
42         }
43
44         /**
45          * Tries to automatically detect the audio codec from the filename. In most
46          * cases this will match the {@link AudioCodecDescription#code} of the codec
47          * but in some cases other extensions are possible, too. Also, on Amiga
48          * filenames usually start with the extension (mod files are often named
49          * “mod.somethingsomethingsomethingdarkside”).
50          *
51          * @param filename
52          *            The name of the file
53          * @return The audio codec of the file, or {@code null} if no codec could be
54          *         detected
55          */
56         public static AudioCodecDescription detect(String filename) {
57                 String extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
58                 if (codecDescriptions.containsKey(extension)) {
59                         return codecDescriptions.get(extension);
60                 }
61                 if (extension.equals("ogg")) {
62                         return codecDescriptions.get("vorbis");
63                 }
64                 if (extension.equals("mp4")) {
65                         return codecDescriptions.get("aac");
66                 }
67                 if (extension.equals("xm")) {
68                         return codecDescriptions.get("ft2");
69                 }
70                 return null;
71         }
72
73         /**
74          * Bundles descriptions for various audio codecs. For simplicity reasons,
75          * file formats such as MOD, IT, or SID are also treated as audio codecs
76          * even though they are not.
77          *
78          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
79          */
80         public final static class AudioCodecDescription {
81
82                 /** The code of this codec. */
83                 public final String code;
84
85                 /** The full name of the codec. */
86                 public final String name;
87
88                 /** The short name of the codec. */
89                 public final String shortName;
90
91                 /** The MIME type. */
92                 public final String mimeType;
93
94                 /** Whether this is a streaming codec. */
95                 public final boolean streaming;
96
97                 /** Whether this codec is lossless. */
98                 public final boolean lossless;
99
100                 /**
101                  * Creates a new audio codec description.
102                  *
103                  * @param code
104                  *            The code of the codec
105                  * @param name
106                  *            The full name of the codec
107                  * @param shortName
108                  *            The short name of the codec
109                  * @param mimeType
110                  *            The MIME type of the audio codec
111                  * @param streaming
112                  *            {@code true} if the codec is a streaming audio codec,
113                  *            {@code false} otherwise
114                  * @param lossless
115                  *            {@code true} if the codec is a lossless audio codec,
116                  *            {@code false} otherwise
117                  */
118                 AudioCodecDescription(String code, String name, String shortName, String mimeType, boolean streaming, boolean lossless) {
119                         this.code = code;
120                         this.name = name;
121                         this.shortName = shortName;
122                         this.mimeType = mimeType;
123                         this.streaming = streaming;
124                         this.lossless = lossless;
125                 }
126
127                 //
128                 // OBJECT METHODS
129                 //
130
131                 /**
132                  * {@inheritDoc}
133                  */
134                 @Override
135                 public String toString() {
136                         return String.format("AudioCodec[%s,%s,%s,%s,%s,%s]", code, name, shortName, mimeType, streaming, lossless);
137                 }
138
139         }
140
141 }