annotate java/src/uk/ac/qmul/eecs/ccmi/sound/BeadsSound.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.sound;
f@0 21
f@0 22 import java.io.InputStream;
f@0 23 import java.util.EnumMap;
f@0 24 import java.util.Map;
f@0 25
f@0 26 import net.beadsproject.beads.core.AudioContext;
f@0 27 import net.beadsproject.beads.core.Bead;
f@0 28 import net.beadsproject.beads.core.UGen;
f@0 29 import net.beadsproject.beads.data.Sample;
f@0 30 import net.beadsproject.beads.data.SampleManager;
f@0 31 import net.beadsproject.beads.ugens.Gain;
f@0 32 import net.beadsproject.beads.ugens.SamplePlayer;
f@0 33 import net.beadsproject.beads.ugens.SamplePlayer.LoopType;
f@0 34
f@0 35 /**
f@0 36 * The Sound interface implementation using the Beads library.
f@0 37 * For more info about the library see http://www.beadsproject.net/.
f@0 38 */
f@0 39 class BeadsSound implements Sound {
f@0 40
f@0 41 public BeadsSound(){
f@0 42 ac = new AudioContext();
f@0 43 playerListeners = new EnumMap<SoundEvent,PlayerListener>(SoundEvent.class);
f@0 44 loopPlayers = new EnumMap<SoundEvent,UGen>(SoundEvent.class);
f@0 45
f@0 46 /* pre load all the sample to avoid future overhead */
f@0 47 for(SoundEvent key : AudioResourcesService.eventTypes()){
f@0 48 SampleManager.sample(AudioResourcesService.getAudiofile(key));
f@0 49 }
f@0 50 ac.start();
f@0 51 }
f@0 52
f@0 53
f@0 54 public void play(InputStream sound, final PlayerListener playerListener) {
f@0 55 if(mute)
f@0 56 return;
f@0 57 SamplePlayer player;
f@0 58 Sample sample = null;
f@0 59 if(sound != null)
f@0 60 sample = SampleManager.sample(sound);
f@0 61 if(sample == null){
f@0 62 /* we got problems retrieving the sample to play
f@0 63 * call the playerListener method and return */
f@0 64 if(playerListener != null)
f@0 65 playerListener.playEnded();
f@0 66 return;
f@0 67 }
f@0 68 player = new SamplePlayer(ac,sample);
f@0 69 player.setKillOnEnd(true);
f@0 70 final Gain g = new Gain(ac,1,MASTER_VOLUME);
f@0 71 g.addInput(player);
f@0 72
f@0 73 Bead killBill;
f@0 74 if(playerListener != null){
f@0 75 killBill = new Bead(){
f@0 76 @Override
f@0 77 protected void messageReceived(Bead message){
f@0 78 playerListener.playEnded();
f@0 79 g.kill();
f@0 80 playingBead = null;
f@0 81 }
f@0 82 };
f@0 83 }else{
f@0 84 killBill = new Bead(){
f@0 85 @Override
f@0 86 protected void messageReceived(Bead message){
f@0 87 g.kill();
f@0 88 playingBead = null;
f@0 89 }
f@0 90 };
f@0 91 }
f@0 92
f@0 93 player.setKillListener(killBill);
f@0 94 playingBead = g;
f@0 95 ac.out.addInput(g);
f@0 96 }
f@0 97
f@0 98 public void play(InputStream sound){
f@0 99 play(sound, null);
f@0 100 }
f@0 101
f@0 102 @Override
f@0 103 public void play(final SoundEvent evt ){
f@0 104 if(evt == null){
f@0 105 InputStream s = null;
f@0 106 play(s);
f@0 107 }else
f@0 108 play(evt,playerListeners.get(evt));
f@0 109 }
f@0 110
f@0 111 public void play(SoundEvent evt, PlayerListener playerListener){
f@0 112 if(evt == null){
f@0 113 InputStream s = null;
f@0 114 play(s,playerListener);
f@0 115 }else
f@0 116 play(AudioResourcesService.getAudiofile(evt),playerListener);
f@0 117 }
f@0 118
f@0 119 public void stop(){
f@0 120 if(mute)
f@0 121 return;
f@0 122 if(playingBead != null){
f@0 123 playingBead.setKillListener(null);
f@0 124 playingBead.kill();
f@0 125 }
f@0 126 }
f@0 127
f@0 128 public void loadSound(InputStream sound){
f@0 129 SampleManager.sample(sound);
f@0 130 }
f@0 131
f@0 132 @Override
f@0 133 public void startLoop(SoundEvent action) {
f@0 134 if(mute)
f@0 135 return;
f@0 136 Sample sample = null;
f@0 137 if(action != null){
f@0 138 InputStream samplePath = AudioResourcesService.getAudiofile(action);
f@0 139 if(samplePath != null)
f@0 140 sample = SampleManager.sample(samplePath);
f@0 141 }
f@0 142 if(sample == null)
f@0 143 return;
f@0 144 SamplePlayer player = new SamplePlayer(ac,sample);
f@0 145 player.setLoopType(LoopType.LOOP_FORWARDS);
f@0 146 Gain g = new Gain(ac,1,MASTER_VOLUME);
f@0 147 g.addInput(player);
f@0 148 ac.out.addInput(g);
f@0 149 loopPlayers.put(action, g);
f@0 150 }
f@0 151
f@0 152 @Override
f@0 153 public void stopLoop(SoundEvent action) {
f@0 154 UGen g = loopPlayers.get(action);
f@0 155 if(g != null){
f@0 156 g.kill();
f@0 157 loopPlayers.remove(action);
f@0 158 }
f@0 159 }
f@0 160
f@0 161 @Override
f@0 162 public void setDefaultPlayerListener(PlayerListener listener, SoundEvent type){
f@0 163 playerListeners.put(type, listener);
f@0 164 }
f@0 165
f@0 166 @Override
f@0 167 public void setMuted(boolean mute){
f@0 168 this.mute = mute;
f@0 169 }
f@0 170
f@0 171 @Override
f@0 172 public void dispose(){
f@0 173 ac.stop();
f@0 174 }
f@0 175
f@0 176 private AudioContext ac;
f@0 177 private Bead playingBead;
f@0 178 private Map<SoundEvent,PlayerListener> playerListeners;
f@0 179 private Map<SoundEvent,UGen> loopPlayers;
f@0 180 private static final float MASTER_VOLUME = 0.35f;
f@0 181 private boolean mute;
f@0 182 }