Skip to content

Commit

Permalink
Renamed Codec to Decoder + simplified constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Apr 29, 2021
1 parent f81b212 commit 2f1cda4
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import xyz.gianlu.librespot.common.Utils;
import xyz.gianlu.librespot.core.Session;
import xyz.gianlu.librespot.core.TimeProvider;
import xyz.gianlu.librespot.player.codecs.AudioQuality;
import xyz.gianlu.librespot.player.decoders.AudioQuality;

import java.io.File;
import java.io.FileReader;
Expand Down
6 changes: 3 additions & 3 deletions player/src/main/java/xyz/gianlu/librespot/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import xyz.gianlu.librespot.metadata.ImageId;
import xyz.gianlu.librespot.metadata.PlayableId;
import xyz.gianlu.librespot.player.StateWrapper.NextPlayable;
import xyz.gianlu.librespot.player.codecs.Codec;
import xyz.gianlu.librespot.player.contexts.AbsSpotifyContext;
import xyz.gianlu.librespot.player.decoders.Decoder;
import xyz.gianlu.librespot.player.metrics.NewPlaybackIdEvent;
import xyz.gianlu.librespot.player.metrics.NewSessionIdEvent;
import xyz.gianlu.librespot.player.metrics.PlaybackMetrics;
Expand Down Expand Up @@ -577,7 +577,7 @@ private void handlePause() {
try {
if (playerSession != null)
state.setPosition(playerSession.currentTime());
} catch (Codec.CannotGetTimeException ex) {
} catch (Decoder.CannotGetTimeException ex) {
state.setPosition(state.getPosition());
}

Expand Down Expand Up @@ -820,7 +820,7 @@ public byte[] currentCoverImage() throws IOException {
public int time() {
try {
return playerSession == null ? -1 : playerSession.currentTime();
} catch (Codec.CannotGetTimeException ex) {
} catch (Decoder.CannotGetTimeException ex) {
return -1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import xyz.gianlu.librespot.player.codecs.AudioQuality;
import xyz.gianlu.librespot.player.decoders.AudioQuality;

import java.io.File;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package xyz.gianlu.librespot.player.codecs;
package xyz.gianlu.librespot.player.decoders;

import com.spotify.metadata.Metadata.AudioFile;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
* limitations under the License.
*/

package xyz.gianlu.librespot.player.codecs;
package xyz.gianlu.librespot.player.decoders;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.gianlu.librespot.audio.AbsChunkedInputStream;
import xyz.gianlu.librespot.audio.GeneralAudioStream;
import xyz.gianlu.librespot.audio.NormalizationData;
import xyz.gianlu.librespot.player.PlayerConfiguration;
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;

import java.io.Closeable;
Expand All @@ -33,9 +30,9 @@
/**
* @author Gianlu
*/
public abstract class Codec implements Closeable {
public abstract class Decoder implements Closeable {
public static final int BUFFER_SIZE = 2048;
private static final Logger LOGGER = LoggerFactory.getLogger(Codec.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Decoder.class);
protected final AbsChunkedInputStream audioIn;
protected final float normalizationFactor;
protected final int duration;
Expand All @@ -44,14 +41,11 @@ public abstract class Codec implements Closeable {
protected int seekZero = 0;
private OutputAudioFormat format;

public Codec(@NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull PlayerConfiguration conf, int duration) {
public Decoder(@NotNull GeneralAudioStream audioFile, float normalizationFactor, int duration) {
this.audioIn = audioFile.stream();
this.audioFile = audioFile;
this.duration = duration;
if (conf.enableNormalisation)
this.normalizationFactor = normalizationData != null ? normalizationData.getFactor(conf.normalisationPregain) : 1;
else
this.normalizationFactor = 1;
this.normalizationFactor = normalizationFactor;
}

public final int writeSomeTo(@NotNull OutputStream out) throws IOException, CodecException {
Expand Down Expand Up @@ -108,15 +102,15 @@ public final int duration() {
return duration;
}

public int size() {
public final int size() {
return audioIn.size();
}

public int decodedLength() {
public final int decodedLength() {
return audioIn.decodedLength();
}

public int decryptTimeMs() {
public final int decryptTimeMs() {
return audioFile.decryptTimeMs();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,61 @@
* limitations under the License.
*/

package xyz.gianlu.librespot.player.codecs;
package xyz.gianlu.librespot.player.decoders;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.gianlu.librespot.audio.GeneralAudioStream;
import xyz.gianlu.librespot.audio.NormalizationData;
import xyz.gianlu.librespot.audio.format.SuperAudioFormat;
import xyz.gianlu.librespot.player.PlayerConfiguration;

import java.util.*;

/**
* @author devgianlu
*/
public final class Codecs {
private static final Map<SuperAudioFormat, HashSet<Class<? extends Codec>>> codecs = new EnumMap<>(SuperAudioFormat.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Codecs.class);
public final class Decoders {
private static final Map<SuperAudioFormat, HashSet<Class<? extends Decoder>>> decoders = new EnumMap<>(SuperAudioFormat.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Decoders.class);

static {
registerCodec(SuperAudioFormat.VORBIS, VorbisCodec.class);
registerCodec(SuperAudioFormat.MP3, Mp3Codec.class);
registerDecoder(SuperAudioFormat.VORBIS, VorbisDecoder.class);
registerDecoder(SuperAudioFormat.MP3, Mp3Decoder.class);
}

private Codecs() {
private Decoders() {
}

@Nullable
public static Codec initCodec(@NotNull SuperAudioFormat format, @NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull PlayerConfiguration conf, int duration) {
Set<Class<? extends Codec>> set = codecs.get(format);
public static Decoder initDecoder(@NotNull SuperAudioFormat format, @NotNull GeneralAudioStream audioFile, float normalizationFactor, int duration) {
Set<Class<? extends Decoder>> set = decoders.get(format);
if (set == null) return null;

Optional<Class<? extends Codec>> opt = set.stream().findFirst();
Optional<Class<? extends Decoder>> opt = set.stream().findFirst();
if (!opt.isPresent()) return null;

try {
Class<? extends Codec> clazz = opt.get();
return clazz.getConstructor(GeneralAudioStream.class, NormalizationData.class, PlayerConfiguration.class, int.class).newInstance(audioFile, normalizationData, conf, duration);
Class<? extends Decoder> clazz = opt.get();
return clazz.getConstructor(GeneralAudioStream.class, float.class, int.class).newInstance(audioFile, normalizationFactor, duration);
} catch (ReflectiveOperationException ex) {
LOGGER.error("Failed initializing Codec instance for {}", format, ex);
return null;
}
}

public static void registerCodec(@NotNull SuperAudioFormat format, @NotNull Class<? extends Codec> clazz) {
codecs.computeIfAbsent(format, (key) -> new HashSet<>(5)).add(clazz);
public static void registerDecoder(@NotNull SuperAudioFormat format, @NotNull Class<? extends Decoder> clazz) {
decoders.computeIfAbsent(format, (key) -> new HashSet<>(5)).add(clazz);
}

public static void replaceCodecs(@NotNull SuperAudioFormat format, @NotNull Class<? extends Codec> clazz) {
Set<Class<? extends Codec>> set = codecs.get(format);
public static void replaceDecoder(@NotNull SuperAudioFormat format, @NotNull Class<? extends Decoder> clazz) {
Set<Class<? extends Decoder>> set = decoders.get(format);
if (set != null) set.clear();
registerCodec(format, clazz);
registerDecoder(format, clazz);
}

public static void unregisterCodec(@NotNull Class<? extends Codec> clazz) {
for (Set<Class<? extends Codec>> set : codecs.values())
public static void unregisterDecoder(@NotNull Class<? extends Decoder> clazz) {
for (Set<Class<? extends Decoder>> set : decoders.values())
set.remove(clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
* limitations under the License.
*/

package xyz.gianlu.librespot.player.codecs;
package xyz.gianlu.librespot.player.decoders;

import javazoom.jl.decoder.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.gianlu.librespot.audio.GeneralAudioStream;
import xyz.gianlu.librespot.audio.NormalizationData;
import xyz.gianlu.librespot.player.PlayerConfiguration;
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;

import java.io.IOException;
Expand All @@ -33,12 +30,12 @@
/**
* @author Gianlu
*/
public final class Mp3Codec extends Codec {
public final class Mp3Decoder extends Decoder {
private final byte[] buffer = new byte[2 * BUFFER_SIZE];
private final Mp3InputStream in;

public Mp3Codec(@NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull PlayerConfiguration conf, int duration) throws IOException, BitstreamException {
super(audioFile, normalizationData, conf, duration);
public Mp3Decoder(@NotNull GeneralAudioStream audioFile, float normalizationFactor, int duration) throws IOException, BitstreamException {
super(audioFile, normalizationFactor, duration);

skipMp3Tags(audioIn);
this.in = new Mp3InputStream(audioIn, normalizationFactor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package xyz.gianlu.librespot.player.codecs;
package xyz.gianlu.librespot.player.decoders;

import com.jcraft.jogg.Packet;
import com.jcraft.jogg.Page;
Expand All @@ -25,10 +25,7 @@
import com.jcraft.jorbis.DspState;
import com.jcraft.jorbis.Info;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.gianlu.librespot.audio.GeneralAudioStream;
import xyz.gianlu.librespot.audio.NormalizationData;
import xyz.gianlu.librespot.player.PlayerConfiguration;
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;

import java.io.IOException;
Expand All @@ -37,7 +34,7 @@
/**
* @author Gianlu
*/
public final class VorbisCodec extends Codec {
public final class VorbisDecoder extends Decoder {
private static final int CONVERTED_BUFFER_SIZE = BUFFER_SIZE * 2;
private final StreamState joggStreamState = new StreamState();
private final DspState jorbisDspState = new DspState();
Expand All @@ -56,8 +53,8 @@ public final class VorbisCodec extends Codec {
private int index;
private long pcm_offset;

public VorbisCodec(@NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull PlayerConfiguration conf, int duration) throws IOException, CodecException {
super(audioFile, normalizationData, conf, duration);
public VorbisDecoder(@NotNull GeneralAudioStream audioFile, float normalizationFactor, int duration) throws IOException, CodecException {
super(audioFile, normalizationFactor, duration);

this.joggSyncState.init();
this.joggSyncState.buffer(BUFFER_SIZE);
Expand Down Expand Up @@ -91,8 +88,8 @@ public int time() {
/**
* Reads the body. All "holes" (-1) in data will stop the playback.
*
* @throws Codec.CodecException if a decoding exception occurs
* @throws IOException if an I/O exception occurs
* @throws Decoder.CodecException if a decoding exception occurs
* @throws IOException if an I/O exception occurs
*/
private void readHeader() throws IOException, CodecException {
boolean finished = false;
Expand Down Expand Up @@ -140,7 +137,7 @@ private void readHeader() throws IOException, CodecException {
/**
* Reads the body. All "holes" (-1) are skipped, and the playback continues
*
* @throws Codec.CodecException if a decoding exception occurs
* @throws Decoder.CodecException if a decoding exception occurs
* @throws IOException if an I/O exception occurs
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package xyz.gianlu.librespot.player.codecs;
package xyz.gianlu.librespot.player.decoders;

import com.spotify.metadata.Metadata;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import org.jetbrains.annotations.Nullable;
import xyz.gianlu.librespot.audio.PlayableContentFeeder;
import xyz.gianlu.librespot.player.codecs.Codec;
import xyz.gianlu.librespot.player.codecs.Mp3Codec;
import xyz.gianlu.librespot.player.codecs.VorbisCodec;
import xyz.gianlu.librespot.player.crossfade.CrossfadeController;
import xyz.gianlu.librespot.player.decoders.Decoder;
import xyz.gianlu.librespot.player.decoders.Mp3Decoder;
import xyz.gianlu.librespot.player.decoders.VorbisDecoder;
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;

/**
Expand All @@ -39,21 +39,21 @@ public final class PlayerMetrics {
public String transition = "none";
public int decryptTime = 0;

public PlayerMetrics(@Nullable PlayableContentFeeder.Metrics contentMetrics, @Nullable CrossfadeController crossfade, @Nullable Codec codec) {
public PlayerMetrics(@Nullable PlayableContentFeeder.Metrics contentMetrics, @Nullable CrossfadeController crossfade, @Nullable Decoder decoder) {
this.contentMetrics = contentMetrics;

if (codec != null) {
size = codec.size();
duration = codec.duration();
decodedLength = codec.decodedLength();
decryptTime = codec.decryptTimeMs();
if (decoder != null) {
size = decoder.size();
duration = decoder.duration();
decodedLength = decoder.decodedLength();
decryptTime = decoder.decryptTimeMs();

OutputAudioFormat format = codec.getAudioFormat();
OutputAudioFormat format = decoder.getAudioFormat();
bitrate = (int) (format.getFrameRate() * format.getFrameSize());
sampleRate = format.getSampleRate();

if (codec instanceof VorbisCodec) encoding = "vorbis";
else if (codec instanceof Mp3Codec) encoding = "mp3";
if (decoder instanceof VorbisDecoder) encoding = "vorbis";
else if (decoder instanceof Mp3Decoder) encoding = "mp3";
}

if (crossfade != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.jetbrains.annotations.Nullable;
import xyz.gianlu.librespot.player.Player;
import xyz.gianlu.librespot.player.PlayerConfiguration;
import xyz.gianlu.librespot.player.codecs.Codec;
import xyz.gianlu.librespot.player.decoders.Decoder;
import xyz.gianlu.librespot.player.mixing.output.*;

import java.io.Closeable;
Expand Down Expand Up @@ -152,7 +152,7 @@ public void close() {

@Override
public void run() {
byte[] buffer = new byte[Codec.BUFFER_SIZE * 2];
byte[] buffer = new byte[Decoder.BUFFER_SIZE * 2];

boolean started = false;
while (!closed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.gianlu.librespot.player.codecs.Codec;
import xyz.gianlu.librespot.player.decoders.Decoder;
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;

import java.io.InputStream;
Expand Down Expand Up @@ -82,7 +82,7 @@ public MixingOutput someOut() {
@NotNull
public MixingOutput firstOut() {
if (fout == null) {
fcb = new GainAwareCircularBuffer(Codec.BUFFER_SIZE * 4);
fcb = new GainAwareCircularBuffer(Decoder.BUFFER_SIZE * 4);
fout = new FirstOutputStream();
}

Expand All @@ -92,7 +92,7 @@ public MixingOutput firstOut() {
@NotNull
public MixingOutput secondOut() {
if (sout == null) {
scb = new GainAwareCircularBuffer(Codec.BUFFER_SIZE * 4);
scb = new GainAwareCircularBuffer(Decoder.BUFFER_SIZE * 4);
sout = new SecondOutputStream();
}

Expand Down
Loading

0 comments on commit 2f1cda4

Please sign in to comment.