001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 * 
032 */
033package org.openimaj.audio.generation;
034
035import java.util.HashMap;
036import java.util.Map;
037import java.util.Stack;
038
039import org.openimaj.audio.AudioFormat;
040import org.openimaj.audio.AudioMixer;
041import org.openimaj.audio.Instrument;
042
043/**
044 *      A class that uses a pool of synthesizers for creating a polyphonic
045 *      synthesizer.
046 *
047 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
048 *  @created 13 Feb 2013
049 *      @version $Author$, $Revision$, $Date$
050 */
051public class PolyphonicSynthesizer extends AudioMixer implements Instrument
052{
053        /** The synth channels */
054        private Map<Integer,Synthesizer> playingSynths = new HashMap<Integer, Synthesizer>();
055        
056        /** A set of playingSynths that we can call upon to make sounds */
057        private final Stack<Synthesizer> voicePool = new Stack<Synthesizer>();
058        
059        /**
060         *      Constructor
061         *      @param nPolyphony Number of voices polyphony allowed 
062         */
063        public PolyphonicSynthesizer( final int nPolyphony )
064        {
065                super( new AudioFormat( 16, 44.1, 1 ) );
066                this.playingSynths = new HashMap<Integer, Synthesizer>();
067                
068                for( int i = 0; i < nPolyphony; i++ )
069                {
070                        final Synthesizer s = new Synthesizer(); 
071                        this.voicePool.add( s );
072                        // super.addStream( s, 1f );
073                }
074        }
075        
076        /**
077         *      Turn the given note on.
078         *      @param noteNumber
079         */
080        @Override
081        public void noteOn( final int noteNumber, final double velocity )
082        {
083                System.out.println( "Playing: "+this.playingSynths );
084                System.out.println( "Pool:    "+this.voicePool );
085                
086                // Check if a synth is already playing a note with the given note
087                // number. If so, we'll just retrigger the note.
088                Synthesizer s = this.playingSynths.get( noteNumber );
089                if( s == null )
090                {
091                        if( this.voicePool.size() > 0 )
092                                s = this.voicePool.pop();
093                }
094
095                System.out.println( "Using synth "+s );
096                
097                // If we've got an existing or new synth...
098                if( s != null )
099                {
100                        super.addStream( s, 1f );
101                        
102                        // Push it into the playing synths map
103                        this.playingSynths.put( noteNumber, s );
104                        
105                        // Make it play the right note
106                        s.noteOn( noteNumber, velocity );
107
108                        final Synthesizer ss = s;
109                        
110                        // Catch when it's finished playing and pop it back on the pool
111                        s.addSynthesizerListener( new SynthesizerListener()
112                        {
113                                @Override
114                                public void synthQuiet()
115                                {
116                                        System.out.println( "Synth quiet "+ss );
117                                        final Synthesizer synth = PolyphonicSynthesizer.this.
118                                                        playingSynths.remove( noteNumber );
119                                        PolyphonicSynthesizer.this.voicePool.push( synth );
120                                        synth.removeSynthesizerListener( this );
121//                                      PolyphonicSynthesizer.super.removeStream( synth );
122                                }
123                        } );
124                }
125        }
126        
127        /**
128         *      Turn the given note off
129         *      @param noteNumber
130         */
131        @Override
132        public void noteOff( final int noteNumber )
133        {
134                final Synthesizer s = this.playingSynths.get( noteNumber );
135                if( s != null )
136                        s.noteOff();
137        }
138        
139        /**
140         *      The number of synths in use.
141         *      @return The number of synths in use.
142         */
143        public int getChannelsInUse()
144        {
145                return this.playingSynths.keySet().size();
146        }
147}