More signal path cleanup, IO now has one deliver_output function that should do the...
[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(NULL)
30         , _nframes_this_cycle(0)
31 {
32         DataType dt(_type);
33         assert(dt == DataType::MIDI);
34
35         reset();
36
37         _buffer = new MidiBuffer(4096); // FIXME FIXME FIXME
38 }
39
40
41 MidiPort::~MidiPort()
42 {
43         delete _buffer;
44 }
45
46 void
47 MidiPort::cycle_start (jack_nframes_t nframes)
48 {
49         _nframes_this_cycle = nframes;
50
51         if (_flags & JackPortIsOutput) {
52                 _buffer->set_size(0);
53                 return;
54         }
55
56         // We're an input - copy Jack events to internal buffer
57         
58         void* jack_buffer = jack_port_get_buffer(_port, nframes);
59
60         const jack_nframes_t event_count
61                 = jack_midi_port_get_info(jack_buffer, nframes)->event_count;
62
63         assert(event_count < _buffer->capacity());
64
65         for (jack_nframes_t i=0; i < event_count; ++i) {
66                 jack_midi_event_t* const ev = &_buffer->data()[i];
67                 jack_midi_event_get(ev, jack_buffer, i, nframes);
68
69                 // Convert note ons with velocity 0 to proper note offs
70                 // FIXME: Jack MIDI should guarantee this - does it?
71                 //if (ev->buffer[0] == MIDI_CMD_NOTE_ON && ev->buffer[2] == 0)
72                 //      ev->buffer[0] = MIDI_CMD_NOTE_OFF;
73         }
74
75         _buffer->set_size(event_count);
76         
77         //if (_buffer->size() > 0)
78         //      cerr << "MIDIPort got " << event_count << " events." << endl;
79 }
80
81 void
82 MidiPort::cycle_end()
83 {
84         if (_flags & JackPortIsInput) {
85                 _nframes_this_cycle = 0; // catch any oopses
86                 return;
87         }
88
89         // We're an output - copy events from internal buffer to Jack buffer
90         
91         void* jack_buffer = jack_port_get_buffer(_port, _nframes_this_cycle);
92
93         const jack_nframes_t event_count = _buffer->size();
94
95         jack_midi_clear_buffer(jack_buffer, _nframes_this_cycle);
96         for (jack_nframes_t i=0; i < event_count; ++i) {
97                 const jack_midi_event_t& ev = _buffer->data()[i];
98                 jack_midi_event_write(jack_buffer, ev.time, ev.buffer, ev.size, _nframes_this_cycle);
99         }
100         
101         _nframes_this_cycle = 0; // catch oopses
102 }