Merge branch 'cairocanvas'
[ardour.git] / libs / backends / wavesaudio / waves_midiport.cc
1 /*
2     Copyright (C) 2013 Waves Audio Ltd.
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
20 #include "waves_midiport.h"
21 #include "waves_midi_event.h"
22
23 using namespace ARDOUR;
24
25 WavesMidiPort::WavesMidiPort (const std::string& port_name, PortFlags flags)
26     : WavesDataPort (port_name, flags)
27     , _midi_device (NULL)
28     , _waves_midi_buffer (port_name)
29 {       
30 }
31
32 struct MidiEventSorter {
33         bool operator() (const WavesMidiEvent* a, const WavesMidiEvent* b) {
34                 return *a < *b;
35         }
36 };
37
38 void* 
39 WavesMidiPort::get_buffer (pframes_t nframes)
40 {
41     if (is_input ()) {
42                 std::vector<WavesDataPort*>::const_iterator cit = get_connections ().begin ();
43         if (cit != get_connections ().end ()) {
44                         _waves_midi_buffer.clear ();
45                         WavesMidiBuffer& target = _waves_midi_buffer;
46
47                         do      {
48                                 /* In fact, the static casting to (const WavesMidiPort*) is not that safe.
49                                  * However, mixing the buffers is assumed in the time critical conditions.
50                                  * Base class WavesDataPort is supposed to provide enough consistentcy
51                                  * of the connections.
52                                  */
53                                 target += ((const WavesMidiPort*)*cit)->const_buffer ();
54                         }while((++cit) != get_connections ().end ());
55
56                         std::sort (target.begin (), target.end (), MidiEventSorter());
57                 }
58         }
59
60     return &_waves_midi_buffer;
61 }
62
63 void
64 WavesMidiPort::_wipe_buffer()
65 {
66         _waves_midi_buffer.clear ();
67 }