Mercurial > hg > ishara
comparison audio/java/AudioSink.java @ 0:672052bd81f8
Initial partial import.
author | samer |
---|---|
date | Wed, 19 Dec 2012 22:38:28 +0000 |
parents | |
children | 63cefb01cbab |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:672052bd81f8 |
---|---|
1 /* | |
2 * AudioSink.java | |
3 * | |
4 * Copyright (c) 2000, Samer Abdallah, King's College London. | |
5 * All rights reserved. | |
6 * | |
7 * This software is provided AS iS and WITHOUT ANY WARRANTY; | |
8 * without even the implied warranty of MERCHANTABILITY or | |
9 * FITNESS FOR A PARTICULAR PURPOSE. | |
10 */ | |
11 | |
12 package samer.audio.alt; | |
13 import javax.sound.sampled.AudioFormat; | |
14 | |
15 /** | |
16 General interface for objects that accept a stream of | |
17 samples. | |
18 */ | |
19 | |
20 public abstract class AudioSink | |
21 { | |
22 AudioFormat format; | |
23 double scale; | |
24 | |
25 public AudioSink(AudioFormat f) { format=f; } | |
26 public AudioFormat getFormat() { return format; } | |
27 | |
28 public double getScale() { return scale; } | |
29 public void setScale(double s) { scale=s; } | |
30 | |
31 public abstract void dispose(); | |
32 public abstract void start(); | |
33 public abstract void stop(); | |
34 public abstract int bwrite(byte [] bbuf, int offs, int len) throws Exception; | |
35 | |
36 /** Return a task which takes samples from the given buffer | |
37 * The idea is that the audio sink can choose the right | |
38 * kind of writer depending on the format of the audio stream, | |
39 * and then handle any conversions automatically. | |
40 * Should return the number of samples NOT written. | |
41 */ | |
42 | |
43 public Writer writer(int len) { | |
44 return new Writer(len,format.getSampleSizeInBits()/8); | |
45 } | |
46 | |
47 public class Writer { | |
48 byte[] bbuf; | |
49 int bps; | |
50 Converter conv; | |
51 | |
52 public Writer(int len, int bps) { | |
53 this.bps=bps; | |
54 bbuf=new byte[len*bps]; | |
55 conv=getConverter(bbuf,bps); | |
56 } | |
57 | |
58 public int write(double dbuf[]) throws Exception { return write(dbuf,0,dbuf.length); } | |
59 public int write(double dbuf[], int off, int len) throws Exception { | |
60 conv.convert(dbuf,off,len); | |
61 int rem=len*bps, pos=0; | |
62 while (rem>0) { | |
63 int count = bwrite(bbuf, pos, rem); | |
64 if (count<=0) { return rem; } | |
65 rem -= count; pos += count; | |
66 } | |
67 return 0; | |
68 } | |
69 } | |
70 | |
71 private interface Converter { public void convert(double [] dbuf, int offset, int count); } | |
72 private Converter getConverter(final byte [] b,final int bps) { | |
73 switch (bps) { | |
74 case 1: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToByte(d,b,i,n,scale); } }; | |
75 case 2: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToShort(d,b,i,n,scale); } }; | |
76 case 3: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToMedium(d,b,i,n,scale); } }; | |
77 case 4: return new Converter() { public void convert(double[] d,int i, int n) { Util.doubleToInt(d,b,i,n,scale); } }; | |
78 } | |
79 throw new Error("Unrecognised sample format"); | |
80 } | |
81 | |
82 protected static void print(String msg) { System.out.println(msg); } | |
83 } | |
84 |