samer@0
|
1 /*
|
samer@0
|
2 * AudioSink.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
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@0
|
12 package samer.audio;
|
samer@0
|
13 import samer.tools.*;
|
samer@0
|
14
|
samer@0
|
15 /**
|
samer@0
|
16 General interface for objects that accept a stream of
|
samer@0
|
17 samples.
|
samer@0
|
18 */
|
samer@0
|
19
|
samer@0
|
20 public interface AudioSink
|
samer@0
|
21 {
|
samer@0
|
22 boolean isOpen();
|
samer@0
|
23 void open() throws Exception;
|
samer@0
|
24 void close();
|
samer@0
|
25 void dispose();
|
samer@0
|
26
|
samer@0
|
27 /** Return a task which takes samples from the given buffer
|
samer@0
|
28 * The idea is that the audio sink can choose the right
|
samer@0
|
29 * kind of writer depending on the format of the audio stream,
|
samer@0
|
30 * and then handle any conversions automatically.
|
samer@0
|
31 */
|
samer@0
|
32 Task writer(double buf[], int off, int len);
|
samer@0
|
33 Task writer(float buf[], int off, int len);
|
samer@0
|
34
|
samer@0
|
35 public static class Util {
|
samer@0
|
36 public static void doubleToShort(double[] src, byte [] dst, int off, int n, double k) {
|
samer@0
|
37 k*=32768;
|
samer@0
|
38 for (int i=0, j=off; j<n+off; j++) {
|
samer@0
|
39 int y = (int)(k*src[j]);
|
samer@0
|
40 dst[i++] = (byte)(y&0xff);
|
samer@0
|
41 dst[i++] = (byte)(y>>8);
|
samer@0
|
42 }
|
samer@0
|
43 }
|
samer@0
|
44
|
samer@0
|
45 public static void doubletoByte(double [] src, byte[] dst, int off, int n, double k) {
|
samer@0
|
46 k*=128;
|
samer@0
|
47 for (int i=0, j=off; j<n+off; j++) {
|
samer@0
|
48 int y = (int)(k*src[j]) + 128;
|
samer@0
|
49 dst[i++] = (byte)(y&0xff);
|
samer@0
|
50 }
|
samer@0
|
51 }
|
samer@0
|
52
|
samer@0
|
53 public static void floatToShort(float [] src, byte [] dst, int off, int n, float k) {
|
samer@0
|
54 k*=32768;
|
samer@0
|
55 for (int i=0, j=off; j<n+off; j++) {
|
samer@0
|
56 int y = (int)(k*src[j]);
|
samer@0
|
57 dst[i++] = (byte)(y&0xff);
|
samer@0
|
58 dst[i++] = (byte)(y>>8);
|
samer@0
|
59 }
|
samer@0
|
60 }
|
samer@0
|
61
|
samer@0
|
62 public static void floatToByte(float [] src, byte [] dst, int off, int n, float k) {
|
samer@0
|
63 k*=128;
|
samer@0
|
64 for (int i=0, j=off; j<n+off; j++) {
|
samer@0
|
65 int y = (int)(128.0*src[j]) + 128;
|
samer@0
|
66 dst[i++] = (byte)(y&0xff);
|
samer@0
|
67 }
|
samer@0
|
68 }
|
samer@0
|
69
|
samer@0
|
70 }
|
samer@0
|
71 }
|
samer@0
|
72
|