00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "ProcessingFactory.hxx"
00023 #include "MIDIDispatcher.hxx"
00024
00025
00026 namespace CLAM
00027 {
00028
00029 namespace Hidden
00030 {
00031 static const char * metadata[] = {
00032 "key", "MIDIDispatcher",
00033 "category", "MIDI",
00034 "description", "MIDIDispatcher",
00035 0
00036 };
00037 static FactoryRegistrator<ProcessingFactory, MIDIDispatcher> reg = metadata;
00038 }
00039
00040 void MIDIDispatcherConfig::DefaultInit()
00041 {
00042 AddNumberOfVoices();
00043 AddNumberOfInControls();
00044 UpdateData();
00045 SetNumberOfVoices( 2 );
00046 SetNumberOfInControls( 2 );
00047 }
00048
00049
00050 MIDIDispatcher::MIDIDispatcher( const MIDIDispatcherConfig& cfg )
00051 : mStateIn("StateIn",this,&MIDIDispatcher::UpdateState)
00052 , mNoteIn( "Note", this, &MIDIDispatcher::UpdateNote )
00053 , mVelocityIn( "Velocity", this, &MIDIDispatcher::UpdateVel )
00054 , mVelocity( 0 )
00055 , mNote( 0 )
00056 {
00057 Configure( cfg );
00058 }
00059
00060 bool MIDIDispatcher::ConcreteConfigure( const ProcessingConfig& cfg )
00061 {
00062
00063 CopyAsConcreteConfig(mConfig, cfg);
00064 RemoveControls();
00065 CreateControls();
00066
00067 return true;
00068 }
00069
00070
00071 int MIDIDispatcher::UpdateState( TControlData availableInstr )
00072 {
00073 std::list<VoiceStatus>::iterator it;
00074
00075 for (it=mVoiceStatusList.begin();it!=mVoiceStatusList.end();it++)
00076 {
00077 if ((*it).mId == availableInstr)
00078 {
00079 mVoiceStatusList.erase(it);
00080 VoiceStatus status = { -1,-1, int(availableInstr) };
00081 mVoiceStatusList.push_front(status);
00082 return 0;
00083 }
00084 }
00085
00086 return 0;
00087
00088 }
00089
00090
00091 void MIDIDispatcher::Dispatch(void)
00092 {
00093 if( mVelocity == 0.0 )
00094 {
00095 std::list<VoiceStatus>::iterator it;
00096
00097 for (it=mVoiceStatusList.begin();it!=mVoiceStatusList.end();it++)
00098 {
00099 if ( it->mNote != mNote ) continue;
00100 if ( !it->mVelocity ) continue;
00101 it->mVelocity = 0;
00102 mOutputControls[ it->mId * + 1]->SendControl( mVelocity );
00103 return;
00104 }
00105 }
00106 else
00107 {
00108 VoiceStatus status = mVoiceStatusList.front();
00109 mVoiceStatusList.pop_front();
00110 status.mNote = int(mNote);
00111 status.mVelocity = int(mVelocity);
00112 mVoiceStatusList.push_back(status);
00113
00114 mOutputControls[ status.mId * mConfig.GetNumberOfInControls() + 1 ]->SendControl( mVelocity );
00115 mOutputControls[ status.mId * mConfig.GetNumberOfInControls() ]->SendControl( mNote );
00116 }
00117 }
00118
00119 void MIDIDispatcher::CreateControls()
00120 {
00121
00122 for (int i=0;i<mConfig.GetNumberOfInControls(); i++)
00123 {
00124 std::stringstream number;
00125 number << i;
00126 InControl* inControl = new InControl( "InControl " + number.str(), this );
00127 mInputControls.push_back( inControl );
00128 }
00129
00130 for( int i=0; i<mConfig.GetNumberOfInControls(); i++ )
00131 mInputControls[i]->DoControl(0.);
00132
00133 for(int i = 0; i < mConfig.GetNumberOfVoices(); i++ )
00134 {
00135
00136
00137
00138 VoiceStatus status = { -1, -1, i };
00139 mVoiceStatusList.push_back(status);
00140 }
00141
00142 int k = 0;
00143 for (int i = 0; i < mConfig.GetNumberOfVoices(); i++ )
00144 {
00145 for ( int j=0; j < mConfig.GetNumberOfInControls();j++)
00146 {
00147 std::stringstream number("");
00148 number << i << j;
00149 mOutputControls.push_back( new OutControl("a" + number.str(),this ) );
00150
00151
00152 k++;
00153 }
00154 }
00155 }
00156
00157 void MIDIDispatcher::RemoveControls()
00158 {
00159 std::vector< InControl* >::iterator itInControl;
00160
00161 for( itInControl=mInputControls.begin(); itInControl!=mInputControls.end(); itInControl++)
00162 delete *itInControl;
00163
00164 mInputControls.clear();
00165 }
00166
00167
00168 }
00169