|
| 1 | +package dev.arbjerg.lavalink.client.protocol |
| 2 | + |
| 3 | +import dev.arbjerg.lavalink.internal.toJsonElement |
| 4 | +import dev.arbjerg.lavalink.protocol.v4.* |
| 5 | +import kotlinx.serialization.json.JsonElement |
| 6 | + |
| 7 | +/** |
| 8 | + * Helper class fo builder [Filters]. |
| 9 | + */ |
| 10 | +class FilterBuilder { |
| 11 | + private var volume: Omissible<Float> = Omissible.Omitted() |
| 12 | + private var equalizer: Omissible<List<Band>> = Omissible.Omitted() |
| 13 | + private var karaoke: Omissible<Karaoke?> = Omissible.Omitted() |
| 14 | + private var timescale: Omissible<Timescale?> = Omissible.Omitted() |
| 15 | + private var tremolo: Omissible<Tremolo?> = Omissible.Omitted() |
| 16 | + private var vibrato: Omissible<Vibrato?> = Omissible.Omitted() |
| 17 | + private var distortion: Omissible<Distortion?> = Omissible.Omitted() |
| 18 | + private var rotation: Omissible<Rotation?> = Omissible.Omitted() |
| 19 | + private var channelMix: Omissible<ChannelMix?> = Omissible.Omitted() |
| 20 | + private var lowPass: Omissible<LowPass?> = Omissible.Omitted() |
| 21 | + private var pluginFilters: MutableMap<String, JsonElement> = mutableMapOf() |
| 22 | + |
| 23 | + /** |
| 24 | + * Sets the filter volume. If you just want to change the volume, it is highly recommended to use [dev.arbjerg.lavalink.client.IUpdatablePlayer.setVolume] instead. |
| 25 | + * |
| 26 | + * This volume takes the time of your buffer size to apply and should only be used if any other filters would increase the overall volume too much. |
| 27 | + * |
| 28 | + * @param volume The volume to apply to the filter. |
| 29 | + * |
| 30 | + * @return The updated builder, useful for chaining |
| 31 | + */ |
| 32 | + fun setVolume(volume: Float) = apply { |
| 33 | + this.volume = volume.toOmissible() |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Set the equalizer bands on the player. |
| 38 | + * |
| 39 | + * @param equalizer Each band to set in the equalizer. Default gain is 1.0f. |
| 40 | + * |
| 41 | + * @return The updated builder, useful for chaining |
| 42 | + */ |
| 43 | + fun setEqualizer(equalizer: List<Band>) = apply { |
| 44 | + this.equalizer = equalizer.toOmissible() |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Set a specific band on the equalizer. |
| 49 | + * |
| 50 | + * @param band The band to set. |
| 51 | + * @param gain The gain to apply to the band. Default gain is 1.0f. |
| 52 | + * |
| 53 | + * @return The updated builder, useful for chaining |
| 54 | + */ |
| 55 | + @JvmOverloads |
| 56 | + fun setEqualizerBand(band: Int, gain: Float = 1.0F) = apply { |
| 57 | + val eq = this.equalizer |
| 58 | + val bandObj = Band(band, gain) |
| 59 | + |
| 60 | + if (eq.isPresent()) { |
| 61 | + val currEq = eq.value.toMutableList() |
| 62 | + val bandIndex = currEq.indexOfFirst { it.band == band } |
| 63 | + |
| 64 | + if (bandIndex > -1) { |
| 65 | + currEq[bandIndex] = bandObj |
| 66 | + } else { |
| 67 | + currEq.add(bandObj) |
| 68 | + } |
| 69 | + |
| 70 | + this.equalizer = currEq.toOmissible() |
| 71 | + } else { |
| 72 | + this.equalizer = listOf(bandObj).toOmissible() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Set the karaoke filter on the player. |
| 78 | + * |
| 79 | + * @param karaoke The karaoke filter to apply to the player. Set to null to disable the filter. |
| 80 | + * |
| 81 | + * @return The updated builder, useful for chaining |
| 82 | + */ |
| 83 | + fun setKaraoke(karaoke: Karaoke?) = apply { |
| 84 | + this.karaoke = Omissible.of(karaoke) |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Sets the timescale filter on the player. |
| 89 | + * |
| 90 | + * @param timescale The timescale filter to apply to the player. Set to null to disable the filter. |
| 91 | + * |
| 92 | + * @return The updated builder, useful for chaining |
| 93 | + */ |
| 94 | + fun setTimescale(timescale: Timescale?) = apply { |
| 95 | + this.timescale = Omissible.of(timescale) |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets the tremolo filter on the player. |
| 100 | + * |
| 101 | + * @param tremolo The tremolo filter to apply to the player. Set to null to disable the filter. |
| 102 | + * |
| 103 | + * @return The updated builder, useful for chaining |
| 104 | + */ |
| 105 | + fun setTremolo(tremolo: Tremolo?) = apply { |
| 106 | + this.tremolo = Omissible.of(tremolo) |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Sets the vibrato filter on the player. |
| 111 | + * |
| 112 | + * @param vibrato The vibrato filter to apply to the player. Set to null to disable the filter. |
| 113 | + * |
| 114 | + * @return The updated builder, useful for chaining |
| 115 | + */ |
| 116 | + fun setVibrato(vibrato: Vibrato?) = apply { |
| 117 | + this.vibrato = Omissible.of(vibrato) |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Sets the distortion filter on the player. |
| 122 | + * |
| 123 | + * @param distortion The distortion filter to apply to the player. Set to null to disable the filter. |
| 124 | + * |
| 125 | + * @return The updated builder, useful for chaining |
| 126 | + */ |
| 127 | + fun setDistortion(distortion: Distortion?) = apply { |
| 128 | + this.distortion = Omissible.of(distortion) |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Sets the rotation filter on the player. |
| 133 | + * |
| 134 | + * @param rotation The rotation filter to apply to the player. Set to null to disable the filter. |
| 135 | + * |
| 136 | + * @return The updated builder, useful for chaining |
| 137 | + */ |
| 138 | + fun setRotation(rotation: Rotation?) = apply { |
| 139 | + this.rotation = Omissible.of(rotation) |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Sets the channel mix filter on the player. |
| 144 | + * |
| 145 | + * @param channelMix The channel mix filter to apply to the player. Set to null to disable the filter. |
| 146 | + * |
| 147 | + * @return The updated builder, useful for chaining |
| 148 | + */ |
| 149 | + fun setChannelMix(channelMix: ChannelMix?) = apply { |
| 150 | + this.channelMix = Omissible.of(channelMix) |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets the low pass filter on the player. |
| 155 | + * |
| 156 | + * @param lowPass The low pass filter to apply to the player. Set to null to disable the filter. |
| 157 | + * |
| 158 | + * @return The updated builder, useful for chaining |
| 159 | + */ |
| 160 | + fun setLowPass(lowPass: LowPass?) = apply { |
| 161 | + this.lowPass = Omissible.of(lowPass) |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Set custom filter data for a plugin. |
| 166 | + * |
| 167 | + * @param name the name of the plugin filter |
| 168 | + * @param filter the filter data, can be a custom class as it will be serialised to json |
| 169 | + * @return The updated builder, useful for chaining |
| 170 | + */ |
| 171 | + fun setPluginFilter(name: String, filter: Any) = apply { |
| 172 | + pluginFilters[name] = toJsonElement(filter) |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Set custom filter data for a plugin. |
| 177 | + * |
| 178 | + * @param name the name of the plugin filter |
| 179 | + * @param filter kotlin [JsonElement] that holds the filter data. |
| 180 | + * @return The updated builder, useful for chaining |
| 181 | + */ |
| 182 | + fun setPluginFilter(name: String, filter: JsonElement) = apply { |
| 183 | + pluginFilters[name] = filter |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Removes a plugin filter with the given name. |
| 188 | + * |
| 189 | + * @param name the name of the plugin filter |
| 190 | + * @return The updated builder, useful for chaining |
| 191 | + */ |
| 192 | + fun removePluginFilter(name: String) = apply { |
| 193 | + pluginFilters.remove(name) |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Builds the [Filters] object with the current configuration. |
| 198 | + * |
| 199 | + * @return the [Filters] object with the current configuration |
| 200 | + */ |
| 201 | + fun build() = Filters( |
| 202 | + volume, |
| 203 | + equalizer, |
| 204 | + karaoke, |
| 205 | + timescale, |
| 206 | + tremolo, |
| 207 | + vibrato, |
| 208 | + distortion, |
| 209 | + rotation, |
| 210 | + channelMix, |
| 211 | + lowPass, |
| 212 | + pluginFilters |
| 213 | + ) |
| 214 | +} |
0 commit comments