CLAM-Development  1.4.0
MIDIDispatcher.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
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 2 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, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "ProcessingFactory.hxx"
23 #include "MIDIDispatcher.hxx"
24 
25 
26 namespace CLAM
27 {
28 
29 namespace Hidden
30 {
31  static const char * metadata[] = {
32  "key", "MIDIDispatcher",
33  "category", "MIDI",
34  "description", "MIDIDispatcher",
35  0
36  };
37  static FactoryRegistrator<ProcessingFactory, MIDIDispatcher> reg = metadata;
38 }
39 
41 {
42  AddNumberOfVoices();
43  AddNumberOfInControls();
44  UpdateData();
45  SetNumberOfVoices( 2 );
46  SetNumberOfInControls( 2 );
47 }
48 
49 
51  : mStateIn("StateIn",this,&MIDIDispatcher::UpdateState)
52  , mNoteIn( "Note", this, &MIDIDispatcher::UpdateNote )
53  , mVelocityIn( "Velocity", this, &MIDIDispatcher::UpdateVel )
54  , mVelocity( 0 )
55  , mNote( 0 )
56 {
57  Configure( cfg );
58 }
59 
61 {
62 
63  CopyAsConcreteConfig(mConfig, cfg);
64  RemoveControls();
65  CreateControls();
66 
67  return true;
68 }
69 
70 
72 {
73  std::list<VoiceStatus>::iterator it;
74 
75  for (it=mVoiceStatusList.begin();it!=mVoiceStatusList.end();it++)
76  {
77  if ((*it).mId == availableInstr)
78  {
79  mVoiceStatusList.erase(it);
80  VoiceStatus status = { -1,-1, int(availableInstr) };
81  mVoiceStatusList.push_front(status);
82  return;
83  }
84  }
85 }
86 
87 
89 {
90  if( mVelocity == 0.0 )
91  {
92  std::list<VoiceStatus>::iterator it;
93 
94  for (it=mVoiceStatusList.begin();it!=mVoiceStatusList.end();it++)
95  {
96  if ( it->mNote != mNote ) continue;
97  if ( !it->mVelocity ) continue;
98  it->mVelocity = 0;
99  mOutputControls[ it->mId * + 1]->SendControl( mVelocity );
100  return;
101  }
102  }
103  else
104  {
105  VoiceStatus status = mVoiceStatusList.front();
106  mVoiceStatusList.pop_front();
107  status.mNote = int(mNote);
108  status.mVelocity = int(mVelocity);
109  mVoiceStatusList.push_back(status);
110 
111  mOutputControls[ status.mId * mConfig.GetNumberOfInControls() + 1 ]->SendControl( mVelocity );
112  mOutputControls[ status.mId * mConfig.GetNumberOfInControls() ]->SendControl( mNote );
113  }
114 }
115 
116 void MIDIDispatcher::CreateControls()
117 {
118 
119  for (int i=0;i<mConfig.GetNumberOfInControls(); i++)
120  {
121  std::stringstream number;
122  number << i;
123  FloatInControl* inControl = new FloatInControl( "InControl " + number.str(), this );
124  mInputControls.push_back( inControl );
125  }
126 
127  for( int i=0; i<mConfig.GetNumberOfInControls(); i++ )
128  mInputControls[i]->DoControl(0.); // set initial value for each InControl
129 
130  for(int i = 0; i < mConfig.GetNumberOfVoices(); i++ )
131  {
132 // mInstruments[ i ]->SetId(i);
133 // mInstruments[ i ]->LinkStateOutWithInControl( this, 0 ) ;
134 //
135  VoiceStatus status = { -1, -1, i };
136  mVoiceStatusList.push_back(status);
137  }
138 
139  int k = 0;
140  for (int i = 0; i < mConfig.GetNumberOfVoices(); i++ )
141  {
142  for ( int j=0; j < mConfig.GetNumberOfInControls();j++)
143  {
144  std::stringstream number("");
145  number << i << j;
146  mOutputControls.push_back( new FloatOutControl("a" + number.str(),this ) );
147 // GetOutControl(k).AddLink( mInstruments[i]->GetImConfig.GetInControl(j+1));
148 // LinkOutWithInControl( k , mInstruments[ i ], j+1);
149  k++;
150  }
151  }
152 }
153 
154 void MIDIDispatcher::RemoveControls()
155 {
156  std::vector< FloatInControl* >::iterator itInControl;
157 
158  for( itInControl=mInputControls.begin(); itInControl!=mInputControls.end(); itInControl++)
159  delete *itInControl;
160 
161  mInputControls.clear();
162 }
163 
164 
165 } // namespace CLAM
166