Merge branch 'clang' of https://github.com/axetota/ardour
[ardour.git] / libs / evoral / evoral / Note.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef EVORAL_NOTE_HPP
20 #define EVORAL_NOTE_HPP
21
22 #include <algorithm>
23 #include <glib.h>
24 #include <stdint.h>
25 #include "evoral/MIDIEvent.hpp"
26
27 namespace Evoral {
28
29 /** An abstract (protocol agnostic) note.
30  *
31  * Currently a note is defined as (on event, length, off event).
32  */
33 template<typename Time>
34 class Note {
35 public:
36         Note(uint8_t chan=0, Time time=0, Time len=0, uint8_t note=0, uint8_t vel=0x40);
37         Note(const Note<Time>& copy);
38         ~Note();
39
40         const Note<Time>& operator=(const Note<Time>& copy);
41
42         inline bool operator==(const Note<Time>& other) {
43                 return musical_time_equal (time(), other.time()) &&
44                         note() == other.note() &&
45                         musical_time_equal (length(), other.length()) &&
46                         velocity() == other.velocity() &&
47                         off_velocity() == other.off_velocity() &&
48                         channel()  == other.channel();
49         }
50
51         inline event_id_t id() const { return _on_event.id(); }
52         void set_id (event_id_t);
53
54         inline Time    time()         const { return _on_event.time(); }
55         inline Time    end_time()     const { return _off_event.time(); }
56         inline uint8_t note()         const { return _on_event.note(); }
57         inline uint8_t velocity()     const { return _on_event.velocity(); }
58         inline uint8_t off_velocity() const { return _off_event.velocity(); }
59         inline Time    length()       const { return _off_event.time() - _on_event.time(); }
60         inline uint8_t channel()      const {
61                 assert(_on_event.channel() == _off_event.channel());
62                 return _on_event.channel();
63         }
64
65 private:
66         inline int clamp(int val, int low, int high) {
67                 return std::min (std::max (val, low), high);
68         }
69
70 public:
71         inline void set_time(Time t) {
72                 _off_event.set_time(t + length());
73                 _on_event.set_time(t);
74         }
75         inline void set_note(uint8_t n) {
76                 const uint8_t nn = clamp(n, 0, 127);
77                 _on_event.buffer()[1] = nn;
78                 _off_event.buffer()[1] = nn;
79         }
80         inline void set_velocity(uint8_t n) {
81                 _on_event.buffer()[2] = clamp(n, 0, 127);
82         }
83         inline void set_off_velocity(uint8_t n) {
84                 _off_event.buffer()[2] = clamp(n, 0, 127);
85         }
86         inline void set_length(Time l) {
87                 _off_event.set_time(_on_event.time() + l);
88         }
89         inline void set_channel(uint8_t c) {
90                 const uint8_t cc = clamp(c, 0, 16);
91                 _on_event.set_channel(cc);
92                 _off_event.set_channel(cc);
93         }
94
95         inline       Event<Time>& on_event()        { return _on_event; }
96         inline const Event<Time>& on_event()  const { return _on_event; }
97         inline       Event<Time>& off_event()       { return _off_event; }
98         inline const Event<Time>& off_event() const { return _off_event; }
99
100 private:
101         // Event buffers are self-contained
102         MIDIEvent<Time> _on_event;
103         MIDIEvent<Time> _off_event;
104 };
105
106 } // namespace Evoral
107
108 template<typename Time>
109 std::ostream& operator<<(std::ostream& o, const Evoral::Note<Time>& n) {
110         o << "Note #" << n.id() << ": pitch = " << (int) n.note()
111           << " @ " << n.time() << " .. " << n.end_time()
112           << " velocity " << (int) n.velocity()
113           << " chn " << (int) n.channel();
114         return o;
115 }
116
117 #endif // EVORAL_NOTE_HPP
118