HaarWaveletCoder.java (2043B)
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; 25 26 import java.util.Arrays; 27 28 import be.tarsos.dsp.AudioEvent; 29 import be.tarsos.dsp.AudioProcessor; 30 31 public class HaarWaveletCoder implements AudioProcessor{ 32 33 private final HaarWaveletTransform transform; 34 35 private int compression; 36 37 public HaarWaveletCoder(){ 38 this(16); 39 } 40 41 public HaarWaveletCoder(int compression){ 42 transform = new HaarWaveletTransform(); 43 this.compression = compression; 44 } 45 46 47 @Override 48 public boolean process(AudioEvent audioEvent) { 49 50 float[] audioBuffer = audioEvent.getFloatBuffer(); 51 float[] sortBuffer = new float[audioBuffer.length]; 52 transform.transform(audioEvent.getFloatBuffer()); 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 67 return true; 68 } 69 70 @Override 71 public void processingFinished() { 72 73 } 74 75 public void setCompression(int compression){ 76 this.compression = compression; 77 } 78 79 public int getCompression(){ 80 return this.compression; 81 } 82 83 }