Marsyas  0.2
/home/gperciva/src/marsyas/src/marsyas/MidiOutput.cpp
00001 /*
00002  ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
00003  **  
00004  ** This program is free software; you can redistribute it and/or modify
00005  ** it under the terms of the GNU General Public License as published by
00006  ** the Free Software Foundation; either version 2 of the License, or
00007  ** (at your option) any later version.
00008  ** 
00009  ** This program is distributed in the hope that it will be useful,
00010  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  ** GNU General Public License for more details.
00013  ** 
00014  ** You should have received a copy of the GNU General Public License
00015  ** along with this program; if not, write to the Free Software 
00016  ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "MidiOutput.h"
00020 
00021 #ifdef MARSYAS_MIDIIO
00022 #include "RtMidi.h"
00023 #endif 
00024 
00025 
00026 using std::ostringstream;
00027 using namespace Marsyas;
00028 
00029 MidiOutput::MidiOutput(mrs_string name):MarSystem("MidiOutput",name)
00030 {  
00031 #ifdef MARSYAS_MIDIIO
00032     midiout = NULL;
00033 #endif 
00034     addControls();
00035 }
00036 
00037 
00038 MidiOutput::MidiOutput(const MidiOutput& a):MarSystem(a)
00039 {
00040 #ifdef MARSYAS_MIDIIO
00041     midiout = NULL;
00042 #endif 
00043     ctrl_byte1_ = getctrl("mrs_natural/byte1");
00044     ctrl_byte2_ = getctrl("mrs_natural/byte2");
00045     ctrl_byte3_ = getctrl("mrs_natural/byte3");
00046     ctrl_sendMessage_ = getctrl("mrs_bool/sendMessage");
00047 }
00048 
00049 MidiOutput::~MidiOutput()
00050 {
00051 #ifdef MARSYAS_MIDIIO
00052     delete midiout;
00053 #endif
00054 }
00055 
00056 
00057 MarSystem* 
00058 MidiOutput::clone() const 
00059 {
00060     return new MidiOutput(*this);
00061 }
00062 
00063 
00064     void 
00065 MidiOutput::addControls()
00066 {
00067     addctrl("mrs_natural/byte1", 64, ctrl_byte1_);
00068     addctrl("mrs_natural/byte2", 64, ctrl_byte2_);
00069     addctrl("mrs_natural/byte3", 64, ctrl_byte3_);
00070     addctrl("mrs_bool/sendMessage", false, ctrl_sendMessage_);
00071 
00072     addctrl("mrs_natural/port", 0);
00073     addctrl("mrs_bool/virtualPort", false);
00074     addctrl("mrs_bool/initMidi", false);
00075 
00076     setctrlState("mrs_bool/sendMessage", true);
00077     setctrlState("mrs_bool/virtualPort", true);
00078     setctrlState("mrs_natural/port", true);
00079     setctrlState("mrs_natural/port", true);
00080 }
00081 
00082  void
00083 MidiOutput::myUpdate(MarControlPtr sender)
00084 {
00085     MRSDIAG("MidiOutput.cpp - MidiOutput:myUpdate");
00086     MarSystem::myUpdate(sender);
00087 
00088 #ifdef MARSYAS_MIDIIO
00089 
00090     initMidi = getctrl("mrs_bool/initMidi")->to<mrs_bool>();
00091     virtualPort = getctrl("mrs_bool/virtualPort")->to<mrs_bool>();
00092     
00093     if (initMidi)
00094       {
00095         if (midiout == NULL) 
00096         {
00097             try { 
00098                 midiout = new RtMidiOut();
00099             } 
00100             catch (RtError3 &error) { 
00101                 error.printMessage();
00102                 return;
00103             }
00104 
00105             if (virtualPort)
00106             {
00107                 try
00108                 {
00109                     midiout->openVirtualPort("MarsyasOutput");                    
00110                 }
00111                 catch(RtError3 &error)
00112                 {
00113                     error.printMessage();
00114                     return;
00115                 }
00116                     message.push_back(0);
00117                     message.push_back(0);
00118                     message.push_back(0);
00119             }
00120             else
00121             {
00122                 {
00123                     try { 
00124                         midiout->openPort( getctrl("mrs_natural/port")->to<mrs_natural>() );
00125                     }
00126                     catch (RtError3 &error) 
00127                     {
00128                         error.printMessage();
00129                         return;
00130                     } 
00131                     message.push_back(0);
00132                     message.push_back(0);
00133                     message.push_back(0);
00134                 }
00135             }
00136         }
00137         mrs_bool sendMessage = ctrl_sendMessage_->to<mrs_bool>();
00138         if (sendMessage) 
00139         {
00140       message[0] = (unsigned char)ctrl_byte1_->to<mrs_natural>();
00141       message[1] = (unsigned char)ctrl_byte2_->to<mrs_natural>();
00142       message[2] = (unsigned char)ctrl_byte3_->to<mrs_natural>();
00143       
00144       midiout->sendMessage( &message );
00145       setctrl(ctrl_sendMessage_, false);
00146         }
00147       }
00148 #endif
00149 
00150 }
00151 
00152 void MidiOutput::myProcess(realvec& in, realvec& out)
00153 {
00154     mrs_natural t,o;
00155     for (o=0; o < inObservations_; o++)
00156         for (t = 0; t < inSamples_; t++)
00157         {
00158             out(o,t) =  in(o,t);
00159         }
00160 }