plectrum

Plectrum: instrument tuner for Android
Log | Files | Refs | README | LICENSE

AmplitudeLFO.java (1837B)


      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 package be.tarsos.dsp.synthesis;
     25 
     26 import be.tarsos.dsp.AudioEvent;
     27 import be.tarsos.dsp.AudioProcessor;
     28 
     29 public class AmplitudeLFO implements AudioProcessor {
     30 	
     31 	private double frequency;
     32 	private double scaleParameter;
     33 	private double phase;
     34 	
     35 	public AmplitudeLFO(){
     36 		this(1.5,0.75);
     37 	}
     38 	
     39 	public AmplitudeLFO(double frequency, double scaleParameter){
     40 		this.frequency = frequency;
     41 		this.scaleParameter = scaleParameter;
     42 		phase = 0;
     43 	}
     44 	
     45 
     46 	@Override
     47 	public boolean process(AudioEvent audioEvent) {
     48 		float[] buffer = audioEvent.getFloatBuffer();
     49 		double sampleRate = audioEvent.getSampleRate();
     50 		double twoPiF = 2 * Math.PI * frequency;
     51 		double time = 0;
     52 		for(int i = 0 ; i < buffer.length ; i++){
     53 			time = i / sampleRate;
     54 			float gain =  (float) (scaleParameter * Math.sin(twoPiF * time + phase));
     55 			buffer[i] = gain * buffer[i];
     56 		}
     57 		phase = twoPiF * buffer.length / sampleRate + phase;
     58 		return true;
     59 	}
     60 
     61 	@Override
     62 	public void processingFinished() {
     63 	}
     64 
     65 }