HaarWaveletFileReader.java (2612B)
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.io.FileInputStream; 27 import java.io.FileNotFoundException; 28 import java.io.IOException; 29 30 import be.tarsos.dsp.AudioEvent; 31 import be.tarsos.dsp.AudioProcessor; 32 33 public class HaarWaveletFileReader implements AudioProcessor { 34 35 private final int compression; 36 private FileInputStream rawInputStream; 37 38 public HaarWaveletFileReader(String fileName, int compression){ 39 this.compression = compression; 40 try { 41 this.rawInputStream = new FileInputStream(fileName); 42 } catch (FileNotFoundException e) { 43 this.rawInputStream = null; 44 } 45 } 46 47 @Override 48 public boolean process(AudioEvent audioEvent) { 49 50 float[] audioBuffer = new float[32]; 51 52 byte[] byteBuffer = new byte[(32-compression)*2]; 53 int placesWithZero = 0; 54 try { 55 rawInputStream.read(byteBuffer); 56 placesWithZero += rawInputStream.read(); 57 placesWithZero += (rawInputStream.read()<<8); 58 placesWithZero += (rawInputStream.read()<<16); 59 placesWithZero += (rawInputStream.read()<<24); 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 64 int byteBufferIndex = 0; 65 for(int i = 0 ; i < audioBuffer.length ; i++){ 66 if((placesWithZero & (1<<i)) != 1<<i){ 67 int x = byteBuffer[byteBufferIndex] & 0xFF ; 68 byteBufferIndex++; 69 int y = byteBuffer[byteBufferIndex] << 8 ; 70 byteBufferIndex++; 71 x = x | y; 72 float value = x / 32767.0f; 73 audioBuffer[i] = value; 74 } 75 } 76 audioEvent.setFloatBuffer(audioBuffer); 77 78 79 boolean more=true; 80 try { 81 more = rawInputStream.available() > 0; 82 } catch (IOException e) { 83 84 e.printStackTrace(); 85 } 86 87 return more; 88 } 89 90 @Override 91 public void processingFinished() { 92 try { 93 rawInputStream.close(); 94 } catch (IOException e) { 95 e.printStackTrace(); 96 } 97 } 98 99 }