4f76200c3546e5f479b6f177369c4629a0ec6573
[ardour.git] / libs / ardour / ardour / midi_region.h
1 /*
2     Copyright (C) 2000-2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: midiregion.h 733 2006-08-01 17:19:38Z drobilla $
19 */
20
21 #ifndef __ardour_midi_region_h__
22 #define __ardour_midi_region_h__
23
24 #include <vector>
25
26 #include "evoral/Beats.hpp"
27 #include "evoral/Range.hpp"
28
29 #include "ardour/ardour.h"
30 #include "ardour/region.h"
31
32 class XMLNode;
33
34 namespace ARDOUR {
35         namespace Properties {
36                 LIBARDOUR_API extern PBD::PropertyDescriptor<Evoral::Beats> start_beats;
37                 LIBARDOUR_API extern PBD::PropertyDescriptor<Evoral::Beats> length_beats;
38         }
39 }
40
41 namespace Evoral {
42 template<typename Time> class EventSink;
43 }
44
45 namespace ARDOUR {
46
47 class MidiChannelFilter;
48 class MidiFilter;
49 class MidiModel;
50 class MidiSource;
51 class MidiStateTracker;
52 class Playlist;
53 class Route;
54 class Session;
55
56 template<typename T> class MidiRingBuffer;
57
58 class LIBARDOUR_API MidiRegion : public Region
59 {
60   public:
61         static void make_property_quarks ();
62
63         ~MidiRegion();
64
65         bool do_export (std::string path) const;
66
67         boost::shared_ptr<MidiRegion> clone (std::string path = std::string()) const;
68         boost::shared_ptr<MidiRegion> clone (boost::shared_ptr<MidiSource>) const;
69
70         boost::shared_ptr<MidiSource> midi_source (uint32_t n=0) const;
71
72         /* Stub Readable interface */
73         virtual framecnt_t read (Sample*, framepos_t /*pos*/, framecnt_t /*cnt*/, int /*channel*/) const { return 0; }
74         virtual framecnt_t readable_length() const { return length(); }
75
76         framecnt_t read_at (Evoral::EventSink<framepos_t>& dst,
77                             framepos_t position,
78                             framecnt_t dur,
79                             Evoral::Range<framepos_t>* loop_range,
80                             uint32_t  chan_n = 0,
81                             NoteMode  mode = Sustained,
82                             MidiStateTracker* tracker = 0,
83                             MidiChannelFilter* filter = 0) const;
84
85         framecnt_t master_read_at (MidiRingBuffer<framepos_t>& dst,
86                                    framepos_t position,
87                                    framecnt_t dur,
88                                    Evoral::Range<framepos_t>* loop_range,
89                                    uint32_t  chan_n = 0,
90                                    NoteMode  mode = Sustained) const;
91
92         XMLNode& state ();
93         int      set_state (const XMLNode&, int version);
94
95         int separate_by_channel (ARDOUR::Session&, std::vector< boost::shared_ptr<Region> >&) const;
96
97         /* automation */
98
99         boost::shared_ptr<Evoral::Control> control(const Evoral::Parameter& id, bool create=false);
100
101         virtual boost::shared_ptr<const Evoral::Control> control(const Evoral::Parameter& id) const;
102
103         /* export */
104
105         boost::shared_ptr<MidiModel> model();
106         boost::shared_ptr<const MidiModel> model() const;
107
108         void fix_negative_start ();
109         Evoral::Beats start_beats () {return _start_beats.val(); }
110         void set_start_beats (const Evoral::Beats start_beats) {_start_beats = start_beats; }
111         Evoral::Beats length_beats () {return _length_beats.val(); }
112         void set_length_beats (const Evoral::Beats length_beats) {_length_beats = length_beats; }
113
114   protected:
115
116         virtual bool can_trim_start_before_source_start () const {
117                 return true;
118         }
119
120   private:
121         friend class RegionFactory;
122         PBD::Property<Evoral::Beats> _start_beats;
123         PBD::Property<Evoral::Beats> _length_beats;
124
125         MidiRegion (const SourceList&);
126         MidiRegion (boost::shared_ptr<const MidiRegion>);
127         MidiRegion (boost::shared_ptr<const MidiRegion>, frameoffset_t offset, const int32_t sub_num = 0);
128
129         framecnt_t _read_at (const SourceList&, Evoral::EventSink<framepos_t>& dst,
130                              framepos_t position,
131                              framecnt_t dur,
132                              Evoral::Range<framepos_t>* loop_range,
133                              uint32_t chan_n = 0,
134                              NoteMode mode = Sustained,
135                              MidiStateTracker* tracker = 0,
136                              MidiChannelFilter* filter = 0) const;
137
138         void register_properties ();
139         void post_set (const PBD::PropertyChange&);
140
141         void recompute_at_start ();
142         void recompute_at_end ();
143
144         void set_position_internal (framepos_t pos, bool allow_bbt_recompute, const int32_t sub_num);
145         void set_length_internal (framecnt_t len, const int32_t sub_num);
146         void set_start_internal (framecnt_t, const int32_t sub_num);
147         void trim_to_internal (framepos_t position, framecnt_t length, const int32_t sub_num);
148         void update_length_beats (const int32_t sub_num);
149
150         void model_changed ();
151         void model_automation_state_changed (Evoral::Parameter const &);
152
153         void set_start_beats_from_start_frames ();
154         void update_after_tempo_map_change (bool send_change = true);
155
156         std::set<Evoral::Parameter> _filtered_parameters; ///< parameters that we ask our source not to return when reading
157         PBD::ScopedConnection _model_connection;
158         PBD::ScopedConnection _source_connection;
159         PBD::ScopedConnection _model_contents_connection;
160
161         double _last_length_beats;
162 };
163
164 } /* namespace ARDOUR */
165
166
167 #endif /* __ardour_midi_region_h__ */