Skip to content

Commit f406e22

Browse files
authored
Merge pull request #154 from Devoxin/feature/equalizer
Equalizer stuff
2 parents 4f2b114 + 1ddbedd commit f406e22

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

IMPLEMENTATION.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ Set player volume. Volume may range from 0 to 1000. 100 is default.
110110
}
111111
```
112112

113+
Using the player equalizer
114+
```json
115+
{
116+
"op": "equalizer",
117+
"guildId": "...",
118+
"bands": [
119+
{
120+
"band": 0,
121+
"gain": 0.2
122+
}
123+
]
124+
}
125+
```
126+
There are 16 bands (0-15) that can be changed.
127+
`gain` is the multiplier for the given band. The default value is 0. Valid values range from -0.25 to 1.0,
128+
where -0.25 means the given band is completely muted, and 0.25 means it is doubled. Modifying the gain could
129+
also change the volume of the output.
130+
113131
Tell the server to potentially disconnect from the voice server and potentially remove the player with all its data.
114132
This is useful if you want to move to a new node for a voice connection. Calling this op does not affect voice state,
115133
and you can send the same VOICE_SERVER_UPDATE to a new node.

LavalinkServer/src/main/java/lavalink/server/io/SocketServer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class SocketServer(private val serverConfig: ServerConfig, private val audioPlay
105105
"pause" -> handlers.pause(session, json)
106106
"seek" -> handlers.seek(session, json)
107107
"volume" -> handlers.volume(session, json)
108+
"equalizer" -> handlers.equalizer(session, json)
108109
"destroy" -> handlers.destroy(session, json)
109110
else -> log.warn("Unexpected operation: " + json.getString("op"))
110111
}

LavalinkServer/src/main/java/lavalink/server/io/WebSocketHandlers.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ class WebSocketHandlers(private val contextMap: Map<String, SocketContext>) {
8888
player.setVolume(json.getInt("volume"))
8989
}
9090

91+
fun equalizer(session: WebSocketSession, json: JSONObject) {
92+
val player = contextMap[session.id]!!.getPlayer(json.getString("guildId"))
93+
val bands = json.getJSONArray("bands")
94+
95+
for (i in 0 until bands.length()) {
96+
val band = bands.getJSONObject(i)
97+
player.setBandGain(band.getInt("band"), band.getFloat("gain"))
98+
}
99+
}
100+
91101
fun destroy(session: WebSocketSession, json: JSONObject) {
92102
val socketContext = contextMap[session.id]!!
93103
val player = socketContext.players.remove(json.getString("guildId"))

LavalinkServer/src/main/java/lavalink/server/player/Player.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package lavalink.server.player;
2424

25+
import com.sedmelluq.discord.lavaplayer.filter.equalizer.Equalizer;
26+
import com.sedmelluq.discord.lavaplayer.filter.equalizer.EqualizerFactory;
2527
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
2628
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
2729
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
@@ -48,6 +50,8 @@ public class Player extends AudioEventAdapter implements AudioSendHandler {
4850
private AudioLossCounter audioLossCounter = new AudioLossCounter();
4951
private AudioFrame lastFrame = null;
5052
private ScheduledFuture myFuture = null;
53+
private EqualizerFactory equalizerFactory = new EqualizerFactory();
54+
private boolean isEqualizerApplied = false;
5155

5256
public Player(SocketContext socketContext, String guildId, AudioPlayerManager audioPlayerManager) {
5357
this.socketContext = socketContext;
@@ -86,6 +90,32 @@ public void setVolume(int volume) {
8690
player.setVolume(volume);
8791
}
8892

93+
public void setBandGain(int band, float gain) {
94+
equalizerFactory.setGain(band, gain);
95+
96+
if (gain == 0.0f) {
97+
if (!isEqualizerApplied) {
98+
return;
99+
}
100+
101+
boolean shouldDisable = true;
102+
103+
for (int i = 0; i < Equalizer.BAND_COUNT; i++) {
104+
if (equalizerFactory.getGain(i) != 0.0f) {
105+
shouldDisable = false;
106+
}
107+
}
108+
109+
if (shouldDisable) {
110+
this.player.setFilterFactory(null);
111+
this.isEqualizerApplied = false;
112+
}
113+
} else {
114+
this.player.setFilterFactory(equalizerFactory);
115+
this.isEqualizerApplied = true;
116+
}
117+
}
118+
89119
public JSONObject getState() {
90120
JSONObject json = new JSONObject();
91121

0 commit comments

Comments
 (0)