rework snap
[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/midi_cursor.h"
31 #include "ardour/region.h"
32
33 class XMLNode;
34
35 namespace ARDOUR {
36         namespace Properties {
37                 LIBARDOUR_API extern PBD::PropertyDescriptor<double> start_beats;
38                 LIBARDOUR_API extern PBD::PropertyDescriptor<double> length_beats;
39         }
40 }
41
42 namespace Evoral {
43 template<typename Time> class EventSink;
44 }
45
46 namespace ARDOUR {
47
48 class MidiChannelFilter;
49 class MidiFilter;
50 class MidiModel;
51 class MidiSource;
52 class MidiStateTracker;
53 class Playlist;
54 class Route;
55 class Session;
56
57 template<typename T> class MidiRingBuffer;
58
59 class LIBARDOUR_API MidiRegion : public Region
60 {
61   public:
62         static void make_property_quarks ();
63
64         ~MidiRegion();
65
66         bool do_export (std::string path) const;
67
68         boost::shared_ptr<MidiRegion> clone (std::string path = std::string()) const;
69         boost::shared_ptr<MidiRegion> clone (boost::shared_ptr<MidiSource>) const;
70
71         boost::shared_ptr<MidiSource> midi_source (uint32_t n=0) const;
72
73         /* Stub Readable interface */
74         virtual framecnt_t read (Sample*, framepos_t /*pos*/, framecnt_t /*cnt*/, int /*channel*/) const { return 0; }
75         virtual framecnt_t readable_length() const { return length(); }
76
77         framecnt_t read_at (Evoral::EventSink<framepos_t>& dst,
78                             framepos_t position,
79                             framecnt_t dur,
80                             Evoral::Range<framepos_t>* loop_range,
81                             MidiCursor& cursor,
82                             uint32_t  chan_n = 0,
83                             NoteMode  mode = Sustained,
84                             MidiStateTracker* tracker = 0,
85                             MidiChannelFilter* filter = 0) const;
86
87         framecnt_t master_read_at (MidiRingBuffer<framepos_t>& dst,
88                                    framepos_t position,
89                                    framecnt_t dur,
90                                    Evoral::Range<framepos_t>* loop_range,
91                                    MidiCursor& cursor,
92                                    uint32_t  chan_n = 0,
93                                    NoteMode  mode = Sustained) const;
94
95         XMLNode& state ();
96         int      set_state (const XMLNode&, int version);
97
98         int separate_by_channel (ARDOUR::Session&, std::vector< boost::shared_ptr<Region> >&) const;
99
100         /* automation */
101
102         boost::shared_ptr<Evoral::Control> control(const Evoral::Parameter& id, bool create=false);
103
104         virtual boost::shared_ptr<const Evoral::Control> control(const Evoral::Parameter& id) const;
105
106         /* export */
107
108         boost::shared_ptr<MidiModel> model();
109         boost::shared_ptr<const MidiModel> model() const;
110
111         void fix_negative_start ();
112         double start_beats () const {return _start_beats; }
113         double length_beats () const {return _length_beats; }
114
115         void clobber_sources (boost::shared_ptr<MidiSource> source);
116
117   protected:
118
119         virtual bool can_trim_start_before_source_start () const {
120                 return true;
121         }
122
123   private:
124         friend class RegionFactory;
125         PBD::Property<double> _start_beats;
126         PBD::Property<double> _length_beats;
127
128         MidiRegion (const SourceList&);
129         MidiRegion (boost::shared_ptr<const MidiRegion>);
130         MidiRegion (boost::shared_ptr<const MidiRegion>, ARDOUR::MusicFrame offset);
131
132         framecnt_t _read_at (const SourceList&, Evoral::EventSink<framepos_t>& dst,
133                              framepos_t position,
134                              framecnt_t dur,
135                              Evoral::Range<framepos_t>* loop_range,
136                              MidiCursor& cursor,
137                              uint32_t chan_n = 0,
138                              NoteMode mode = Sustained,
139                              MidiStateTracker* tracker = 0,
140                              MidiChannelFilter* filter = 0) const;
141
142         void register_properties ();
143         void post_set (const PBD::PropertyChange&);
144
145         void recompute_at_start ();
146         void recompute_at_end ();
147
148         void set_position_internal (framepos_t pos, bool allow_bbt_recompute, const int32_t sub_num);
149         void set_position_music_internal (double qn);
150         void set_length_internal (framecnt_t len, const int32_t sub_num);
151         void set_start_internal (framecnt_t, const int32_t sub_num);
152         void trim_to_internal (framepos_t position, framecnt_t length, const int32_t sub_num);
153         void update_length_beats (const int32_t sub_num);
154
155         void model_changed ();
156         void model_automation_state_changed (Evoral::Parameter const &);
157
158         void set_start_beats_from_start_frames ();
159         void update_after_tempo_map_change (bool send_change = true);
160
161         std::set<Evoral::Parameter> _filtered_parameters; ///< parameters that we ask our source not to return when reading
162         PBD::ScopedConnection _model_connection;
163         PBD::ScopedConnection _source_connection;
164         PBD::ScopedConnection _model_contents_connection;
165 };
166
167 } /* namespace ARDOUR */
168
169
170 #endif /* __ardour_midi_region_h__ */