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