Mercurial > hg > ishara
view audio/java/AudioSink.java @ 61:eff6bddf82e3 tip
Finally implemented perceptual brightness thing.
author | samer |
---|---|
date | Sun, 11 Oct 2015 10:20:42 +0100 |
parents | 63cefb01cbab |
children |
line wrap: on
line source
/* * AudioSink.java * * Copyright (c) 2000, Samer Abdallah, King's College London. * All rights reserved. * * This software is provided AS iS and WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. */ package ishara.audio; import javax.sound.sampled.AudioFormat; /** General interface for objects that accept a stream of samples. */ public abstract class AudioSink { AudioFormat format; double scale; public AudioSink(AudioFormat f) { format=f; } public AudioFormat getFormat() { return format; } public double getScale() { return scale; } public void setScale(double s) { scale=s; } public abstract void dispose(); public abstract void start(); public abstract void stop(); public abstract int bwrite(byte [] bbuf, int offs, int len) throws Exception; /** Return a task which takes samples from the given buffer * The idea is that the audio sink can choose the right * kind of writer depending on the format of the audio stream, * and then handle any conversions automatically. * Should return the number of samples NOT written. */ public Writer writer(int len) { return new Writer(len,format.getSampleSizeInBits()/8); } public class Writer { byte[] bbuf; int bps; Converter conv; public Writer(int len, int bps) { this.bps=bps; bbuf=new byte[len*bps]; conv=getConverter(bbuf,bps); } public int write(double dbuf[]) throws Exception { return write(dbuf,0,dbuf.length); } public int write(double dbuf[], int off, int len) throws Exception { conv.convert(dbuf,off,len); int rem=len*bps, pos=0; while (rem>0) { int count = bwrite(bbuf, pos, rem); if (count<=0) { return rem; } rem -= count; pos += count; } return 0; } } private interface Converter { public void convert(double [] dbuf, int offset, int count); } private Converter getConverter(final byte [] b,final int bps) { switch (bps) { case 1: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToByte(d,b,i,n,scale); } }; case 2: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToShort(d,b,i,n,scale); } }; case 3: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToMedium(d,b,i,n,scale); } }; case 4: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToInt(d,b,i,n,scale); } }; } throw new Error("Unrecognised sample format"); } protected static void print(String msg) { System.out.println(msg); } }