CLAM-Development  1.4.0
TextFileMIDIDevice.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "MIDIIn.hxx"
23 #include <stdio.h>
24 #include "MIDIDeviceList.hxx"
25 #include "MIDIDevice.hxx"
26 
27 namespace CLAM {
28 
29  class TextFileMIDIDevice: public MIDIDevice
30  {
31  private:
32  std::string mDevice;
33  FILE* mOut;
34  FILE* mIn;
35  TControlData mClock;
36  public:
37  TextFileMIDIDevice(const std::string& name,const std::string& device);
38  ~TextFileMIDIDevice();
39 
40  void ConcreteStart(void) throw(Err);
41  void ConcreteStop(void) throw(Err);
42 
43  void Read(void) throw(Err);
44  void Write(unsigned char* msg,int size) throw(Err);
45 
46  void SetClock(TControlData val) { mClock = val; }
47 
48  };
49 
50  TextFileMIDIDevice::TextFileMIDIDevice(const std::string& name,const std::string& device):
51  MIDIDevice(name)
52  {
53  mDevice = device;
54  mOut = 0;
55  mIn = 0;
56  mClock = 0;
57  }
58 
59  void TextFileMIDIDevice::ConcreteStart(void) throw(Err)
60  {
61  if (mOut) fclose(mOut);
62  if (mIn) fclose(mIn);
63 
64  mIn = 0;
65  mOut = 0;
66 
67  if (mInputs.size() && mOutputs.size() && mDevice!="-")
68  {
69  throw Err("TextFileMIDIDevice: Cannot use the same file for reading and writing");
70  }
71  if (mInputs.size())
72  {
73  if (mDevice == "-")
74  mIn = stdin;
75  else
76  mIn = fopen(mDevice.c_str(),"r");
77  }
78  if (mOutputs.size())
79  {
80  if (mDevice == "-")
81  mOut = stdout;
82  else
83  mOut = fopen(mDevice.c_str(),"w");
84  }
85  }
86 
87  void TextFileMIDIDevice::ConcreteStop(void) throw(Err)
88  {
89  if (mOut) fclose(mOut);
90  if (mIn) fclose(mOut);
91  mOut = 0;
92  mIn = 0;
93  }
94 
95  void TextFileMIDIDevice::Write(unsigned char* msg,int size) throw(Err)
96  {
97  printf("TextFileMIDIDevice::Write:");
98  printf("%f",mClock);
99  for (int i=0;i<size;i++)
100  {
101  printf(" 0x%02x",msg[i]);
102  }
103  printf("\n");
104  fflush(stdout);
105 
106  fprintf(mOut,"%f",mClock);
107  for (int i=0;i<size;i++)
108  {
109  fprintf(mOut," 0x%02x",msg[i]);
110  }
111  fprintf(mOut,"\n");
112  }
113 
114  void TextFileMIDIDevice::Read(void) throw(Err)
115  {
116  }
117 
118  TextFileMIDIDevice::~TextFileMIDIDevice()
119  {
120  Stop();
121  }
122 
123  class TextFileMIDIDeviceList: public MIDIDeviceList
124  {
125  static TextFileMIDIDeviceList sDevices;
126 
127  TextFileMIDIDeviceList()
128  :MIDIDeviceList(std::string("textfile"))
129  {
130  mAvailableDevices.push_back("-");
131 
132  AddMe();
133  }
134  public:
135 
136  std::string DefaultDevice(void)
137  {
138  return "-";
139  }
140 
141  MIDIDevice* Create(
142  const std::string& name,const std::string& device)
143  {
144  return new TextFileMIDIDevice(name,device);
145  }
146  };
147 
148  TextFileMIDIDeviceList TextFileMIDIDeviceList::sDevices;
149 }
150