GainProcessor.java (1997B)
1 /* 2 * _______ _____ _____ _____ 3 * |__ __| | __ \ / ____| __ \ 4 * | | __ _ _ __ ___ ___ ___| | | | (___ | |__) | 5 * | |/ _` | '__/ __|/ _ \/ __| | | |\___ \| ___/ 6 * | | (_| | | \__ \ (_) \__ \ |__| |____) | | 7 * |_|\__,_|_| |___/\___/|___/_____/|_____/|_| 8 * 9 * ------------------------------------------------------------- 10 * 11 * TarsosDSP is developed by Joren Six at IPEM, University Ghent 12 * 13 * ------------------------------------------------------------- 14 * 15 * Info: http://0110.be/tag/TarsosDSP 16 * Github: https://github.com/JorenSix/TarsosDSP 17 * Releases: http://0110.be/releases/TarsosDSP/ 18 * 19 * TarsosDSP includes modified source code by various authors, 20 * for credits and info, see README. 21 * 22 */ 23 24 25 package be.tarsos.dsp; 26 27 /** 28 * With the gain processor it is possible to adapt the volume of the sound. With 29 * a gain of 1, nothing happens. A gain greater than one is a volume increase a 30 * gain between zero and one, exclusive, is a decrease. If you need to flip the 31 * sign of the audio samples, you can by providing a gain of -1.0. but I have no 32 * idea what you could gain by doing that (pathetic pun, I know). 33 * 34 * @author Joren Six 35 */ 36 public class GainProcessor implements AudioProcessor { 37 private double gain; 38 39 public GainProcessor(double newGain) { 40 setGain(newGain); 41 } 42 43 public void setGain(double newGain) { 44 this.gain = newGain; 45 } 46 47 @Override 48 public boolean process(AudioEvent audioEvent) { 49 float[] audioFloatBuffer = audioEvent.getFloatBuffer(); 50 for (int i = audioEvent.getOverlap(); i < audioFloatBuffer.length ; i++) { 51 float newValue = (float) (audioFloatBuffer[i] * gain); 52 if(newValue > 1.0f) { 53 newValue = 1.0f; 54 } else if(newValue < -1.0f) { 55 newValue = -1.0f; 56 } 57 audioFloatBuffer[i] = newValue; 58 } 59 return true; 60 } 61 62 @Override 63 public void processingFinished() { 64 // NOOP 65 } 66 }