Ardour  9.0-pre0-582-g084a23a80d
Note.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 David Robillard <d@drobilla.net>
3  * Copyright (C) 2009-2014 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2014 John Emmas <john@creativepost.co.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef EVORAL_NOTE_HPP
22 #define EVORAL_NOTE_HPP
23 
24 #include <algorithm>
25 #include <assert.h>
26 #include <glib.h>
27 #include <stdint.h>
28 
29 #include "evoral/visibility.h"
30 #include "evoral/Event.h"
31 
32 namespace Evoral {
33 
38 template<typename Time>
39 #ifdef COMPILER_MSVC
40 class LIBEVORAL_LOCAL Note {
41 #else
43 #endif
44 public:
45  Note(uint8_t chan=0, Time time=Time(), Time len=Time(), uint8_t note=0, uint8_t vel=0x40);
46  Note(const Note<Time>& copy);
47  ~Note();
48 
49  inline bool operator==(const Note<Time>& other) {
50  return time() == other.time() &&
51  note() == other.note() &&
52  length() == other.length() &&
53  velocity() == other.velocity() &&
54  off_velocity() == other.off_velocity() &&
55  channel() == other.channel();
56  }
57 
58  inline event_id_t id() const { return _on_event.id(); }
60 
61  inline Time time() const { return _on_event.time(); }
62  inline Time end_time() const { return _off_event.time(); }
63  inline uint8_t note() const { return _on_event.note(); }
64  inline uint8_t velocity() const { return _on_event.velocity(); }
65  inline uint8_t off_velocity() const { return _off_event.velocity(); }
66  inline Time length() const { return _off_event.time() - _on_event.time(); }
67  inline uint8_t channel() const {
68  assert(_on_event.channel() == _off_event.channel());
69  return _on_event.channel();
70  }
71 
72 private:
73  const Note<Time>& operator=(const Note<Time>& copy); // undefined (unsafe)
74 
75  inline uint8_t clamp(int val, int low, int high) {
76  const int r = std::min (std::max (val, low), high);
77  assert (r < 256 && r >= 0);
78  return (uint8_t) r;
79  }
80 
81 public:
82  inline void set_time(Time t) {
83  _off_event.set_time(t + length());
84  _on_event.set_time(t);
85  }
86  inline void set_note(uint8_t n) {
87  const uint8_t nn = clamp(n, 0, 127);
88  _on_event.buffer()[1] = nn;
89  _off_event.buffer()[1] = nn;
90  }
91  inline void set_velocity(uint8_t n) {
92  _on_event.buffer()[2] = clamp(n, 0, 127);
93  }
94  inline void set_off_velocity(uint8_t n) {
95  _off_event.buffer()[2] = clamp(n, 0, 127);
96  }
97  inline void set_length(Time l) {
98  _off_event.set_time(_on_event.time() + l);
99  }
100  inline void set_channel(uint8_t c) {
101  const uint8_t cc = clamp(c, 0, 16);
102  _on_event.set_channel(cc);
103  _off_event.set_channel(cc);
104  }
105 
106  inline Event<Time>& on_event() { return _on_event; }
107  inline const Event<Time>& on_event() const { return _on_event; }
108  inline Event<Time>& off_event() { return _off_event; }
109  inline const Event<Time>& off_event() const { return _off_event; }
110 
111 private:
112  // Event buffers are self-contained
115 };
116 
117 template<typename Time>
118 /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Note<Time>& n) {
119  o << "Note #" << n.id() << ": pitch = " << (int) n.note()
120  << " @ " << n.time() << " .. " << n.end_time()
121  << " velocity " << (int) n.velocity()
122  << " chn " << (int) n.channel();
123  return o;
124 }
125 
126 } // namespace Evoral
127 
128 #ifdef COMPILER_MSVC
129 #include "../src/Note.impl"
130 #endif
131 
132 #endif // EVORAL_NOTE_HPP
133 
uint8_t off_velocity() const
Definition: Note.h:65
void set_length(Time l)
Definition: Note.h:97
bool operator==(const Note< Time > &other)
Definition: Note.h:49
void set_id(event_id_t)
const Event< Time > & on_event() const
Definition: Note.h:107
void set_off_velocity(uint8_t n)
Definition: Note.h:94
uint8_t velocity() const
Definition: Note.h:64
Time length() const
Definition: Note.h:66
uint8_t note() const
Definition: Note.h:63
void set_note(uint8_t n)
Definition: Note.h:86
Event< Time > _off_event
Definition: Note.h:114
void set_time(Time t)
Definition: Note.h:82
void set_velocity(uint8_t n)
Definition: Note.h:91
event_id_t id() const
Definition: Note.h:58
Event< Time > _on_event
Definition: Note.h:113
const Event< Time > & off_event() const
Definition: Note.h:109
uint8_t channel() const
Definition: Note.h:67
Event< Time > & off_event()
Definition: Note.h:108
Note(const Note< Time > &copy)
Time time() const
Definition: Note.h:61
Event< Time > & on_event()
Definition: Note.h:106
Time end_time() const
Definition: Note.h:62
const Note< Time > & operator=(const Note< Time > &copy)
Note(uint8_t chan=0, Time time=Time(), Time len=Time(), uint8_t note=0, uint8_t vel=0x40)
uint8_t clamp(int val, int low, int high)
Definition: Note.h:75
void set_channel(uint8_t c)
Definition: Note.h:100
#define LIBEVORAL_LOCAL
#define LIBEVORAL_TEMPLATE_API
PBD::PropertyDescriptor< timecnt_t > length
Definition: editor.h:86
int32_t event_id_t
std::ostream & operator<<(std::ostream &o, const Evoral::Event< Time > &ev)
Definition: Event.h:223