initial fixes to get build to work without new JACK MIDI, and to get it running with...
[ardour.git] / libs / ardour / midi_port.cc
1 /*
2     Copyright (C) 2006 Paul 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
19 #include <cassert>
20 #include <iostream>
21
22 #include <ardour/midi_port.h>
23 #include <ardour/data_type.h>
24 #include <midi++/jackcompat.h>
25
26 using namespace ARDOUR;
27 using namespace std;
28
29 MidiPort::MidiPort(jack_port_t* p)
30         : Port(p)
31         , _buffer(4096) // FIXME FIXME FIXME Jack needs to tell us this
32         , _nframes_this_cycle(0)
33 {
34         DataType dt(_type);
35         assert(dt == DataType::MIDI);
36
37         reset();
38
39
40 }
41
42
43 MidiPort::~MidiPort()
44 {
45 }
46
47 void
48 MidiPort::cycle_start (jack_nframes_t nframes)
49 {
50         _buffer.clear();
51         assert(_buffer.size() == 0);
52
53         _nframes_this_cycle = nframes;
54
55         if (_flags & JackPortIsOutput) {
56                 _buffer.silence(nframes);
57                 assert(_buffer.size() == 0);
58                 return;
59         }
60
61         // We're an input - copy Jack events to internal buffer
62         
63         void* jack_buffer = jack_port_get_buffer(_port, nframes);
64
65         const jack_nframes_t event_count
66                 = jack_midi_get_event_count(jack_buffer);
67
68         assert(event_count < _buffer.capacity());
69
70         MidiEvent ev;
71
72         // FIXME: too slow, event struct is copied twice (here and MidiBuffer::push_back)
73         for (jack_nframes_t i=0; i < event_count; ++i) {
74
75                 // This will fail to compile if we change MidiEvent to our own class
76                 jack_midi_event_get(static_cast<jack_midi_event_t*>(&ev), jack_buffer, i);
77
78                 _buffer.push_back(ev);
79                 // Convert note ons with velocity 0 to proper note offs
80                 // FIXME: Jack MIDI should guarantee this - does it?
81                 //if (ev->buffer[0] == MIDI_CMD_NOTE_ON && ev->buffer[2] == 0)
82                 //      ev->buffer[0] = MIDI_CMD_NOTE_OFF;
83         }
84
85         assert(_buffer.size() == event_count);
86
87         //if (_buffer.size() > 0)
88         //      cerr << "MIDIPort got " << event_count << " events." << endl;
89 }
90
91 void
92 MidiPort::cycle_end()
93 {
94         if (_flags & JackPortIsInput) {
95                 _nframes_this_cycle = 0;
96                 return;
97         }
98
99         // We're an output - copy events from internal buffer to Jack buffer
100         
101         void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle);
102
103         const jack_nframes_t event_count = _buffer.size();
104         
105         //if (event_count > 0)
106         //      cerr << "MIDIPort writing " << event_count << " events." << endl;
107
108         jack_midi_clear_buffer(jack_buffer);
109         for (jack_nframes_t i=0; i < event_count; ++i) {
110                 const jack_midi_event_t& ev = _buffer[i];
111                 assert(ev.time < _nframes_this_cycle);
112                 jack_midi_event_write(jack_buffer, ev.time, ev.buffer, ev.size);
113         }
114         
115         _nframes_this_cycle = 0;
116 }