The Big Change: Store time in MidiModel as tempo time, not frame time.
[ardour.git] / gtk2_ardour / midi_region_view.h
1 /*
2     Copyright (C) 2001-2007 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
19 #ifndef __gtk_ardour_midi_region_view_h__
20 #define __gtk_ardour_midi_region_view_h__
21
22 #include <vector>
23
24 #include <libgnomecanvasmm.h>
25 #include <libgnomecanvasmm/polygon.h>
26 #include <ardour/midi_track.h>
27 #include <ardour/midi_region.h>
28 #include <ardour/midi_model.h>
29 #include <ardour/diskstream.h>
30 #include <ardour/types.h>
31
32 #include "region_view.h"
33 #include "midi_time_axis.h"
34 #include "time_axis_view_item.h"
35 #include "automation_line.h"
36 #include "enums.h"
37 #include "canvas.h"
38 #include "canvas-note.h"
39 #include "canvas-note-event.h"
40 #include "canvas-program-change.h"
41
42 namespace ARDOUR {
43         class MidiRegion;
44         class MidiModel;
45 };
46
47 namespace MIDI {
48         namespace Name {
49                 struct PatchPrimaryKey;
50         };
51 };
52
53 class MidiTimeAxisView;
54 class GhostRegion;
55 class AutomationTimeAxisView;
56 class AutomationRegionView;
57
58 class MidiRegionView : public RegionView
59 {
60   public:
61         typedef Evoral::Note<ARDOUR::MidiModel::TimeType> NoteType;
62
63         MidiRegionView (ArdourCanvas::Group *,
64                         RouteTimeAxisView&,
65                         boost::shared_ptr<ARDOUR::MidiRegion>,
66                         double initial_samples_per_unit,
67                         Gdk::Color& basic_color);
68         
69         MidiRegionView (const MidiRegionView& other);
70         MidiRegionView (const MidiRegionView& other, boost::shared_ptr<ARDOUR::MidiRegion>);
71
72         ~MidiRegionView ();
73
74         virtual void init (Gdk::Color& basic_color, bool wfd);
75
76         inline const boost::shared_ptr<ARDOUR::MidiRegion> midi_region() const
77                 { return boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region); }
78
79         inline MidiTimeAxisView* midi_view() const
80                 { return dynamic_cast<MidiTimeAxisView*>(&trackview); }
81
82         inline MidiStreamView* midi_stream_view() const
83                 { return midi_view()->midi_view(); }
84
85         void set_height (double);
86         void apply_note_range(uint8_t lowest, uint8_t highest, bool force=false);
87
88         inline ARDOUR::ColorMode color_mode() const { return midi_view()->color_mode(); }
89         
90         void set_frame_color();
91
92         void redisplay_model();
93
94         GhostRegion* add_ghost (TimeAxisView&);
95
96         void add_note(const boost::shared_ptr<NoteType> note);
97         void resolve_note(uint8_t note_num, double end_time);
98         
99         struct ControlEvent
100         {
101                 nframes_t time;
102                 uint8_t   value;
103                 uint8_t   channel;
104                 
105                 ControlEvent(nframes_t a_time, uint8_t a_value, uint8_t a_channel) 
106                         : time(a_time), value(a_value), channel(a_channel) {}
107                 
108                 ControlEvent& operator=(const ControlEvent& other) {
109                         time = other.time;
110                         value = other.value;
111                         channel = other.channel;
112                         return *this;
113                 }
114         };
115         
116         /** Add a new program change flag to the canvas.
117          * @param program the MidiRegionView::ControlEvent to add
118          * @param the text to display in the flag
119          */
120         void add_pgm_change(ControlEvent& program, string displaytext);
121         
122         /** Look up the given time and channel in the 'automation' and set keys accordingly.
123          * @param time the time of the program change event
124          * @param channel the MIDI channel of the event
125          * @key a reference to an instance of MIDI::Name::PatchPrimaryKey whose fields will 
126          *        will be set according to the result of the lookup
127          */
128         void get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key);
129         
130         /** Change the 'automation' data of old_program to new values which correspond to new_patch.
131          * @param old_program identifies the program change event which is to be altered
132          * @param new_patch defines the new lsb, msb and program number which are to be set in the automation list data
133          */
134         void alter_program_change(ControlEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch);
135         
136         /** Alter a given program to the new given one.
137          * (Called on context menu select on CanvasProgramChange)
138          */
139         void program_selected(
140                 ArdourCanvas::CanvasProgramChange& program, 
141                 const MIDI::Name::PatchPrimaryKey& new_patch);
142         
143         /** Alter a given program to be its predecessor in the MIDNAM file.
144          */
145         void previous_program(ArdourCanvas::CanvasProgramChange& program);
146
147         /** Alters a given program to be its successor in the MIDNAM file.
148          */
149         void next_program(ArdourCanvas::CanvasProgramChange& program);
150         
151         /** Displays all program changed events in the region as flags on the canvas.
152          */
153         void display_program_change_flags();
154
155         void begin_write();
156         void end_write();
157         void extend_active_notes();
158
159         void create_note_at(double x, double y, double length);
160
161         void display_model(boost::shared_ptr<ARDOUR::MidiModel> model);
162
163         void start_delta_command(string name = "midi edit");
164         void command_add_note(const boost::shared_ptr<NoteType> note, bool selected);
165         void command_remove_note(ArdourCanvas::CanvasNoteEvent* ev);
166
167         void apply_command();
168         void abort_command();
169
170         void   note_entered(ArdourCanvas::CanvasNoteEvent* ev);
171         void   unique_select(ArdourCanvas::CanvasNoteEvent* ev);
172         void   note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add);
173         void   note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add);
174         void   delete_selection();
175         size_t selection_size() { return _selection.size(); }
176
177         void move_selection(double dx, double dy);
178         void note_dropped(ArdourCanvas::CanvasNoteEvent* ev, double d_frames, uint8_t d_note);
179
180         /** Get the region position in pixels relative to session. */
181         double get_position_pixels();
182
183         /** Begin resizing of some notes.
184          * Called by CanvasMidiNote when resizing starts.
185          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
186          */
187         void begin_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end);
188
189         /** Update resizing notes while user drags.
190          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
191          * @param x the difference in mouse motion, ie the motion difference if relative=true
192          *           or the absolute mouse position (track-relative) if relative is false
193          * @param relative true if relative resizing is taking place, false if absolute resizing
194          */
195         void update_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end, double x, bool relative);
196
197         /** Finish resizing notes when the user releases the mouse button.
198          * @param note_end which end of the note, NOTE_ON or NOTE_OFF
199          * @param event_x the absolute mouse position (track-relative)
200          * @param relative true if relative resizing is taking place, false if absolute resizing
201          */
202         void commit_resizing(ArdourCanvas::CanvasNote::NoteEnd note_end, double event_x, bool relative);
203
204         /** Adjust the velocity on a note, and the selection if applicable.
205          * @param velocity the relative or absolute velocity
206          * @param relative whether velocity is relative or absolute
207          */
208         void change_velocity(ArdourCanvas::CanvasNoteEvent* ev, int8_t velocity, bool relative=false);
209         
210         /** Change the channel of the selection.
211          * @param channel - the channel number of the new channel, zero-based
212          */
213         void change_channel(uint8_t channel);
214
215         enum MouseState {
216                 None,
217                 Pressed,
218                 SelectTouchDragging,
219                 SelectRectDragging,
220                 AddDragging,
221                 EraseTouchDragging
222         };
223
224         MouseState mouse_state() const { return _mouse_state; }
225
226         struct NoteResizeData {
227                 ArdourCanvas::CanvasNote  *canvas_note;
228                 ArdourCanvas::SimpleRect  *resize_rect;
229                 double                     current_x;
230         };
231         
232         /** Snap a region relative pixel coordinate to pixel units.
233          * for pixel units (double) instead of nframes64_t
234          * @param x a pixel coordinate relative to region start
235          * @return the snapped pixel coordinate relative to region start
236          */
237         double snap_to_pixel(double x);
238
239         /** Snap a region relative pixel coordinate to frame units.
240          * @param x a pixel coordinate relative to region start
241          * @return the snapped nframes64_t coordinate relative to region start
242          */
243         nframes64_t snap_to_frame(double x);
244
245         /** Snap a region relative frame coordinate to frame units.
246          * @param x a pixel coordinate relative to region start
247          * @return the snapped nframes64_t coordinate relative to region start
248          */
249         nframes64_t snap_to_frame(nframes64_t x);
250         
251         /** Convert a timestamp in beats to frames (both relative to region start) */
252         nframes64_t beats_to_frames(double beats) const;
253         
254         /** Convert a timestamp in frames to beats (both relative to region start) */
255         double frames_to_beats(nframes64_t beats) const;
256         
257   protected:
258     /** Allows derived types to specify their visibility requirements
259      * to the TimeAxisViewItem parent class.
260      */
261     MidiRegionView (ArdourCanvas::Group *,
262                         RouteTimeAxisView&,
263                         boost::shared_ptr<ARDOUR::MidiRegion>,
264                         double samples_per_unit,
265                         Gdk::Color& basic_color,
266                         TimeAxisViewItem::Visibility);
267
268     void region_resized (ARDOUR::Change);
269
270     void set_flags (XMLNode *);
271     void store_flags ();
272
273         void reset_width_dependent_items (double pixel_width);
274
275   private:
276         /** Play the NoteOn event of the given note immediately
277          * and schedule the playback of the corresponding NoteOff event.
278          */
279         void play_midi_note(boost::shared_ptr<NoteType> note);
280         
281         /** Play the NoteOff-Event of the given note immediately
282          * (scheduled by @ref play_midi_note()).
283          */
284         bool play_midi_note_off(boost::shared_ptr<NoteType> note);
285
286         void clear_events();
287         void switch_source(boost::shared_ptr<ARDOUR::Source> src);
288
289         bool canvas_event(GdkEvent* ev);
290         bool note_canvas_event(GdkEvent* ev);
291         
292         void midi_channel_mode_changed(ARDOUR::ChannelMode mode, uint16_t mask);
293         void midi_patch_settings_changed(std::string model, std::string custom_device_mode);
294         
295         void change_note_velocity(ArdourCanvas::CanvasNoteEvent* ev, int8_t vel, bool relative=false);
296
297         void clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev);
298         void clear_selection() { clear_selection_except(NULL); }
299         void update_drag_selection(double last_x, double x, double last_y, double y);
300
301         int8_t   _force_channel;
302         uint16_t _last_channel_selection;
303         double   _default_note_length;
304         uint8_t  _current_range_min;
305         uint8_t  _current_range_max;
306         
307         /// MIDNAM information of the current track: Model name of MIDNAM file
308         string   _model_name;
309         
310         /// MIDNAM information of the current track: CustomDeviceMode
311         string   _custom_device_mode;   
312
313         typedef std::vector<ArdourCanvas::CanvasNoteEvent*> Events;
314         typedef std::vector< boost::shared_ptr<ArdourCanvas::CanvasProgramChange> > PgmChanges;
315         
316         boost::shared_ptr<ARDOUR::MidiModel> _model;
317         Events                               _events;
318         PgmChanges                           _pgm_changes;
319         ArdourCanvas::CanvasNote**           _active_notes;
320         ArdourCanvas::Group*                 _note_group;
321         ARDOUR::MidiModel::DeltaCommand*     _delta_command;
322
323         MouseState _mouse_state;
324         int _pressed_button;
325
326         typedef std::set<ArdourCanvas::CanvasNoteEvent*> Selection;
327         /// Currently selected CanvasNoteEvents
328         Selection _selection;
329
330         /** New notes (created in the current command) which should be selected
331          * when they appear after the command is applied. */
332         std::set< boost::shared_ptr<NoteType> > _marked_for_selection;
333
334         std::vector<NoteResizeData *> _resize_data;
335 };
336
337 #endif /* __gtk_ardour_midi_region_view_h__ */