make ALSA MIDI I/O work with timestamps; more MTC debug tracing
[ardour.git] / libs / midi++2 / port.cc
1 /*
2     Copyright (C) 1998 Paul Barton-Davis
3     
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20 #include <iostream>
21 #include <cstdio>
22 #include <fcntl.h>
23 #include <errno.h>
24
25 #include "pbd/xml++.h"
26 #include "pbd/error.h"
27 #include "pbd/failed_constructor.h"
28
29 #include "midi++/types.h"
30 #include "midi++/port.h"
31 #include "midi++/channel.h"
32 #include "midi++/factory.h"
33
34 using namespace MIDI;
35 using namespace std;
36 using namespace PBD;
37
38 size_t Port::nports = 0;
39
40 Port::Port (const XMLNode& node)
41         : _currently_in_cycle(false)
42         , _nframes_this_cycle(0)
43 {
44         Descriptor desc (node);
45
46         _ok = false;  /* derived class must set to true if constructor
47                          succeeds.
48                       */
49
50         bytes_written = 0;
51         bytes_read = 0;
52         input_parser = 0;
53         output_parser = 0;
54         slowdown = 0;
55
56         _devname = desc.device;
57         _tagname = desc.tag;
58         _mode = desc.mode;
59
60         if (_mode == O_RDONLY || _mode == O_RDWR) {
61                 input_parser = new Parser (*this);
62         } else {
63                 input_parser = 0;
64         }
65
66         if (_mode == O_WRONLY || _mode == O_RDWR) {
67                 output_parser = new Parser (*this);
68         } else {
69                 output_parser = 0;
70         }
71
72         for (int i = 0; i < 16; i++) {
73                 _channel[i] =  new Channel (i, *this);
74
75                 if (input_parser) {
76                         _channel[i]->connect_input_signals ();
77                 }
78
79                 if (output_parser) {
80                         _channel[i]->connect_output_signals ();
81                 }
82         }
83 }
84
85
86 Port::~Port ()
87 {
88         for (int i = 0; i < 16; i++) {
89                 delete _channel[i];
90         }
91 }
92
93 void
94 Port::parse (nframes_t timestamp)
95 {
96         byte buf[512];
97
98         /* NOTE: parsing is done (if at all) by initiating a read from 
99            the port. Each port implementation calls on the parser
100            once it has data ready.
101         */
102         
103         if (input_parser) {
104                 input_parser->set_timestamp (timestamp);
105         }
106
107         while (1) {
108                 
109                 // cerr << "+++ READ ON " << name() << endl;
110
111                 int nread = read (buf, sizeof (buf));
112
113                 // cerr << "-- READ (" << nread << " ON " << name() << endl;
114                 
115                 if (nread > 0) {
116                         if ((size_t) nread < sizeof (buf)) {
117                                 break;
118                         } else {
119                                 continue;
120                         }
121                 } else if (nread == 0) {
122                         break;
123                 } else if (errno == EAGAIN) {
124                         break;
125                 } else {
126                         fatal << "Error reading from MIDI port " << name() << endmsg;
127                         /*NOTREACHED*/
128                 }
129         }
130 }
131
132 /** Send a clock tick message.
133  * \return true on success.
134  */
135 bool
136 Port::clock (timestamp_t timestamp)
137 {
138         static byte clockmsg = 0xf8;
139         
140         if (_mode != O_RDONLY) {
141                 return midimsg (&clockmsg, 1, timestamp);
142         }
143         
144         return false;
145 }
146
147 void
148 Port::cycle_start (nframes_t nframes)
149 {
150         _currently_in_cycle = true;
151         _nframes_this_cycle = nframes;
152 }
153
154 void
155 Port::cycle_end ()
156 {
157         _currently_in_cycle = false;
158         _nframes_this_cycle = 0;
159 }
160
161 XMLNode&
162 Port::get_state () const
163 {
164         XMLNode* node = new XMLNode ("MIDI-port");
165         node->add_property ("tag", _tagname);
166         node->add_property ("device", _devname);
167         node->add_property ("mode", PortFactory::mode_to_string (_mode));
168         node->add_property ("type", get_typestring());
169
170         return *node;
171 }
172
173 void
174 Port::set_state (const XMLNode& /*node*/)
175 {
176         // relax
177 }
178
179 void
180 Port::gtk_read_callback (void *ptr, int /*fd*/, int /*cond*/)
181 {
182         byte buf[64];
183         ((Port *)ptr)->read (buf, sizeof (buf));
184 }
185
186 void
187 Port::write_callback (byte *msg, unsigned int len, void *ptr)
188 {
189         ((Port *)ptr)->write (msg, len, 0);
190 }
191
192 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
193 {
194         using namespace std;
195         os << "MIDI::Port { ";
196         os << "device: " << port.device();
197         os << "; ";
198         os << "name: " << port.name();
199         os << "; ";
200         os << "type: " << port.type();
201         os << "; ";
202         os << "mode: " << port.mode();
203         os << "; ";
204         os << "ok: " << port.ok();
205         os << "; ";
206         os << " }";
207         return os;
208 }
209
210 Port::Descriptor::Descriptor (const XMLNode& node)
211 {
212         const XMLProperty *prop;
213         bool have_tag = false;
214         bool have_device = false;
215         bool have_type = false;
216         bool have_mode = false;
217
218         if ((prop = node.property ("tag")) != 0) {
219                 tag = prop->value();
220                 have_tag = true;
221         }
222
223         if ((prop = node.property ("device")) != 0) {
224                 device = prop->value();
225                 have_device = true;
226         }
227
228         if ((prop = node.property ("type")) != 0) {
229                 type = PortFactory::string_to_type (prop->value());
230                 have_type = true;
231         }
232
233         if ((prop = node.property ("mode")) != 0) {
234                 mode = PortFactory::string_to_mode (prop->value());
235                 have_mode = true;
236         }
237
238         if (!have_tag || !have_device || !have_type || !have_mode) {
239                 throw failed_constructor();
240         }
241 }
242