move ownership of an RT MIDI buffer from DiskIO to MidiPlaylist
[ardour.git] / libs / ardour / ardour / note_fixer.h
1 /*
2  * Copyright (C) 2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #ifndef __ardour_note_fixer_h__
21 #define __ardour_note_fixer_h__
22
23 #include <list>
24
25 #include <boost/utility.hpp>
26
27 #include "temporal/beats.h"
28
29 #include "evoral/Note.hpp"
30
31 #include "ardour/midi_model.h"
32 #include "ardour/types.h"
33
34 namespace Evoral { template<typename Time> class EventSink; }
35
36 namespace ARDOUR {
37
38 class BeatsSamplesConverter;
39 class MidiStateTracker;
40 class TempoMap;
41
42 /** A tracker and compensator for note edit operations.
43  *
44  * This monitors edit operations sent to a model that affect active notes
45  * during a read, and maintains a queue of synthetic events that should be sent
46  * at the start of the next read to maintain coherent MIDI state.
47  */
48 class NoteFixer : public boost::noncopyable
49 {
50 public:
51         typedef Evoral::Note<Temporal::Beats> Note;
52
53         ~NoteFixer();
54
55         /** Clear all internal state. */
56         void clear();
57
58         /** Handle a region edit during read.
59          *
60          * This must be called before the command is applied to the model.  Events
61          * are enqueued to compensate for edits which should be later sent with
62          * emit() at the start of the next read.
63          *
64          * @param tempo_map The tempo-map
65          * @param cmd Command to compensate for.
66          * @param origin Timeline position of edited source.
67          * @param pos Current read position (last read end).
68          * @param active_notes currently active notes (read/write)
69          */
70         void prepare(TempoMap&                          tempo_map,
71                      const MidiModel::NoteDiffCommand*  cmd,
72                      samplepos_t                        origin,
73                      samplepos_t                        pos,
74                      std::set< boost::weak_ptr<Note> >& active_notes);
75
76         /** Emit any pending edit compensation events.
77          *
78          * @param dst Destination for events.
79          * @param pos Timestamp to be used for every event, should be the start of
80          * the read block immediately following any calls to prepare().
81          * @param tracker Tracker to update with emitted events.
82          */
83         void emit(Evoral::EventSink<samplepos_t>& dst,
84                   samplepos_t                     pos,
85                   MidiStateTracker&               tracker);
86
87 private:
88         typedef Evoral::Event<samplepos_t> Event;
89         typedef std::list<Event*>         Events;
90
91         /** Copy a beats event to a samples event with the given time stamp. */
92         Event* copy_event(samplepos_t time, const Evoral::Event<Temporal::Beats>& ev);
93
94         /** Return true iff `note` is active at `pos`. */
95         bool note_is_active(const BeatsSamplesConverter& converter,
96                             boost::shared_ptr<Note>      note,
97                             samplepos_t                  pos);
98
99         Events _events;
100 };
101
102 } /* namespace ARDOUR */
103
104 #endif /* __ardour_note_fixer_h__ */