gjackspa: ControlSlider: UPDATE_DISCONTINUOUS eases the comparisons of 2 values
[ng-jackspa.git] / gjackspa.cpp
blob44b51ff6b9b9b791d509eeeb7f9785968d41f3a1
1 // gjackspa.cpp - simple GTK+ LADSPA host for the Jack Audio Connection Kit
2 // Copyright © 2007 Nick Thomas
3 // Copyright © 2013 Géraud Meyer <graud@gmx.com>
4 //
5 // This file is part of ng-jackspa.
6 //
7 // ng-jackspa is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License version 2 as published by the
9 // Free Software Foundation.
11 // This program is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // more details.
16 // You should have received a copy of the GNU General Public License along
17 // with ng-jackspa. If not, see <http://www.gnu.org/licenses/>.
19 #include <gtkmm/scrolledwindow.h>
20 #include <gtkmm/box.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/main.h>
23 #include <gtkmm/adjustment.h>
24 #include <gtkmm/scale.h>
25 #include <gtkmm/window.h>
26 #include "jackspa.h"
27 using namespace Gtk;
29 class ControlSlider : public HScale
31 public:
32 ControlSlider(state_t *state, int port);
33 private:
34 void on_value_changed();
35 float *dest;
38 int main(int argc, char **argv)
40 Main kit(argc, argv);
42 state_t state;
43 if (!jackspa_init(&state, argc, argv))
44 return 1;
46 ScrolledWindow scroll;
47 scroll.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC);
48 VBox slider_box;
49 slider_box.pack_start
50 (*new Label(state.descriptor->Name, 0.0, 0.5, false), PACK_SHRINK);
52 for (int i = 0; i < (int)state.descriptor->PortCount; i++)
53 if ( LADSPA_IS_PORT_INPUT(state.descriptor->PortDescriptors[i]) &&
54 LADSPA_IS_PORT_CONTROL(state.descriptor->PortDescriptors[i]) )
56 HBox *box = new HBox;
57 box->set_spacing(10);
58 box->pack_start
59 ( *new Label(state.descriptor->PortNames[i], ALIGN_RIGHT),
60 PACK_SHRINK );
61 box->pack_start
62 ( *new ControlSlider(&state, i), PACK_EXPAND_WIDGET );
63 slider_box.pack_start(*box, PACK_SHRINK);
66 Window main_window;
67 scroll.add(slider_box);
68 main_window.add(scroll);
69 main_window.resize(400, main_window.get_height());
70 main_window.set_title(state.client_name);
71 main_window.show_all_children();
73 Main::run(main_window);
75 return 0;
78 ControlSlider::ControlSlider(state_t *state, int port)
80 LADSPA_PortRangeHint hint = state->descriptor->PortRangeHints[port];
81 LADSPA_PortRangeHintDescriptor descriptor = hint.HintDescriptor;
82 float lower_bound = hint.LowerBound;
83 float upper_bound = hint.UpperBound;
85 set_update_policy(UPDATE_DISCONTINUOUS);
86 dest = &state->control_port_values[port];
88 if (LADSPA_IS_HINT_SAMPLE_RATE(descriptor)) {
89 int sample_rate = jack_get_sample_rate(state->jack_client);
90 lower_bound *= sample_rate;
91 upper_bound *= sample_rate;
94 if ( LADSPA_IS_HINT_BOUNDED_BELOW(descriptor) &&
95 LADSPA_IS_HINT_BOUNDED_ABOVE(descriptor) )
96 set_range(lower_bound, upper_bound);
97 else if (LADSPA_IS_HINT_BOUNDED_BELOW(descriptor))
98 set_range(lower_bound, 1.0);
99 else if (LADSPA_IS_HINT_BOUNDED_ABOVE(descriptor))
100 set_range(0.0, upper_bound);
101 else
102 set_range(-1.0, 1.0);
104 if (LADSPA_IS_HINT_TOGGLED(descriptor)) {
105 set_range(0.0, 1.0);
106 set_increments(1.0, 1.0);
109 if (LADSPA_IS_HINT_INTEGER(descriptor)) {
110 set_increments(1.0, 1.0);
111 set_digits(0);
113 else {
114 set_increments(0.05, 0.1);
115 set_digits(2);
118 *dest = get_adjustment()->get_lower();
119 if (LADSPA_IS_HINT_HAS_DEFAULT(descriptor))
120 switch (descriptor & LADSPA_HINT_DEFAULT_MASK) {
121 case LADSPA_HINT_DEFAULT_MINIMUM:
122 *dest = lower_bound;
123 break;
124 case LADSPA_HINT_DEFAULT_LOW:
125 *dest = lower_bound * 0.75 + upper_bound * 0.25;
126 break;
127 case LADSPA_HINT_DEFAULT_MIDDLE:
128 *dest = lower_bound * 0.5 + upper_bound * 0.5;
129 break;
130 case LADSPA_HINT_DEFAULT_HIGH:
131 *dest = lower_bound * 0.25 + upper_bound * 0.75;
132 break;
133 case LADSPA_HINT_DEFAULT_MAXIMUM:
134 *dest = upper_bound;
135 break;
136 case LADSPA_HINT_DEFAULT_0:
137 *dest = 0.0;
138 break;
139 case LADSPA_HINT_DEFAULT_1:
140 *dest = 1.0;
141 break;
142 case LADSPA_HINT_DEFAULT_100:
143 *dest = 100.0;
144 break;
145 case LADSPA_HINT_DEFAULT_440:
146 *dest = 440.0;
147 break;
149 set_value(*dest);
152 void ControlSlider::on_value_changed()
154 *dest = get_value();