From 1b183e7fa693e39c6033d9a49cb5da5e5ad6d500 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 27 May 2013 11:39:55 +0200 Subject: [PATCH] Add volume filter. --- .../sonitus/data/filter/VolumeFilter.java | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java new file mode 100644 index 0000000..702594a --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/VolumeFilter.java @@ -0,0 +1,79 @@ +/* + * Sonitus - VolumeFilter.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.data.filter; + +import java.util.Arrays; +import java.util.List; + +import net.pterodactylus.sonitus.data.Controller; +import net.pterodactylus.sonitus.data.Filter; +import net.pterodactylus.sonitus.data.controller.Fader; +import net.pterodactylus.sonitus.data.controller.Switch; + +import com.google.common.eventbus.EventBus; + +/** + * Internal {@link Filter} implementation that can reduce the volume of the + * signal. + * + * @author David ‘Bombe’ Roden + */ +public class VolumeFilter extends AudioProcessingFilter { + + /** The volume fader. */ + private final Fader volumeFader; + + /** The mute switch. */ + private final Switch muteSwitch; + + /** + * Creates a new volume filter. + * + * @param eventBus + * The event bus + */ + public VolumeFilter(EventBus eventBus) { + super(eventBus, "Volume"); + volumeFader = new Fader("Volume", 1.0); + muteSwitch = new Switch("Mute", false); + } + + // + // CONTROLLED METHODS + // + + @Override + public List> controllers() { + return Arrays.>asList(volumeFader, muteSwitch); + } + + // + // AUDIOPROCESSINGFILTER METHODS + // + + @Override + protected int[] processSamples(int[] samples) { + int[] processedSamples = new int[samples.length]; + double volumeFactor = volumeFader.value(); + for (int channel = 0; channel < samples.length; ++channel) { + processedSamples[channel] = muteSwitch.value() ? 0 : (int) (samples[channel] * volumeFactor); + } + return processedSamples; + } + +} -- 2.7.4