2 Copyright (C) 2000 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.
26 #include <sigc++/bind.h>
28 #include "pbd/error.h"
29 #include "pbd/stl_delete.h"
30 #include "pbd/whitespace.h"
31 #include "pbd/basename.h"
32 #include "pbd/enumwriter.h"
33 #include "pbd/memento_command.h"
35 #include <gtkmm2ext/gtk_ui.h>
36 #include <gtkmm2ext/selector.h>
37 #include <gtkmm2ext/stop_signal.h>
38 #include <gtkmm2ext/bindable_button.h>
39 #include <gtkmm2ext/utils.h>
41 #include "ardour/midi_playlist.h"
42 #include "ardour/midi_diskstream.h"
43 #include "ardour/midi_patch_manager.h"
44 #include "ardour/midi_source.h"
45 #include "ardour/processor.h"
46 #include "ardour/ladspa_plugin.h"
47 #include "ardour/location.h"
48 #include "ardour/playlist.h"
49 #include "ardour/region_factory.h"
50 #include "ardour/session.h"
51 #include "ardour/session_playlist.h"
52 #include "ardour/tempo.h"
53 #include "ardour/utils.h"
55 #include "add_midi_cc_track_dialog.h"
56 #include "ardour_ui.h"
57 #include "automation_line.h"
58 #include "automation_time_axis.h"
59 #include "canvas-note-event.h"
60 #include "canvas_impl.h"
61 #include "crossfade_view.h"
64 #include "ghostregion.h"
65 #include "gui_thread.h"
67 #include "midi_scroomer.h"
68 #include "midi_streamview.h"
69 #include "midi_region_view.h"
70 #include "midi_time_axis.h"
71 #include "piano_roll_header.h"
72 #include "playlist_selector.h"
73 #include "plugin_selector.h"
74 #include "plugin_ui.h"
75 #include "point_selection.h"
77 #include "region_view.h"
78 #include "rgb_macros.h"
79 #include "selection.h"
80 #include "simplerect.h"
83 #include "ardour/midi_track.h"
87 using namespace ARDOUR
;
91 using namespace Editing
;
93 // Minimum height at which a control is displayed
94 static const uint32_t MIDI_CONTROLS_BOX_MIN_HEIGHT
= 162;
95 static const uint32_t KEYBOARD_MIN_HEIGHT
= 140;
97 MidiTimeAxisView::MidiTimeAxisView (PublicEditor
& ed
, Session
& sess
,
98 boost::shared_ptr
<Route
> rt
, Canvas
& canvas
)
99 : AxisView(sess
) // virtually inherited
100 , RouteTimeAxisView(ed
, sess
, rt
, canvas
)
101 , _ignore_signals(false)
103 , _piano_roll_header(0)
104 , _note_mode(Sustained
)
106 , _percussion_mode_item(0)
107 , _color_mode(MeterColors
)
108 , _meter_color_mode_item(0)
109 , _channel_color_mode_item(0)
110 , _track_color_mode_item(0)
111 , _step_edit_item (0)
112 , _midi_thru_item (0)
113 , default_channel_menu (0)
115 subplugin_menu
.set_name ("ArdourContextMenu");
117 _view
= new MidiStreamView (*this);
119 ignore_toggle
= false;
121 mute_button
->set_active (false);
122 solo_button
->set_active (false);
124 step_edit_insert_position
= 0;
126 if (is_midi_track()) {
127 controls_ebox
.set_name ("MidiTimeAxisViewControlsBaseUnselected");
128 _note_mode
= midi_track()->note_mode();
129 } else { // MIDI bus (which doesn't exist yet..)
130 controls_ebox
.set_name ("MidiBusControlsBaseUnselected");
133 /* map current state of the route */
135 processors_changed ();
139 set_state (*xml_node
, Stateful::loading_state_version
);
141 _route
->processors_changed
.connect (mem_fun(*this, &MidiTimeAxisView::processors_changed
));
144 _piano_roll_header
= new PianoRollHeader(*midi_view());
145 _range_scroomer
= new MidiScroomer(midi_view()->note_range_adjustment
);
147 controls_hbox
.pack_start(*_range_scroomer
);
148 controls_hbox
.pack_start(*_piano_roll_header
);
150 controls_ebox
.set_name ("MidiTrackControlsBaseUnselected");
151 controls_base_selected_name
= "MidiTrackControlsBaseSelected";
152 controls_base_unselected_name
= "MidiTrackControlsBaseUnselected";
154 midi_view()->NoteRangeChanged
.connect (mem_fun(*this, &MidiTimeAxisView::update_range
));
156 /* ask for notifications of any new RegionViews */
157 _view
->RegionViewAdded
.connect (mem_fun(*this, &MidiTimeAxisView::region_view_added
));
161 HBox
* midi_controls_hbox
= manage(new HBox());
163 MIDI::Name::MidiPatchManager
& patch_manager
= MIDI::Name::MidiPatchManager::instance();
165 MIDI::Name::MasterDeviceNames::Models::const_iterator m
= patch_manager
.all_models().begin();
166 for (; m
!= patch_manager
.all_models().end(); ++m
) {
167 _model_selector
.append_text(m
->c_str());
170 _model_selector
.signal_changed().connect(mem_fun(*this, &MidiTimeAxisView::model_changed
));
172 _custom_device_mode_selector
.signal_changed().connect(
173 mem_fun(*this, &MidiTimeAxisView::custom_device_mode_changed
));
175 // TODO: persist the choice
176 // this initializes the comboboxes and sends out the signal
177 _model_selector
.set_active(0);
179 midi_controls_hbox
->pack_start(_channel_selector
, true, false);
180 if (!patch_manager
.all_models().empty()) {
181 _midi_controls_box
.pack_start(_model_selector
, true, false);
182 _midi_controls_box
.pack_start(_custom_device_mode_selector
, true, false);
185 _midi_controls_box
.pack_start(*midi_controls_hbox
, true, true);
187 controls_vbox
.pack_start(_midi_controls_box
, false, false);
189 boost::shared_ptr
<MidiDiskstream
> diskstream
= midi_track()->midi_diskstream();
191 // restore channel selector settings
192 _channel_selector
.set_channel_mode(diskstream
->get_channel_mode(),
193 diskstream
->get_channel_mask());
194 _channel_selector
.mode_changed
.connect(
195 mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_channel_mode
));
198 if ((prop
= xml_node
->property ("color-mode")) != 0) {
199 _color_mode
= ColorMode (string_2_enum(prop
->value(), _color_mode
));
200 if (_color_mode
== ChannelColors
) {
201 _channel_selector
.set_channel_colors(CanvasNoteEvent::midi_channel_colors
);
205 if ((prop
= xml_node
->property ("note-mode")) != 0) {
206 _note_mode
= NoteMode (string_2_enum(prop
->value(), _note_mode
));
207 if (_percussion_mode_item
) {
208 _percussion_mode_item
->set_active (_note_mode
== Percussive
);
213 MidiTimeAxisView::~MidiTimeAxisView ()
215 delete _piano_roll_header
;
216 _piano_roll_header
= 0;
218 delete _range_scroomer
;
222 void MidiTimeAxisView::model_changed()
224 std::list
<std::string
> device_modes
= MIDI::Name::MidiPatchManager::instance()
225 .custom_device_mode_names_by_model(_model_selector
.get_active_text());
227 _custom_device_mode_selector
.clear_items();
229 for (std::list
<std::string
>::const_iterator i
= device_modes
.begin();
230 i
!= device_modes
.end(); ++i
) {
231 cerr
<< "found custom device mode " << *i
<< " thread_id: " << pthread_self() << endl
;
232 _custom_device_mode_selector
.append_text(*i
);
235 _custom_device_mode_selector
.set_active(0);
238 void MidiTimeAxisView::custom_device_mode_changed()
240 _midi_patch_settings_changed
.emit(_model_selector
.get_active_text(),
241 _custom_device_mode_selector
.get_active_text());
245 MidiTimeAxisView::midi_view()
247 return dynamic_cast<MidiStreamView
*>(_view
);
251 MidiTimeAxisView::show_at (double y
, int& nth
, Gtk::VBox
*parent
)
254 xml_node
->add_property ("shown-editor", "yes");
256 guint32 ret
= TimeAxisView::show_at (y
, nth
, parent
);
261 MidiTimeAxisView::hide ()
264 xml_node
->add_property ("shown-editor", "no");
266 TimeAxisView::hide ();
270 MidiTimeAxisView::set_height (uint32_t h
)
272 RouteTimeAxisView::set_height (h
);
274 if (height
>= MIDI_CONTROLS_BOX_MIN_HEIGHT
) {
275 _midi_controls_box
.show();
277 _midi_controls_box
.hide();
280 if (height
>= KEYBOARD_MIN_HEIGHT
) {
281 if (is_track() && _range_scroomer
)
282 _range_scroomer
->show();
283 if (is_track() && _piano_roll_header
)
284 _piano_roll_header
->show();
286 if (is_track() && _range_scroomer
)
287 _range_scroomer
->hide();
288 if (is_track() && _piano_roll_header
)
289 _piano_roll_header
->hide();
294 MidiTimeAxisView::append_extra_display_menu_items ()
296 using namespace Menu_Helpers
;
298 MenuList
& items
= display_menu
->items();
301 Menu
*range_menu
= manage(new Menu
);
302 MenuList
& range_items
= range_menu
->items();
303 range_menu
->set_name ("ArdourContextMenu");
305 range_items
.push_back (MenuElem (_("Show Full Range"), bind (
306 mem_fun(*this, &MidiTimeAxisView::set_note_range
),
307 MidiStreamView::FullRange
)));
309 range_items
.push_back (MenuElem (_("Fit Contents"), bind (
310 mem_fun(*this, &MidiTimeAxisView::set_note_range
),
311 MidiStreamView::ContentsRange
)));
313 items
.push_back (MenuElem (_("Note range"), *range_menu
));
314 items
.push_back (MenuElem (_("Note mode"), *build_note_mode_menu()));
315 items
.push_back (MenuElem (_("Default Channel"), *build_def_channel_menu()));
317 items
.push_back (CheckMenuElem (_("MIDI Thru"), mem_fun(*this, &MidiTimeAxisView::toggle_midi_thru
)));
318 _midi_thru_item
= dynamic_cast<CheckMenuItem
*>(&items
.back());
322 MidiTimeAxisView::build_def_channel_menu ()
324 using namespace Menu_Helpers
;
326 if (default_channel_menu
== 0) {
327 default_channel_menu
= manage (new Menu ());
330 uint8_t defchn
= midi_track()->default_channel();
331 MenuList
& def_channel_items
= default_channel_menu
->items();
333 RadioMenuItem::Group dc_group
;
335 for (int i
= 0; i
< 16; ++i
) {
337 snprintf (buf
, sizeof (buf
), "%d", i
+1);
339 def_channel_items
.push_back (RadioMenuElem (dc_group
, buf
,
340 bind (mem_fun (*this, &MidiTimeAxisView::set_default_channel
), i
)));
341 item
= dynamic_cast<RadioMenuItem
*>(&def_channel_items
.back());
342 item
->set_active ((i
== defchn
));
345 return default_channel_menu
;
349 MidiTimeAxisView::set_default_channel (int chn
)
351 midi_track()->set_default_channel (chn
);
355 MidiTimeAxisView::toggle_midi_thru ()
357 if (!_midi_thru_item
) {
361 bool view_yn
= _midi_thru_item
->get_active();
362 if (view_yn
!= midi_track()->midi_thru()) {
363 midi_track()->set_midi_thru (view_yn
);
368 MidiTimeAxisView::build_automation_action_menu ()
370 using namespace Menu_Helpers
;
372 RouteTimeAxisView::build_automation_action_menu ();
374 MenuList
& automation_items
= automation_action_menu
->items();
376 automation_items
.push_back (SeparatorElem());
377 automation_items
.push_back (MenuElem (_("Controller..."),
378 mem_fun(*this, &MidiTimeAxisView::add_cc_track
)));
379 automation_items
.push_back (MenuElem (_("Program Change"),
380 sigc::bind(mem_fun(*this, &MidiTimeAxisView::add_parameter_track
),
381 Evoral::Parameter(MidiPgmChangeAutomation
))));
382 automation_items
.push_back (MenuElem (_("Bender"),
383 sigc::bind(mem_fun(*this, &MidiTimeAxisView::add_parameter_track
),
384 Evoral::Parameter(MidiPitchBenderAutomation
))));
385 automation_items
.push_back (MenuElem (_("Pressure"),
386 sigc::bind(mem_fun(*this, &MidiTimeAxisView::add_parameter_track
),
387 Evoral::Parameter(MidiChannelPressureAutomation
))));
391 MidiTimeAxisView::build_note_mode_menu()
393 using namespace Menu_Helpers
;
395 Menu
* mode_menu
= manage (new Menu
);
396 MenuList
& items
= mode_menu
->items();
397 mode_menu
->set_name ("ArdourContextMenu");
399 RadioMenuItem::Group mode_group
;
400 items
.push_back (RadioMenuElem (mode_group
, _("Sustained"),
401 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode
), Sustained
)));
402 _note_mode_item
= dynamic_cast<RadioMenuItem
*>(&items
.back());
403 _note_mode_item
->set_active(_note_mode
== Sustained
);
405 items
.push_back (RadioMenuElem (mode_group
, _("Percussive"),
406 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode
), Percussive
)));
407 _percussion_mode_item
= dynamic_cast<RadioMenuItem
*>(&items
.back());
408 _percussion_mode_item
->set_active(_note_mode
== Percussive
);
414 MidiTimeAxisView::build_color_mode_menu()
416 using namespace Menu_Helpers
;
418 Menu
* mode_menu
= manage (new Menu
);
419 MenuList
& items
= mode_menu
->items();
420 mode_menu
->set_name ("ArdourContextMenu");
422 RadioMenuItem::Group mode_group
;
423 items
.push_back (RadioMenuElem (mode_group
, _("Meter Colors"),
424 bind (mem_fun (*this, &MidiTimeAxisView::set_color_mode
), MeterColors
)));
425 _meter_color_mode_item
= dynamic_cast<RadioMenuItem
*>(&items
.back());
426 _meter_color_mode_item
->set_active(_color_mode
== MeterColors
);
428 items
.push_back (RadioMenuElem (mode_group
, _("Channel Colors"),
429 bind (mem_fun (*this, &MidiTimeAxisView::set_color_mode
), ChannelColors
)));
430 _channel_color_mode_item
= dynamic_cast<RadioMenuItem
*>(&items
.back());
431 _channel_color_mode_item
->set_active(_color_mode
== ChannelColors
);
433 items
.push_back (RadioMenuElem (mode_group
, _("Track Color"),
434 bind (mem_fun (*this, &MidiTimeAxisView::set_color_mode
), TrackColor
)));
435 _channel_color_mode_item
= dynamic_cast<RadioMenuItem
*>(&items
.back());
436 _channel_color_mode_item
->set_active(_color_mode
== TrackColor
);
442 MidiTimeAxisView::set_note_mode(NoteMode mode
)
444 if (_note_mode
!= mode
|| midi_track()->note_mode() != mode
) {
446 midi_track()->set_note_mode(mode
);
447 xml_node
->add_property ("note-mode", enum_2_string(_note_mode
));
448 _view
->redisplay_diskstream();
453 MidiTimeAxisView::set_color_mode(ColorMode mode
)
455 if (_color_mode
!= mode
) {
456 if (mode
== ChannelColors
) {
457 _channel_selector
.set_channel_colors(CanvasNoteEvent::midi_channel_colors
);
459 _channel_selector
.set_default_channel_color();
463 xml_node
->add_property ("color-mode", enum_2_string(_color_mode
));
464 _view
->redisplay_diskstream();
469 MidiTimeAxisView::set_note_range(MidiStreamView::VisibleNoteRange range
)
471 if (!_ignore_signals
)
472 midi_view()->set_note_range(range
);
477 MidiTimeAxisView::update_range()
479 MidiGhostRegion
* mgr
;
481 for(list
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
482 if ((mgr
= dynamic_cast<MidiGhostRegion
*>(*i
)) != 0) {
489 MidiTimeAxisView::show_all_automation ()
492 const set
<Evoral::Parameter
> params
= midi_track()->midi_diskstream()->
493 midi_playlist()->contained_automation();
495 for (set
<Evoral::Parameter
>::const_iterator i
= params
.begin(); i
!= params
.end(); ++i
) {
496 create_automation_child(*i
, true);
500 RouteTimeAxisView::show_all_automation ();
504 MidiTimeAxisView::show_existing_automation ()
507 const set
<Evoral::Parameter
> params
= midi_track()->midi_diskstream()->
508 midi_playlist()->contained_automation();
510 for (set
<Evoral::Parameter
>::const_iterator i
= params
.begin(); i
!= params
.end(); ++i
) {
511 create_automation_child(*i
, true);
515 RouteTimeAxisView::show_existing_automation ();
518 /** Prompt for a controller with a dialog and add an automation track for it
521 MidiTimeAxisView::add_cc_track()
524 Evoral::Parameter
param(0, 0, 0);
527 AddMidiCCTrackDialog dialog
;
528 dialog
.set_transient_for (_editor
);
529 response
= dialog
.run();
531 if (response
== Gtk::RESPONSE_ACCEPT
)
532 param
= dialog
.parameter();
535 if (param
.type() != 0 && response
== Gtk::RESPONSE_ACCEPT
)
536 create_automation_child(param
, true);
540 /** Add an automation track for the given parameter (pitch bend, channel pressure).
543 MidiTimeAxisView::add_parameter_track(const Evoral::Parameter
& param
)
545 if ( !EventTypeMap::instance().is_midi_parameter(param
) ) {
546 error
<< "MidiTimeAxisView: unknown automation child "
547 << ARDOUR::EventTypeMap::instance().to_symbol(param
) << endmsg
;
551 // create the parameter lane for each selected channel
552 uint16_t selected_channels
= _channel_selector
.get_selected_channels();
554 for (uint8_t i
= 0; i
< 16; i
++) {
555 if (selected_channels
& (0x0001 << i
)) {
556 Evoral::Parameter
param_with_channel(param
.type(), i
, param
.id());
557 create_automation_child(param_with_channel
, true);
563 MidiTimeAxisView::create_automation_child (const Evoral::Parameter
& param
, bool show
)
565 /* These controllers are region "automation", so we do not create
566 * an AutomationList/Line for the track */
568 if (param
.type() == NullAutomation
) {
569 cerr
<< "WARNING: Attempt to create NullAutomation child, ignoring" << endl
;
573 AutomationTracks::iterator existing
= _automation_tracks
.find(param
);
574 if (existing
!= _automation_tracks
.end())
577 boost::shared_ptr
<AutomationControl
> c
= _route
->get_control (param
);
581 boost::shared_ptr
<AutomationTimeAxisView
> track(new AutomationTimeAxisView (_session
,
582 _route
, boost::shared_ptr
<ARDOUR::Automatable
>(), c
,
587 _route
->describe_parameter(param
)));
589 add_automation_child(param
, track
, show
);
594 MidiTimeAxisView::route_active_changed ()
596 RouteUI::route_active_changed ();
599 if (_route
->active()) {
600 controls_ebox
.set_name ("MidiTrackControlsBaseUnselected");
601 controls_base_selected_name
= "MidiTrackControlsBaseSelected";
602 controls_base_unselected_name
= "MidiTrackControlsBaseUnselected";
604 controls_ebox
.set_name ("MidiTrackControlsBaseInactiveUnselected");
605 controls_base_selected_name
= "MidiTrackControlsBaseInactiveSelected";
606 controls_base_unselected_name
= "MidiTrackControlsBaseInactiveUnselected";
612 if (_route
->active()) {
613 controls_ebox
.set_name ("BusControlsBaseUnselected");
614 controls_base_selected_name
= "BusControlsBaseSelected";
615 controls_base_unselected_name
= "BusControlsBaseUnselected";
617 controls_ebox
.set_name ("BusControlsBaseInactiveUnselected");
618 controls_base_selected_name
= "BusControlsBaseInactiveSelected";
619 controls_base_unselected_name
= "BusControlsBaseInactiveUnselected";
625 MidiTimeAxisView::start_step_editing ()
627 step_edit_insert_position
= _editor
.get_preferred_edit_position ();
628 step_edit_beat_pos
= 0;
629 step_edit_region
= playlist()->top_region_at (step_edit_insert_position
);
631 if (step_edit_region
) {
632 RegionView
* rv
= view()->find_view (step_edit_region
);
633 step_edit_region_view
= dynamic_cast<MidiRegionView
*> (rv
);
635 step_edit_region_view
= 0;
638 midi_track()->set_step_editing (true);
642 MidiTimeAxisView::stop_step_editing ()
644 midi_track()->set_step_editing (false);
648 MidiTimeAxisView::check_step_edit ()
650 MidiRingBuffer
<nframes_t
>& incoming (midi_track()->step_edit_ring_buffer());
651 Evoral::Note
<Evoral::MusicalTime
> note
;
653 uint32_t bufsize
= 32;
655 buf
= new uint8_t[bufsize
];
657 while (incoming
.read_space()) {
659 Evoral::EventType type
;
662 incoming
.read_prefix (&time
, &type
, &size
);
664 if (size
> bufsize
) {
667 buf
= new uint8_t[bufsize
];
670 incoming
.read_contents (size
, buf
);
672 if ((buf
[0] & 0xf0) == MIDI_CMD_NOTE_ON
) {
674 if (step_edit_region
== 0) {
676 step_edit_region
= add_region (step_edit_insert_position
);
677 RegionView
* rv
= view()->find_view (step_edit_region
);
680 step_edit_region_view
= dynamic_cast<MidiRegionView
*>(rv
);
682 fatal
<< X_("programming error: no view found for new MIDI region") << endmsg
;
687 if (step_edit_region_view
) {
690 Evoral::MusicalTime beats
= _editor
.get_grid_type_as_beats (success
, step_edit_insert_position
);
696 step_edit_region_view
->add_note (buf
[0] & 0xf, buf
[1], buf
[2], step_edit_beat_pos
, beats
);
697 step_edit_beat_pos
+= beats
;
705 MidiTimeAxisView::step_edit_rest ()
708 Evoral::MusicalTime beats
= _editor
.get_grid_type_as_beats (success
, step_edit_insert_position
);
709 step_edit_beat_pos
+= beats
;
712 boost::shared_ptr
<Region
>
713 MidiTimeAxisView::add_region (nframes64_t pos
)
715 Editor
* real_editor
= dynamic_cast<Editor
*> (&_editor
);
717 real_editor
->begin_reversible_command (_("create region"));
718 XMLNode
&before
= playlist()->get_state();
720 nframes64_t start
= pos
;
721 real_editor
->snap_to (start
, -1);
722 const Meter
& m
= _session
.tempo_map().meter_at(start
);
723 const Tempo
& t
= _session
.tempo_map().tempo_at(start
);
724 double length
= floor (m
.frames_per_bar(t
, _session
.frame_rate()));
726 const boost::shared_ptr
<MidiDiskstream
> diskstream
=
727 boost::dynamic_pointer_cast
<MidiDiskstream
>(view()->trackview().track()->diskstream());
729 boost::shared_ptr
<Source
> src
= _session
.create_midi_source_for_session (*diskstream
.get());
731 boost::shared_ptr
<Region
> region
= (RegionFactory::create (src
, 0, (nframes_t
) length
,
732 PBD::basename_nosuffix(src
->name())));
734 playlist()->add_region (region
, start
);
735 XMLNode
&after
= playlist()->get_state();
736 _session
.add_command (new MementoCommand
<Playlist
> (*playlist().get(), &before
, &after
));
738 real_editor
->commit_reversible_command();