e84d6b9dc4e2a29f1102e7db86f3d9f76df73eef
[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
26 #include "evoral/visibility.h"
27 #include "evoral/MIDIEvent.hpp"
28
29 namespace Evoral {
30
31 /** An abstract (protocol agnostic) note.
32  *
33  * Currently a note is defined as (on event, length, off event).
34  */
35 template<typename Time>
36 class LIBEVORAL_TEMPLATE_API Note {
37 public:
38         Note(uint8_t chan=0, Time time=0, Time len=0, uint8_t note=0, uint8_t vel=0x40);
39         Note(const Note<Time>& copy);
40         ~Note();
41
42         const Note<Time>& operator=(const Note<Time>& copy);
43
44         inline bool operator==(const Note<Time>& other) {
45                 return musical_time_equal (time(), other.time()) &&
46                         note() == other.note() &&
47                         musical_time_equal (length(), other.length()) &&
48                         velocity() == other.velocity() &&
49                         off_velocity() == other.off_velocity() &&
50                         channel()  == other.channel();
51         }
52
53         inline event_id_t id() const { return _on_event.id(); }
54         void set_id (event_id_t);
55
56         inline Time    time()         const { return _on_event.time(); }
57         inline Time    end_time()     const { return _off_event.time(); }
58         inline uint8_t note()         const { return _on_event.note(); }
59         inline uint8_t velocity()     const { return _on_event.velocity(); }
60         inline uint8_t off_velocity() const { return _off_event.velocity(); }
61         inline Time    length()       const { return _off_event.time() - _on_event.time(); }
62         inline uint8_t channel()      const {
63                 assert(_on_event.channel() == _off_event.channel());
64                 return _on_event.channel();
65         }
66
67 private:
68         inline int clamp(int val, int low, int high) {
69                 return std::min (std::max (val, low), high);
70         }
71
72 public:
73         inline void set_time(Time t) {
74                 _off_event.set_time(t + length());
75                 _on_event.set_time(t);
76         }
77         inline void set_note(uint8_t n) {
78                 const uint8_t nn = clamp(n, 0, 127);
79                 _on_event.buffer()[1] = nn;
80                 _off_event.buffer()[1] = nn;
81         }
82         inline void set_velocity(uint8_t n) {
83                 _on_event.buffer()[2] = clamp(n, 0, 127);
84         }
85         inline void set_off_velocity(uint8_t n) {
86                 _off_event.buffer()[2] = clamp(n, 0, 127);
87         }
88         inline void set_length(Time l) {
89                 _off_event.set_time(_on_event.time() + l);
90         }
91         inline void set_channel(uint8_t c) {
92                 const uint8_t cc = clamp(c, 0, 16);
93                 _on_event.set_channel(cc);
94                 _off_event.set_channel(cc);
95         }
96
97         inline       Event<Time>& on_event()        { return _on_event; }
98         inline const Event<Time>& on_event()  const { return _on_event; }
99         inline       Event<Time>& off_event()       { return _off_event; }
100         inline const Event<Time>& off_event() const { return _off_event; }
101
102 private:
103         // Event buffers are self-contained
104         MIDIEvent<Time> _on_event;
105         MIDIEvent<Time> _off_event;
106 };
107
108 } // namespace Evoral
109
110 template<typename Time>
111 /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Note<Time>& n) {
112         o << "Note #" << n.id() << ": pitch = " << (int) n.note()
113           << " @ " << n.time() << " .. " << n.end_time()
114           << " velocity " << (int) n.velocity()
115           << " chn " << (int) n.channel();
116         return o;
117 }
118
119 #ifdef COMPILER_MSVC
120 #include "../src/Note.impl"
121 #endif
122
123 #endif // EVORAL_NOTE_HPP
124