Skip to content

Commit

Permalink
Merge pull request lavalink-devs#154 from Devoxin/feature/equalizer
Browse files Browse the repository at this point in the history
Equalizer stuff
  • Loading branch information
freyacodes authored Oct 27, 2018
2 parents 4f2b114 + 1ddbedd commit f406e22
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ Set player volume. Volume may range from 0 to 1000. 100 is default.
}
```

Using the player equalizer
```json
{
"op": "equalizer",
"guildId": "...",
"bands": [
{
"band": 0,
"gain": 0.2
}
]
}
```
There are 16 bands (0-15) that can be changed.
`gain` is the multiplier for the given band. The default value is 0. Valid values range from -0.25 to 1.0,
where -0.25 means the given band is completely muted, and 0.25 means it is doubled. Modifying the gain could
also change the volume of the output.

Tell the server to potentially disconnect from the voice server and potentially remove the player with all its data.
This is useful if you want to move to a new node for a voice connection. Calling this op does not affect voice state,
and you can send the same VOICE_SERVER_UPDATE to a new node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class SocketServer(private val serverConfig: ServerConfig, private val audioPlay
"pause" -> handlers.pause(session, json)
"seek" -> handlers.seek(session, json)
"volume" -> handlers.volume(session, json)
"equalizer" -> handlers.equalizer(session, json)
"destroy" -> handlers.destroy(session, json)
else -> log.warn("Unexpected operation: " + json.getString("op"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ class WebSocketHandlers(private val contextMap: Map<String, SocketContext>) {
player.setVolume(json.getInt("volume"))
}

fun equalizer(session: WebSocketSession, json: JSONObject) {
val player = contextMap[session.id]!!.getPlayer(json.getString("guildId"))
val bands = json.getJSONArray("bands")

for (i in 0 until bands.length()) {
val band = bands.getJSONObject(i)
player.setBandGain(band.getInt("band"), band.getFloat("gain"))
}
}

fun destroy(session: WebSocketSession, json: JSONObject) {
val socketContext = contextMap[session.id]!!
val player = socketContext.players.remove(json.getString("guildId"))
Expand Down
30 changes: 30 additions & 0 deletions LavalinkServer/src/main/java/lavalink/server/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package lavalink.server.player;

import com.sedmelluq.discord.lavaplayer.filter.equalizer.Equalizer;
import com.sedmelluq.discord.lavaplayer.filter.equalizer.EqualizerFactory;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
Expand All @@ -48,6 +50,8 @@ public class Player extends AudioEventAdapter implements AudioSendHandler {
private AudioLossCounter audioLossCounter = new AudioLossCounter();
private AudioFrame lastFrame = null;
private ScheduledFuture myFuture = null;
private EqualizerFactory equalizerFactory = new EqualizerFactory();
private boolean isEqualizerApplied = false;

public Player(SocketContext socketContext, String guildId, AudioPlayerManager audioPlayerManager) {
this.socketContext = socketContext;
Expand Down Expand Up @@ -86,6 +90,32 @@ public void setVolume(int volume) {
player.setVolume(volume);
}

public void setBandGain(int band, float gain) {
equalizerFactory.setGain(band, gain);

if (gain == 0.0f) {
if (!isEqualizerApplied) {
return;
}

boolean shouldDisable = true;

for (int i = 0; i < Equalizer.BAND_COUNT; i++) {
if (equalizerFactory.getGain(i) != 0.0f) {
shouldDisable = false;
}
}

if (shouldDisable) {
this.player.setFilterFactory(null);
this.isEqualizerApplied = false;
}
} else {
this.player.setFilterFactory(equalizerFactory);
this.isEqualizerApplied = true;
}
}

public JSONObject getState() {
JSONObject json = new JSONObject();

Expand Down

0 comments on commit f406e22

Please sign in to comment.