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