samer@0: /* samer@0: * AudioSource.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.audio; samer@0: import samer.tools.*; samer@0: samer@0: /** samer@0: General interface for objects that can supply an samer@0: audio stream. samer@0: */ samer@0: samer@0: public interface AudioSource samer@0: { samer@0: boolean isOpen(); samer@0: void open() throws Exception; samer@0: void close(); samer@0: void dispose(); samer@0: samer@0: /** return a task which reads samples into the given buffer samer@0: * The idea is that the audio source can choose the right samer@0: * kind of reader depending on the format of the audio stream, samer@0: * and then handle the conversion to doubles automatically samer@0: */ samer@0: Task reader(double buf[], int off, int len); samer@0: Task reader(float buf[], int off, int len); samer@0: samer@1: int getChannels(); samer@1: float getRate(); samer@1: samer@0: public static class Util { samer@0: /* NB. These copying functions allow NEGATIVE offset (off<0). The semantics samer@0: * of this is are that n values are copied from source to destination, writing samer@0: * into the destination starting at the negative offset, but that values before samer@0: * index 0 will never be accessed, hence, they are not actually copied. Only samer@0: * the last n-off values will be available in dst starting from index 0. samer@0: * This is useful as it allows block-wise audio input where the block lenght samer@0: * is smaller than the step length. samer@0: */ samer@0: samer@0: public static void shortToDoubleMixDown(byte [] src, double [] dst, int off, int n, int m) { samer@0: double a = (1.0/(m*32768.0)); // scaling factor including division by mixdown factor m samer@0: int skip = off<0 ? -off : 0; samer@0: for (int i=2*m*skip, j=off+skip; j