CLAM-Development
1.1
|
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 "MIDIIn.hxx" 00023 #include <stdio.h> 00024 #include "MIDIDeviceList.hxx" 00025 #include "MIDIDevice.hxx" 00026 00027 namespace CLAM { 00028 00029 class TextFileMIDIDevice: public MIDIDevice 00030 { 00031 private: 00032 std::string mDevice; 00033 FILE* mOut; 00034 FILE* mIn; 00035 TControlData mClock; 00036 public: 00037 TextFileMIDIDevice(const std::string& name,const std::string& device); 00038 ~TextFileMIDIDevice(); 00039 00040 void ConcreteStart(void) throw(Err); 00041 void ConcreteStop(void) throw(Err); 00042 00043 void Read(void) throw(Err); 00044 void Write(unsigned char* msg,int size) throw(Err); 00045 00046 void SetClock(TControlData val) { mClock = val; } 00047 00048 }; 00049 00050 TextFileMIDIDevice::TextFileMIDIDevice(const std::string& name,const std::string& device): 00051 MIDIDevice(name) 00052 { 00053 mDevice = device; 00054 mOut = 0; 00055 mIn = 0; 00056 mClock = 0; 00057 } 00058 00059 void TextFileMIDIDevice::ConcreteStart(void) throw(Err) 00060 { 00061 if (mOut) fclose(mOut); 00062 if (mIn) fclose(mIn); 00063 00064 mIn = 0; 00065 mOut = 0; 00066 00067 if (mInputs.size() && mOutputs.size() && mDevice!="-") 00068 { 00069 throw Err("TextFileMIDIDevice: Cannot use the same file for reading and writing"); 00070 } 00071 if (mInputs.size()) 00072 { 00073 if (mDevice == "-") 00074 mIn = stdin; 00075 else 00076 mIn = fopen(mDevice.c_str(),"r"); 00077 } 00078 if (mOutputs.size()) 00079 { 00080 if (mDevice == "-") 00081 mOut = stdout; 00082 else 00083 mOut = fopen(mDevice.c_str(),"w"); 00084 } 00085 } 00086 00087 void TextFileMIDIDevice::ConcreteStop(void) throw(Err) 00088 { 00089 if (mOut) fclose(mOut); 00090 if (mIn) fclose(mOut); 00091 mOut = 0; 00092 mIn = 0; 00093 } 00094 00095 void TextFileMIDIDevice::Write(unsigned char* msg,int size) throw(Err) 00096 { 00097 printf("TextFileMIDIDevice::Write:"); 00098 printf("%f",mClock); 00099 for (int i=0;i<size;i++) 00100 { 00101 printf(" 0x%02x",msg[i]); 00102 } 00103 printf("\n"); 00104 fflush(stdout); 00105 00106 fprintf(mOut,"%f",mClock); 00107 for (int i=0;i<size;i++) 00108 { 00109 fprintf(mOut," 0x%02x",msg[i]); 00110 } 00111 fprintf(mOut,"\n"); 00112 } 00113 00114 void TextFileMIDIDevice::Read(void) throw(Err) 00115 { 00116 } 00117 00118 TextFileMIDIDevice::~TextFileMIDIDevice() 00119 { 00120 Stop(); 00121 } 00122 00123 class TextFileMIDIDeviceList: public MIDIDeviceList 00124 { 00125 static TextFileMIDIDeviceList sDevices; 00126 00127 TextFileMIDIDeviceList() 00128 :MIDIDeviceList(std::string("textfile")) 00129 { 00130 mAvailableDevices.push_back("-"); 00131 00132 AddMe(); 00133 } 00134 public: 00135 00136 std::string DefaultDevice(void) 00137 { 00138 return "-"; 00139 } 00140 00141 MIDIDevice* Create( 00142 const std::string& name,const std::string& device) 00143 { 00144 return new TextFileMIDIDevice(name,device); 00145 } 00146 }; 00147 00148 TextFileMIDIDeviceList TextFileMIDIDeviceList::sDevices; 00149 } 00150