ardour
Note.hpp
Go to the documentation of this file.
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 
35 template<typename Time>
36 #ifdef COMPILER_MSVC
37 class LIBEVORAL_LOCAL Note {
38 #else
40 #endif
41 public:
42  Note(uint8_t chan=0, Time time=Time(), Time len=Time(), uint8_t note=0, uint8_t vel=0x40);
43  Note(const Note<Time>& copy);
44  ~Note();
45 
46  const Note<Time>& operator=(const Note<Time>& copy);
47 
48  inline bool operator==(const Note<Time>& other) {
49  return time() == other.time() &&
50  note() == other.note() &&
51  length() == other.length() &&
52  velocity() == other.velocity() &&
53  off_velocity() == other.off_velocity() &&
54  channel() == other.channel();
55  }
56 
57  inline event_id_t id() const { return _on_event.id(); }
58  void set_id (event_id_t);
59 
60  inline Time time() const { return _on_event.time(); }
61  inline Time end_time() const { return _off_event.time(); }
62  inline uint8_t note() const { return _on_event.note(); }
63  inline uint8_t velocity() const { return _on_event.velocity(); }
64  inline uint8_t off_velocity() const { return _off_event.velocity(); }
65  inline Time length() const { return _off_event.time() - _on_event.time(); }
66  inline uint8_t channel() const {
67  assert(_on_event.channel() == _off_event.channel());
68  return _on_event.channel();
69  }
70 
71 private:
72  inline int clamp(int val, int low, int high) {
73  return std::min (std::max (val, low), high);
74  }
75 
76 public:
77  inline void set_time(Time t) {
78  _off_event.set_time(t + length());
79  _on_event.set_time(t);
80  }
81  inline void set_note(uint8_t n) {
82  const uint8_t nn = clamp(n, 0, 127);
83  _on_event.buffer()[1] = nn;
84  _off_event.buffer()[1] = nn;
85  }
86  inline void set_velocity(uint8_t n) {
87  _on_event.buffer()[2] = clamp(n, 0, 127);
88  }
89  inline void set_off_velocity(uint8_t n) {
90  _off_event.buffer()[2] = clamp(n, 0, 127);
91  }
92  inline void set_length(Time l) {
93  _off_event.set_time(_on_event.time() + l);
94  }
95  inline void set_channel(uint8_t c) {
96  const uint8_t cc = clamp(c, 0, 16);
97  _on_event.set_channel(cc);
98  _off_event.set_channel(cc);
99  }
100 
101  inline Event<Time>& on_event() { return _on_event; }
102  inline const Event<Time>& on_event() const { return _on_event; }
103  inline Event<Time>& off_event() { return _off_event; }
104  inline const Event<Time>& off_event() const { return _off_event; }
105 
106 private:
107  // Event buffers are self-contained
110 };
111 
112 template<typename Time>
113 /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream& o, const Evoral::Note<Time>& n) {
114  o << "Note #" << n.id() << ": pitch = " << (int) n.note()
115  << " @ " << n.time() << " .. " << n.end_time()
116  << " velocity " << (int) n.velocity()
117  << " chn " << (int) n.channel();
118  return o;
119 }
120 
121 } // namespace Evoral
122 
123 #ifdef COMPILER_MSVC
124 #include "../src/Note.impl"
125 #endif
126 
127 #endif // EVORAL_NOTE_HPP
128 
Event< Time > & on_event()
Definition: Note.hpp:101
void set_velocity(uint8_t n)
Definition: Note.hpp:86
uint8_t velocity() const
Definition: Note.hpp:63
int32_t event_id_t
Definition: types.hpp:40
Definition: note.h:32
LIBEVORAL_API uint64_t Note
Definition: debug.cpp:4
bool operator==(const Note< Time > &other)
Definition: Note.hpp:48
Time time() const
Definition: Note.hpp:60
void set_length(Time l)
Definition: Note.hpp:92
MIDIEvent< Time > _off_event
Definition: Note.hpp:109
uint8_t note() const
Definition: Note.hpp:62
void set_channel(uint8_t c)
Definition: Note.hpp:95
void set_note(uint8_t n)
Definition: Note.hpp:81
Event< Time > & off_event()
Definition: Note.hpp:103
event_id_t id() const
Definition: Note.hpp:57
const Event< Time > & off_event() const
Definition: Note.hpp:104
uint8_t channel() const
Definition: Note.hpp:66
const Event< Time > & on_event() const
Definition: Note.hpp:102
void set_time(Time t)
Definition: Note.hpp:77
#define LIBEVORAL_LOCAL
Definition: visibility.h:48
uint8_t off_velocity() const
Definition: Note.hpp:64
Time end_time() const
Definition: Note.hpp:61
#define LIBEVORAL_TEMPLATE_API
Definition: visibility.h:46
void set_off_velocity(uint8_t n)
Definition: Note.hpp:89
static double clamp(double val, double min, double max)
Time length() const
Definition: Note.hpp:65
MIDIEvent< Time > _on_event
Definition: Note.hpp:108
int clamp(int val, int low, int high)
Definition: Note.hpp:72
LIBARDOUR_API PBD::PropertyDescriptor< framecnt_t > length
Definition: region.cc:64