ab0f0279f1ea5e64927cb3c5e0e34a9c6ba910ca
[ardour.git] / gtk2_ardour / midi_time_axis.cc
1 /*
2     Copyright (C) 2000 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 #include <cstdlib>
20 #include <cmath>
21
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25
26 #include <sigc++/bind.h>
27
28 #include <pbd/error.h>
29 #include <pbd/stl_delete.h>
30 #include <pbd/whitespace.h>
31
32 #include <gtkmm2ext/gtk_ui.h>
33 #include <gtkmm2ext/selector.h>
34 #include <gtkmm2ext/stop_signal.h>
35 #include <gtkmm2ext/bindable_button.h>
36 #include <gtkmm2ext/utils.h>
37
38 #include <ardour/midi_playlist.h>
39 #include <ardour/midi_diskstream.h>
40 #include <ardour/midi_patch_manager.h>
41 #include <ardour/processor.h>
42 #include <ardour/ladspa_plugin.h>
43 #include <ardour/location.h>
44 #include <ardour/playlist.h>
45 #include <ardour/session.h>
46 #include <ardour/session_playlist.h>
47 #include <ardour/utils.h>
48
49 #include "ardour_ui.h"
50 #include "midi_time_axis.h"
51 #include "automation_time_axis.h"
52 #include "automation_line.h"
53 #include "add_midi_cc_track_dialog.h"
54 #include "canvas_impl.h"
55 #include "crossfade_view.h"
56 #include "enums.h"
57 #include "gui_thread.h"
58 #include "keyboard.h"
59 #include "playlist_selector.h"
60 #include "plugin_selector.h"
61 #include "plugin_ui.h"
62 #include "point_selection.h"
63 #include "prompter.h"
64 #include "public_editor.h"
65 #include "region_view.h"
66 #include "rgb_macros.h"
67 #include "selection.h"
68 #include "simplerect.h"
69 #include "midi_streamview.h"
70 #include "utils.h"
71 #include "midi_scroomer.h"
72 #include "piano_roll_header.h"
73 #include "ghostregion.h"
74
75 #include <ardour/midi_track.h>
76
77 #include "i18n.h"
78
79 using namespace ARDOUR;
80 using namespace PBD;
81 using namespace Gtk;
82 using namespace sigc;
83 using namespace Editing;
84         
85 // Minimum height at which a control is displayed
86 static const uint32_t MIDI_CONTROLS_BOX_MIN_HEIGHT = 162;
87 static const uint32_t KEYBOARD_MIN_HEIGHT = 140;
88
89 MidiTimeAxisView::MidiTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
90         : AxisView(sess) // virtually inherited
91         , RouteTimeAxisView(ed, sess, rt, canvas)
92         , _ignore_signals(false)  
93         , _range_scroomer(0)
94         , _piano_roll_header(0)
95         , _note_mode(Sustained)
96         , _note_mode_item(NULL)
97         , _percussion_mode_item(NULL)
98 {
99         subplugin_menu.set_name ("ArdourContextMenu");
100
101         _view = new MidiStreamView (*this);
102
103         ignore_toggle = false;
104
105         mute_button->set_active (false);
106         solo_button->set_active (false);
107         
108         if (is_midi_track()) {
109                 controls_ebox.set_name ("MidiTimeAxisViewControlsBaseUnselected");
110                 _note_mode = midi_track()->note_mode();
111         } else { // MIDI bus (which doesn't exist yet..)
112                 controls_ebox.set_name ("MidiBusControlsBaseUnselected");
113         }
114
115         /* map current state of the route */
116
117         processors_changed ();
118
119         ensure_xml_node ();
120
121         set_state (*xml_node);
122         
123         _route->processors_changed.connect (mem_fun(*this, &MidiTimeAxisView::processors_changed));
124
125         if (is_track()) {
126                 _piano_roll_header = new PianoRollHeader(*midi_view());
127                 _range_scroomer = new MidiScroomer(midi_view()->note_range_adjustment);
128
129                 controls_hbox.pack_start(*_range_scroomer);
130                 controls_hbox.pack_start(*_piano_roll_header);
131
132                 controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
133                 controls_base_selected_name = "MidiTrackControlsBaseSelected";
134                 controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
135
136                 midi_view()->NoteRangeChanged.connect (mem_fun(*this, &MidiTimeAxisView::update_range));
137
138                 /* ask for notifications of any new RegionViews */
139                 _view->RegionViewAdded.connect (mem_fun(*this, &MidiTimeAxisView::region_view_added));
140                 _view->attach ();
141         }
142                 
143         HBox* midi_controls_hbox = manage(new HBox());
144         
145         // Instrument patch selector
146         ComboBoxText*       model_selector              = manage(new ComboBoxText());
147         ComboBoxText*       custom_device_mode_selector = manage(new ComboBoxText());
148         
149         MIDI::Name::MidiPatchManager& patch_manager = MIDI::Name::MidiPatchManager::instance();
150
151         for (MIDI::Name::MasterDeviceNames::Models::const_iterator model = patch_manager.all_models().begin();
152              model != patch_manager.all_models().end();
153              ++model) {
154                 model_selector->append_text(model->c_str());
155         }
156         
157         // TODO: persist the choice
158         model_selector->set_active(0);
159         
160         std::list<std::string> device_modes = patch_manager.custom_device_mode_names_by_model(model_selector->get_active_text());
161         
162         for (std::list<std::string>::const_iterator i = device_modes.begin(); i != device_modes.end(); ++i) {
163                 cerr << "found custom device mode " << *i << endl;
164                 custom_device_mode_selector->append_text(*i);
165         }
166
167         // TODO: persist the choice
168         custom_device_mode_selector->set_active(0);
169         
170         midi_controls_hbox->pack_start(_channel_selector, true, false);
171         if (!patch_manager.all_models().empty()) {
172                 _midi_controls_box.pack_start(*model_selector, true, false);
173                 _midi_controls_box.pack_start(*custom_device_mode_selector, true, false);
174         }
175         
176         _midi_controls_box.pack_start(*midi_controls_hbox, true, true);
177         
178         controls_vbox.pack_start(_midi_controls_box, false, false);
179         
180         boost::shared_ptr<MidiDiskstream> diskstream = midi_track()->midi_diskstream();
181
182         // restore channel selector settings
183         _channel_selector.set_channel_mode(diskstream->get_channel_mode(), diskstream->get_channel_mask());
184         _channel_selector.mode_changed.connect(
185                 mem_fun(*midi_track()->midi_diskstream(), &MidiDiskstream::set_channel_mode));
186
187 }
188
189 MidiTimeAxisView::~MidiTimeAxisView ()
190 {
191         delete _piano_roll_header;
192         _piano_roll_header = 0;
193
194         delete _range_scroomer;
195         _range_scroomer = 0;
196 }
197
198 MidiStreamView*
199 MidiTimeAxisView::midi_view()
200 {
201         return dynamic_cast<MidiStreamView*>(_view);
202 }
203
204 guint32
205 MidiTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
206 {
207         ensure_xml_node ();
208         xml_node->add_property ("shown-editor", "yes");
209                 
210         guint32 ret = TimeAxisView::show_at (y, nth, parent);
211         return ret;
212 }
213
214 void
215 MidiTimeAxisView::hide ()
216 {
217         ensure_xml_node ();
218         xml_node->add_property ("shown-editor", "no");
219
220         TimeAxisView::hide ();
221 }
222
223 void
224 MidiTimeAxisView::set_height (uint32_t h)
225 {
226         RouteTimeAxisView::set_height (h);
227
228         if (height >= MIDI_CONTROLS_BOX_MIN_HEIGHT) {
229                 _midi_controls_box.show();
230         } else {
231                 _midi_controls_box.hide();
232         }
233         
234         if (height >= KEYBOARD_MIN_HEIGHT) {
235                 if (is_track() && _range_scroomer)
236                         _range_scroomer->show();
237                 if (is_track() && _piano_roll_header)
238                         _piano_roll_header->show();
239         } else {
240                 if (is_track() && _range_scroomer)
241                         _range_scroomer->hide();
242                 if (is_track() && _piano_roll_header)
243                         _piano_roll_header->hide();
244         }
245 }
246
247 void
248 MidiTimeAxisView::append_extra_display_menu_items ()
249 {
250         using namespace Menu_Helpers;
251
252         MenuList& items = display_menu->items();
253
254         // Note range
255         Menu *range_menu = manage(new Menu);
256         MenuList& range_items = range_menu->items();
257         range_menu->set_name ("ArdourContextMenu");
258         
259         range_items.push_back (MenuElem (_("Show Full Range"), bind (
260                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
261                         MidiStreamView::FullRange)));
262         
263         range_items.push_back (MenuElem (_("Fit Contents"), bind (
264                         mem_fun(*this, &MidiTimeAxisView::set_note_range),
265                         MidiStreamView::ContentsRange)));
266
267         items.push_back (MenuElem (_("Note range"), *range_menu));
268 }
269
270 void
271 MidiTimeAxisView::build_automation_action_menu ()
272 {
273         using namespace Menu_Helpers;
274
275         RouteTimeAxisView::build_automation_action_menu ();
276
277         MenuList& automation_items = automation_action_menu->items();
278         
279         automation_items.push_back (SeparatorElem());
280         automation_items.push_back (MenuElem (_("Controller..."), 
281                         mem_fun(*this, &MidiTimeAxisView::add_cc_track)));
282         automation_items.push_back (MenuElem (_("Bender"), 
283                         sigc::bind(mem_fun(*this, &MidiTimeAxisView::add_parameter_track),
284                                 Evoral::Parameter(MidiPitchBenderAutomation))));
285         automation_items.push_back (MenuElem (_("Pressure"), 
286                         sigc::bind(mem_fun(*this, &MidiTimeAxisView::add_parameter_track),
287                                 Evoral::Parameter(MidiChannelPressureAutomation))));
288
289 }
290
291 Gtk::Menu*
292 MidiTimeAxisView::build_mode_menu()
293 {
294         using namespace Menu_Helpers;
295
296         Menu* mode_menu = manage (new Menu);
297         MenuList& items = mode_menu->items();
298         mode_menu->set_name ("ArdourContextMenu");
299
300         RadioMenuItem::Group mode_group;
301         items.push_back (RadioMenuElem (mode_group, _("Sustained"),
302                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Sustained)));
303         _note_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
304         _note_mode_item->set_active(_note_mode == Sustained);
305
306         items.push_back (RadioMenuElem (mode_group, _("Percussive"),
307                                 bind (mem_fun (*this, &MidiTimeAxisView::set_note_mode), Percussive)));
308         _percussion_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
309         _percussion_mode_item->set_active(_note_mode == Percussive);
310
311         return mode_menu;
312 }
313         
314 void
315 MidiTimeAxisView::set_note_mode(NoteMode mode)
316 {
317         if (_note_mode != mode || midi_track()->note_mode() != mode) {
318                 _note_mode = mode;
319                 midi_track()->set_note_mode(mode);
320                 _view->redisplay_diskstream();
321         }
322 }
323
324
325 void
326 MidiTimeAxisView::set_note_range(MidiStreamView::VisibleNoteRange range)
327 {
328         if (!_ignore_signals)
329                 midi_view()->set_note_range(range);
330 }
331
332
333 void
334 MidiTimeAxisView::update_range()
335 {
336         MidiGhostRegion* mgr;
337
338         for(list<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
339                 if ((mgr = dynamic_cast<MidiGhostRegion*>(*i)) != 0) {
340                         mgr->update_range();
341                 }
342         }
343 }
344
345 void
346 MidiTimeAxisView::show_all_automation ()
347 {
348         if (midi_track()) {
349                 const set<Evoral::Parameter> params = midi_track()->midi_diskstream()->
350                                 midi_playlist()->contained_automation();
351
352                 for (set<Evoral::Parameter>::const_iterator i = params.begin(); i != params.end(); ++i) {
353                         create_automation_child(*i, true);
354                 }
355         }
356
357         RouteTimeAxisView::show_all_automation ();
358 }
359
360 void
361 MidiTimeAxisView::show_existing_automation ()
362 {
363         if (midi_track()) {
364                 const set<Evoral::Parameter> params = midi_track()->midi_diskstream()->
365                                 midi_playlist()->contained_automation();
366
367                 for (set<Evoral::Parameter>::const_iterator i = params.begin(); i != params.end(); ++i) {
368                         create_automation_child(*i, true);
369                 }
370         }
371
372         RouteTimeAxisView::show_existing_automation ();
373 }
374
375 /** Prompt for a controller with a dialog and add an automation track for it
376  */
377 void
378 MidiTimeAxisView::add_cc_track()
379 {
380         int response;
381         Evoral::Parameter param(0, 0, 0);
382
383         {
384                 AddMidiCCTrackDialog dialog;
385                 dialog.set_transient_for(editor);
386                 response = dialog.run();
387                 
388                 if (response == Gtk::RESPONSE_ACCEPT)
389                         param = dialog.parameter();
390         }
391
392         if (param.type() != 0 && response == Gtk::RESPONSE_ACCEPT)
393                 create_automation_child(param, true);
394 }
395
396
397 /** Add an automation track for the given parameter (pitch bend, channel pressure).
398  */
399 void
400 MidiTimeAxisView::add_parameter_track(const Evoral::Parameter& param)
401 {
402         create_automation_child(param, true);
403 }
404
405 void
406 MidiTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
407 {
408         if (    param.type() == MidiCCAutomation ||
409                         param.type() == MidiPgmChangeAutomation ||
410                         param.type() == MidiPitchBenderAutomation ||
411                         param.type() == MidiChannelPressureAutomation
412            ) {
413         
414                 /* These controllers are region "automation", so we do not create
415                  * an AutomationList/Line for the track */
416
417                 AutomationTracks::iterator existing = _automation_tracks.find(param);
418                 if (existing != _automation_tracks.end())
419                         return;
420
421                 boost::shared_ptr<AutomationControl> c
422                         = boost::dynamic_pointer_cast<AutomationControl>(_route->data().control(param));
423
424                 if (!c) {
425                         c = boost::dynamic_pointer_cast<AutomationControl>(_route->control_factory(param));
426                         _route->add_control(c);
427                 }
428
429                 boost::shared_ptr<AutomationTimeAxisView> track(new AutomationTimeAxisView (_session,
430                                 _route, boost::shared_ptr<ARDOUR::Automatable>(), c,
431                                 editor,
432                                 *this,
433                                 true,
434                                 parent_canvas,
435                                 _route->describe_parameter(param)));
436                 
437                 add_automation_child(param, track, show);
438
439         } else {
440                 error << "MidiTimeAxisView: unknown automation child "
441                         << ARDOUR::EventTypeMap::instance().to_symbol(param) << endmsg;
442         }
443 }
444
445 void
446 MidiTimeAxisView::route_active_changed ()
447 {
448         RouteUI::route_active_changed ();
449
450         if (is_track()) {
451                 if (_route->active()) {
452                         controls_ebox.set_name ("MidiTrackControlsBaseUnselected");
453                         controls_base_selected_name = "MidiTrackControlsBaseSelected";
454                         controls_base_unselected_name = "MidiTrackControlsBaseUnselected";
455                 } else {
456                         controls_ebox.set_name ("MidiTrackControlsBaseInactiveUnselected");
457                         controls_base_selected_name = "MidiTrackControlsBaseInactiveSelected";
458                         controls_base_unselected_name = "MidiTrackControlsBaseInactiveUnselected";
459                 }
460         } else {
461
462                 throw; // wha?
463                 
464                 if (_route->active()) {
465                         controls_ebox.set_name ("BusControlsBaseUnselected");
466                         controls_base_selected_name = "BusControlsBaseSelected";
467                         controls_base_unselected_name = "BusControlsBaseUnselected";
468                 } else {
469                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
470                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
471                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
472                 }
473         }
474 }
475
476
477
478