DelayEffect.java (3159B)
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.effects; 26 27 import be.tarsos.dsp.AudioEvent; 28 import be.tarsos.dsp.AudioProcessor; 29 30 31 /** 32 * <p> 33 * Adds an echo effect to the signal. 34 * </p> 35 * 36 * @author Joren Six 37 */ 38 public class DelayEffect implements AudioProcessor { 39 40 private double sampleRate; 41 private float[] echoBuffer;//in seconds 42 private int position; 43 private float decay; 44 45 private double newEchoLength; 46 47 /** 48 * @param echoLength in seconds 49 * @param sampleRate the sample rate in Hz. 50 * @param decay The decay of the echo, a value between 0 and 1. 1 meaning no decay, 0 means immediate decay (not echo effect). 51 */ 52 public DelayEffect(double echoLength,double decay,double sampleRate) { 53 this.sampleRate = sampleRate; 54 setDecay(decay); 55 setEchoLength(echoLength); 56 applyNewEchoLength(); 57 } 58 59 /** 60 * @param newEchoLength A new echo buffer length in seconds. 61 */ 62 public void setEchoLength(double newEchoLength){ 63 this.newEchoLength = newEchoLength; 64 } 65 66 private void applyNewEchoLength(){ 67 if(newEchoLength != -1){ 68 69 //create a new buffer with the information of the previous buffer 70 float[] newEchoBuffer = new float[(int) (sampleRate * newEchoLength)]; 71 if(echoBuffer != null){ 72 for(int i = 0 ; i < newEchoBuffer.length; i++){ 73 if(position >= echoBuffer.length){ 74 position = 0; 75 } 76 newEchoBuffer[i] = echoBuffer[position]; 77 position++; 78 } 79 } 80 this.echoBuffer = newEchoBuffer; 81 newEchoLength = -1; 82 } 83 } 84 85 /** 86 * A decay, should be a value between zero and one. 87 * @param newDecay the new decay (preferably between zero and one). 88 */ 89 public void setDecay(double newDecay){ 90 this.decay = (float) newDecay; 91 } 92 93 @Override 94 public boolean process(AudioEvent audioEvent) { 95 float[] audioFloatBuffer = audioEvent.getFloatBuffer(); 96 int overlap = audioEvent.getOverlap(); 97 98 for(int i = overlap ; i < audioFloatBuffer.length ; i++){ 99 if(position >= echoBuffer.length){ 100 position = 0; 101 } 102 103 //output is the input added with the decayed echo 104 audioFloatBuffer[i] = audioFloatBuffer[i] + echoBuffer[position] * decay; 105 //store the sample in the buffer; 106 echoBuffer[position] = audioFloatBuffer[i]; 107 108 position++; 109 } 110 111 applyNewEchoLength(); 112 113 return true; 114 } 115 116 @Override 117 public void processingFinished() { 118 } 119 }