Skip to content

Commit 2f1cda4

Browse files
committed
Renamed Codec to Decoder + simplified constructor
1 parent f81b212 commit 2f1cda4

File tree

14 files changed

+97
-107
lines changed

14 files changed

+97
-107
lines changed

player/src/main/java/xyz/gianlu/librespot/player/FileConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import xyz.gianlu.librespot.common.Utils;
3838
import xyz.gianlu.librespot.core.Session;
3939
import xyz.gianlu.librespot.core.TimeProvider;
40-
import xyz.gianlu.librespot.player.codecs.AudioQuality;
40+
import xyz.gianlu.librespot.player.decoders.AudioQuality;
4141

4242
import java.io.File;
4343
import java.io.FileReader;

player/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import xyz.gianlu.librespot.metadata.ImageId;
4141
import xyz.gianlu.librespot.metadata.PlayableId;
4242
import xyz.gianlu.librespot.player.StateWrapper.NextPlayable;
43-
import xyz.gianlu.librespot.player.codecs.Codec;
4443
import xyz.gianlu.librespot.player.contexts.AbsSpotifyContext;
44+
import xyz.gianlu.librespot.player.decoders.Decoder;
4545
import xyz.gianlu.librespot.player.metrics.NewPlaybackIdEvent;
4646
import xyz.gianlu.librespot.player.metrics.NewSessionIdEvent;
4747
import xyz.gianlu.librespot.player.metrics.PlaybackMetrics;
@@ -577,7 +577,7 @@ private void handlePause() {
577577
try {
578578
if (playerSession != null)
579579
state.setPosition(playerSession.currentTime());
580-
} catch (Codec.CannotGetTimeException ex) {
580+
} catch (Decoder.CannotGetTimeException ex) {
581581
state.setPosition(state.getPosition());
582582
}
583583

@@ -820,7 +820,7 @@ public byte[] currentCoverImage() throws IOException {
820820
public int time() {
821821
try {
822822
return playerSession == null ? -1 : playerSession.currentTime();
823-
} catch (Codec.CannotGetTimeException ex) {
823+
} catch (Decoder.CannotGetTimeException ex) {
824824
return -1;
825825
}
826826
}

player/src/main/java/xyz/gianlu/librespot/player/PlayerConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import org.jetbrains.annotations.Contract;
2020
import org.jetbrains.annotations.NotNull;
21-
import xyz.gianlu.librespot.player.codecs.AudioQuality;
21+
import xyz.gianlu.librespot.player.decoders.AudioQuality;
2222

2323
import java.io.File;
2424

player/src/main/java/xyz/gianlu/librespot/player/codecs/AudioQuality.java renamed to player/src/main/java/xyz/gianlu/librespot/player/decoders/AudioQuality.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package xyz.gianlu.librespot.player.codecs;
17+
package xyz.gianlu.librespot.player.decoders;
1818

1919
import com.spotify.metadata.Metadata.AudioFile;
2020
import org.jetbrains.annotations.NotNull;

player/src/main/java/xyz/gianlu/librespot/player/codecs/Codec.java renamed to player/src/main/java/xyz/gianlu/librespot/player/decoders/Decoder.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
package xyz.gianlu.librespot.player.codecs;
17+
package xyz.gianlu.librespot.player.decoders;
1818

1919
import org.jetbrains.annotations.NotNull;
20-
import org.jetbrains.annotations.Nullable;
2120
import org.slf4j.Logger;
2221
import org.slf4j.LoggerFactory;
2322
import xyz.gianlu.librespot.audio.AbsChunkedInputStream;
2423
import xyz.gianlu.librespot.audio.GeneralAudioStream;
25-
import xyz.gianlu.librespot.audio.NormalizationData;
26-
import xyz.gianlu.librespot.player.PlayerConfiguration;
2724
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;
2825

2926
import java.io.Closeable;
@@ -33,9 +30,9 @@
3330
/**
3431
* @author Gianlu
3532
*/
36-
public abstract class Codec implements Closeable {
33+
public abstract class Decoder implements Closeable {
3734
public static final int BUFFER_SIZE = 2048;
38-
private static final Logger LOGGER = LoggerFactory.getLogger(Codec.class);
35+
private static final Logger LOGGER = LoggerFactory.getLogger(Decoder.class);
3936
protected final AbsChunkedInputStream audioIn;
4037
protected final float normalizationFactor;
4138
protected final int duration;
@@ -44,14 +41,11 @@ public abstract class Codec implements Closeable {
4441
protected int seekZero = 0;
4542
private OutputAudioFormat format;
4643

47-
public Codec(@NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull PlayerConfiguration conf, int duration) {
44+
public Decoder(@NotNull GeneralAudioStream audioFile, float normalizationFactor, int duration) {
4845
this.audioIn = audioFile.stream();
4946
this.audioFile = audioFile;
5047
this.duration = duration;
51-
if (conf.enableNormalisation)
52-
this.normalizationFactor = normalizationData != null ? normalizationData.getFactor(conf.normalisationPregain) : 1;
53-
else
54-
this.normalizationFactor = 1;
48+
this.normalizationFactor = normalizationFactor;
5549
}
5650

5751
public final int writeSomeTo(@NotNull OutputStream out) throws IOException, CodecException {
@@ -108,15 +102,15 @@ public final int duration() {
108102
return duration;
109103
}
110104

111-
public int size() {
105+
public final int size() {
112106
return audioIn.size();
113107
}
114108

115-
public int decodedLength() {
109+
public final int decodedLength() {
116110
return audioIn.decodedLength();
117111
}
118112

119-
public int decryptTimeMs() {
113+
public final int decryptTimeMs() {
120114
return audioFile.decryptTimeMs();
121115
}
122116

player/src/main/java/xyz/gianlu/librespot/player/codecs/Codecs.java renamed to player/src/main/java/xyz/gianlu/librespot/player/decoders/Decoders.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,61 @@
1414
* limitations under the License.
1515
*/
1616

17-
package xyz.gianlu.librespot.player.codecs;
17+
package xyz.gianlu.librespot.player.decoders;
1818

1919
import org.jetbrains.annotations.NotNull;
2020
import org.jetbrains.annotations.Nullable;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323
import xyz.gianlu.librespot.audio.GeneralAudioStream;
24-
import xyz.gianlu.librespot.audio.NormalizationData;
2524
import xyz.gianlu.librespot.audio.format.SuperAudioFormat;
26-
import xyz.gianlu.librespot.player.PlayerConfiguration;
2725

2826
import java.util.*;
2927

3028
/**
3129
* @author devgianlu
3230
*/
33-
public final class Codecs {
34-
private static final Map<SuperAudioFormat, HashSet<Class<? extends Codec>>> codecs = new EnumMap<>(SuperAudioFormat.class);
35-
private static final Logger LOGGER = LoggerFactory.getLogger(Codecs.class);
31+
public final class Decoders {
32+
private static final Map<SuperAudioFormat, HashSet<Class<? extends Decoder>>> decoders = new EnumMap<>(SuperAudioFormat.class);
33+
private static final Logger LOGGER = LoggerFactory.getLogger(Decoders.class);
3634

3735
static {
38-
registerCodec(SuperAudioFormat.VORBIS, VorbisCodec.class);
39-
registerCodec(SuperAudioFormat.MP3, Mp3Codec.class);
36+
registerDecoder(SuperAudioFormat.VORBIS, VorbisDecoder.class);
37+
registerDecoder(SuperAudioFormat.MP3, Mp3Decoder.class);
4038
}
4139

42-
private Codecs() {
40+
private Decoders() {
4341
}
4442

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

50-
Optional<Class<? extends Codec>> opt = set.stream().findFirst();
48+
Optional<Class<? extends Decoder>> opt = set.stream().findFirst();
5149
if (!opt.isPresent()) return null;
5250

5351
try {
54-
Class<? extends Codec> clazz = opt.get();
55-
return clazz.getConstructor(GeneralAudioStream.class, NormalizationData.class, PlayerConfiguration.class, int.class).newInstance(audioFile, normalizationData, conf, duration);
52+
Class<? extends Decoder> clazz = opt.get();
53+
return clazz.getConstructor(GeneralAudioStream.class, float.class, int.class).newInstance(audioFile, normalizationFactor, duration);
5654
} catch (ReflectiveOperationException ex) {
5755
LOGGER.error("Failed initializing Codec instance for {}", format, ex);
5856
return null;
5957
}
6058
}
6159

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

66-
public static void replaceCodecs(@NotNull SuperAudioFormat format, @NotNull Class<? extends Codec> clazz) {
67-
Set<Class<? extends Codec>> set = codecs.get(format);
64+
public static void replaceDecoder(@NotNull SuperAudioFormat format, @NotNull Class<? extends Decoder> clazz) {
65+
Set<Class<? extends Decoder>> set = decoders.get(format);
6866
if (set != null) set.clear();
69-
registerCodec(format, clazz);
67+
registerDecoder(format, clazz);
7068
}
7169

72-
public static void unregisterCodec(@NotNull Class<? extends Codec> clazz) {
73-
for (Set<Class<? extends Codec>> set : codecs.values())
70+
public static void unregisterDecoder(@NotNull Class<? extends Decoder> clazz) {
71+
for (Set<Class<? extends Decoder>> set : decoders.values())
7472
set.remove(clazz);
7573
}
7674
}

player/src/main/java/xyz/gianlu/librespot/player/codecs/Mp3Codec.java renamed to player/src/main/java/xyz/gianlu/librespot/player/decoders/Mp3Decoder.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
package xyz.gianlu.librespot.player.codecs;
17+
package xyz.gianlu.librespot.player.decoders;
1818

1919
import javazoom.jl.decoder.*;
2020
import org.jetbrains.annotations.NotNull;
21-
import org.jetbrains.annotations.Nullable;
2221
import xyz.gianlu.librespot.audio.GeneralAudioStream;
23-
import xyz.gianlu.librespot.audio.NormalizationData;
24-
import xyz.gianlu.librespot.player.PlayerConfiguration;
2522
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;
2623

2724
import java.io.IOException;
@@ -33,12 +30,12 @@
3330
/**
3431
* @author Gianlu
3532
*/
36-
public final class Mp3Codec extends Codec {
33+
public final class Mp3Decoder extends Decoder {
3734
private final byte[] buffer = new byte[2 * BUFFER_SIZE];
3835
private final Mp3InputStream in;
3936

40-
public Mp3Codec(@NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull PlayerConfiguration conf, int duration) throws IOException, BitstreamException {
41-
super(audioFile, normalizationData, conf, duration);
37+
public Mp3Decoder(@NotNull GeneralAudioStream audioFile, float normalizationFactor, int duration) throws IOException, BitstreamException {
38+
super(audioFile, normalizationFactor, duration);
4239

4340
skipMp3Tags(audioIn);
4441
this.in = new Mp3InputStream(audioIn, normalizationFactor);

player/src/main/java/xyz/gianlu/librespot/player/codecs/VorbisCodec.java renamed to player/src/main/java/xyz/gianlu/librespot/player/decoders/VorbisDecoder.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package xyz.gianlu.librespot.player.codecs;
17+
package xyz.gianlu.librespot.player.decoders;
1818

1919
import com.jcraft.jogg.Packet;
2020
import com.jcraft.jogg.Page;
@@ -25,10 +25,7 @@
2525
import com.jcraft.jorbis.DspState;
2626
import com.jcraft.jorbis.Info;
2727
import org.jetbrains.annotations.NotNull;
28-
import org.jetbrains.annotations.Nullable;
2928
import xyz.gianlu.librespot.audio.GeneralAudioStream;
30-
import xyz.gianlu.librespot.audio.NormalizationData;
31-
import xyz.gianlu.librespot.player.PlayerConfiguration;
3229
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;
3330

3431
import java.io.IOException;
@@ -37,7 +34,7 @@
3734
/**
3835
* @author Gianlu
3936
*/
40-
public final class VorbisCodec extends Codec {
37+
public final class VorbisDecoder extends Decoder {
4138
private static final int CONVERTED_BUFFER_SIZE = BUFFER_SIZE * 2;
4239
private final StreamState joggStreamState = new StreamState();
4340
private final DspState jorbisDspState = new DspState();
@@ -56,8 +53,8 @@ public final class VorbisCodec extends Codec {
5653
private int index;
5754
private long pcm_offset;
5855

59-
public VorbisCodec(@NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull PlayerConfiguration conf, int duration) throws IOException, CodecException {
60-
super(audioFile, normalizationData, conf, duration);
56+
public VorbisDecoder(@NotNull GeneralAudioStream audioFile, float normalizationFactor, int duration) throws IOException, CodecException {
57+
super(audioFile, normalizationFactor, duration);
6158

6259
this.joggSyncState.init();
6360
this.joggSyncState.buffer(BUFFER_SIZE);
@@ -91,8 +88,8 @@ public int time() {
9188
/**
9289
* Reads the body. All "holes" (-1) in data will stop the playback.
9390
*
94-
* @throws Codec.CodecException if a decoding exception occurs
95-
* @throws IOException if an I/O exception occurs
91+
* @throws Decoder.CodecException if a decoding exception occurs
92+
* @throws IOException if an I/O exception occurs
9693
*/
9794
private void readHeader() throws IOException, CodecException {
9895
boolean finished = false;
@@ -140,7 +137,7 @@ private void readHeader() throws IOException, CodecException {
140137
/**
141138
* Reads the body. All "holes" (-1) are skipped, and the playback continues
142139
*
143-
* @throws Codec.CodecException if a decoding exception occurs
140+
* @throws Decoder.CodecException if a decoding exception occurs
144141
* @throws IOException if an I/O exception occurs
145142
*/
146143
@Override

player/src/main/java/xyz/gianlu/librespot/player/codecs/VorbisOnlyAudioQuality.java renamed to player/src/main/java/xyz/gianlu/librespot/player/decoders/VorbisOnlyAudioQuality.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package xyz.gianlu.librespot.player.codecs;
17+
package xyz.gianlu.librespot.player.decoders;
1818

1919
import com.spotify.metadata.Metadata;
2020
import org.jetbrains.annotations.NotNull;

player/src/main/java/xyz/gianlu/librespot/player/metrics/PlayerMetrics.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import org.jetbrains.annotations.Nullable;
2020
import xyz.gianlu.librespot.audio.PlayableContentFeeder;
21-
import xyz.gianlu.librespot.player.codecs.Codec;
22-
import xyz.gianlu.librespot.player.codecs.Mp3Codec;
23-
import xyz.gianlu.librespot.player.codecs.VorbisCodec;
2421
import xyz.gianlu.librespot.player.crossfade.CrossfadeController;
22+
import xyz.gianlu.librespot.player.decoders.Decoder;
23+
import xyz.gianlu.librespot.player.decoders.Mp3Decoder;
24+
import xyz.gianlu.librespot.player.decoders.VorbisDecoder;
2525
import xyz.gianlu.librespot.player.mixing.output.OutputAudioFormat;
2626

2727
/**
@@ -39,21 +39,21 @@ public final class PlayerMetrics {
3939
public String transition = "none";
4040
public int decryptTime = 0;
4141

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

45-
if (codec != null) {
46-
size = codec.size();
47-
duration = codec.duration();
48-
decodedLength = codec.decodedLength();
49-
decryptTime = codec.decryptTimeMs();
45+
if (decoder != null) {
46+
size = decoder.size();
47+
duration = decoder.duration();
48+
decodedLength = decoder.decodedLength();
49+
decryptTime = decoder.decryptTimeMs();
5050

51-
OutputAudioFormat format = codec.getAudioFormat();
51+
OutputAudioFormat format = decoder.getAudioFormat();
5252
bitrate = (int) (format.getFrameRate() * format.getFrameSize());
5353
sampleRate = format.getSampleRate();
5454

55-
if (codec instanceof VorbisCodec) encoding = "vorbis";
56-
else if (codec instanceof Mp3Codec) encoding = "mp3";
55+
if (decoder instanceof VorbisDecoder) encoding = "vorbis";
56+
else if (decoder instanceof Mp3Decoder) encoding = "mp3";
5757
}
5858

5959
if (crossfade != null) {

0 commit comments

Comments
 (0)