annotate java/src/uk/ac/qmul/eecs/ccmi/speech/BeadsAudioPlayer.java @ 1:e3935c01cde2 tip

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