Fix loading, recording & saving MIDI with PolyKeyPressure events.
[ardour.git] / libs / evoral / src / Sequence.cpp
index c8a0dd4f23aac62528f35db50635692901e61f52..1d518c3f07d765c5054e88018ed5bdca25004d41 100644 (file)
@@ -31,6 +31,7 @@
 #include "pbd/compose.h"
 #include "pbd/error.h"
 
+#include "evoral/Beats.hpp"
 #include "evoral/Control.hpp"
 #include "evoral/ControlList.hpp"
 #include "evoral/ControlSet.hpp"
@@ -40,7 +41,7 @@
 #include "evoral/TypeMap.hpp"
 #include "evoral/midi_util.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace PBD;
@@ -244,7 +245,7 @@ Sequence<Time>::const_iterator::choose_next(Time earliest_t)
 
        // Use the next note off iff it's earlier or the same time as the note on
        if ((!_active_notes.empty())) {
-               if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
+               if (_type == NIL || _active_notes.top()->end_time().to_double() <= earliest_t.to_double()) {
                        _type      = NOTE_OFF;
                        earliest_t = _active_notes.top()->end_time();
                }
@@ -284,18 +285,18 @@ Sequence<Time>::const_iterator::set_event()
        switch (_type) {
        case NOTE_ON:
                DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
-               *_event = (*_note_iter)->on_event();
+               _event->assign ((*_note_iter)->on_event());
                _active_notes.push(*_note_iter);
                break;
        case NOTE_OFF:
                DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
                assert(!_active_notes.empty());
-               *_event = _active_notes.top()->off_event();
+               _event->assign (_active_notes.top()->off_event());
                // We don't pop the active note until we increment past it
                break;
        case SYSEX:
                DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
-               *_event = *(*_sysex_iter);
+               _event->assign (*(*_sysex_iter));
                break;
        case CONTROL:
                DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
@@ -303,7 +304,7 @@ Sequence<Time>::const_iterator::set_event()
                break;
        case PATCH_CHANGE:
                DEBUG_TRACE(DEBUG::Sequence, "iterator = program change\n");
-               *_event = (*_patch_change_iter)->message (_active_patch_change_message);
+               _event->assign ((*_patch_change_iter)->message (_active_patch_change_message));
                break;
        default:
                _is_end = true;
@@ -336,6 +337,7 @@ Sequence<Time>::const_iterator::operator++()
                   || ev.is_pgm_change()
                   || ev.is_pitch_bender()
                   || ev.is_channel_pressure()
+                  || ev.is_poly_pressure()
                   || ev.is_sysex()) ) {
                cerr << "WARNING: Unknown event (type " << _type << "): " << hex
                     << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
@@ -534,6 +536,7 @@ Sequence<Time>::control_to_midi_event(
 
        uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
        ev->set_event_type(_type_map.midi_event_type(midi_type));
+       ev->set_id(-1);
        switch (midi_type) {
        case MIDI_CMD_CONTROL:
                assert(iter.list.get());
@@ -571,6 +574,19 @@ Sequence<Time>::control_to_midi_event(
                ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
                break;
 
+       case MIDI_CMD_NOTE_PRESSURE:
+               assert(iter.list.get());
+               assert(iter.list->parameter().channel() < 16);
+               assert(iter.list->parameter().id() <= INT8_MAX);
+               assert(iter.y <= INT8_MAX);
+
+               ev->set_time(Time(iter.x));
+               ev->realloc(3);
+               ev->buffer()[0] = MIDI_CMD_NOTE_PRESSURE + iter.list->parameter().channel();
+               ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
+               ev->buffer()[2] = (uint8_t)iter.y;
+               break;
+
        case MIDI_CMD_CHANNEL_PRESSURE:
                assert(iter.list.get());
                assert(iter.list->parameter().channel() < 16);
@@ -722,7 +738,7 @@ Sequence<Time>::remove_note_unlocked(const constNotePtr note)
         */
 
        typename Sequence<Time>::Notes::iterator i;
-               
+
        for (i = note_lower_bound(note->time()); i != _notes.end() && (*i)->time() == note->time(); ++i) {
 
                if (*i == note) {
@@ -762,19 +778,19 @@ Sequence<Time>::remove_note_unlocked(const constNotePtr note)
                 * in this scenario, we have no choice other than to linear
                 * search the list of notes and find the note by ID.
                 */
-               
+
                for (i = _notes.begin(); i != _notes.end(); ++i) {
 
                        if ((*i)->id() == note->id()) {
-                               
+
                                DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\tID-based pass, erasing note #%2 %3 @ %4\n", this, (*i)->id(), (int)(*i)->note(), (*i)->time()));
                                _notes.erase (i);
-                               
+
                                if (note->note() == _lowest_note || note->note() == _highest_note) {
-                                       
+
                                        _lowest_note = 127;
                                        _highest_note = 0;
-                                       
+
                                        for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
                                                if ((*ii)->note() < _lowest_note)
                                                        _lowest_note = (*ii)->note();
@@ -782,18 +798,18 @@ Sequence<Time>::remove_note_unlocked(const constNotePtr note)
                                                        _highest_note = (*ii)->note();
                                        }
                                }
-                               
+
                                erased = true;
                                id_matched = true;
                                break;
                        }
                }
        }
-       
+
        if (erased) {
 
                Pitches& p (pitches (note->channel()));
-               
+
                typename Pitches::iterator j;
 
                /* if we had to ID-match above, we can't expect to find it in
@@ -817,11 +833,11 @@ Sequence<Time>::remove_note_unlocked(const constNotePtr note)
                         * notes by channel+time. We care only about its note number
                         * so the search_note has all other properties unset.
                         */
-                       
+
                        NotePtr search_note (new Note<Time>(0, Time(), Time(), note->note(), 0));
 
                        for (j = p.lower_bound (search_note); j != p.end() && (*j)->note() == note->note(); ++j) {
-                               
+
                                if ((*j) == note) {
                                        DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*j)->note(), (*j)->time()));
                                        p.erase (j);
@@ -835,7 +851,7 @@ Sequence<Time>::remove_note_unlocked(const constNotePtr note)
                }
 
                _edited = true;
-       
+
        } else {
                cerr << "Unable to find note to erase matching " << *note.get() << endmsg;
        }
@@ -931,6 +947,8 @@ Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
                        ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
                                           | (0x7F & ev.pitch_bender_lsb())),
                        evid);
+       } else if (ev.is_poly_pressure()) {
+               append_control_unlocked (Parameter (ev.event_type(), ev.channel(), ev.poly_note()), ev.time(), ev.poly_pressure(), evid);
        } else if (ev.is_channel_pressure()) {
                append_control_unlocked(
                        Parameter(ev.event_type(), ev.channel()),
@@ -1043,7 +1061,7 @@ Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, doubl
        DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3 = %4 # controls: %5\n",
                                                      this, _type_map.to_symbol(param), time, value, _controls.size()));
        boost::shared_ptr<Control> c = control(param, true);
-       c->list()->add (time.to_double(), value);
+       c->list()->add (time.to_double(), value, true, false);
        /* XXX control events should use IDs */
 }