Redraw MIDI region views on zoom and track height changes.
[ardour.git] / libs / ardour / midi_model.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3         Written by Dave Robillard, 2006
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <iostream>
22 #include <ardour/midi_model.h>
23 #include <ardour/types.h>
24
25 using namespace std;
26 using namespace ARDOUR;
27
28
29 MidiModel::MidiModel(size_t size)
30 : _events(size)
31 {
32 }
33
34 MidiModel::~MidiModel()
35 {
36         for (size_t i=0; i < _events.size(); ++i)
37                 delete _events[i].buffer;
38 }
39
40
41 /** Append contents of \a buf to model.  NOT realtime safe.
42  *
43  * Timestamps of events in \a buf are expected to be relative to
44  * the start of this model (t=0) and MUST be monotonically increasing
45  * and MUST be >= the latest event currently in the model.
46  *
47  * Events in buf are deep copied.
48  */
49 void
50 MidiModel::append(const MidiBuffer& buf)
51 {
52         for (size_t i=0; i < buf.size(); ++i) {
53                 const MidiEvent& buf_event = buf[i];
54                 assert(_events.empty() || buf_event.time >= _events.back().time);
55
56                 _events.push_back(buf_event);
57                 MidiEvent& my_event = _events.back();
58                 assert(my_event.time == buf_event.time);
59                 assert(my_event.size == buf_event.size);
60                 
61                 my_event.buffer = new Byte[my_event.size];
62                 memcpy(my_event.buffer, buf_event.buffer, my_event.size);
63         }
64 }
65
66
67 /** Append \a in_event to model.  NOT realtime safe.
68  *
69  * Timestamps of events in \a buf are expected to be relative to
70  * the start of this model (t=0) and MUST be monotonically increasing
71  * and MUST be >= the latest event currently in the model.
72  */
73 void
74 MidiModel::append(double time, size_t size, Byte* in_buffer)
75 {
76         assert(_events.empty() || time >= _events.back().time);
77
78         //cerr << "Model event: time = " << time << endl;
79
80         Byte* my_buffer = new Byte[size];
81         memcpy(my_buffer, in_buffer, size);
82         _events.push_back(MidiEvent(time, size, my_buffer));
83 }
84