ddbbcf7b5e002d70909197546420398aafd0b166
[ardour.git] / libs / ardour / ardour / midi_playlist.h
1 /*
2  * Copyright (C) 2006-2016 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
6  * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef __ardour_midi_playlist_h__
24 #define __ardour_midi_playlist_h__
25
26 #include <vector>
27 #include <list>
28
29 #include <boost/utility.hpp>
30
31 #include "ardour/ardour.h"
32 #include "ardour/midi_cursor.h"
33 #include "ardour/midi_model.h"
34 #include "ardour/midi_state_tracker.h"
35 #include "ardour/note_fixer.h"
36 #include "ardour/playlist.h"
37 #include "evoral/Note.hpp"
38 #include "evoral/Parameter.hpp"
39 #include "ardour/rt_midibuffer.h"
40
41 namespace Evoral {
42 template<typename Time> class EventSink;
43 class                         Beats;
44 }
45
46 namespace ARDOUR
47 {
48
49 class BeatsSamplesConverter;
50 class MidiChannelFilter;
51 class MidiRegion;
52 class Session;
53 class Source;
54
55 template<typename T> class MidiRingBuffer;
56
57 class LIBARDOUR_API MidiPlaylist : public ARDOUR::Playlist
58 {
59 public:
60         MidiPlaylist (Session&, const XMLNode&, bool hidden = false);
61         MidiPlaylist (Session&, std::string name, bool hidden = false);
62         MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other, std::string name, bool hidden = false);
63
64         /** This constructor does NOT notify others (session) */
65         MidiPlaylist (boost::shared_ptr<const MidiPlaylist> other,
66                       samplepos_t                           start,
67                       samplecnt_t                           cnt,
68                       std::string                           name,
69                       bool                                  hidden = false);
70
71         ~MidiPlaylist ();
72
73         /** Read a range from the playlist into an event sink.
74          *
75          * @param buf Destination for events.
76          * @param start First sample of read range.
77          * @param cnt Number of samples in read range.
78          * @param loop_range If non-null, all event times will be mapped into this loop range.
79          * @param chan_n Must be 0 (this is the audio-style "channel", where each
80          * channel is backed by a separate region, not MIDI channels, which all
81          * exist in the same region and are not handled here).
82          * @param filter Channel filter to apply or NULL to disable filter
83          * @return The number of samples read (time, not an event count).
84          */
85         samplecnt_t read (Evoral::EventSink<samplepos_t>& buf,
86                          samplepos_t                      start,
87                          samplecnt_t                      cnt,
88                          Evoral::Range<samplepos_t>*      loop_range,
89                          uint32_t                         chan_n = 0,
90                          MidiChannelFilter*               filter = NULL);
91
92         void render (MidiChannelFilter*);
93         RTMidiBuffer* rendered();
94
95         int set_state (const XMLNode&, int version);
96
97         bool destroy_region (boost::shared_ptr<Region>);
98         void _split_region (boost::shared_ptr<Region>, const MusicSample& position);
99
100         void set_note_mode (NoteMode m) { _note_mode = m; }
101
102         std::set<Evoral::Parameter> contained_automation();
103
104         /** Clear all note trackers. */
105         void reset_note_trackers ();
106
107         /** Resolve all pending notes and clear all note trackers.
108          *
109          * @param dst Sink to write note offs to.
110          * @param time Time stamp of all written note offs.
111          */
112         void resolve_note_trackers (Evoral::EventSink<samplepos_t>& dst, samplepos_t time);
113
114 protected:
115         void remove_dependents (boost::shared_ptr<Region> region);
116         void region_going_away (boost::weak_ptr<Region> region);
117
118 private:
119         typedef Evoral::Note<Temporal::Beats> Note;
120         typedef Evoral::Event<samplepos_t>   Event;
121
122         struct RegionTracker : public boost::noncopyable {
123                 MidiCursor       cursor;   ///< Cursor (iterator and read state)
124                 MidiStateTracker tracker;  ///< Active note tracker
125                 NoteFixer        fixer;    ///< Edit compensation
126         };
127
128         typedef std::map< Region*, boost::shared_ptr<RegionTracker> > NoteTrackers;
129
130         void dump () const;
131
132         NoteTrackers _note_trackers;
133         NoteMode     _note_mode;
134         samplepos_t  _read_end;
135
136         RTMidiBuffer _rendered;
137 };
138
139 } /* namespace ARDOUR */
140
141 #endif /* __ardour_midi_playlist_h__ */