0f649b337096730366a9d8cfac530c3ab1014ffe
[ardour.git] / libs / ardour / ardour / note.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_note_h__ 
22 #define __ardour_note_h__
23
24 #include <stdint.h>
25 #include <midi++/event.h>
26
27 namespace ARDOUR {
28
29
30 /** A MIDI Note.
31  *
32  * A note is (unfortunately) special and not just another MIDI::Event as it
33  * has a duration and two separate MIDI events (on and off).
34  */
35 class Note {
36 public:
37         Note(uint8_t chan=0, double time=0, double dur=0, uint8_t note=0, uint8_t vel=0x40);
38         Note(const Note& copy);
39         ~Note();
40
41         const Note& operator=(const Note& copy);
42
43         inline bool operator==(const Note& other)
44         { return time() == other.time() && 
45                  note() == other.note() && 
46                  duration() == other.duration() &&
47                  velocity() == other.velocity() &&
48                  channel()  == other.channel();
49         }
50
51         inline double  time()     const { return _on_event.time(); }
52         inline double  end_time() const { return _off_event.time(); }
53         inline uint8_t note()     const { return _on_event.note(); }
54         inline uint8_t velocity() const { return _on_event.velocity(); }
55         inline double  duration() const { return _off_event.time() - _on_event.time(); }
56         inline uint8_t channel()  const { 
57                 assert(_on_event.channel() == _off_event.channel()); 
58             return _on_event.channel(); 
59         }
60
61         inline void set_time(double t)      { _off_event.time() = t + duration(); _on_event.time() = t; }
62         inline void set_note(uint8_t n)     { _on_event.buffer()[1] = n; _off_event.buffer()[1] = n; }
63         inline void set_velocity(uint8_t n) { _on_event.buffer()[2] = n; }
64         inline void set_duration(double d)  { _off_event.time() = _on_event.time() + d; }
65         inline void set_channel(uint8_t c)  { _on_event.set_channel(c);  _off_event.set_channel(c); }
66
67         inline MIDI::Event& on_event()  { return _on_event; }
68         inline MIDI::Event& off_event() { return _off_event; }
69
70         inline const MIDI::Event& on_event()  const { return _on_event; }
71         inline const MIDI::Event& off_event() const { return _off_event; }
72
73 private:
74         // Event buffers are self-contained
75         MIDI::Event _on_event;
76         MIDI::Event _off_event;
77 };
78
79
80 } // namespace ARDOUR
81
82 #endif /* __ardour_note_h__ */