1 /**
2 * Copyright (c) 2011, The University of Southampton and the individual contributors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * * Neither the name of the University of Southampton nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 /**
31 *
32 */
33 package org.openimaj.audio.generation;
34
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.Stack;
38
39 import org.openimaj.audio.AudioFormat;
40 import org.openimaj.audio.AudioMixer;
41 import org.openimaj.audio.Instrument;
42
43 /**
44 * A class that uses a pool of synthesizers for creating a polyphonic
45 * synthesizer.
46 *
47 * @author David Dupplaw (dpd@ecs.soton.ac.uk)
48 * @created 13 Feb 2013
49 * @version $Author$, $Revision$, $Date$
50 */
51 public class PolyphonicSynthesizer extends AudioMixer implements Instrument
52 {
53 /** The synth channels */
54 private Map<Integer,Synthesizer> playingSynths = new HashMap<Integer, Synthesizer>();
55
56 /** A set of playingSynths that we can call upon to make sounds */
57 private final Stack<Synthesizer> voicePool = new Stack<Synthesizer>();
58
59 /**
60 * Constructor
61 * @param nPolyphony Number of voices polyphony allowed
62 */
63 public PolyphonicSynthesizer( final int nPolyphony )
64 {
65 super( new AudioFormat( 16, 44.1, 1 ) );
66 this.playingSynths = new HashMap<Integer, Synthesizer>();
67
68 for( int i = 0; i < nPolyphony; i++ )
69 {
70 final Synthesizer s = new Synthesizer();
71 this.voicePool.add( s );
72 // super.addStream( s, 1f );
73 }
74 }
75
76 /**
77 * Turn the given note on.
78 * @param noteNumber
79 */
80 @Override
81 public void noteOn( final int noteNumber, final double velocity )
82 {
83 System.out.println( "Playing: "+this.playingSynths );
84 System.out.println( "Pool: "+this.voicePool );
85
86 // Check if a synth is already playing a note with the given note
87 // number. If so, we'll just retrigger the note.
88 Synthesizer s = this.playingSynths.get( noteNumber );
89 if( s == null )
90 {
91 if( this.voicePool.size() > 0 )
92 s = this.voicePool.pop();
93 }
94
95 System.out.println( "Using synth "+s );
96
97 // If we've got an existing or new synth...
98 if( s != null )
99 {
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 }