From 61eacc021a1783836f338638594e9bb5594c3d83 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 12 Aug 2011 14:40:04 +0000 Subject: prevent illegal data being used to set note values, velocities and channels git-svn-id: svn://localhost/ardour2/branches/3.0@9982 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/evoral/evoral/Note.hpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'libs/evoral/evoral') diff --git a/libs/evoral/evoral/Note.hpp b/libs/evoral/evoral/Note.hpp index d195e0d754..35b0b18f30 100644 --- a/libs/evoral/evoral/Note.hpp +++ b/libs/evoral/evoral/Note.hpp @@ -19,6 +19,7 @@ #ifndef EVORAL_NOTE_HPP #define EVORAL_NOTE_HPP +#include #include #include #include "evoral/MIDIEvent.hpp" @@ -61,12 +62,16 @@ public: return _on_event.channel(); } + private: + inline int clamp (int val, int low, int high) { return std::min (std::max (val, low), high); } + + public: inline void set_time(Time t) { _off_event.time() = t + length(); _on_event.time() = t; } - inline void set_note(uint8_t n) { _on_event.buffer()[1] = n; _off_event.buffer()[1] = n; } - inline void set_velocity(uint8_t n) { _on_event.buffer()[2] = n; } - inline void set_off_velocity(uint8_t n) { _off_event.buffer()[2] = n; } + inline void set_note(uint8_t n) { uint8_t nn = clamp (n, 0, 127); _on_event.buffer()[1] = nn; _off_event.buffer()[1] = nn; } + inline void set_velocity(uint8_t n) { _on_event.buffer()[2] = clamp (n, 0, 127); } + inline void set_off_velocity(uint8_t n) { _off_event.buffer()[2] = clamp (n, 0, 127); } inline void set_length(Time l) { _off_event.time() = _on_event.time() + l; } - inline void set_channel(uint8_t c) { _on_event.set_channel(c); _off_event.set_channel(c); } + inline void set_channel(uint8_t c) { uint8_t cc = clamp (c, 0, 16); _on_event.set_channel(cc); _off_event.set_channel(cc); } inline Event