ff5239818056f0e4e0589c1f0e2ab295ed96eb58
[ardour.git] / libs / ardour / ardour / midi_model.h
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: Dave Robillard
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 #ifndef __ardour_midi_model_h__ 
22 #define __ardour_midi_model_h__
23
24 #include <boost/utility.hpp>
25 #include <ardour/types.h>
26 #include <ardour/midi_buffer.h>
27
28 namespace ARDOUR {
29
30 /** This is a slightly higher level (than MidiBuffer) model of MIDI note data.
31  * Currently it only represents note data, which is represented as complete
32  * note events (ie with a start time and a duration) rather than separate
33  * note on and off events (controller data is not here since it's represented
34  * as an AutomationList)
35  */
36 class MidiModel : public boost::noncopyable {
37 public:
38         struct Note {
39                 Note(double s=0, double d=0, uint8_t n=0, uint8_t v=0)
40                 : start(s), duration(d), note(n), velocity(v) {}
41
42                 double start;
43                 double duration;
44                 uint8_t note;
45                 uint8_t velocity;
46         };
47
48         MidiModel(size_t size=0);
49
50         void clear() { _notes.clear(); }
51
52         void start_write();
53         void end_write(bool delete_stuck=false);
54
55         /** Resizes vector if necessary (NOT realtime safe) */
56         void append(const MidiBuffer& data);
57         
58         /** Resizes vector if necessary (NOT realtime safe) */
59         void append(double time, size_t size, Byte* in_buffer);
60         
61         inline const Note& note_at(unsigned i) const { return _notes[i]; }
62
63         inline size_t n_notes() const { return _notes.size(); }
64
65         typedef std::vector<Note> Notes;
66         
67         struct NoteTimeComparator {
68                 inline bool operator() (const Note& a, const Note& b) const { 
69                         return a.start < b.start;
70                 }
71         };
72
73         inline       Notes& notes()       { return _notes; }
74         inline const Notes& notes() const { return _notes; }
75
76 private:
77
78         void append_note_on(double time, uint8_t note, uint8_t velocity);
79         void append_note_off(double time, uint8_t note);
80
81         Notes _notes;
82         Notes _write_notes;
83 };
84
85 } /* namespace ARDOUR */
86
87 #endif /* __ardour_midi_model_h__ */
88