fiore@3
|
1 /*
|
fiore@3
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
fiore@3
|
3
|
fiore@3
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
fiore@3
|
5
|
fiore@3
|
6 This program is free software: you can redistribute it and/or modify
|
fiore@3
|
7 it under the terms of the GNU General Public License as published by
|
fiore@3
|
8 the Free Software Foundation, either version 3 of the License, or
|
fiore@3
|
9 (at your option) any later version.
|
fiore@3
|
10
|
fiore@3
|
11 This program is distributed in the hope that it will be useful,
|
fiore@3
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
fiore@3
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
fiore@3
|
14 GNU General Public License for more details.
|
fiore@3
|
15
|
fiore@3
|
16 You should have received a copy of the GNU General Public License
|
fiore@3
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
fiore@3
|
18 */
|
fiore@3
|
19
|
fiore@3
|
20 package uk.ac.qmul.eecs.ccmi.speech;
|
fiore@3
|
21
|
fiore@3
|
22 import java.io.ByteArrayInputStream;
|
fiore@3
|
23 import java.io.ByteArrayOutputStream;
|
fiore@3
|
24 import java.io.IOException;
|
fiore@3
|
25
|
fiore@3
|
26 import javax.sound.sampled.AudioFileFormat;
|
fiore@3
|
27 import javax.sound.sampled.AudioFormat;
|
fiore@3
|
28 import javax.sound.sampled.AudioInputStream;
|
fiore@3
|
29 import javax.sound.sampled.AudioSystem;
|
fiore@3
|
30 import javax.sound.sampled.UnsupportedAudioFileException;
|
fiore@3
|
31
|
fiore@3
|
32
|
fiore@3
|
33 import net.beadsproject.beads.core.AudioContext;
|
fiore@3
|
34 import net.beadsproject.beads.core.Bead;
|
fiore@3
|
35 import net.beadsproject.beads.data.Sample;
|
fiore@3
|
36 import net.beadsproject.beads.ugens.Gain;
|
fiore@3
|
37 import net.beadsproject.beads.ugens.Panner;
|
fiore@3
|
38 import net.beadsproject.beads.ugens.SamplePlayer;
|
fiore@3
|
39
|
fiore@3
|
40 import com.sun.speech.freetts.audio.AudioPlayer;
|
fiore@3
|
41
|
fiore@5
|
42 /**
|
fiore@5
|
43 * An implementation of {@code AudioPlayer} using the {@code Beads}
|
fiore@5
|
44 * library.
|
fiore@5
|
45 */
|
fiore@3
|
46 public class BeadsAudioPlayer implements AudioPlayer {
|
fiore@3
|
47 public BeadsAudioPlayer(){
|
fiore@3
|
48 format = new AudioFormat(8000f, 16, 1, true, true);
|
fiore@3
|
49 ac = new AudioContext(format);
|
fiore@3
|
50 volume = 1.0f;
|
fiore@3
|
51 monitor = new Object();
|
fiore@3
|
52 }
|
fiore@3
|
53
|
fiore@3
|
54 public BeadsAudioPlayer(float vol, float pan){
|
fiore@3
|
55 this();
|
fiore@3
|
56 volume = vol;
|
fiore@3
|
57 this.pan = pan;
|
fiore@3
|
58 }
|
fiore@3
|
59
|
fiore@3
|
60
|
fiore@3
|
61 @Override
|
fiore@3
|
62 public void begin(int size) {
|
fiore@3
|
63 buffer = new byte[size];
|
fiore@3
|
64 bufferPosition = 0;
|
fiore@3
|
65 ac = new AudioContext();
|
fiore@3
|
66 }
|
fiore@3
|
67
|
fiore@3
|
68 @Override
|
fiore@3
|
69 public void cancel() {
|
fiore@3
|
70
|
fiore@3
|
71 }
|
fiore@3
|
72
|
fiore@3
|
73 @Override
|
fiore@3
|
74 public void close() {
|
fiore@3
|
75 ac.stop();
|
fiore@3
|
76 }
|
fiore@3
|
77
|
fiore@3
|
78 @Override
|
fiore@3
|
79 public boolean drain() {
|
fiore@3
|
80 synchronized(monitor){
|
fiore@3
|
81 if(!finished)
|
fiore@3
|
82 try {
|
fiore@3
|
83 monitor.wait();
|
fiore@3
|
84 } catch (InterruptedException e) {
|
fiore@3
|
85 throw new RuntimeException(e);
|
fiore@3
|
86 }
|
fiore@3
|
87 finished = false;
|
fiore@3
|
88 }
|
fiore@3
|
89 return false;
|
fiore@3
|
90 }
|
fiore@3
|
91
|
fiore@3
|
92 @Override
|
fiore@3
|
93 public boolean end() {
|
fiore@3
|
94 ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
|
fiore@3
|
95 AudioInputStream audioStream = new AudioInputStream(stream, format, bufferPosition/format.getFrameSize());
|
fiore@3
|
96 ByteArrayOutputStream out = new ByteArrayOutputStream();
|
fiore@3
|
97 Sample sample = null;
|
fiore@3
|
98 try {
|
fiore@3
|
99 AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE,out);
|
fiore@3
|
100 sample = new Sample(new ByteArrayInputStream(out.toByteArray()));
|
fiore@3
|
101 } catch (IOException e) {
|
fiore@3
|
102 e.printStackTrace();
|
fiore@3
|
103 return false;
|
fiore@3
|
104 } catch (UnsupportedAudioFileException e) {
|
fiore@3
|
105 e.printStackTrace();
|
fiore@3
|
106 return false;
|
fiore@3
|
107 }
|
fiore@3
|
108
|
fiore@3
|
109 SamplePlayer player = new SamplePlayer(ac,sample);
|
fiore@3
|
110 player.setKillOnEnd(true);
|
fiore@3
|
111 Gain g = new Gain(ac,1,volume);
|
fiore@3
|
112 g.addInput(player);
|
fiore@3
|
113 final Panner panner = new Panner(ac,pan);
|
fiore@3
|
114 panner.addInput(g);
|
fiore@3
|
115 player.setKillListener(new Bead(){
|
fiore@3
|
116 @Override
|
fiore@3
|
117 protected void messageReceived(Bead message){
|
fiore@3
|
118 panner.kill();
|
fiore@3
|
119 synchronized(monitor){
|
fiore@3
|
120 finished = true;
|
fiore@3
|
121 monitor.notify();
|
fiore@3
|
122 }
|
fiore@3
|
123 }
|
fiore@3
|
124 });
|
fiore@3
|
125
|
fiore@3
|
126 /* starts playing the sample */
|
fiore@3
|
127 ac.out.addInput(panner);
|
fiore@3
|
128 ac.start();
|
fiore@3
|
129 return true;
|
fiore@3
|
130 }
|
fiore@3
|
131
|
fiore@3
|
132 @Override
|
fiore@3
|
133 public AudioFormat getAudioFormat() {
|
fiore@3
|
134 return format;
|
fiore@3
|
135 }
|
fiore@3
|
136
|
fiore@3
|
137 @Override
|
fiore@3
|
138 public long getTime() {
|
fiore@3
|
139 return -1L;
|
fiore@3
|
140 }
|
fiore@3
|
141
|
fiore@3
|
142 @Override
|
fiore@3
|
143 public float getVolume() {
|
fiore@3
|
144 return volume;
|
fiore@3
|
145 }
|
fiore@3
|
146
|
fiore@3
|
147 @Override
|
fiore@3
|
148 public void pause() {
|
fiore@3
|
149
|
fiore@3
|
150 }
|
fiore@3
|
151
|
fiore@3
|
152 @Override
|
fiore@3
|
153 public void reset() {
|
fiore@3
|
154
|
fiore@3
|
155 }
|
fiore@3
|
156
|
fiore@3
|
157 @Override
|
fiore@3
|
158 public void resetTime() {
|
fiore@3
|
159
|
fiore@3
|
160 }
|
fiore@3
|
161
|
fiore@3
|
162 @Override
|
fiore@3
|
163 public void resume() {
|
fiore@3
|
164
|
fiore@3
|
165 }
|
fiore@3
|
166
|
fiore@3
|
167 @Override
|
fiore@3
|
168 public void setAudioFormat(AudioFormat format) {
|
fiore@3
|
169 this.format = format;
|
fiore@3
|
170 ac.setInputAudioFormat(format);
|
fiore@3
|
171 }
|
fiore@3
|
172
|
fiore@3
|
173 @Override
|
fiore@3
|
174 public void setVolume(float vol) {
|
fiore@3
|
175 volume = vol;
|
fiore@3
|
176 }
|
fiore@3
|
177
|
fiore@3
|
178 public void setPan(float pan){
|
fiore@3
|
179 this.pan = pan;
|
fiore@3
|
180 }
|
fiore@3
|
181
|
fiore@3
|
182 public float getPan(){
|
fiore@3
|
183 return pan;
|
fiore@3
|
184 }
|
fiore@3
|
185
|
fiore@3
|
186 @Override
|
fiore@3
|
187 public void showMetrics() {
|
fiore@3
|
188
|
fiore@3
|
189 }
|
fiore@3
|
190
|
fiore@3
|
191 @Override
|
fiore@3
|
192 public void startFirstSampleTimer() {
|
fiore@3
|
193
|
fiore@3
|
194 }
|
fiore@3
|
195
|
fiore@3
|
196 @Override
|
fiore@3
|
197 public boolean write(byte[] audioData) {
|
fiore@3
|
198 return write(audioData,0,audioData.length);
|
fiore@3
|
199 }
|
fiore@3
|
200
|
fiore@3
|
201 @Override
|
fiore@3
|
202 public boolean write(byte[] audioData, int offset, int size) {
|
fiore@3
|
203 System.arraycopy(audioData, offset, buffer, bufferPosition, size);
|
fiore@3
|
204 bufferPosition += size;
|
fiore@3
|
205 return true;
|
fiore@3
|
206 }
|
fiore@3
|
207
|
fiore@3
|
208 private byte[] buffer;
|
fiore@3
|
209 private int bufferPosition;
|
fiore@3
|
210 private AudioFormat format;
|
fiore@3
|
211 private float volume;
|
fiore@3
|
212 private float pan;
|
fiore@3
|
213 private Object monitor;
|
fiore@3
|
214 private boolean finished;
|
fiore@3
|
215 private AudioContext ac;
|
fiore@3
|
216
|
fiore@3
|
217 }
|