2 Copyright (C) 2001-2007 Paul Davis
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.
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.
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.
19 #ifndef __gtk_ardour_midi_region_view_h__
20 #define __gtk_ardour_midi_region_view_h__
25 #include <libgnomecanvasmm.h>
26 #include <libgnomecanvasmm/polygon.h>
27 #include "ardour/midi_track.h"
28 #include "ardour/midi_region.h"
29 #include "ardour/midi_model.h"
30 #include "ardour/diskstream.h"
31 #include "ardour/types.h"
34 #include "region_view.h"
35 #include "midi_time_axis.h"
36 #include "time_axis_view_item.h"
37 #include "automation_line.h"
40 #include "canvas-note.h"
41 #include "canvas-note-event.h"
42 #include "canvas-program-change.h"
43 #include "canvas-sysex.h"
53 struct PatchPrimaryKey;
57 class MidiTimeAxisView;
59 class AutomationTimeAxisView;
60 class AutomationRegionView;
63 class MidiRegionView : public RegionView
66 typedef Evoral::Note<Evoral::MusicalTime> NoteType;
67 typedef Evoral::Sequence<Evoral::MusicalTime>::Notes NoteList;
69 MidiRegionView (ArdourCanvas::Group *,
71 boost::shared_ptr<ARDOUR::MidiRegion>,
72 double initial_samples_per_unit,
73 Gdk::Color const & basic_color);
75 MidiRegionView (const MidiRegionView& other);
76 MidiRegionView (const MidiRegionView& other, boost::shared_ptr<ARDOUR::MidiRegion>);
80 virtual void init (Gdk::Color const & basic_color, bool wfd);
82 inline const boost::shared_ptr<ARDOUR::MidiRegion> midi_region() const
83 { return boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region); }
85 inline MidiTimeAxisView* midi_view() const
86 { return dynamic_cast<MidiTimeAxisView*>(&trackview); }
88 inline MidiStreamView* midi_stream_view() const
89 { return midi_view()->midi_view(); }
91 void add_note (uint8_t channel, uint8_t number, uint8_t velocity,
92 Evoral::MusicalTime pos, Evoral::MusicalTime len);
94 void set_height (double);
95 void apply_note_range(uint8_t lowest, uint8_t highest, bool force=false);
97 inline ARDOUR::ColorMode color_mode() const { return midi_view()->color_mode(); }
99 void set_frame_color();
101 void redisplay_model();
103 GhostRegion* add_ghost (TimeAxisView&);
105 void add_note(const boost::shared_ptr<NoteType> note);
106 void resolve_note(uint8_t note_num, double end_time);
108 void cut_copy_clear (Editing::CutCopyOp);
109 void paste (nframes64_t pos, float times, const MidiCutBuffer&);
112 PCEvent(double a_time, uint8_t a_value, uint8_t a_channel)
113 : time(a_time), value(a_value), channel(a_channel) {}
120 /** Add a new program change flag to the canvas.
121 * @param program the MidiRegionView::PCEvent to add
122 * @param the text to display in the flag
124 void add_pgm_change(PCEvent& program, const std::string& displaytext);
126 /** Look up the given time and channel in the 'automation' and set keys accordingly.
127 * @param time the time of the program change event
128 * @param channel the MIDI channel of the event
129 * @key a reference to an instance of MIDI::Name::PatchPrimaryKey whose fields will
130 * will be set according to the result of the lookup
132 void get_patch_key_at(double time, uint8_t channel, MIDI::Name::PatchPrimaryKey& key);
134 /** Change the 'automation' data of old_program to new values which correspond to new_patch.
135 * @param old_program the program change event which is to be altered
136 * @param new_patch the new lsb, msb and program number which are to be set
138 void alter_program_change(PCEvent& old_program, const MIDI::Name::PatchPrimaryKey& new_patch);
140 /** Alter a given program to the new given one.
141 * (Called on context menu select on CanvasProgramChange)
143 void program_selected(
144 ArdourCanvas::CanvasProgramChange& program,
145 const MIDI::Name::PatchPrimaryKey& new_patch);
147 /** Alter a given program to be its predecessor in the MIDNAM file.
149 void previous_program(ArdourCanvas::CanvasProgramChange& program);
151 /** Alters a given program to be its successor in the MIDNAM file.
153 void next_program(ArdourCanvas::CanvasProgramChange& program);
155 /** Displays all program change events in the region as flags on the canvas.
157 void display_program_changes();
159 /** Displays all system exclusive events in the region as flags on the canvas.
161 void display_sysexes();
165 void extend_active_notes();
167 void create_note_at(double x, double y, double length);
169 void display_model(boost::shared_ptr<ARDOUR::MidiModel> model);
171 void start_delta_command(std::string name = "midi edit");
172 void delta_add_note(const boost::shared_ptr<NoteType> note, bool selected, bool show_velocity=false);
173 void delta_remove_note(ArdourCanvas::CanvasNoteEvent* ev);
175 void start_diff_command(std::string name = "midi edit");
176 void diff_add_change(ArdourCanvas::CanvasNoteEvent* ev, ARDOUR::MidiModel::DiffCommand::Property, uint8_t val);
177 void diff_add_change(ArdourCanvas::CanvasNoteEvent* ev, ARDOUR::MidiModel::DiffCommand::Property, Evoral::MusicalTime val);
181 void apply_delta_as_subcommand();
182 void apply_diff_as_subcommand();
183 void abort_command();
185 void note_entered(ArdourCanvas::CanvasNoteEvent* ev);
186 void note_left(ArdourCanvas::CanvasNoteEvent* ev);
187 void unique_select(ArdourCanvas::CanvasNoteEvent* ev);
188 void note_selected(ArdourCanvas::CanvasNoteEvent* ev, bool add, bool extend=false);
189 void note_deselected(ArdourCanvas::CanvasNoteEvent* ev, bool add);
190 void delete_selection();
191 size_t selection_size() { return _selection.size(); }
193 void move_selection(double dx, double dy);
194 void note_dropped(ArdourCanvas::CanvasNoteEvent* ev, double d_pixels, uint8_t d_note);
196 /** Return true iff the note is within the currently visible range */
197 bool note_in_visible_range(const boost::shared_ptr<NoteType> note) const;
199 /** Get the region position in pixels relative to session. */
200 double get_position_pixels();
202 /** Begin resizing of some notes.
203 * Called by CanvasMidiNote when resizing starts.
204 * @param at_front which end of the note (true == note on, false == note off)
206 void begin_resizing(bool at_front);
208 /** Update resizing notes while user drags.
209 * @param at_front which end of the note (true == note on, false == note off)
210 * @param x the difference in mouse motion, ie the motion difference if relative=true
211 * or the absolute mouse position (track-relative) if relative is false
212 * @param relative true if relative resizing is taking place, false if absolute resizing
214 void update_resizing(bool at_front, double x, bool relative);
216 /** Finish resizing notes when the user releases the mouse button.
217 * @param at_front which end of the note (true == note on, false == note off)
218 * @param event_x the absolute mouse position (track-relative)
219 * @param relative true if relative resizing is taking place, false if absolute resizing
221 void commit_resizing(bool at_front, double event_x, bool relative);
223 /** Adjust the velocity on a note, and the selection if applicable.
224 * @param velocity the relative or absolute velocity
225 * @param relative whether velocity is relative or absolute
227 void change_velocity(ArdourCanvas::CanvasNoteEvent* ev, int8_t velocity, bool relative=false);
229 /** Change the channel of the selection.
230 * @param channel - the channel number of the new channel, zero-based
232 void change_channel(uint8_t channel);
242 MouseState mouse_state() const { return _mouse_state; }
244 struct NoteResizeData {
245 ArdourCanvas::CanvasNote *canvas_note;
246 ArdourCanvas::SimpleRect *resize_rect;
249 /** Snap a region relative pixel coordinate to pixel units.
250 * @param x a pixel coordinate relative to region start
251 * @return the snapped pixel coordinate relative to region start
253 double snap_to_pixel(double x);
255 /** Snap a region relative pixel coordinate to frame units.
256 * @param x a pixel coordinate relative to region start
257 * @return the snapped nframes64_t coordinate relative to region start
259 nframes64_t snap_pixel_to_frame(double x);
261 /** Snap a region relative frame coordinate to frame units.
262 * @param x a pixel coordinate relative to region start
263 * @return the snapped nframes64_t coordinate relative to region start
265 nframes64_t snap_frame_to_frame(nframes64_t x);
267 /** Convert a timestamp in beats to frames (both relative to region start) */
268 nframes64_t beats_to_frames(double beats) const;
270 /** Convert a timestamp in frames to beats (both relative to region start) */
271 double frames_to_beats(nframes64_t beats) const;
273 void goto_previous_note ();
274 void goto_next_note ();
275 void change_note_lengths (bool, bool, bool start, bool end);
276 void change_velocities (bool up, bool fine, bool allow_smush);
277 void transpose (bool up, bool fine, bool allow_smush);
278 void nudge_notes (bool forward);
280 void show_list_editor ();
282 void selection_as_notelist (NoteList& selected);
285 /** Allows derived types to specify their visibility requirements
286 * to the TimeAxisViewItem parent class.
288 MidiRegionView (ArdourCanvas::Group *,
290 boost::shared_ptr<ARDOUR::MidiRegion>,
291 double samples_per_unit,
292 Gdk::Color& basic_color,
293 TimeAxisViewItem::Visibility);
295 void region_resized (ARDOUR::Change);
297 void set_flags (XMLNode *);
300 void reset_width_dependent_items (double pixel_width);
303 /** Play the NoteOn event of the given note immediately
304 * and schedule the playback of the corresponding NoteOff event.
306 void play_midi_note(boost::shared_ptr<NoteType> note);
308 /** Play the NoteOff-Event of the given note immediately
309 * (scheduled by @ref play_midi_note()).
311 bool play_midi_note_off(boost::shared_ptr<NoteType> note);
314 void switch_source(boost::shared_ptr<ARDOUR::Source> src);
316 bool canvas_event(GdkEvent* ev);
317 bool note_canvas_event(GdkEvent* ev);
319 void midi_channel_mode_changed(ARDOUR::ChannelMode mode, uint16_t mask);
320 void midi_patch_settings_changed(std::string model, std::string custom_device_mode);
322 void change_note_velocity(ArdourCanvas::CanvasNoteEvent* ev, int8_t vel, bool relative=false);
323 void change_note_note(ArdourCanvas::CanvasNoteEvent* ev, int8_t note, bool relative=false);
324 void change_note_time(ArdourCanvas::CanvasNoteEvent* ev, ARDOUR::MidiModel::TimeType, bool relative=false);
325 void trim_note(ArdourCanvas::CanvasNoteEvent* ev, ARDOUR::MidiModel::TimeType start_delta,
326 ARDOUR::MidiModel::TimeType end_delta);
328 void clear_selection_except(ArdourCanvas::CanvasNoteEvent* ev);
329 void clear_selection() { clear_selection_except(NULL); }
330 void update_drag_selection(double last_x, double x, double last_y, double y);
332 void add_to_selection (ArdourCanvas::CanvasNoteEvent*);
333 void remove_from_selection (ArdourCanvas::CanvasNoteEvent*);
335 int8_t _force_channel;
336 uint16_t _last_channel_selection;
337 double _default_note_length;
338 uint8_t _current_range_min;
339 uint8_t _current_range_max;
341 /// MIDNAM information of the current track: Model name of MIDNAM file
342 std::string _model_name;
344 /// MIDNAM information of the current track: CustomDeviceMode
345 std::string _custom_device_mode;
347 typedef std::vector<ArdourCanvas::CanvasNoteEvent*> Events;
348 typedef std::vector< boost::shared_ptr<ArdourCanvas::CanvasProgramChange> > PgmChanges;
349 typedef std::vector< boost::shared_ptr<ArdourCanvas::CanvasSysEx> > SysExes;
351 boost::shared_ptr<ARDOUR::MidiModel> _model;
353 PgmChanges _pgm_changes;
355 ArdourCanvas::CanvasNote** _active_notes;
356 ArdourCanvas::Group* _note_group;
357 ARDOUR::MidiModel::DeltaCommand* _delta_command;
358 ARDOUR::MidiModel::DiffCommand* _diff_command;
360 MouseState _mouse_state;
363 typedef std::set<ArdourCanvas::CanvasNoteEvent*> Selection;
364 /// Currently selected CanvasNoteEvents
365 Selection _selection;
367 MidiCutBuffer* selection_as_cut_buffer () const;
369 /** New notes (created in the current command) which should be selected
370 * when they appear after the command is applied. */
371 std::set< boost::shared_ptr<NoteType> > _marked_for_selection;
373 /** New notes (created in the current command) which should have visible velocity
374 * when they appear after the command is applied. */
375 std::set< boost::shared_ptr<NoteType> > _marked_for_velocity;
377 std::vector<NoteResizeData *> _resize_data;
379 /* connection used to connect to model's ContentChanged signal */
380 sigc::connection content_connection;
384 #endif /* __gtk_ardour_midi_region_view_h__ */