annotate audio/java/AudioSource.java @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 63cefb01cbab
children
rev   line source
samer@0 1 /*
samer@0 2 * AudioSource.java
samer@0 3 *
samer@0 4 * Copyright (c) 2012, Samer Abdallah
samer@0 5 * All rights reserved.
samer@0 6 *
samer@0 7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 8 * without even the implied warranty of MERCHANTABILITY or
samer@0 9 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 10 */
samer@0 11
samer@46 12 package ishara.audio;
samer@0 13 import javax.sound.sampled.AudioFormat;
samer@0 14
samer@0 15 public abstract class AudioSource
samer@0 16 {
samer@0 17 AudioFormat format;
samer@0 18
samer@0 19 public AudioSource(AudioFormat f) { format=f; }
samer@0 20
samer@0 21 public AudioFormat getFormat() { return format; }
samer@0 22
samer@0 23 public abstract void dispose();
samer@0 24 public abstract void start();
samer@0 25 public abstract void stop();
samer@0 26 public abstract int read(byte [] bbuf, int offset, int len) throws Exception;
samer@0 27
samer@0 28 public Reader reader(int len) {
samer@0 29 return new Reader(len,format.getSampleSizeInBits()/8);
samer@0 30 }
samer@0 31
samer@0 32 public class Reader {
samer@0 33 byte[] bbuf;
samer@0 34 double[] dbuf;
samer@0 35 int rem, numbytes, bps;
samer@0 36 Converter conv;
samer@0 37
samer@0 38 public Reader(int numsamples, int bps) {
samer@0 39 this.bps=bps;
samer@0 40 numbytes=bps*numsamples;
samer@0 41 bbuf=new byte[numbytes];
samer@0 42 dbuf=new double[numsamples];
samer@0 43 conv=getConverter(bbuf,dbuf,bps);
samer@0 44 rem=0;
samer@0 45 }
samer@0 46
samer@0 47 public int unread() { return rem; }
samer@0 48 public double[] next() throws Exception {
samer@0 49 int rem=numbytes, pos=0;
samer@0 50 while (rem>0) {
samer@0 51 int bytesRead=read(bbuf, 0, rem);
samer@0 52 if (bytesRead<=0) { this.rem=rem/bps; return dbuf; }
samer@0 53 int count=bytesRead/bps;
samer@0 54 conv.convert(pos,count);
samer@0 55 pos+=count; rem-=bytesRead;
samer@0 56 }
samer@0 57 return dbuf;
samer@0 58 }
samer@0 59 }
samer@0 60
samer@0 61 protected interface Converter { public void convert(int pos, int count); }
samer@0 62 protected static Converter getConverter(final byte [] bbuf,final double [] dbuf,final int bps) {
samer@0 63 switch (bps) {
samer@0 64 case 1: return new Converter() { public void convert(int pos, int count) { Util.byteToDouble(bbuf,dbuf,pos,count); } };
samer@0 65 case 2: return new Converter() { public void convert(int pos, int count) { Util.shortToDouble(bbuf,dbuf,pos,count); } };
samer@0 66 case 3: return new Converter() { public void convert(int pos, int count) { Util.mediumToDouble(bbuf,dbuf,pos,count); } };
samer@0 67 case 4: return new Converter() { public void convert(int pos, int count) { Util.intToDouble(bbuf,dbuf,pos,count); } };
samer@0 68 }
samer@0 69 throw new Error("Unrecognised sample format");
samer@0 70 }
samer@0 71 public static void print(String msg) { System.out.println(msg); }
samer@0 72 }
samer@0 73