Merge remote-tracking branch 'origin/master' into export-dialog
[ardour.git] / gtk2_ardour / midi_automation_line.cc
1 /*
2     Copyright (C) 2010 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
20 #include "ardour/midi_automation_list_binder.h"
21 #include "midi++/midnam_patch.h"
22 #include "midi_automation_line.h"
23 #include "midi_time_axis.h"
24
25 #include "i18n.h"
26
27 using namespace std;
28
29 MidiAutomationLine::MidiAutomationLine (
30         const std::string&                                      name,
31         TimeAxisView&                                           tav,
32         ArdourCanvas::Group&                                    group,
33         boost::shared_ptr<ARDOUR::AutomationList>               list,
34         boost::shared_ptr<ARDOUR::MidiRegion>                   region,
35         Evoral::Parameter                                       parameter,
36         Evoral::TimeConverter<double, ARDOUR::framepos_t>*      converter)
37         : AutomationLine (name, tav, group, list, converter)
38         , _region (region)
39         , _parameter (parameter)
40 {
41
42 }
43
44 MementoCommandBinder<ARDOUR::AutomationList>*
45 MidiAutomationLine::memento_command_binder ()
46 {
47         return new ARDOUR::MidiAutomationListBinder (_region->midi_source(), _parameter);
48 }
49
50 string
51 MidiAutomationLine::get_verbose_cursor_string (double fraction) const
52 {
53         using namespace MIDI::Name;
54
55         if (_parameter.type() != ARDOUR::MidiCCAutomation) {
56                 return AutomationLine::get_verbose_cursor_string(fraction);
57         }
58
59         MidiTimeAxisView* const mtv = dynamic_cast<MidiTimeAxisView*>(trackview.get_parent());
60         if (!mtv) {
61                 return AutomationLine::get_verbose_cursor_string(fraction);
62         }
63
64         boost::shared_ptr<MasterDeviceNames> device_names(mtv->get_device_names());
65         if (!device_names) {
66                 return AutomationLine::get_verbose_cursor_string(fraction);
67         }
68
69         const std::string& device_mode = mtv->gui_property(X_("midnam-custom-device-mode"));
70         const uint8_t      channel     = mtv->get_channel_for_add();
71
72         boost::shared_ptr<const ValueNameList> value_names = device_names->value_name_list_by_control(
73                 device_mode, channel, _parameter.id());
74         if (!value_names) {
75                 return AutomationLine::get_verbose_cursor_string(fraction);
76         }
77
78         const uint16_t cc_value = floor(std::max(std::min(fraction * 127.0, 127.0), 0.0));
79
80         boost::shared_ptr<const Value> value = value_names->max_value_below(cc_value);
81         if (!value) {
82                 return AutomationLine::get_verbose_cursor_string(fraction);
83         }
84
85         return value->name();
86 }
87