Skip to content

Commit

Permalink
Setting volume at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Oct 6, 2018
1 parent d1764a9 commit b3925bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/librespot/spotify/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private void loadTrack(boolean play, int pos) throws IOException, MercuryClient.

try {
if (playerRunner != null) playerRunner.stop();
playerRunner = new PlayerRunner(audioStreaming, normalizationData, conf, this);
playerRunner = new PlayerRunner(audioStreaming, normalizationData, spirc.deviceState(), conf, this);
new Thread(playerRunner).start();

if (play) {
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/librespot/spotify/player/PlayerRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.jcraft.jorbis.Info;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.librespot.spotify.proto.Spirc;

import javax.sound.sampled.*;
import java.io.IOException;
Expand All @@ -20,6 +21,7 @@
*/
public class PlayerRunner implements Runnable {
public static final int VOLUME_STEPS = 64;
public static final int VOLUME_MAX = 65536;
private static final int VOLUME_STEP = 65536 / VOLUME_STEPS;
private static final int BUFFER_SIZE = 2048;
private static final int CONVERTED_BUFFER_SIZE = BUFFER_SIZE * 2;
Expand Down Expand Up @@ -47,7 +49,8 @@ public class PlayerRunner implements Runnable {
private volatile boolean playing = false;
private volatile boolean stopped = false;

public PlayerRunner(@NotNull AudioFileStreaming audioFile, @NotNull NormalizationData normalizationData, @NotNull Player.Configuration configuration, @NotNull Listener listener) throws IOException, PlayerException {
public PlayerRunner(@NotNull AudioFileStreaming audioFile, @NotNull NormalizationData normalizationData, @NotNull Spirc.DeviceState.Builder deviceState,
@NotNull Player.Configuration configuration, @NotNull Listener listener) throws IOException, PlayerException {
this.audioIn = audioFile.stream();
this.listener = listener;
this.normalizationFactor = normalizationData.getFactor(configuration);
Expand All @@ -59,7 +62,7 @@ public PlayerRunner(@NotNull AudioFileStreaming audioFile, @NotNull Normalizatio

readHeader();
initializeSound();
this.controller = new Controller(outputLine);
this.controller = new Controller(outputLine, deviceState);

LOGGER.trace(String.format("Player ready for playback, fileId: %s", audioFile.getFileIdHex()));
}
Expand Down Expand Up @@ -275,24 +278,26 @@ public static class Controller {
private final DynamicRange dynamicRange;
private int volume = 0;

Controller(@NotNull Line line) {
Controller(@NotNull Line line, @NotNull Spirc.DeviceState.Builder deviceState) {
if (line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
masterGain = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
dynamicRange = DynamicRange.get(masterGain);

setVolume(deviceState.getVolume());
} else {
masterGain = null;
dynamicRange = null;
}
}

private static double map(double x, double in_min, double in_max, double out_min, double out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
private static double mapLogarithmic(double x, double out_min, double out_max) {
return x * (out_max - out_min) / 1 + out_min;
}

private float calcLogarithmic(int val) {
float normalized = (float) val / 65536f;
return (float) map(Math.exp(normalized * dynamicRange.b) * dynamicRange.a,
0, 1, masterGain.getMinimum(), masterGain.getMaximum());
float normalized = (float) val / VOLUME_MAX;
return (float) mapLogarithmic(Math.exp(normalized * dynamicRange.b) * dynamicRange.a,
masterGain.getMinimum(), masterGain.getMaximum());
}

public void setVolume(int val) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/librespot/spotify/spirc/SpotifyIrc.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private Spirc.DeviceState.Builder initializeDeviceState() {
return Spirc.DeviceState.newBuilder()
.setCanPlay(true)
.setIsActive(false)
.setVolume(0)
.setVolume(PlayerRunner.VOLUME_MAX)
.setName(session.deviceName())
.setSwVersion(Version.versionString())
.addCapabilities(Spirc.Capability.newBuilder()
Expand Down

0 comments on commit b3925bc

Please sign in to comment.