plectrum

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

BitDepthProcessor.java (1809B)


      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;
     25 
     26 /**
     27  * Can be used to show the effect of bit depth modification in real-time.
     28  * It simply transforms every sample to the requested bit depth.
     29  * @author Joren Six
     30  */
     31 public class BitDepthProcessor implements AudioProcessor {
     32 
     33 	int bitDepth = 16;
     34 			
     35 	public void setBitDepth(int newBitDepth){
     36 		this.bitDepth = newBitDepth;
     37 	}
     38 	
     39 	public int getBitDepth(){
     40 		return this.bitDepth;
     41 	}
     42 	
     43 	
     44 	@Override
     45 	public boolean process(AudioEvent audioEvent) {
     46 		float[] buffer = audioEvent.getFloatBuffer();
     47 		//For e.g. a bith depth of 3, the factor is
     48 		// 2^3 - 1 = 7
     49 		float factor = (float) Math.pow(2, bitDepth)/2.0f - 1;
     50 		
     51 		for(int i = 0 ; i < buffer.length ; i++){
     52 			//the float is scaled to the bith depth
     53 			// e.g. if the bit depth is 3 and the value is 0.3:
     54 			// ((int)(0.3 * 7)) / 7 = 0.28
     55 			buffer[i]=((int) (buffer[i] * factor))/factor;
     56 		}
     57 		return true;
     58 	}
     59 
     60 	@Override
     61 	public void processingFinished() {
     62 
     63 	}	
     64 }