diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/src/uk/ac/qmul/eecs/ccmi/sound/BeadsSound.java	Fri Dec 16 17:35:51 2011 +0000
@@ -0,0 +1,187 @@
+/*  
+ CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
+  
+ Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
+
+ This program 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 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+*/  
+
+package uk.ac.qmul.eecs.ccmi.sound;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import net.beadsproject.beads.core.AudioContext;
+import net.beadsproject.beads.core.Bead;
+import net.beadsproject.beads.core.UGen;
+import net.beadsproject.beads.data.Sample;
+import net.beadsproject.beads.data.SampleManager;
+import net.beadsproject.beads.ugens.Gain;
+import net.beadsproject.beads.ugens.SamplePlayer;
+import net.beadsproject.beads.ugens.SamplePlayer.LoopType;
+
+/**
+ * The Sound interface implementation using the Beads library.
+ * For more info abou the library see http://www.beadsproject.net/.
+ */
+class BeadsSound implements Sound {
+	
+	public BeadsSound(){
+		ac = new AudioContext();
+		playerListeners = new HashMap<SoundEvent,PlayerListener>();
+		loopPlayers = new HashMap<SoundEvent,UGen>();
+		
+		/* pre load all the sample to avoid future overhead */
+		for(SoundEvent key : AudioResourcesService.eventTypes()){
+			SampleManager.sample(AudioResourcesService.getAudiofile(key));
+		}
+		ac.start();
+	}
+	
+
+	public void play(InputStream sound, final PlayerListener playerListener) {
+		if(mute)
+			return;
+		SamplePlayer player;
+		Sample sample = null; 
+		if(sound != null)
+			sample = SampleManager.sample(sound);
+		if(sample == null){
+			/* we got problems retrieving the sample to play 
+			*  call the playerListener method and return */
+			if(playerListener != null)
+				playerListener.playEnded();
+			return;
+		}
+		player = new SamplePlayer(ac,sample);
+		player.setKillOnEnd(true);
+		final Gain g = new Gain(ac,1,MASTER_VOLUME); 
+		g.addInput(player);
+		
+		Bead killBill; 
+		if(playerListener != null){
+			killBill = new Bead(){
+				@Override
+				protected void messageReceived(Bead message){
+					playerListener.playEnded();
+					g.kill();
+					playingBead = null;
+				}
+			};
+		}else{
+			killBill = new Bead(){
+				@Override
+				protected void messageReceived(Bead message){
+					g.kill();
+					playingBead = null;
+				}
+			};
+		}
+		
+		player.setKillListener(killBill);
+		playingBead = g;
+		ac.out.addInput(g);
+	}
+
+	public void play(InputStream sound){
+		play(sound, null);
+	}
+	
+	@Override
+	public void play(final SoundEvent evt ){
+		if(evt == null){
+			InputStream s = null;
+			play(s);
+		}else
+			play(evt,playerListeners.get(evt));
+	}
+	
+	public void play(SoundEvent evt, PlayerListener playerListener){
+		if(evt == null){
+			InputStream s = null;
+			play(s,playerListener);
+		}else
+			play(AudioResourcesService.getAudiofile(evt),playerListener);
+	}
+	
+	public void stop(){
+		if(mute)
+			return;
+		if(playingBead != null){
+			playingBead.setKillListener(null);
+			playingBead.kill();
+		}
+	}
+	
+	public void loadSound(InputStream sound){
+		SampleManager.sample(sound);
+	}
+	
+	@Override
+	public void startLoop(SoundEvent action) {
+		if(mute)
+			return;
+		Sample sample = null; 
+		if(action != null){
+			InputStream samplePath = AudioResourcesService.getAudiofile(action);
+			if(samplePath != null)
+				sample = SampleManager.sample(samplePath);
+		}
+		if(sample == null)
+			return;
+		SamplePlayer player = new SamplePlayer(ac,sample);
+		player.setLoopType(LoopType.LOOP_FORWARDS);
+		Gain g = new Gain(ac,1,MASTER_VOLUME); 
+		g.addInput(player);
+		ac.out.addInput(g);
+		loopPlayers.put(action, g);
+	}
+
+	@Override
+	public void stopLoop(SoundEvent action) {
+		UGen g = loopPlayers.get(action);
+		if(g != null){
+			g.kill();
+			loopPlayers.remove(action);
+		}
+	}
+
+	@Override
+	public void setPlayerListener(PlayerListener listener, SoundEvent type){
+		playerListeners.put(type, listener);
+	}
+	
+	@Override
+	public void unsetPlayerListener(SoundEvent type){
+		playerListeners.remove(type);
+	}
+	
+	@Override
+	public void setMuted(boolean mute){
+		this.mute = mute;
+	}
+	
+	@Override
+	public void dispose(){
+		ac.stop();
+	}
+	
+	private AudioContext ac;	
+	private Bead playingBead;
+	private Map<SoundEvent,PlayerListener> playerListeners;
+	private Map<SoundEvent,UGen> loopPlayers;
+	private static final float MASTER_VOLUME = 0.35f;
+	private boolean mute;
+}