From: David ‘Bombe’ Roden Date: Sun, 17 Mar 2013 08:43:49 +0000 (+0100) Subject: Move format into metadata. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=865e9a8fce99285dfd6c1efb6a01991076e1a773;p=sonitus.git Move format into metadata. --- diff --git a/src/main/java/net/pterodactylus/sonitus/data/Format.java b/src/main/java/net/pterodactylus/sonitus/data/Format.java deleted file mode 100644 index fab7967..0000000 --- a/src/main/java/net/pterodactylus/sonitus/data/Format.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Sonitus - Format.java - Copyright © 2013 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sonitus.data; - -/** - * A format is a combination of a number of channels, a sampling frequency, and - * an encoding scheme. - * - * @author David ‘Bombe’ Roden - */ -public class Format { - - /** Constant for an unknown number of channels. */ - public static final int UNKNOWN_CHANNELS = -1; - - /** Constant for an unknown frequency. */ - public static final int UNKNOWN_FREQUENCY = -1; - - /** Constant for an unknown format. */ - public static final String UNKNOWN_ENCODING = "UNKNOWN"; - - /** The number of channels of this format. */ - private final int channels; - - /** The sampling frequency of this format. */ - private final int frequency; - - /** The encoding of this format. */ - private final String encoding; - - /** - * Creates a new format. - * - * @param channels - * The number of channels of this format - * @param frequency - * The sampling frequency of this format - * @param encoding - * The encoding of this format - */ - public Format(int channels, int frequency, String encoding) { - this.channels = channels; - this.frequency = frequency; - this.encoding = encoding; - } - - // - // ACCESSORS - // - - /** - * Returns the number of channels of this format. - * - * @return The number of channels of this format - */ - public int channels() { - return channels; - } - - /** - * Returns the sampling frequency of this format. - * - * @return The sampling frequency of this format - */ - public int frequency() { - return frequency; - } - - /** - * Returns the encoding of this format - * - * @return The encoding of this format - */ - public String encoding() { - return encoding; - } - - // - // MUTATORS - // - - /** - * Returns a format with the same parameters as this format and the given - * number of channels. - * - * @param channels - * The new number of channels - * @return A new format with the given number of channels - */ - public Format channels(int channels) { - return new Format(channels, frequency, encoding); - } - - /** - * Returns a new format with the same parameters as this format and the given - * frequency. - * - * @param frequency - * The new frequency - * @return A new format with the given frequency - */ - public Format frequency(int frequency) { - return new Format(channels, frequency, encoding); - } - - /** - * Returns a new format with the same parameters as this format and the given - * encoding. - * - * @param encoding - * The new encoding - * @return A new format with the given encoding - */ - public Format encoding(String encoding) { - return new Format(channels, frequency, encoding); - } - - // - // OBJECT METHODS - // - - @Override - public int hashCode() { - return (channels << 16) ^ frequency ^ encoding.toUpperCase().hashCode(); - } - - @Override - public boolean equals(Object object) { - if ((object == null) || (getClass() != object.getClass())) { - return false; - } - Format format = (Format) object; - return (format.channels == channels) && (format.frequency == frequency) && format.encoding.equalsIgnoreCase(encoding()); - } - - @Override - public String toString() { - return String.format("%d Channel%s, %d Hz, %s", channels, channels != 1 ? "s" : "", frequency, encoding); - } - -} diff --git a/src/main/java/net/pterodactylus/sonitus/data/Metadata.java b/src/main/java/net/pterodactylus/sonitus/data/Metadata.java index 22fbb27..e99d378 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/Metadata.java +++ b/src/main/java/net/pterodactylus/sonitus/data/Metadata.java @@ -20,10 +20,9 @@ package net.pterodactylus.sonitus.data; import com.google.common.base.Optional; /** - * Metadata contains information about a source, e.g. the name of the content, - * the artist performing it, dates, comments, URLs, etc. The {@link Format}, - * however, is not part of the metadata because a {@link Source} already exposes - * it. + * Metadata contains information about a source, e.g. the number of channels, + * the frequency, the encoding, the name of the content, the artist performing + * it, dates, comments, URLs, etc. *

* Metadata, once created, is immutable. * @@ -31,6 +30,24 @@ import com.google.common.base.Optional; */ public class Metadata { + /** Constant for an unknown number of channels. */ + public static final int UNKNOWN_CHANNELS = -1; + + /** Constant for an unknown frequency. */ + public static final int UNKNOWN_FREQUENCY = -1; + + /** Constant for an unknown metadata. */ + public static final String UNKNOWN_ENCODING = "UNKNOWN"; + + /** The number of channels of this metadata. */ + private final int channels; + + /** The sampling frequency of this metadata. */ + private final int frequency; + + /** The encoding of this metadata. */ + private final String encoding; + /** The artist performing the content. */ private final Optional artist; @@ -38,8 +55,8 @@ public class Metadata { private final Optional name; /** Creates empty metadata. */ - public Metadata() { - this(null, null); + public Metadata(int channels, int frequency, String encoding) { + this(channels, frequency, encoding, null, null); } /** @@ -50,11 +67,81 @@ public class Metadata { * @param name * The name of the content (may be {@code null}) */ - private Metadata(String artist, String name) { + private Metadata(int channels, int frequency, String encoding, String artist, String name) { + this.channels = channels; + this.frequency = frequency; + this.encoding = encoding; this.artist = Optional.fromNullable(artist); this.name = Optional.fromNullable(name); } + // + // ACCESSORS + // + + /** + * Returns the number of channels of this metadata. + * + * @return The number of channels of this metadata + */ + public int channels() { + return channels; + } + + /** + * Returns a metadata with the same parameters as this metadata and the given + * number of channels. + * + * @param channels + * The new number of channels + * @return A new metadata with the given number of channels + */ + public Metadata channels(int channels) { + return new Metadata(channels, frequency, encoding, artist.orNull(), name.orNull()); + } + + /** + * Returns the sampling frequency of this metadata. + * + * @return The sampling frequency of this metadata + */ + public int frequency() { + return frequency; + } + + /** + * Returns a new metadata with the same parameters as this metadata and the + * given frequency. + * + * @param frequency + * The new frequency + * @return A new metadata with the given frequency + */ + public Metadata frequency(int frequency) { + return new Metadata(channels, frequency, encoding, artist.orNull(), name.orNull()); + } + + /** + * Returns the encoding of this metadata + * + * @return The encoding of this metadata + */ + public String encoding() { + return encoding; + } + + /** + * Returns a new metadata with the same parameters as this metadata and the + * given encoding. + * + * @param encoding + * The new encoding + * @return A new metadata with the given encoding + */ + public Metadata encoding(String encoding) { + return new Metadata(channels, frequency, encoding, artist.orNull(), name.orNull()); + } + /** * Returns the artist, if any. * @@ -73,7 +160,7 @@ public class Metadata { * @return New metadata with a changed artist */ public Metadata artist(String artist) { - return new Metadata(artist, this.artist.orNull()); + return new Metadata(channels, frequency, encoding, artist, this.artist.orNull()); } /** @@ -94,7 +181,54 @@ public class Metadata { * @return New metadata with a changed name */ public Metadata name(String name) { - return new Metadata(name, this.name.orNull()); + return new Metadata(channels, frequency, encoding, name, this.name.orNull()); + } + + // + // OBJECT METHODS + // + + @Override + public int hashCode() { + int hashCode = (channels << 16) ^ frequency ^ encoding.toUpperCase().hashCode(); + if (artist.isPresent()) { + hashCode ^= artist.get().hashCode(); + } + if (name.isPresent()) { + hashCode ^= name.get().hashCode(); + } + return hashCode; + } + + @Override + public boolean equals(Object object) { + if ((object == null) || (getClass() != object.getClass())) { + return false; + } + Metadata metadata = (Metadata) object; + if ((metadata.channels != channels) || (metadata.frequency != frequency) || !metadata.encoding.equalsIgnoreCase(encoding())) { + return false; + } + if (artist.equals(metadata.artist)) { + return false; + } + if (name.equals(metadata.name)) { + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder string = new StringBuilder(); + string.append(String.format("%d Channel%s, %d Hz, %s:", channels, channels != 1 ? "s" : "", frequency, encoding)); + if (artist.isPresent()) { + string.append(" Artist(").append(artist.get()).append(")"); + } + if (name.isPresent()) { + string.append(" Name(").append(name.get()).append(")"); + } + return string.toString(); } } diff --git a/src/main/java/net/pterodactylus/sonitus/data/Sink.java b/src/main/java/net/pterodactylus/sonitus/data/Sink.java index 7f6ce6b..0fbfb9e 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/Sink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/Sink.java @@ -32,7 +32,8 @@ public interface Sink { * @param source * The source to connect to * @throws ConnectException - * if the source can not be connected, e.g. due to a {@link Format} mismatch + * if the source can not be connected, e.g. due to a {@link Metadata} + * mismatch */ void connect(Source source) throws ConnectException; diff --git a/src/main/java/net/pterodactylus/sonitus/data/Source.java b/src/main/java/net/pterodactylus/sonitus/data/Source.java index 270a8bf..7bb10bd 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/Source.java +++ b/src/main/java/net/pterodactylus/sonitus/data/Source.java @@ -29,13 +29,6 @@ import java.io.IOException; public interface Source { /** - * Returns the format of this source. - * - * @return The format of this source - */ - Format format(); - - /** * Returns the metadata of this source. * * @return The metadata of this source diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java index 3452c76..9e298b4 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java @@ -29,7 +29,6 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.ConnectException; import net.pterodactylus.sonitus.data.Connection; import net.pterodactylus.sonitus.data.Filter; -import net.pterodactylus.sonitus.data.Format; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; import net.pterodactylus.sonitus.io.InputStreamDrainer; @@ -61,11 +60,6 @@ public abstract class ExternalFilter implements Filter { // @Override - public Format format() { - return source.format(); - } - - @Override public Metadata metadata() { return source.metadata(); } @@ -86,7 +80,7 @@ public abstract class ExternalFilter implements Filter { this.source = source; try { - final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.builder().add(binary(source.format())).addAll(parameters(source.format())).build(), String.class)); + final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.builder().add(binary(source.metadata())).addAll(parameters(source.metadata())).build(), String.class)); final InputStream processOutput = process.getInputStream(); final OutputStream processInput = process.getOutputStream(); final InputStream processError = process.getErrorStream(); @@ -142,19 +136,19 @@ public abstract class ExternalFilter implements Filter { /** * Returns the location of the binary to execute. * - * @param format - * The format being processed + * @param metadata + * The metadata being processed * @return The location of the binary to execute */ - protected abstract String binary(Format format); + protected abstract String binary(Metadata metadata); /** * Returns the parameters for the binary. * - * @param format - * The format being processed + * @param metadata + * The metadata being processed * @return The parameters for the binary */ - protected abstract Iterable parameters(Format format); + protected abstract Iterable parameters(Metadata metadata); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java index 88afa30..cc52e41 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Decoder.java @@ -20,7 +20,7 @@ package net.pterodactylus.sonitus.data.filter; import static com.google.common.base.Preconditions.*; import net.pterodactylus.sonitus.data.ConnectException; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; /** @@ -32,14 +32,14 @@ import net.pterodactylus.sonitus.data.Source; public abstract class ExternalMp3Decoder extends ExternalFilter { @Override - public Format format() { - return super.format().encoding("PCM"); + public Metadata metadata() { + return super.metadata().encoding("PCM"); } @Override public void connect(Source source) throws ConnectException { checkNotNull(source, "source must not be null"); - checkState(source.format().encoding().equalsIgnoreCase("MP3"), "source must be MP3-encoded"); + checkState(source.metadata().encoding().equalsIgnoreCase("MP3"), "source must be MP3-encoded"); super.connect(source); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java index d26febe..1975e36 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalMp3Encoder.java @@ -18,28 +18,28 @@ package net.pterodactylus.sonitus.data.filter; import net.pterodactylus.sonitus.data.ConnectException; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; import com.google.common.base.Preconditions; /** * Basic {@link ExternalFilter} implementation that verifies that the connected - * source is PCM-encoded and that returns an MP3-encoded format. + * source is PCM-encoded and that returns an MP3-encoded metadata. * * @author David ‘Bombe’ Roden */ public abstract class ExternalMp3Encoder extends ExternalFilter { @Override - public Format format() { - return super.format().encoding("MP3"); + public Metadata metadata() { + return super.metadata().encoding("MP3"); } @Override public void connect(Source source) throws ConnectException { Preconditions.checkNotNull(source, "source must not be null"); - Preconditions.checkState(source.format().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded"); + Preconditions.checkState(source.metadata().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded"); super.connect(source); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java index fb7bf94..b2d8ea0 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Decoder.java @@ -17,7 +17,7 @@ package net.pterodactylus.sonitus.data.filter; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import com.google.common.collect.ImmutableList; @@ -62,12 +62,12 @@ public class LameMp3Decoder extends ExternalMp3Decoder { // @Override - protected String binary(Format format) { + protected String binary(Metadata metadata) { return binary; } @Override - protected Iterable parameters(Format format) { + protected Iterable parameters(Metadata metadata) { ImmutableList.Builder parameters = ImmutableList.builder(); parameters.add("--mp3input").add("--decode").add("-t"); if (swapBytes) { diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java index 2436b52..98fdd91 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/LameMp3Encoder.java @@ -19,7 +19,7 @@ package net.pterodactylus.sonitus.data.filter; import java.util.Arrays; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; @@ -133,15 +133,15 @@ public class LameMp3Encoder extends ExternalMp3Encoder { // @Override - protected String binary(Format format) { + protected String binary(Metadata metadata) { return binary; } @Override - protected Iterable parameters(Format format) { + protected Iterable parameters(Metadata metadata) { ImmutableList.Builder parameters = ImmutableList.builder(); parameters.add("-r"); - parameters.add("-s").add(String.valueOf(format.frequency() / 1000.0)); + parameters.add("-s").add(String.valueOf(metadata.frequency() / 1000.0)); if (swapBytes) { parameters.add("-x"); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/MultiSourceFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/MultiSourceFilter.java index fb52c9d..1dc3a8c 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/MultiSourceFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/MultiSourceFilter.java @@ -31,7 +31,6 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.ConnectException; import net.pterodactylus.sonitus.data.Filter; -import net.pterodactylus.sonitus.data.Format; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.ReusableSink; import net.pterodactylus.sonitus.data.Source; @@ -66,13 +65,6 @@ public class MultiSourceFilter implements Filter, ReusableSink { } @Override - public Format format() { - synchronized (syncObject) { - return connection.source.format(); - } - } - - @Override public Metadata metadata() { synchronized (syncObject) { return connection.source.metadata(); @@ -94,7 +86,9 @@ public class MultiSourceFilter implements Filter, ReusableSink { public void connect(Source source) throws ConnectException { checkNotNull(source, "source must not be null"); if ((connection != null) && (connection.source != null)) { - checkArgument(connection.source.format().equals(source.format()), "source’s format must equal this sink’s format"); + checkArgument(connection.source.metadata().channels() == source.metadata().channels(), "source’s channel count must equal existing source’s channel count"); + checkArgument(connection.source.metadata().frequency() == source.metadata().frequency(), "source’s frequency must equal existing source’s frequency"); + checkArgument(connection.source.metadata().encoding().equalsIgnoreCase(source.metadata().encoding()), "source’s encoding must equal existing source’s encoding"); } if (connection == null) { diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java index 66ca8a7..e108a3a 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/RateLimitingFilter.java @@ -29,7 +29,6 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.ConnectException; import net.pterodactylus.sonitus.data.Connection; import net.pterodactylus.sonitus.data.Filter; -import net.pterodactylus.sonitus.data.Format; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; @@ -49,7 +48,7 @@ public class RateLimitingFilter implements Filter { /** The limiting rate in bytes/second. */ private final int rate; - /** The source’s format. */ + /** The source. */ private Source source; /** The input stream to read from. */ @@ -70,11 +69,6 @@ public class RateLimitingFilter implements Filter { // @Override - public Format format() { - return source.format(); - } - - @Override public Metadata metadata() { return source.metadata(); } diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java index 520be04..b3aa7e7 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/AudioSink.java @@ -27,7 +27,7 @@ import javax.sound.sampled.SourceDataLine; import net.pterodactylus.sonitus.data.ConnectException; import net.pterodactylus.sonitus.data.Connection; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Sink; import net.pterodactylus.sonitus.data.Source; @@ -45,10 +45,10 @@ public class AudioSink implements Sink { @Override public void connect(Source source) throws ConnectException { checkNotNull(source, "source must not be null"); - checkState(source.format().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded"); + checkState(source.metadata().encoding().equalsIgnoreCase("PCM"), "source must be PCM-encoded"); - final Format sourceFormat = source.format(); - AudioFormat audioFormat = new AudioFormat(sourceFormat.frequency(), 16, sourceFormat.channels(), true, false); + final Metadata sourceMetadata = source.metadata(); + AudioFormat audioFormat = new AudioFormat(sourceMetadata.frequency(), 16, sourceMetadata.channels(), true, false); try { final SourceDataLine sourceDataLine = AudioSystem.getSourceDataLine(audioFormat); sourceDataLine.open(audioFormat); @@ -57,7 +57,7 @@ public class AudioSink implements Sink { @Override protected int bufferSize() { - return sourceFormat.channels() * sourceFormat.frequency() * 2; + return sourceMetadata.channels() * sourceMetadata.frequency() * 2; } @Override diff --git a/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java b/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java index 3d41413..cf098a9 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java +++ b/src/main/java/net/pterodactylus/sonitus/data/sink/Icecast2Sink.java @@ -31,7 +31,6 @@ import java.util.logging.Logger; import net.pterodactylus.sonitus.data.ConnectException; import net.pterodactylus.sonitus.data.Connection; -import net.pterodactylus.sonitus.data.Format; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Sink; import net.pterodactylus.sonitus.data.Source; @@ -132,7 +131,7 @@ public class Icecast2Sink implements Sink { sendLine(socketOutputStream, String.format("SOURCE /%s ICE/1.0", mountPoint)); sendLine(socketOutputStream, String.format("Authorization: Basic %s", generatePassword(password))); - sendLine(socketOutputStream, String.format("Content-Type: %s", getContentType(source.format()))); + sendLine(socketOutputStream, String.format("Content-Type: %s", getContentType(source.metadata()))); sendLine(socketOutputStream, String.format("ICE-Name: %s", serverName)); sendLine(socketOutputStream, String.format("ICE-Description: %s", serverDescription)); sendLine(socketOutputStream, String.format("ICE-Genre: %s", genre)); @@ -242,15 +241,15 @@ public class Icecast2Sink implements Sink { } /** - * Returns a MIME type for the given format. Currently only Vorbis, MP3, and + * Returns a MIME type for the given metadata. Currently only Vorbis, MP3, and * PCM formats are recognized. * - * @param format - * The format to get a MIME type for - * @return The MIME type of the format + * @param metadata + * The metadata to get a MIME type for + * @return The MIME type of the metadata */ - private static String getContentType(Format format) { - String encoding = format.encoding(); + private static String getContentType(Metadata metadata) { + String encoding = metadata.encoding(); if ("Vorbis".equalsIgnoreCase(encoding)) { return "audio/ogg"; } diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java index c967e12..6c28522 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java +++ b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java @@ -18,9 +18,9 @@ package net.pterodactylus.sonitus.data.source; import static com.google.common.base.Preconditions.*; -import static net.pterodactylus.sonitus.data.Format.UNKNOWN_CHANNELS; -import static net.pterodactylus.sonitus.data.Format.UNKNOWN_ENCODING; -import static net.pterodactylus.sonitus.data.Format.UNKNOWN_FREQUENCY; +import static net.pterodactylus.sonitus.data.Metadata.UNKNOWN_CHANNELS; +import static net.pterodactylus.sonitus.data.Metadata.UNKNOWN_ENCODING; +import static net.pterodactylus.sonitus.data.Metadata.UNKNOWN_FREQUENCY; import java.io.EOFException; import java.io.FileInputStream; @@ -28,7 +28,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; -import net.pterodactylus.sonitus.data.Format; import net.pterodactylus.sonitus.data.Metadata; import net.pterodactylus.sonitus.data.Source; import net.pterodactylus.sonitus.io.IdentifyingInputStream; @@ -46,8 +45,8 @@ public class FileSource implements Source { /** The path of the file. */ private final String path; - /** The identified format of the file. */ - private final Format format; + /** The identified metadata of the file. */ + private final Metadata metadata; /** The input stream. */ private InputStream fileInputStream; @@ -67,10 +66,10 @@ public class FileSource implements Source { /* identify file type. */ Optional identifyingInputStream = IdentifyingInputStream.create(new FileInputStream(path)); if (identifyingInputStream.isPresent()) { - format = identifyingInputStream.get().format(); + metadata = identifyingInputStream.get().metadata(); } else { /* fallback. */ - format = new Format(UNKNOWN_CHANNELS, UNKNOWN_FREQUENCY, UNKNOWN_ENCODING); + metadata = new Metadata(UNKNOWN_CHANNELS, UNKNOWN_FREQUENCY, UNKNOWN_ENCODING); } } @@ -79,13 +78,8 @@ public class FileSource implements Source { // @Override - public Format format() { - return format; - } - - @Override public Metadata metadata() { - return new Metadata().name(path); + return metadata.name(path); } @Override @@ -104,7 +98,7 @@ public class FileSource implements Source { @Override public String toString() { - return String.format("%s (%s)", path, format); + return String.format("%s (%s)", path, metadata); } } diff --git a/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java index c63e565..faf4f01 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java +++ b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java @@ -21,33 +21,33 @@ import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import com.google.common.base.Optional; import com.google.common.io.ByteStreams; /** - * Wrapper around an {@link InputStream} that identifies the {@link Format} of + * Wrapper around an {@link InputStream} that identifies the {@link Metadata} of * the wrapped stream. * * @author David ‘Bombe’ Roden */ public class IdentifyingInputStream extends FilterInputStream { - /** The identified format. */ - private final Format format; + /** The identified metadata. */ + private final Metadata metadata; /** * Creates a new identifying input stream. * * @param inputStream * The input stream to wrap - * @param format - * The format of the stream + * @param metadata + * The metadata of the stream */ - private IdentifyingInputStream(InputStream inputStream, Format format) { + private IdentifyingInputStream(InputStream inputStream, Metadata metadata) { super(inputStream); - this.format = format; + this.metadata = metadata; } // @@ -55,12 +55,12 @@ public class IdentifyingInputStream extends FilterInputStream { // /** - * Returns the identified format. + * Returns the identified metadata. * - * @return The identified format + * @return The identified metadata */ - public Format format() { - return format; + public Metadata metadata() { + return metadata; } // @@ -73,8 +73,8 @@ public class IdentifyingInputStream extends FilterInputStream { * @param inputStream * The input stream to identify * @return An identifying input stream that delivers the original stream and - * the format it detected, or {@link com.google.common.base.Optional#absent()} - * if no format could be identified + * the metadata it detected, or {@link Optional#absent()} if no + * metadata could be identified * @throws IOException * if an I/O error occurs */ @@ -84,17 +84,17 @@ public class IdentifyingInputStream extends FilterInputStream { RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream); /* try Ogg Vorbis first. */ - Optional format = OggVorbisIdentifier.identify(rememberingInputStream); - if (format.isPresent()) { - return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), format.get())); + Optional metadata = OggVorbisIdentifier.identify(rememberingInputStream); + if (metadata.isPresent()) { + return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get())); } /* try MP3 now. */ rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered()); InputStream limitedInputStream = ByteStreams.limit(rememberingInputStream, 1048576); - format = Mp3Identifier.identify(limitedInputStream); - if (format.isPresent()) { - return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), format.get())); + metadata = Mp3Identifier.identify(limitedInputStream); + if (metadata.isPresent()) { + return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get())); } return Optional.absent(); diff --git a/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java b/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java index b4001bb..e9a550b 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java +++ b/src/main/java/net/pterodactylus/sonitus/io/Mp3Identifier.java @@ -20,7 +20,7 @@ package net.pterodactylus.sonitus.io; import java.io.IOException; import java.io.InputStream; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import com.google.common.base.Optional; import javazoom.jl.decoder.Bitstream; @@ -39,19 +39,19 @@ public class Mp3Identifier { * * @param inputStream * The input stream - * @return The identified format, or {@link com.google.common.base.Optional#absent()} - * if the format can not be identified + * @return The identified metadata, or {@link Optional#absent()} if the + * metadata can not be identified * @throws IOException * if an I/O error occurs */ - public static Optional identify(InputStream inputStream) throws IOException { + public static Optional identify(InputStream inputStream) throws IOException { Bitstream bitstream = new Bitstream(inputStream); try { Header frame = bitstream.readFrame(); if (frame == null) { return Optional.absent(); } - return Optional.of(new Format(frame.mode() == Header.SINGLE_CHANNEL ? 1 : 2, frame.frequency(), "MP3")); + return Optional.of(new Metadata(frame.mode() == Header.SINGLE_CHANNEL ? 1 : 2, frame.frequency(), "MP3")); } catch (BitstreamException be1) { return Optional.absent(); } diff --git a/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java index 49daa27..769acf2 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java +++ b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java @@ -20,7 +20,7 @@ package net.pterodactylus.sonitus.io; import java.io.IOException; import java.io.InputStream; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import com.google.common.base.Optional; import com.jcraft.jogg.Packet; @@ -49,16 +49,16 @@ public class OggVorbisIdentifier { /** * Tries to parse the given stream as Ogg Vorbis file and returns a {@link - * Format} describing the stream. + * Metadata} describing the stream. * * @param inputStream * The input stream to identify as Ogg Vorbis - * @return The identified format, or {@link com.google.common.base.Optional#absent()} - * if the stream could not be identified + * @return The identified metadata, or {@link Optional#absent()} if the stream + * could not be identified * @throws IOException * if an I/O error occurs */ - public static Optional identify(InputStream inputStream) throws IOException { + public static Optional identify(InputStream inputStream) throws IOException { /* stuff needed to decode Ogg. */ Packet packet = new Packet(); @@ -115,7 +115,7 @@ public class OggVorbisIdentifier { buffer = syncState.data; } - return Optional.of(new Format(info.channels, info.rate, "Vorbis")); + return Optional.of(new Metadata(info.channels, info.rate, "Vorbis")); } }