Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/MidiOutput.cpp
Go to the documentation of this file.
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 #include "../common_source.h"
00021 
00022 #ifdef MARSYAS_MIDIIO
00023 #include "RtMidi.h"
00024 #endif
00025 
00026 
00027 using std::ostringstream;
00028 using namespace Marsyas;
00029 
00030 MidiOutput::MidiOutput(mrs_string name):MarSystem("MidiOutput",name)
00031 {
00032 #ifdef MARSYAS_MIDIIO
00033   midiout = NULL;
00034 #endif
00035   addControls();
00036 }
00037 
00038 
00039 MidiOutput::MidiOutput(const MidiOutput& a):MarSystem(a)
00040 {
00041 #ifdef MARSYAS_MIDIIO
00042   midiout = NULL;
00043 #endif
00044   ctrl_byte1_ = getctrl("mrs_natural/byte1");
00045   ctrl_byte2_ = getctrl("mrs_natural/byte2");
00046   ctrl_byte3_ = getctrl("mrs_natural/byte3");
00047   ctrl_sendMessage_ = getctrl("mrs_bool/sendMessage");
00048 }
00049 
00050 MidiOutput::~MidiOutput()
00051 {
00052 #ifdef MARSYAS_MIDIIO
00053   delete midiout;
00054 #endif
00055 }
00056 
00057 
00058 MarSystem*
00059 MidiOutput::clone() const
00060 {
00061   return new MidiOutput(*this);
00062 }
00063 
00064 
00065 void
00066 MidiOutput::addControls()
00067 {
00068   addctrl("mrs_natural/byte1", 64, ctrl_byte1_);
00069   addctrl("mrs_natural/byte2", 64, ctrl_byte2_);
00070   addctrl("mrs_natural/byte3", 64, ctrl_byte3_);
00071   addctrl("mrs_bool/sendMessage", false, ctrl_sendMessage_);
00072 
00073   addctrl("mrs_natural/port", 0);
00074   addctrl("mrs_bool/virtualPort", false);
00075   addctrl("mrs_bool/initMidi", false);
00076 
00077   setctrlState("mrs_bool/sendMessage", true);
00078   setctrlState("mrs_bool/virtualPort", true);
00079   setctrlState("mrs_natural/port", true);
00080   setctrlState("mrs_natural/port", true);
00081 }
00082 
00083 void
00084 MidiOutput::myUpdate(MarControlPtr sender)
00085 {
00086   MRSDIAG("MidiOutput.cpp - MidiOutput:myUpdate");
00087   MarSystem::myUpdate(sender);
00088 
00089 #ifdef MARSYAS_MIDIIO
00090 
00091   initMidi = getctrl("mrs_bool/initMidi")->to<mrs_bool>();
00092   virtualPort = getctrl("mrs_bool/virtualPort")->to<mrs_bool>();
00093 
00094   if (initMidi)
00095   {
00096     if (midiout == NULL)
00097     {
00098       try {
00099         midiout = new RtMidiOut();
00100       }
00101       catch (RtError &error) {
00102         error.printMessage();
00103         return;
00104       }
00105 
00106       if (virtualPort)
00107       {
00108         try
00109         {
00110           midiout->openVirtualPort("MarsyasOutput");
00111         }
00112         catch(RtError &error)
00113         {
00114           error.printMessage();
00115           return;
00116         }
00117         message.push_back(0);
00118         message.push_back(0);
00119         message.push_back(0);
00120       }
00121       else
00122       {
00123         {
00124           try {
00125             midiout->openPort( getctrl("mrs_natural/port")->to<mrs_natural>() );
00126           }
00127           catch (RtError &error)
00128           {
00129             error.printMessage();
00130             return;
00131           }
00132           message.push_back(0);
00133           message.push_back(0);
00134           message.push_back(0);
00135         }
00136       }
00137     }
00138     mrs_bool sendMessage = ctrl_sendMessage_->to<mrs_bool>();
00139     if (sendMessage)
00140     {
00141       message[0] = (unsigned char)ctrl_byte1_->to<mrs_natural>();
00142       message[1] = (unsigned char)ctrl_byte2_->to<mrs_natural>();
00143       message[2] = (unsigned char)ctrl_byte3_->to<mrs_natural>();
00144 
00145       midiout->sendMessage( &message );
00146       setctrl(ctrl_sendMessage_, false);
00147     }
00148   }
00149 #endif
00150 
00151 }
00152 
00153 void MidiOutput::myProcess(realvec& in, realvec& out)
00154 {
00155   mrs_natural t,o;
00156   for (o=0; o < inObservations_; o++)
00157     for (t = 0; t < inSamples_; t++)
00158     {
00159       out(o,t) =  in(o,t);
00160     }
00161 }