Add method that tries to automatically detect the audio codec from the filename.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 10:02:04 +0000 (12:02 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 30 Jul 2012 10:02:04 +0000 (12:02 +0200)
src/main/java/net/pterodactylus/demoscenemusic/utils/AudioCodecs.java

index f09e79f..4ec19e8 100644 (file)
@@ -41,6 +41,35 @@ public class AudioCodecs {
        }
 
        /**
        }
 
        /**
+        * Tries to automatically detect the audio codec from the filename. In most
+        * cases this will match the {@link AudioCodecDescription#code} of the codec
+        * but in some cases other extensions are possible, too. Also, on Amiga
+        * filenames usually start with the extension (mod files are often named
+        * “mod.somethingsomethingsomethingdarkside”).
+        *
+        * @param filename
+        *            The name of the file
+        * @return The audio codec of the file, or {@code null} if no codec could be
+        *         detected
+        */
+       public static AudioCodecDescription detect(String filename) {
+               String extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase();
+               if (codecDescriptions.containsKey(extension)) {
+                       return codecDescriptions.get(extension);
+               }
+               if (extension.equals("ogg")) {
+                       return codecDescriptions.get("vorbis");
+               }
+               if (extension.equals("mp4")) {
+                       return codecDescriptions.get("aac");
+               }
+               if (extension.equals("xm")) {
+                       return codecDescriptions.get("ft2");
+               }
+               return null;
+       }
+
+       /**
         * Bundles descriptions for various audio codecs. For simplicity reasons,
         * file formats such as MOD, IT, or SID are also treated as audio codecs
         * even though they are not.
         * Bundles descriptions for various audio codecs. For simplicity reasons,
         * file formats such as MOD, IT, or SID are also treated as audio codecs
         * even though they are not.