/
RadioStreamTargetDataLine.java
183 lines (153 loc) · 5.2 KB
/
RadioStreamTargetDataLine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (c) 2004-2010 Peter Johan Salomonsen (http://www.petersalomonsen.com)
*
* http://www.frinika.com
*
* This file is part of Frinika.
*
* Frinika is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Frinika is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Frinika; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.frinika.radio;
import com.frinika.global.FrinikaConfig;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.Control;
import javax.sound.sampled.Control.Type;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
/**
*
* @author Peter Johan Salomonsen
*/
public class RadioStreamTargetDataLine implements TargetDataLine {
PipedInputStream queue = null;
PipedOutputStream pos = null;
int bufferSize;
public void open(AudioFormat format, int bufferSize) throws LineUnavailableException {
queue = new PipedInputStream();
this.bufferSize = bufferSize;
try {
pos = new PipedOutputStream(queue);
} catch (IOException ex) {
throw new LineUnavailableException(ex.toString());
}
}
public void open(AudioFormat format) throws LineUnavailableException {
open(format, 1024*1024*8);
}
public int read(byte[] b, int off, int len) {
try {
return queue.read(b, off, len);
} catch (IOException ex) {
return -1;
}
}
public void drain() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void flush() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void start() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void stop() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isRunning() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isActive() {
throw new UnsupportedOperationException("Not supported yet.");
}
public AudioFormat getFormat() {
return new AudioFormat((float) FrinikaConfig.sampleRate,16,2,true,false);
}
public int getBufferSize() {
return bufferSize;
}
public int available() {
try {
return queue.available();
} catch (IOException ex) {
Logger.getLogger(RadioStreamTargetDataLine.class.getName()).log(Level.SEVERE, null, ex);
return 0;
}
}
public int getFramePosition() {
throw new UnsupportedOperationException("Not supported yet.");
}
public long getLongFramePosition() {
throw new UnsupportedOperationException("Not supported yet.");
}
public long getMicrosecondPosition() {
throw new UnsupportedOperationException("Not supported yet.");
}
public float getLevel() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Info getLineInfo() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void open() throws LineUnavailableException {
open(getFormat());
}
public void close() {
try {
queue.close();
} catch (IOException ex) {
Logger.getLogger(RadioStreamTargetDataLine.class.getName()).log(Level.SEVERE, null, ex);
}
try {
pos.close();
} catch (IOException ex) {
Logger.getLogger(RadioStreamTargetDataLine.class.getName()).log(Level.SEVERE, null, ex);
}
queue = null;
pos = null;
}
public boolean isOpen() {
return queue !=null;
}
public Control[] getControls() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isControlSupported(Type control) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Control getControl(Type control) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void addLineListener(LineListener listener) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void removeLineListener(LineListener listener) {
throw new UnsupportedOperationException("Not supported yet.");
}
void addFrame(byte[] b) {
try {
if(queue!=null && queue.available()<(bufferSize-4))
{
pos.write(b);
}
} catch (IOException ex) {
}
}
}