952b0ab00f32ccec2782356ee7b7cd69d4f7e164
[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_cursor.h"
30 #include "ardour/midi_model.h"
31 #include "ardour/midi_state_tracker.h"
32 #include "ardour/note_fixer.h"
33 #include "ardour/playlist.h"
34 #include "evoral/Note.hpp"
35 #include "evoral/Parameter.hpp"
36
37 namespace Evoral {
38 template<typename Time> class EventSink;
39 class                         Beats;
40 }
41
42 namespace ARDOUR
43 {
44
45 class BeatsSamplesConverter;
46 class MidiChannelFilter;
47 class MidiRegion;
48 class Session;
49 class Source;
50
51 template<typename T> class MidiRingBuffer;
52
53 class LIBARDOUR_API MidiPlaylist : public ARDOUR::Playlist
54 {
55 public:
56         MidiPlaylist (Session&, const XMLNode&, bool hidden = false);
57         MidiPlaylist (Session&, std::string name, bool hidden = false);
58         MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, std::string name, bool hidden = false);
59
60         /** This constructor does NOT notify others (session) */
61         MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other,
62                       samplepos_t                            start,
63                       samplecnt_t                            cnt,
64                       std::string                           name,
65                       bool                                  hidden = false);
66
67         ~MidiPlaylist ();
68
69         /** Read a range from the playlist into an event sink.
70          *
71          * @param buf Destination for events.
72          * @param start First sample of read range.
73          * @param cnt Number of samples in read range.
74          * @param loop_range If non-null, all event times will be mapped into this loop range.
75          * @param chan_n Must be 0 (this is the audio-style "channel", where each
76          * channel is backed by a separate region, not MIDI channels, which all
77          * exist in the same region and are not handled here).
78          * @return The number of samples read (time, not an event count).
79          */
80         samplecnt_t read (Evoral::EventSink<samplepos_t>& buf,
81                          samplepos_t                     start,
82                          samplecnt_t                     cnt,
83                          Evoral::Range<samplepos_t>*     loop_range,
84                          uint32_t                       chan_n = 0,
85                          MidiChannelFilter*             filter = NULL);
86
87         int set_state (const XMLNode&, int version);
88
89         bool destroy_region (boost::shared_ptr<Region>);
90         void _split_region (boost::shared_ptr<Region>, const MusicSample& position);
91
92         void set_note_mode (NoteMode m) { _note_mode = m; }
93
94         std::set<Evoral::Parameter> contained_automation();
95
96         /** Handle a region edit during read.
97          *
98          * This must be called before the command is applied to the model.  Events
99          * are injected into the playlist output to compensate for edits to active
100          * notes and maintain coherent output and tracker state.
101          */
102         void region_edited(boost::shared_ptr<Region>         region,
103                            const MidiModel::NoteDiffCommand* cmd);
104
105         /** Clear all note trackers. */
106         void reset_note_trackers ();
107
108         /** Resolve all pending notes and clear all note trackers.
109          *
110          * @param dst Sink to write note offs to.
111          * @param time Time stamp of all written note offs.
112          */
113         void resolve_note_trackers (Evoral::EventSink<samplepos_t>& dst, samplepos_t time);
114
115 protected:
116         void remove_dependents (boost::shared_ptr<Region> region);
117         void region_going_away (boost::weak_ptr<Region> region);
118
119 private:
120         typedef Evoral::Note<Evoral::Beats> Note;
121         typedef Evoral::Event<samplepos_t>   Event;
122
123         struct RegionTracker : public boost::noncopyable {
124                 MidiCursor       cursor;   ///< Cursor (iterator and read state)
125                 MidiStateTracker tracker;  ///< Active note tracker
126                 NoteFixer        fixer;    ///< Edit compensation
127         };
128
129         typedef std::map< Region*, boost::shared_ptr<RegionTracker> > NoteTrackers;
130
131         void dump () const;
132
133         NoteTrackers _note_trackers;
134         NoteMode     _note_mode;
135         samplepos_t   _read_end;
136 };
137
138 } /* namespace ARDOUR */
139
140 #endif  /* __ardour_midi_playlist_h__ */
141
142