X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fdemoscenemusic%2Futils%2FAudioCodecs.java;h=4329fccb438d75992224423b7e4428c341a288c7;hb=70e99ae2ccefa4cebec5b282ff0d528ab1654d74;hp=a3c90c36206d0a4e97e0f54e4329050c2b921265;hpb=19389257f2b497d8edc6eb6d3ea1ca5f3a4bcbbc;p=demoscenemusic.git diff --git a/src/main/java/net/pterodactylus/demoscenemusic/utils/AudioCodecs.java b/src/main/java/net/pterodactylus/demoscenemusic/utils/AudioCodecs.java index a3c90c3..4329fcc 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/utils/AudioCodecs.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/utils/AudioCodecs.java @@ -31,13 +31,42 @@ public class AudioCodecs { public static final Map codecDescriptions = new HashMap(); static { - codecDescriptions.put("mp3", new AudioCodecDescription("MPEG 1 Layer 3", "MP3", "audio/mpeg", true, false)); - codecDescriptions.put("vorbis", new AudioCodecDescription("Ogg Vorbis", "Vorbis", "audio/vorbis", true, false)); - codecDescriptions.put("aac", new AudioCodecDescription("Advanced Audio Coding", "AAC", "audio/x-aac", true, false)); - codecDescriptions.put("flac", new AudioCodecDescription("Free Lossless Audio Codec", "FLAC", "audio/ogg", true, true)); - codecDescriptions.put("WAV", new AudioCodecDescription("Waveform Audio", "WAV", "audio/vnc.wave", true, true)); - codecDescriptions.put("mod", new AudioCodecDescription("Module", "MOD", "audio/mod", false, true)); - codecDescriptions.put("ft2", new AudioCodecDescription("FastTracker II Module", "XM", "audio/xm", false, true)); + codecDescriptions.put("mp3", new AudioCodecDescription("mp3", "MPEG 1 Layer 3", "MP3", "audio/mpeg", true, false)); + codecDescriptions.put("vorbis", new AudioCodecDescription("vorbis", "Ogg Vorbis", "Vorbis", "audio/vorbis", true, false)); + codecDescriptions.put("aac", new AudioCodecDescription("aac", "Advanced Audio Coding", "AAC", "audio/x-aac", true, false)); + codecDescriptions.put("flac", new AudioCodecDescription("flac", "Free Lossless Audio Codec", "FLAC", "audio/ogg", true, true)); + codecDescriptions.put("wav", new AudioCodecDescription("wav", "Waveform Audio", "WAV", "audio/vnc.wave", true, true)); + codecDescriptions.put("mod", new AudioCodecDescription("mod", "Module", "MOD", "audio/mod", false, true)); + codecDescriptions.put("ft2", new AudioCodecDescription("ft2", "FastTracker II Module", "XM", "audio/xm", false, true)); + } + + /** + * 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; } /** @@ -49,6 +78,9 @@ public class AudioCodecs { */ public final static class AudioCodecDescription { + /** The code of this codec. */ + public final String code; + /** The full name of the codec. */ public final String name; @@ -67,6 +99,8 @@ public class AudioCodecs { /** * Creates a new audio codec description. * + * @param code + * The code of the codec * @param name * The full name of the codec * @param shortName @@ -80,7 +114,8 @@ public class AudioCodecs { * {@code true} if the codec is a lossless audio codec, * {@code false} otherwise */ - AudioCodecDescription(String name, String shortName, String mimeType, boolean streaming, boolean lossless) { + AudioCodecDescription(String code, String name, String shortName, String mimeType, boolean streaming, boolean lossless) { + this.code = code; this.name = name; this.shortName = shortName; this.mimeType = mimeType; @@ -88,6 +123,18 @@ public class AudioCodecs { this.lossless = lossless; } + // + // OBJECT METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return String.format("AudioCodec[%s,%s,%s,%s,%s,%s]", code, name, shortName, mimeType, streaming, lossless); + } + } }