MIDIOutControl.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "InControl.hxx"
00023 #include "MIDIOutControl.hxx"
00024 #include <string>
00025 #include <cstdio>
00026
00027 namespace CLAM {
00028
00029 MIDIOutControl::MIDIOutControl():MIDIOut(false)
00030 {
00031 mpDevice = 0;
00032 InitMembers();
00033 Configure(MIDIIOConfig());
00034 }
00035
00036 MIDIOutControl::MIDIOutControl(const MIDIIOConfig &c):MIDIOut(false)
00037 {
00038 mpDevice = 0;
00039 InitMembers();
00040 Configure(c);
00041 }
00042
00043 void MIDIOutControl::InitMembers(void)
00044 {
00045 mUniqId = 0;
00046
00047 mMessage = 0;
00048 mReceivedUniqId = 0;
00049 mControlIdToMsgByteId = 0;
00050
00051 mMessageSize = 0;
00052
00053 mControlledBytes = 0;
00054 mControlsReceived = 0;
00055 }
00056
00057 bool MIDIOutControl::ConcreteConfigure(const ProcessingConfig& c)
00058 throw(ErrProcessingObj)
00059 {
00060 bool ret = MIDIOut::ConcreteConfigure(c);
00061
00062 if (ret==false) return false;
00063
00064 MIDI::Message m = MIDI::Message(mConfig.GetMessage());
00065
00066 mMessageSize = MIDI::GetMessageInfo(m).length;
00067
00068
00069
00070 mControlledBytes = MIDI::GetMessageInfo(m).length;
00071
00072 if (mConfig.GetChannel()!=0) mControlledBytes--;
00073
00074
00075 if (mConfig.GetFirstData()!=128) mControlledBytes--;
00076
00077 mControlsReceived = 0;
00078
00079
00080 if (mMessage) delete [] mMessage;
00081 mMessage = new unsigned char[mMessageSize];
00082
00083 if (mReceivedUniqId) delete [] mReceivedUniqId;
00084 mReceivedUniqId = new unsigned char[mControlledBytes];
00085
00086 if (mControlIdToMsgByteId) delete mControlIdToMsgByteId;
00087 mControlIdToMsgByteId = new unsigned char[mControlledBytes];
00088
00089
00090 for (int i = 0; i < mControlledBytes ; i++ )
00091 {
00092 mReceivedUniqId[i] = mUniqId;
00093 }
00094
00095 if (m==MIDI::eNoteOnOff) m = MIDI::eNoteOn;
00096
00097 mStatusByte = 0x80|(int(m)<<4);
00098
00099 int ctrlid = 0;
00100
00101
00102 for (int i=0;i<MIDI::GetMessageInfo(m).length;i++)
00103 {
00104 const char* fieldname = 0;
00105
00106
00107 switch (i)
00108 {
00109 case 0:
00110 if (mConfig.GetChannel()==0)
00111
00112 fieldname = "Channel";
00113 else
00114
00115
00116 mStatusByte |= (mConfig.GetChannel()+1);
00117 break;
00118 case 1:
00119 if (mConfig.GetFirstData()==128)
00120
00121 fieldname = MIDI::GetMessageInfo(m).field[i-1];
00122 else
00123
00124 mMessage[1] = mConfig.GetFirstData();
00125 break;
00126 default:
00127
00128 fieldname = MIDI::GetMessageInfo(m).field[i-1];
00129 break;
00130 }
00131 if (fieldname)
00132 {
00133 std::string tmp = std::string() + MIDI::GetMessageInfo(m).name + ":" + fieldname;
00134
00135
00136 mControlIdToMsgByteId[ctrlid] = i;
00137 mMyInControls.AddElem(new InControlTmpl<MIDIOutControl>(ctrlid++,tmp.c_str(),this,&MIDIOutControl::DoControl));
00138 }
00139 }
00140
00141 return true;
00142 }
00143
00144 int MIDIOutControl::DoControl(int id,TControlData val)
00145 {
00146
00147
00148 if (mReceivedUniqId[id]==mUniqId)
00149 {
00150
00151 int i = mControlIdToMsgByteId[id];
00152
00153 if (i==0)
00154 {
00155
00156
00157 mStatusByte = (mStatusByte&0xF0) | ((unsigned char)(val)-1);
00158 }else{
00159 mMessage[i] = (unsigned char) val;
00160 }
00161 mReceivedUniqId[id]++;
00162 mControlsReceived++;
00163 if (mControlsReceived==mControlledBytes)
00164 {
00165
00166
00167
00168 mUniqId++;
00169 mControlsReceived = 0;
00170 mMessage[0]=mStatusByte;
00171 Handle(mMessage,mMessageSize);
00172 }
00173 }else{
00174
00175
00176 fprintf(stderr,"ERROR!!!! receiving a byte when the prev message was not fully constructed yet... TODO: should we throw or assert?\n");
00177 }
00178 return 1;
00179 }
00180
00181 void MIDIOutControl::Handle(unsigned char* msg,int size)
00182 {
00183
00184 CLAM_ASSERT(mpDevice,"MIDIOutControl used without a valid device");
00185 if ((msg[0]&0xF0)==0x90 && msg[2]==0) msg[0] &=0x8F;
00186 mpDevice->Write(msg,size);
00187 }
00188
00189 }
00190