MIDI2Melody.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "MIDI2Melody.hxx"
00023 
00024 namespace CLAM {
00025 
00026 MIDI2Melody::MIDI2Melody()
00027         : mOutput("Output",this),
00028           mTime("time",this)
00029 {
00030         ConcreteConfigure(Control2DataConfig());
00031 }
00032         
00033 bool MIDI2Melody::GenerateOutputData(int id, TControlData val)
00034 {
00035         switch(id)
00036         {
00037                 case 0://key for note off
00038                 {
00039                         //find the note with that key in the member buffers, remove it, add it to melody after computing duration
00040                         int index,velocity,beginTime,endTime;
00041                         index=FindNote(int(val));
00042                         if(index==-1) break; //I don't know why this happens but it does
00043                         velocity=(int) mVelocities[index];
00044                         beginTime=(int) mBeginTimes[index];
00045                         endTime=(int) mTime.GetLastValue();
00046                         DeleteNoteFromIndex(index);
00047 
00048                         MIDINote newNote;
00049                         newNote.SetKey(int(val));//should be MidiNote!
00050                         newNote.SetVelocity(velocity);//should be MidiVelocity
00051                         MediaTime time;
00052                         time.AddBegin();
00053                         time.AddEnd();
00054                         time.RemoveDuration();
00055                         time.UpdateData();
00056                         time.SetBegin(beginTime);
00057                         time.SetEnd(endTime);
00058                         newNote.SetTime(time);
00059                         mOutput.GetData().GetNoteArray().AddElem(newNote);
00060 
00061                         break;
00062                 }
00063                 case 1://velocity for note off
00064                 {
00065                         //for the time being this will not be acknowledged
00066                         break;
00067                 }
00068                 case 2://key for note on
00069                 {
00070                         //add note to member array with velocity 0 and modify mLastKey
00071                         AddNote(int(val),0,int(mTime.GetLastValue()));
00072                         mLastKey=int(val);
00073                         break;
00074                 }
00075                 case 3://velocity for note on
00076                 {
00077                         //Modify velocity for mLastKey
00078                         ModifyVelocity(mLastKey,int(val));
00079                         break;
00080                 }
00081                 default:
00082                 {
00083                         CLAM_ASSERT(false,"Not a valid MIDI control");
00084                 }
00085         }
00086         return true;
00087 }
00088 
00089 
00090 bool MIDI2Melody::ConcreteConfigure(const ProcessingConfig& c)
00091 {
00092         //Ugly, ugly. Don't try this at home.
00093         Control2DataConfig &tmp=const_cast<Control2DataConfig&>(dynamic_cast<const Control2DataConfig&>(c));
00094         //We harcode this value as the concrete MIDI2Melody now will only work on 4 controls
00095         tmp.SetNumControls(4);
00096         Control2Data::ConcreteConfigure(tmp);
00097         return true;
00098 }
00099 
00100 int MIDI2Melody::FindNote(int key)
00101 {
00102         int index=-1;
00103         int i;
00104         for(i=0;i<mKeys.Size();i++)
00105         {
00106                 if (mKeys[i]==key){
00107                 index=i;
00108                 break;
00109                 }
00110         }
00111         return index;
00112 }
00113 
00114 void MIDI2Melody::AddNote(int key, int velocity,int time)
00115 {
00116         mKeys.AddElem(key);
00117         mVelocities.AddElem(velocity);
00118         mBeginTimes.AddElem(time);
00119 }
00120 
00121 void MIDI2Melody::DeleteNote(int key)
00122 {
00123         DeleteNoteFromIndex(FindNote(key));
00124 }
00125 
00126 void MIDI2Melody::DeleteNoteFromIndex(int index)
00127 {
00128         mKeys.DeleteElem(index);
00129         mVelocities.DeleteElem(index);
00130         mBeginTimes.DeleteElem(index);
00131 }
00132 
00133 void MIDI2Melody::ModifyVelocity(int key,int newVelocity)
00134 {       
00135         int index=FindNote(key);
00136         mVelocities[index]=newVelocity;
00137 }
00138 
00139 } // namespace CLAM
00140 
Generated by  doxygen 1.6.3