Daubechies4WaveletCoder.java (2068B)
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.wavelet.lift; 25 26 import java.util.Arrays; 27 28 import be.tarsos.dsp.AudioEvent; 29 import be.tarsos.dsp.AudioProcessor; 30 31 public class Daubechies4WaveletCoder implements AudioProcessor { 32 33 private final Daubechies4Wavelet transform; 34 35 private int compression; 36 37 public Daubechies4WaveletCoder() { 38 this(16); 39 } 40 41 public Daubechies4WaveletCoder(int compression) { 42 transform = new Daubechies4Wavelet(); 43 this.compression = compression; 44 } 45 46 @Override 47 public boolean process(AudioEvent audioEvent) { 48 49 float[] audioBuffer = audioEvent.getFloatBuffer(); 50 float[] sortBuffer = new float[audioBuffer.length]; 51 52 transform.forwardTrans(audioBuffer); 53 54 for (int i = 0; i < sortBuffer.length; i++) { 55 sortBuffer[i] = Math.abs(audioBuffer[i]); 56 } 57 Arrays.sort(sortBuffer); 58 59 double threshold = sortBuffer[compression]; 60 61 for (int i = 0; i < audioBuffer.length; i++) { 62 if (Math.abs(audioBuffer[i]) <= threshold) { 63 audioBuffer[i] = 0; 64 } 65 } 66 return true; 67 } 68 69 @Override 70 public void processingFinished() { 71 72 } 73 74 public void setCompression(int compression) { 75 this.compression = compression; 76 } 77 78 public int getCompression() { 79 return this.compression; 80 } 81 82 }