view 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
line wrap: on
line source
/*  
 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.EnumMap;
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 about the library see http://www.beadsproject.net/.
 */
class BeadsSound implements Sound {
	
	public BeadsSound(){
		ac = new AudioContext();
		playerListeners = new EnumMap<SoundEvent,PlayerListener>(SoundEvent.class);
		loopPlayers = new EnumMap<SoundEvent,UGen>(SoundEvent.class);
		
		/* 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 setDefaultPlayerListener(PlayerListener listener, SoundEvent type){
		playerListeners.put(type, listener);
	}
	
	@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;
}