Fix crash on startup if an LV2 plugin has a bad .ttl file.
[ardour.git] / libs / evoral / evoral / MIDIEvent.hpp
index 92372541427da5de6363846d1c554c352798dad5..f7df4980fe6988fe2625fb0bff6240fc1ebb13bb 100644 (file)
@@ -19,6 +19,7 @@
 #ifndef EVORAL_MIDI_EVENT_HPP
 #define EVORAL_MIDI_EVENT_HPP
 
+#include <cmath>
 #include <boost/shared_ptr.hpp>
 #include "evoral/Event.hpp"
 #include "evoral/midi_events.h"
@@ -34,23 +35,21 @@ namespace Evoral {
  * but the application must make sure the event actually contains
  * valid MIDI data for these functions to make sense.
  */
-template<typename T>
-struct MIDIEvent : public Event<T> {
-       MIDIEvent(EventType type=0, T t=0, uint32_t s=0, uint8_t* b=NULL, bool alloc=false)
-               : Event<T>(type, t, s, b, alloc)
+template<typename Time>
+struct MIDIEvent : public Event<Time> {
+       MIDIEvent(EventType type=0, Time time=0, uint32_t size=0, uint8_t* buf=NULL, bool alloc=false)
+               : Event<Time>(type, time, size, buf, alloc)
        {}
        
-       MIDIEvent(const Event<T>& copy, bool alloc)
-               : Event<T>(copy, alloc)
+       MIDIEvent(const Event<Time>& copy, bool alloc)
+               : Event<Time>(copy, alloc)
        {}
 
 #ifdef EVORAL_MIDI_XML
-       /** Event from XML ala http://www.midi.org/dtds/MIDIEvents10.dtd
-        */
+       /** Event from XML ala http://www.midi.org/dtds/MIDIEvents10.dtd */
        MIDIEvent(const XMLNode& event);
        
-       /** Event to XML ala http://www.midi.org/dtds/MIDIEvents10.dtd
-        */
+       /** Event to XML ala http://www.midi.org/dtds/MIDIEvents10.dtd */
        boost::shared_ptr<XMLNode> to_xml() const;
 #endif
 
@@ -70,6 +69,12 @@ struct MIDIEvent : public Event<T> {
        inline bool     is_channel_pressure()   const { return (type() == MIDI_CMD_CHANNEL_PRESSURE); }
        inline uint8_t  note()                  const { return (this->_buf[1]); }
        inline uint8_t  velocity()              const { return (this->_buf[2]); }
+        inline void     set_velocity(uint8_t value)   { this->_buf[2] = value; }
+        inline void     scale_velocity(float factor)  { 
+               if (factor < 0) factor = 0;
+               this->_buf[2] = (uint8_t) lrintf (this->_buf[2]*factor);
+               if (this->_buf[2] > 127) this->_buf[2] = 127;
+       }
        inline uint8_t  cc_number()             const { return (this->_buf[1]); }
        inline void     set_cc_number(uint8_t number) { this->_buf[1] = number; }
        inline uint8_t  cc_value()              const { return (this->_buf[2]); }
@@ -82,7 +87,7 @@ struct MIDIEvent : public Event<T> {
        inline void     set_pgm_number(uint8_t number){ this->_buf[1] = number; }
        inline uint8_t  aftertouch()            const { return (this->_buf[1]); }
        inline uint8_t  channel_pressure()      const { return (this->_buf[1]); }
-       inline bool     is_channel_event()      const { return (0x80 <= type()) && (type() <= 0xE0);    }
+       inline bool     is_channel_event()      const { return (0x80 <= type()) && (type() <= 0xE0); }
        inline bool     is_smf_meta_event()     const { return this->_buf[0] == 0xFF; }
        inline bool     is_sysex()              const { return this->_buf[0] == 0xF0
                                                            || this->_buf[0] == 0xF7; }