Skip to content

Commit

Permalink
Working filter stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Mar 30, 2020
1 parent 44c272e commit 386c2f3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 59 deletions.
61 changes: 32 additions & 29 deletions LavalinkServer/src/main/java/lavalink/server/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ 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;
private FilterChain filters;

public Player(SocketContext socketContext, String guildId, AudioPlayerManager audioPlayerManager) {
Expand Down Expand Up @@ -93,32 +91,32 @@ public void setVolume(int volume) {
player.setVolume(volume);
}

public void setBandGain(int band, float gain) {
log.debug("Setting band {}'s gain to {}", band, 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 if (!this.isEqualizerApplied) {
this.player.setFilterFactory(equalizerFactory);
this.isEqualizerApplied = true;
}
}
// public void setBandGain(int band, float gain) {
// log.debug("Setting band {}'s gain to {}", band, 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 if (!this.isEqualizerApplied) {
// this.player.setFilterFactory(equalizerFactory);
// this.isEqualizerApplied = true;
// }
// }

public JSONObject getState() {
JSONObject json = new JSONObject();
Expand Down Expand Up @@ -197,6 +195,11 @@ public FilterChain getFilters() {

public void setFilters(FilterChain filters) {
this.filters = filters;
player.setFilterFactory(filters);

if (filters.isEnabled()) {
player.setFilterFactory(filters);
} else {
player.setFilterFactory(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package lavalink.server.player.filters

import com.google.gson.Gson
import com.sedmelluq.discord.lavaplayer.filter.AudioFilter
import com.sedmelluq.discord.lavaplayer.filter.FilterChainBuilder
import com.sedmelluq.discord.lavaplayer.filter.PcmFilterFactory
import com.sedmelluq.discord.lavaplayer.filter.UniversalPcmAudioFilter
import com.sedmelluq.discord.lavaplayer.filter.*
import com.sedmelluq.discord.lavaplayer.format.AudioDataFormat
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import org.slf4j.Logger
Expand Down Expand Up @@ -37,25 +34,17 @@ class FilterChain : PcmFilterFactory {
val isEnabled get() = buildList().any { it.isEnabled }

override fun buildChain(track: AudioTrack?, format: AudioDataFormat, output: UniversalPcmAudioFilter): MutableList<AudioFilter> {
val list = listOfNotNull(
volume?.let { VolumeConfig(it) },
equalizer?.let { EqualizerConfig(it) },
karaoke,
timescale,
tremolo,
vibrato
)
val builder = FilterChainBuilder()
builder.addFirst(output)
list.filter { it.isEnabled }
.map { it.build(format, builder.makeFirstFloat(format.channelCount)) }
.forEach { builder.addFirst(it) }

return builder.build(null, format.channelCount)
.filters//.apply { dropLast(1) }
.apply {
log.info(this.map { it.javaClass.simpleName }.toString())
}
val enabledFilters = buildList().takeIf { it.isNotEmpty() }
?: return mutableListOf()

val pipeline = mutableListOf<FloatPcmAudioFilter>()

for (filter in enabledFilters) {
val outputTo = pipeline.lastOrNull() ?: output
pipeline.add(filter.build(format, outputTo))
}

return pipeline.reversed().toMutableList() // Output last
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import com.github.natanbc.lavadsp.vibrato.VibratoPcmAudioFilter
import com.github.natanbc.lavadsp.volume.VolumePcmAudioFilter
import com.sedmelluq.discord.lavaplayer.filter.AudioFilter
import com.sedmelluq.discord.lavaplayer.filter.FloatPcmAudioFilter
import com.sedmelluq.discord.lavaplayer.filter.UniversalPcmAudioFilter
import com.sedmelluq.discord.lavaplayer.filter.equalizer.Equalizer
import com.sedmelluq.discord.lavaplayer.format.AudioDataFormat

class VolumeConfig(private var volume: Float) : FilterConfig() {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): AudioFilter {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): FloatPcmAudioFilter {
return VolumePcmAudioFilter(output, format.channelCount).also {
it.volume = volume
}
Expand All @@ -27,7 +28,7 @@ class EqualizerConfig(bands: List<Band>) : FilterConfig() {
bands.forEach { array[it.band] = it.gain }
}

override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): AudioFilter =
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): FloatPcmAudioFilter =
Equalizer(format.channelCount, output, array)

override val isEnabled: Boolean get() = array.any { it != 0.0f }
Expand All @@ -41,7 +42,7 @@ class KaraokeConfig(
private val filterBand: Float = 220.0f,
private val filterWidth: Float = 100.0f
) : FilterConfig() {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): AudioFilter {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): FloatPcmAudioFilter {
return KaraokePcmAudioFilter(output, format.channelCount, format.sampleRate)
.setLevel(level)
.setMonoLevel(monoLevel)
Expand All @@ -57,7 +58,7 @@ class TimescaleConfig(
private val rate: Double = 1.0
) : FilterConfig() {

override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): AudioFilter {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): FloatPcmAudioFilter {
return TimescalePcmAudioFilter(output, format.channelCount, format.sampleRate)
.setSpeed(speed)
.setPitch(pitch)
Expand All @@ -71,7 +72,7 @@ class TremoloConfig(
private val frequency: Float = 2.0f,
private val depth: Float = 0.5f
) : FilterConfig() {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): AudioFilter {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): FloatPcmAudioFilter {
return TremoloPcmAudioFilter(output, format.channelCount, format.sampleRate)
.setFrequency(frequency)
.setDepth(depth)
Expand All @@ -85,7 +86,7 @@ class VibratoConfig(
private val depth: Float = 0.5f
) : FilterConfig() {

override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): AudioFilter {
override fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): FloatPcmAudioFilter {
return VibratoPcmAudioFilter(output, format.channelCount, format.sampleRate)
.setFrequency(frequency)
.setDepth(depth)
Expand All @@ -96,6 +97,6 @@ class VibratoConfig(
}

abstract class FilterConfig {
abstract fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): AudioFilter
abstract fun build(format: AudioDataFormat, output: FloatPcmAudioFilter): FloatPcmAudioFilter
abstract val isEnabled: Boolean
}

0 comments on commit 386c2f3

Please sign in to comment.