2 * Copyright (C) 2005-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2008-2011 David Robillard <d@drobilla.net>
5 * Copyright (C) 2010-2011 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
7 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <gtkmm/menu.h>
31 #include "gtkmm2ext/gtk_ui.h"
33 #include "pbd/error.h"
34 #include "pbd/cartesian.h"
35 #include "ardour/panner.h"
36 #include "ardour/panner_shell.h"
37 #include "ardour/pannable.h"
38 #include "ardour/speakers.h"
40 #include "gtkmm2ext/colors.h"
44 #include "gui_thread.h"
45 #include "rgb_macros.h"
47 #include "public_editor.h"
48 #include "ui_config.h"
54 using namespace ARDOUR;
55 using namespace ARDOUR_UI_UTILS;
57 using Gtkmm2ext::Keyboard;
59 Panner2d::ColorScheme Panner2d::colors;
60 bool Panner2d::have_colors = false;
62 static const int large_size_threshold = 100;
63 static const int large_border_width = 25;
64 static const int small_border_width = 8;
66 Panner2d::Target::Target (const AngularVector& a, const char *txt)
73 Panner2d::Target::~Target ()
78 Panner2d::Target::set_text (const char* txt)
83 Panner2d::Panner2d (boost::shared_ptr<PannerShell> p, int32_t h)
85 , position (AngularVector (0.0, 0.0), "")
89 , have_elevation (false)
97 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &Panner2d::color_handler));
99 panner_shell->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
101 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
104 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
106 handle_state_change ();
107 handle_position_change ();
110 Panner2d::~Panner2d()
112 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
115 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
121 Panner2d::set_colors ()
123 // TODO get all colors from theme, resolve dups
124 colors.background = UIConfiguration::instance().color ("mono panner bg");
125 colors.crosshairs = 0x4884a9ff; // 0.282, 0.517, 0.662, 1.0
126 colors.signalcircle_border = 0x84c5e1ff; // 0.517, 0.772, 0.882, 1.0
127 colors.signalcircle = 0x4884a9ff; // 0.282, 0.517, 0.662, 1.0 // also used with a = 0.9
128 colors.diffusion = 0x4884a973; // 0.282, 0.517, 0.662, 0.45
129 colors.diffusion_inv = 0xff6b6b73; // 1.0, 0.419, 0.419, 0.45
130 colors.pos_outline = 0xffe7e7d9; // 1.0, 0.905, 0.905, 0.85
131 colors.pos_fill = 0xff6b6bd9; // 1.0, 0.419, 0.419, 0.85
132 colors.signal_outline = 0x84c5e1cc; // 0.517, 0.772, 0.882, 0.8
133 colors.signal_fill = 0x4884a9bf; // 0.282, 0.517, 0.662, 0.75
134 colors.speaker_fill = 0x4884a9ff; // 0.282, 0.517, 0.662, 1.0
135 colors.text = 0x84c5e1e6; // 0.517, 0.772, 0.882, 0.9
139 Panner2d::color_handler ()
146 Panner2d::reset (uint32_t n_inputs)
148 uint32_t nouts = panner_shell->panner()->out().n_audio();
152 while (signals.size() < n_inputs) {
153 add_signal ("", AngularVector());
156 if (signals.size() > n_inputs) {
157 for (uint32_t i = signals.size(); i < n_inputs; ++i) {
161 signals.resize (n_inputs);
166 for (uint32_t i = 0; i < n_inputs; ++i) {
167 signals[i]->position = panner_shell->panner()->signal_position (i);
170 /* add all outputs */
172 while (speakers.size() < nouts) {
173 add_speaker (AngularVector());
176 if (speakers.size() > nouts) {
177 for (uint32_t i = nouts; i < speakers.size(); ++i) {
181 speakers.resize (nouts);
184 for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
185 (*x)->visible = false;
188 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
190 for (uint32_t n = 0; n < nouts; ++n) {
193 snprintf (buf, sizeof (buf), "%d", n+1);
194 speakers[n]->set_text (buf);
195 speakers[n]->position = the_speakers[n].angles();
196 speakers[n]->visible = true;
203 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
205 width = alloc.get_width();
206 height = alloc.get_height();
208 if (height > large_size_threshold) {
209 border = large_border_width;
211 border = small_border_width;
214 radius = min (width, height);
217 radius = rint(radius) + .5;
219 hoffset = max ((double) (width - height), border);
220 voffset = max ((double) (height - width), border);
222 hoffset = rint(hoffset / 2.0);
223 voffset = rint(voffset / 2.0);
225 DrawingArea::on_size_allocate (alloc);
229 Panner2d::add_signal (const char* text, const AngularVector& a)
231 Target* signal = new Target (a, text);
232 signals.push_back (signal);
233 signal->visible = true;
239 Panner2d::add_speaker (const AngularVector& a)
241 Target* speaker = new Target (a, "");
242 speakers.push_back (speaker);
243 speaker->visible = true;
246 return speakers.size() - 1;
250 Panner2d::handle_state_change ()
252 panner_connections.drop_connections();
253 if (!panner_shell->panner()) {
257 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
259 set<Evoral::Parameter> params = panner_shell->panner()->what_can_be_automated();
260 set<Evoral::Parameter>::iterator p = params.find(PanElevationAutomation);
261 bool elev = have_elevation;
262 have_elevation = (p == params.end()) ? false : true;
263 if (elev != have_elevation) {
264 handle_position_change();
270 Panner2d::label_signals ()
272 uint32_t sz = signals.size();
279 signals[0]->set_text ("");
283 signals[0]->set_text (S_("Panner|L"));
284 signals[1]->set_text (S_("Panner|R"));
288 for (uint32_t i = 0; i < sz; ++i) {
290 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
291 signals[i]->set_text (buf);
298 Panner2d::handle_position_change ()
301 double w = panner_shell->pannable()->pan_width_control->get_value();
303 position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0,
304 panner_shell->pannable()->pan_elevation_control->get_value() * 90.0);
306 for (uint32_t i = 0; i < signals.size(); ++i) {
307 signals[i]->position = panner_shell->panner()->signal_position (i);
310 if (w * last_width <= 0) {
317 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
319 for (n = 0; n < speakers.size(); ++n) {
320 speakers[n]->position = the_speakers[n].angles();
327 Panner2d::move_signal (int which, const AngularVector& a)
329 if (which >= int (speakers.size())) {
333 speakers[which]->position = a;
338 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
343 float best_distance = FLT_MAX;
346 /* start with the position itself */
348 PBD::AngularVector dp = position.position;
349 if (!have_elevation) dp.ele = 0;
350 dp.azi = 270 - dp.azi;
354 best_distance = sqrt ((c.x - x) * (c.x - x) +
355 (c.y - y) * (c.y - y));
358 #if 0 // TODO signal grab -> change width, not position
359 for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
362 candidate->position.cartesian (c);
365 distance = sqrt ((c.x - x) * (c.x - x) +
366 (c.y - y) * (c.y - y));
368 if (distance < best_distance) {
370 best_distance = distance;
377 if (height > large_size_threshold) {
379 if (best_distance > 30) { // arbitrary
384 if (best_distance > 10) { // arbitrary
389 /* if we didn't find a signal close by, check the speakers */
392 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
394 PBD::AngularVector sp = candidate->position;
395 sp.azi = 270 -sp.azi;
400 distance = sqrt ((c.x - x) * (c.x - x) +
401 (c.y - y) * (c.y - y));
403 if (distance < best_distance) {
405 best_distance = distance;
409 if (height > large_size_threshold) {
411 if (best_distance < 30) { // arbitrary
418 if (best_distance < 10) { // arbitrary
430 Panner2d::set_send_drawing_mode (bool onoff)
432 if (_send_mode != onoff) {
439 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
442 GdkModifierType state;
445 gdk_window_get_pointer (ev->window, &x, &y, &state);
447 x = (int) floor (ev->x);
448 y = (int) floor (ev->y);
449 state = (GdkModifierType) ev->state;
452 if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
456 return handle_motion (x, y, state);
459 #define CSSRGBA(CL) \
460 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(CL), UINT_RGBA_G_FLT(CL), UINT_RGBA_B_FLT(CL), UINT_RGBA_A_FLT(CL));
462 #define CSSRGB(CL, A) \
463 cairo_set_source_rgba (cr, UINT_RGBA_R_FLT(CL), UINT_RGBA_G_FLT(CL), UINT_RGBA_B_FLT(CL), A);
465 Panner2d::on_expose_event (GdkEventExpose *event)
469 bool xsmall = (height <= large_size_threshold);
470 const double diameter = radius*2.0;
472 cr = gdk_cairo_create (get_window()->gobj());
476 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
478 uint32_t bg = colors.background;
480 bg = UIConfiguration::instance().color ("send bg");
483 if (!panner_shell->bypassed()) {
488 cairo_fill_preserve (cr);
491 /* offset to give us some border */
493 cairo_translate (cr, hoffset, voffset);
495 cairo_set_line_width (cr, 1.0);
497 /* horizontal line of "crosshairs" */
499 CSSRGBA(colors.crosshairs);
500 cairo_move_to (cr, 0.0, radius);
501 cairo_line_to (cr, diameter, radius);
504 /* vertical line of "crosshairs" */
506 cairo_move_to (cr, radius, 0);
507 cairo_line_to (cr, radius, diameter);
510 /* the circle on which signals live */
512 cairo_set_line_width (cr, 1.5);
513 CSSRGBA(colors.signalcircle_border);
514 cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
517 for (uint32_t rad = 15; rad < 90; rad += 15) {
518 cairo_set_line_width (cr, .5 + (float)rad / 150.0);
520 CSSRGBA(colors.signalcircle);
522 CSSRGB(colors.signalcircle, 0.9);
525 cairo_arc (cr, radius, radius, radius * sin(M_PI * (float) rad / 180.0), 0, 2.0 * M_PI);
529 if (!panner_shell->bypassed()) {
530 /* convention top == front ^= azimuth == .5 (same as stereo/mono panners) */
532 if (signals.size() > 1) {
533 /* arc to show "diffusion" */
535 double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
536 double position_angle = panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
539 cairo_translate (cr, radius, radius);
540 cairo_rotate (cr, M_PI / 2.0);
541 cairo_rotate (cr, position_angle - (width_angle/2.0));
542 cairo_move_to (cr, 0, 0);
543 cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
544 cairo_close_path (cr);
545 if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
547 CSSRGBA(colors.diffusion);
550 CSSRGBA(colors.diffusion_inv);
558 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
563 cairo_set_font_size (cr, 10);
569 PBD::AngularVector dp = position.position;
570 if (!have_elevation) dp.ele = 0;
571 dp.azi = 270 - dp.azi;
576 cairo_arc (cr, c.x, c.y, arc_radius + 1.0, 0, 2.0 * M_PI);
577 CSSRGBA(colors.pos_fill);
578 cairo_fill_preserve (cr);
579 CSSRGBA(colors.pos_outline);
584 if (signals.size() > 0) {
585 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
588 if (signal->visible) {
590 /* TODO check for overlap - multiple src at same position
591 * -> visualize it properly
593 PBD::AngularVector sp = signal->position;
594 if (!have_elevation) sp.ele = 0;
600 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
601 CSSRGBA(colors.signal_fill);
602 cairo_fill_preserve (cr);
603 CSSRGBA(colors.signal_outline);
606 if (!xsmall && !signal->text.empty()) {
607 CSSRGBA(colors.text);
608 /* the +/- adjustments are a hack to try to center the text in the circle
609 * TODO use pango get_pixel_size() -- see mono_panner.cc
612 cairo_move_to (cr, c.x - 1, c.y + 1);
614 cairo_move_to (cr, c.x - 4, c.y + 4);
616 cairo_show_text (cr, signal->text.c_str());
626 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
627 Target *speaker = *i;
631 if (speaker->visible) {
633 PBD::AngularVector sp = speaker->position;
639 snprintf (buf, sizeof (buf), "%d", n);
641 /* stroke out a speaker shape */
643 cairo_move_to (cr, c.x, c.y);
645 cairo_rotate (cr, -(sp.azi/360.0) * (2.0 * M_PI));
647 cairo_scale (cr, 0.8, 0.8);
649 cairo_scale (cr, 1.2, 1.2);
651 cairo_rel_line_to (cr, 4, -2);
652 cairo_rel_line_to (cr, 0, -7);
653 cairo_rel_line_to (cr, 5, +5);
654 cairo_rel_line_to (cr, 5, 0);
655 cairo_rel_line_to (cr, 0, 5);
656 cairo_rel_line_to (cr, -5, 0);
657 cairo_rel_line_to (cr, -5, +5);
658 cairo_rel_line_to (cr, 0, -7);
659 cairo_close_path (cr);
660 CSSRGBA(colors.speaker_fill);
665 cairo_set_font_size (cr, 16);
667 /* move the text in just a bit */
669 AngularVector textpos (speaker->position.azi + 270.0, speaker->position.ele, 0.85);
671 textpos.cartesian (c);
673 cairo_move_to (cr, c.x, c.y);
674 cairo_show_text (cr, buf);
687 Panner2d::on_button_press_event (GdkEventButton *ev)
689 GdkModifierType state;
694 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
700 switch (ev->button) {
706 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
708 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
711 drag_target->set_selected (true);
715 state = (GdkModifierType) ev->state;
716 return handle_motion (ev->x, ev->y, state);
727 Panner2d::on_button_release_event (GdkEventButton *ev)
730 GdkModifierType state;
733 switch (ev->button) {
735 x = (int) floor (ev->x);
736 y = (int) floor (ev->y);
737 state = (GdkModifierType) ev->state;
738 ret = handle_motion (x, y, state);
743 x = (int) floor (ev->x);
744 y = (int) floor (ev->y);
745 state = (GdkModifierType) ev->state;
747 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
751 ret = handle_motion (x, y, state);
766 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
768 if (drag_target == 0) {
772 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
779 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
781 bool need_move = false;
783 drag_target->position.cartesian (c);
786 if ((evx != c.x) || (evy != c.y)) {
791 CartesianVector cp (evx, evy, 0.0);
795 if (!have_elevation) {
796 clamp_to_circle (cp.x, cp.y);
798 av.azi = fmod(270 - av.azi, 360);
799 if (drag_target == &position) {
800 double degree_fract = av.azi / 360.0;
801 panner_shell->panner()->set_position (degree_fract);
804 /* sphere projection */
805 sphere_project (cp.x, cp.y, cp.z);
807 double r2d = 180.0 / M_PI;
808 av.azi = r2d * atan2(cp.y, cp.x);
809 av.ele = r2d * asin(cp.z);
810 av.azi = fmod(270 - av.azi, 360);
812 if (drag_target == &position) {
813 double azi_fract = av.azi / 360.0;
814 double ele_fract = av.ele / 90.0;
815 panner_shell->panner()->set_position (azi_fract);
816 panner_shell->panner()->set_elevation (ele_fract);
826 Panner2d::on_scroll_event (GdkEventScroll* ev)
828 switch (ev->direction) {
830 case GDK_SCROLL_RIGHT:
831 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
834 case GDK_SCROLL_DOWN:
835 case GDK_SCROLL_LEFT:
836 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
843 Panner2d::cart_to_gtk (CartesianVector& c) const
845 /* cartesian coordinate space:
847 * dimension = 2.0 * 2.0
848 * increasing y moves up
849 * so max values along each axis are -1..+1
851 * GTK uses a coordinate space that is:
853 * dimension = (radius*2.0) * (radius*2.0)
854 * increasing y moves down
856 const double diameter = radius*2.0;
858 c.x = diameter * ((c.x + 1.0) / 2.0);
859 /* extra subtraction inverts the y-axis to match "increasing y moves down" */
860 c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
864 Panner2d::gtk_to_cart (CartesianVector& c) const
866 const double diameter = radius*2.0;
867 c.x = ((c.x / diameter) * 2.0) - 1.0;
868 c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
872 Panner2d::sphere_project (double& x, double& y, double& z)
887 Panner2d::clamp_to_circle (double& x, double& y)
892 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
893 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
897 Panner2d::toggle_bypass ()
899 panner_shell->set_bypassed (!panner_shell->bypassed());
902 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
903 : ArdourWindow (_("Panner (2D)"))
905 , bypass_button (_("Bypass"))
906 , width_adjustment (0, -100, 100, 1, 5, 0)
907 , width_spinner (width_adjustment)
909 widget.set_name ("MixerPanZone");
911 set_title (_("Panner"));
912 widget.set_size_request (h, h);
914 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
915 width_spinner.signal_changed().connect (sigc::mem_fun (*this, &Panner2dWindow::width_changed));
917 p->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::set_bypassed, this), gui_context());
918 /* needed for the width-spinbox in the main window */
919 p->PannableChanged.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::pannable_handler, this), gui_context());
920 p->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
923 button_box.set_spacing (6);
924 button_box.pack_start (bypass_button, false, false);
926 left_side.set_spacing (6);
928 left_side.pack_start (button_box, false, false);
930 Gtk::Label* l = manage (new Label (
931 p->panner()->describe_parameter(PanWidthAutomation),
932 Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
933 spinner_box.pack_start (*l, false, false);
934 spinner_box.pack_start (width_spinner, false, false);
935 left_side.pack_start (spinner_box, false, false);
938 bypass_button.show ();
940 width_spinner.show ();
944 hpacker.set_spacing (6);
945 hpacker.set_border_width (12);
946 hpacker.pack_start (widget, false, false);
947 hpacker.pack_start (left_side, false, false);
958 Panner2dWindow::reset (uint32_t n_inputs)
960 widget.reset (n_inputs);
964 Panner2dWindow::bypass_toggled ()
966 bool view = bypass_button.get_active ();
967 bool model = widget.get_panner_shell()->bypassed ();
970 widget.get_panner_shell()->set_bypassed (view);
974 Panner2dWindow::width_changed ()
976 float model = widget.get_panner_shell()->pannable()->pan_width_control->get_value();
977 float view = width_spinner.get_value() / 100.0;
979 widget.get_panner_shell()->panner()->set_width (view);
984 Panner2dWindow::pannable_handler ()
986 panvalue_connections.drop_connections();
987 widget.get_panner_shell()->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
992 Panner2dWindow::set_bypassed ()
994 bool view = bypass_button.get_active ();
995 bool model = widget.get_panner_shell()->bypassed ();
997 bypass_button.set_active(model);
1000 set<Evoral::Parameter> params = widget.get_panner_shell()->panner()->what_can_be_automated();
1001 set<Evoral::Parameter>::iterator p = params.find(PanWidthAutomation);
1002 if (p == params.end()) {
1003 spinner_box.set_sensitive(false);
1005 spinner_box.set_sensitive(true);
1010 Panner2dWindow::set_width ()
1012 // rounding of spinbox is different from slider -- TODO use slider
1013 float model = (widget.get_panner_shell()->pannable()->pan_width_control->get_value() * 100.0);
1014 float view = (width_spinner.get_value());
1015 if (model != view) {
1016 width_spinner.set_value (model);
1021 Panner2dWindow::on_key_press_event (GdkEventKey* event)
1023 return relay_key_press (event, this);
1027 Panner2dWindow::on_key_release_event (GdkEventKey*)