Handle edits while playing precisely.
[ardour.git] / libs / ardour / ardour / midi_playlist.h
1 /*
2     Copyright (C) 2006 Paul Davis
3     Author: David Robillard
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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_midi_playlist_h__
21 #define __ardour_midi_playlist_h__
22
23 #include <vector>
24 #include <list>
25
26 #include <boost/utility.hpp>
27
28 #include "ardour/ardour.h"
29 #include "ardour/midi_model.h"
30 #include "ardour/midi_state_tracker.h"
31 #include "ardour/note_fixer.h"
32 #include "ardour/playlist.h"
33 #include "evoral/Beats.hpp"
34 #include "evoral/Note.hpp"
35 #include "evoral/Parameter.hpp"
36
37 namespace Evoral {
38 template<typename Time> class EventSink;
39 }
40
41 namespace ARDOUR
42 {
43
44 class BeatsFramesConverter;
45 class MidiRegion;
46 class Session;
47 class Source;
48
49 template<typename T> class MidiRingBuffer;
50
51 class LIBARDOUR_API MidiPlaylist : public ARDOUR::Playlist
52 {
53 public:
54         MidiPlaylist (Session&, const XMLNode&, bool hidden = false);
55         MidiPlaylist (Session&, std::string name, bool hidden = false);
56         MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, std::string name, bool hidden = false);
57
58         /** This constructor does NOT notify others (session) */
59         MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other,
60                       framepos_t                            start,
61                       framecnt_t                            cnt,
62                       std::string                           name,
63                       bool                                  hidden = false);
64
65         ~MidiPlaylist ();
66
67         /** Read a range from the playlist into an event sink.
68          *
69          * @param buf Destination for events.
70          * @param start First frame of read range.
71          * @param cnt Number of frames in read range.
72          * @param chan_n Must be 0 (this is the audio-style "channel", where each
73          * channel is backed by a separate region, not MIDI channels, which all
74          * exist in the same region and are not handled here).
75          * @return The number of frames read (time, not an event count).
76          */
77         framecnt_t read (Evoral::EventSink<framepos_t>& buf,
78                          framepos_t                     start,
79                          framecnt_t                     cnt,
80                          uint32_t                       chan_n = 0);
81
82         int set_state (const XMLNode&, int version);
83
84         bool destroy_region (boost::shared_ptr<Region>);
85
86         void set_note_mode (NoteMode m) { _note_mode = m; }
87
88         std::set<Evoral::Parameter> contained_automation();
89
90         /** Handle a region edit during read.
91          *
92          * This must be called before the command is applied to the model.  Events
93          * are injected into the playlist output to compensate for edits to active
94          * notes and maintain coherent output and tracker state.
95          */
96         void region_edited(boost::shared_ptr<Region>         region,
97                            const MidiModel::NoteDiffCommand* cmd);
98
99         /** Clear all note trackers. */
100         void reset_note_trackers ();
101
102         /** Resolve all pending notes and clear all note trackers.
103          *
104          * @param dst Sink to write note offs to.
105          * @param time Time stamp of all written note offs.
106          */
107         void resolve_note_trackers (Evoral::EventSink<framepos_t>& dst, framepos_t time);
108
109 protected:
110         void remove_dependents (boost::shared_ptr<Region> region);
111
112 private:
113         typedef Evoral::Note<Evoral::Beats> Note;
114         typedef Evoral::Event<framepos_t>   Event;
115
116         struct RegionTracker : public boost::noncopyable {
117                 MidiStateTracker tracker;  ///< Active note tracker
118                 NoteFixer        fixer;    ///< Edit compensation
119         };
120
121         typedef std::map< Region*, boost::shared_ptr<RegionTracker> > NoteTrackers;
122
123         void dump () const;
124
125         NoteTrackers _note_trackers;
126         NoteMode     _note_mode;
127         framepos_t   _read_end;
128 };
129
130 } /* namespace ARDOUR */
131
132 #endif  /* __ardour_midi_playlist_h__ */
133
134