comparison java/src/uk/ac/qmul/eecs/ccmi/sound/BeadsSound.java @ 0:9418ab7b7f3f

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children 9e67171477bc
comparison
equal deleted inserted replaced
-1:000000000000 0:9418ab7b7f3f
1 /*
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
3
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package uk.ac.qmul.eecs.ccmi.sound;
21
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import net.beadsproject.beads.core.AudioContext;
27 import net.beadsproject.beads.core.Bead;
28 import net.beadsproject.beads.core.UGen;
29 import net.beadsproject.beads.data.Sample;
30 import net.beadsproject.beads.data.SampleManager;
31 import net.beadsproject.beads.ugens.Gain;
32 import net.beadsproject.beads.ugens.SamplePlayer;
33 import net.beadsproject.beads.ugens.SamplePlayer.LoopType;
34
35 /**
36 * The Sound interface implementation using the Beads library.
37 * For more info abou the library see http://www.beadsproject.net/.
38 */
39 class BeadsSound implements Sound {
40
41 public BeadsSound(){
42 ac = new AudioContext();
43 playerListeners = new HashMap<SoundEvent,PlayerListener>();
44 loopPlayers = new HashMap<SoundEvent,UGen>();
45
46 /* pre load all the sample to avoid future overhead */
47 for(SoundEvent key : AudioResourcesService.eventTypes()){
48 SampleManager.sample(AudioResourcesService.getAudiofile(key));
49 }
50 ac.start();
51 }
52
53
54 public void play(InputStream sound, final PlayerListener playerListener) {
55 if(mute)
56 return;
57 SamplePlayer player;
58 Sample sample = null;
59 if(sound != null)
60 sample = SampleManager.sample(sound);
61 if(sample == null){
62 /* we got problems retrieving the sample to play
63 * call the playerListener method and return */
64 if(playerListener != null)
65 playerListener.playEnded();
66 return;
67 }
68 player = new SamplePlayer(ac,sample);
69 player.setKillOnEnd(true);
70 final Gain g = new Gain(ac,1,MASTER_VOLUME);
71 g.addInput(player);
72
73 Bead killBill;
74 if(playerListener != null){
75 killBill = new Bead(){
76 @Override
77 protected void messageReceived(Bead message){
78 playerListener.playEnded();
79 g.kill();
80 playingBead = null;
81 }
82 };
83 }else{
84 killBill = new Bead(){
85 @Override
86 protected void messageReceived(Bead message){
87 g.kill();
88 playingBead = null;
89 }
90 };
91 }
92
93 player.setKillListener(killBill);
94 playingBead = g;
95 ac.out.addInput(g);
96 }
97
98 public void play(InputStream sound){
99 play(sound, null);
100 }
101
102 @Override
103 public void play(final SoundEvent evt ){
104 if(evt == null){
105 InputStream s = null;
106 play(s);
107 }else
108 play(evt,playerListeners.get(evt));
109 }
110
111 public void play(SoundEvent evt, PlayerListener playerListener){
112 if(evt == null){
113 InputStream s = null;
114 play(s,playerListener);
115 }else
116 play(AudioResourcesService.getAudiofile(evt),playerListener);
117 }
118
119 public void stop(){
120 if(mute)
121 return;
122 if(playingBead != null){
123 playingBead.setKillListener(null);
124 playingBead.kill();
125 }
126 }
127
128 public void loadSound(InputStream sound){
129 SampleManager.sample(sound);
130 }
131
132 @Override
133 public void startLoop(SoundEvent action) {
134 if(mute)
135 return;
136 Sample sample = null;
137 if(action != null){
138 InputStream samplePath = AudioResourcesService.getAudiofile(action);
139 if(samplePath != null)
140 sample = SampleManager.sample(samplePath);
141 }
142 if(sample == null)
143 return;
144 SamplePlayer player = new SamplePlayer(ac,sample);
145 player.setLoopType(LoopType.LOOP_FORWARDS);
146 Gain g = new Gain(ac,1,MASTER_VOLUME);
147 g.addInput(player);
148 ac.out.addInput(g);
149 loopPlayers.put(action, g);
150 }
151
152 @Override
153 public void stopLoop(SoundEvent action) {
154 UGen g = loopPlayers.get(action);
155 if(g != null){
156 g.kill();
157 loopPlayers.remove(action);
158 }
159 }
160
161 @Override
162 public void setPlayerListener(PlayerListener listener, SoundEvent type){
163 playerListeners.put(type, listener);
164 }
165
166 @Override
167 public void unsetPlayerListener(SoundEvent type){
168 playerListeners.remove(type);
169 }
170
171 @Override
172 public void setMuted(boolean mute){
173 this.mute = mute;
174 }
175
176 @Override
177 public void dispose(){
178 ac.stop();
179 }
180
181 private AudioContext ac;
182 private Bead playingBead;
183 private Map<SoundEvent,PlayerListener> playerListeners;
184 private Map<SoundEvent,UGen> loopPlayers;
185 private static final float MASTER_VOLUME = 0.35f;
186 private boolean mute;
187 }