2 * Sonitus - ContentMetadata.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.data;
21 * The part of the {@link Metadata} that contains information about the format
22 * of a track. It specifies the number of channels, the samplerate, and the
23 * encoding of a track.
25 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27 public class FormatMetadata {
29 /** Constant for an unknown number of channels. */
30 public static final int UNKNOWN_CHANNELS = -1;
32 /** Constant for an unknown frequency. */
33 public static final int UNKNOWN_FREQUENCY = -1;
35 /** Constant for an unknown metadata. */
36 public static final String UNKNOWN_ENCODING = "UNKNOWN";
38 /** The number of channels of this metadata. */
39 private final int channels;
41 /** The sampling frequency of this metadata. */
42 private final int frequency;
44 /** The encoding of this metadata. */
45 private final String encoding;
47 /** Creates new format metadata whose parameters are all unknown. */
48 public FormatMetadata() {
49 this(UNKNOWN_CHANNELS, UNKNOWN_FREQUENCY, UNKNOWN_ENCODING);
53 * Creates new format metadata with the given parameters.
56 * The number of channels
58 * The sampling frequency (in Hertz)
60 * The encoding (e.g. “PCM” or “MP3”)
62 public FormatMetadata(int channels, int frequency, String encoding) {
63 this.channels = channels;
64 this.frequency = frequency;
65 this.encoding = encoding;
73 * Returns the number of channels of this metadata.
75 * @return The number of channels of this metadata
77 public int channels() {
82 * Returns the sampling frequency of this metadata.
84 * @return The sampling frequency of this metadata
86 public int frequency() {
91 * Returns the encoding of this metadata
93 * @return The encoding of this metadata
95 public String encoding() {
104 * Creates new format metadata that is a copy of this format metadata but with
105 * the number of channels changed to the given number of channels.
108 * The new number of channels
109 * @return The new format metadata
111 public FormatMetadata channels(int channels) {
112 return new FormatMetadata(channels, frequency(), encoding());
116 * Creates new format metadata that is a copy of this format metadata but with
117 * the sampling frequency changed to the given sampling frequency.
120 * The new sampling frequency
121 * @return The new format metadata
123 public FormatMetadata frequency(int frequency) {
124 return new FormatMetadata(channels(), frequency, encoding());
128 * Creates new format metadata that is a copy of this format metadata but with
129 * the encoding changed to the given encoding.
133 * @return The new format metadata
135 public FormatMetadata encoding(String encoding) {
136 return new FormatMetadata(channels(), frequency(), encoding);
144 public int hashCode() {
145 return (channels() << 16) ^ frequency() ^ encoding().toUpperCase().hashCode();
149 public boolean equals(Object object) {
150 if (!(object instanceof FormatMetadata)) {
153 FormatMetadata formatMetadata = (FormatMetadata) object;
154 return (channels() == formatMetadata.channels()) && (frequency() == formatMetadata.frequency()) && (encoding().equalsIgnoreCase(formatMetadata.encoding()));
158 public String toString() {
159 return String.format("%s kHz, %d Channel%s, %s", frequency() / 1000.0, channels(), channels() != 1 ? "s" : "", encoding());