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