2 Copyright (C) 2002 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.
25 #include <gtkmm/menu.h>
27 #include "gtkmm2ext/gtk_ui.h"
29 #include "pbd/error.h"
30 #include "pbd/cartesian.h"
31 #include "ardour/panner.h"
32 #include "ardour/panner_shell.h"
33 #include "ardour/pannable.h"
34 #include "ardour/speakers.h"
38 #include "gui_thread.h"
40 #include "public_editor.h"
46 using namespace ARDOUR;
48 using Gtkmm2ext::Keyboard;
50 static const int large_size_threshold = 100;
51 static const int large_border_width = 25;
52 static const int small_border_width = 8;
54 Panner2d::Target::Target (const AngularVector& a, const char *txt)
61 Panner2d::Target::~Target ()
66 Panner2d::Target::set_text (const char* txt)
71 Panner2d::Panner2d (boost::shared_ptr<PannerShell> p, int32_t h)
73 , position (AngularVector (0.0, 0.0), "")
77 , have_elevation (false)
79 panner_shell->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
81 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
84 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
86 handle_state_change ();
87 handle_position_change ();
92 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
98 Panner2d::reset (uint32_t n_inputs)
100 uint32_t nouts = panner_shell->panner()->out().n_audio();
104 while (signals.size() < n_inputs) {
105 add_signal ("", AngularVector());
108 if (signals.size() > n_inputs) {
109 for (uint32_t i = signals.size(); i < n_inputs; ++i) {
113 signals.resize (n_inputs);
118 for (uint32_t i = 0; i < n_inputs; ++i) {
119 signals[i]->position = panner_shell->panner()->signal_position (i);
122 /* add all outputs */
124 while (speakers.size() < nouts) {
125 add_speaker (AngularVector());
128 if (speakers.size() > nouts) {
129 for (uint32_t i = nouts; i < speakers.size(); ++i) {
133 speakers.resize (nouts);
136 for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
137 (*x)->visible = false;
140 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
142 for (uint32_t n = 0; n < nouts; ++n) {
145 snprintf (buf, sizeof (buf), "%d", n+1);
146 speakers[n]->set_text (buf);
147 speakers[n]->position = the_speakers[n].angles();
148 speakers[n]->visible = true;
155 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
157 width = alloc.get_width();
158 height = alloc.get_height();
160 if (height > large_size_threshold) {
161 border = large_border_width;
163 border = small_border_width;
166 radius = min (width, height);
170 hoffset = max ((double) (width - height), border);
171 voffset = max ((double) (height - width), border);
173 hoffset = rint(hoffset / 2.0);
174 voffset = rint(voffset / 2.0);
176 DrawingArea::on_size_allocate (alloc);
180 Panner2d::add_signal (const char* text, const AngularVector& a)
182 Target* signal = new Target (a, text);
183 signals.push_back (signal);
184 signal->visible = true;
190 Panner2d::add_speaker (const AngularVector& a)
192 Target* speaker = new Target (a, "");
193 speakers.push_back (speaker);
194 speaker->visible = true;
197 return speakers.size() - 1;
201 Panner2d::handle_state_change ()
203 panner_connections.drop_connections();
204 if (!panner_shell->panner()) {
208 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
210 set<Evoral::Parameter> params = panner_shell->panner()->what_can_be_automated();
211 set<Evoral::Parameter>::iterator p = params.find(PanElevationAutomation);
212 bool elev = have_elevation;
213 have_elevation = (p == params.end()) ? false : true;
214 if (elev != have_elevation) {
215 handle_position_change();
221 Panner2d::label_signals ()
223 uint32_t sz = signals.size();
230 signals[0]->set_text ("");
234 signals[0]->set_text (_("L"));
235 signals[1]->set_text (_("R"));
239 for (uint32_t i = 0; i < sz; ++i) {
241 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
242 signals[i]->set_text (buf);
249 Panner2d::handle_position_change ()
252 double w = panner_shell->pannable()->pan_width_control->get_value();
254 if (have_elevation) {
255 position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0,
256 panner_shell->pannable()->pan_elevation_control->get_value() * 90.0);
258 position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0, 0);
261 for (uint32_t i = 0; i < signals.size(); ++i) {
262 signals[i]->position = panner_shell->panner()->signal_position (i);
265 if (w * last_width <= 0) {
272 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
274 for (n = 0; n < speakers.size(); ++n) {
275 speakers[n]->position = the_speakers[n].angles();
282 Panner2d::move_signal (int which, const AngularVector& a)
284 if (which >= int (speakers.size())) {
288 speakers[which]->position = a;
293 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
298 float best_distance = FLT_MAX;
301 /* start with the position itself */
303 position.position.cartesian (c);
305 best_distance = sqrt ((c.x - x) * (c.x - x) +
306 (c.y - y) * (c.y - y));
309 #if 0 // TODO signal grab -> change width, not position
310 for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
313 candidate->position.cartesian (c);
316 distance = sqrt ((c.x - x) * (c.x - x) +
317 (c.y - y) * (c.y - y));
319 if (distance < best_distance) {
321 best_distance = distance;
328 if (height > large_size_threshold) {
330 if (best_distance > 30) { // arbitrary
335 if (best_distance > 10) { // arbitrary
340 /* if we didn't find a signal close by, check the speakers */
343 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
346 candidate->position.cartesian (c);
349 distance = sqrt ((c.x - x) * (c.x - x) +
350 (c.y - y) * (c.y - y));
352 if (distance < best_distance) {
354 best_distance = distance;
358 if (height > large_size_threshold) {
360 if (best_distance < 30) { // arbitrary
367 if (best_distance < 10) { // arbitrary
379 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
382 GdkModifierType state;
385 gdk_window_get_pointer (ev->window, &x, &y, &state);
387 x = (int) floor (ev->x);
388 y = (int) floor (ev->y);
389 state = (GdkModifierType) ev->state;
392 if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
396 return handle_motion (x, y, state);
400 Panner2d::on_expose_event (GdkEventExpose *event)
404 bool small = (height <= large_size_threshold);
405 const double diameter = radius*2.0;
407 cr = gdk_cairo_create (get_window()->gobj());
411 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
412 if (!panner_shell->bypassed()) {
413 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
415 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
417 cairo_fill_preserve (cr);
420 /* offset to give us some border */
422 cairo_translate (cr, hoffset, voffset);
424 cairo_set_line_width (cr, 1.0);
426 /* horizontal line of "crosshairs" */
428 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
429 cairo_move_to (cr, 0.0, rint(radius) + .5);
430 cairo_line_to (cr, diameter, rint(radius) + .5);
433 /* vertical line of "crosshairs" */
435 cairo_move_to (cr, rint(radius) + .5, 0);
436 cairo_line_to (cr, rint(radius) + .5, diameter);
439 /* the circle on which signals live */
441 cairo_set_line_width (cr, 1.5);
442 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
443 cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
446 for (uint32_t rad = 15; rad < 90; rad += 15) {
447 cairo_set_line_width (cr, .5 + (float)rad / 150.0);
449 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
451 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.8);
454 cairo_arc (cr, radius, radius, radius * sin(M_PI * (float) rad / 180.0), 0, 2.0 * M_PI);
458 if (!panner_shell->bypassed()) {
460 if (signals.size() > 1) {
461 /* arc to show "diffusion" */
463 double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
464 double position_angle = (2 * M_PI) - panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
467 cairo_translate (cr, radius, radius);
468 cairo_rotate (cr, position_angle - (width_angle/2.0));
469 cairo_move_to (cr, 0, 0);
470 cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
471 cairo_close_path (cr);
472 if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
474 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.45);
477 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
485 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
490 cairo_set_font_size (cr, 10);
496 position.position.cartesian (c);
500 cairo_arc (cr, c.x, c.y, arc_radius + 1.0, 0, 2.0 * M_PI);
501 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
502 cairo_fill_preserve (cr);
503 cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
508 if (signals.size() > 1) {
509 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
512 if (signal->visible) {
514 /* TODO check for overlap - multiple src at same position
515 * -> visualize it properly
517 PBD::AngularVector sp = signal->position;
518 if (!have_elevation) sp.ele = 0;
523 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
524 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.75);
525 cairo_fill_preserve (cr);
526 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 0.8);
529 if (!small && !signal->text.empty()) {
530 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, .9);
531 /* the +/- adjustments are a hack to try to center the text in the circle
532 * TODO use pango get_pixel_size() -- see mono_panner.cc
535 cairo_move_to (cr, c.x - 1, c.y + 1);
537 cairo_move_to (cr, c.x - 4, c.y + 4);
539 cairo_show_text (cr, signal->text.c_str());
549 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
550 Target *speaker = *i;
554 if (speaker->visible) {
558 speaker->position.cartesian (c);
561 snprintf (buf, sizeof (buf), "%d", n);
563 /* stroke out a speaker shape */
565 cairo_move_to (cr, c.x, c.y);
567 cairo_rotate (cr, -(speaker->position.azi/360.0) * (2.0 * M_PI));
569 cairo_scale (cr, 0.8, 0.8);
571 cairo_scale (cr, 1.2, 1.2);
573 cairo_rel_line_to (cr, 4, -2);
574 cairo_rel_line_to (cr, 0, -7);
575 cairo_rel_line_to (cr, 5, +5);
576 cairo_rel_line_to (cr, 5, 0);
577 cairo_rel_line_to (cr, 0, 5);
578 cairo_rel_line_to (cr, -5, 0);
579 cairo_rel_line_to (cr, -5, +5);
580 cairo_rel_line_to (cr, 0, -7);
581 cairo_close_path (cr);
582 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
587 cairo_set_font_size (cr, 16);
589 /* move the text in just a bit */
591 AngularVector textpos (speaker->position.azi, speaker->position.ele, 0.85);
592 textpos.cartesian (c);
594 cairo_move_to (cr, c.x, c.y);
595 cairo_show_text (cr, buf);
608 Panner2d::on_button_press_event (GdkEventButton *ev)
610 GdkModifierType state;
615 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
621 switch (ev->button) {
627 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
629 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
632 drag_target->set_selected (true);
636 state = (GdkModifierType) ev->state;
637 return handle_motion (ev->x, ev->y, state);
648 Panner2d::on_button_release_event (GdkEventButton *ev)
651 GdkModifierType state;
654 switch (ev->button) {
656 x = (int) floor (ev->x);
657 y = (int) floor (ev->y);
658 state = (GdkModifierType) ev->state;
659 ret = handle_motion (x, y, state);
664 x = (int) floor (ev->x);
665 y = (int) floor (ev->y);
666 state = (GdkModifierType) ev->state;
668 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
672 ret = handle_motion (x, y, state);
687 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
689 if (drag_target == 0) {
693 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
700 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
702 bool need_move = false;
704 drag_target->position.cartesian (c);
707 if ((evx != c.x) || (evy != c.y)) {
712 CartesianVector cp (evx, evy, 0.0);
716 if (!have_elevation) {
717 clamp_to_circle (cp.x, cp.y);
719 if (drag_target == &position) {
720 double degree_fract = av.azi / 360.0;
721 panner_shell->panner()->set_position (degree_fract);
724 /* sphere projection */
725 sphere_project (cp.x, cp.y, cp.z);
727 double r2d = 180.0 / M_PI;
728 av.azi = r2d * atan2(cp.y, cp.x);
729 av.ele = r2d * asin(cp.z);
731 if (drag_target == &position) {
732 double azi_fract = av.azi / 360.0;
733 double ele_fract = av.ele / 90.0;
734 panner_shell->panner()->set_position (azi_fract);
735 panner_shell->panner()->set_elevation (ele_fract);
745 Panner2d::on_scroll_event (GdkEventScroll* ev)
747 switch (ev->direction) {
749 case GDK_SCROLL_RIGHT:
750 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
753 case GDK_SCROLL_DOWN:
754 case GDK_SCROLL_LEFT:
755 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
762 Panner2d::cart_to_gtk (CartesianVector& c) const
764 /* cartesian coordinate space:
766 dimension = 2.0 * 2.0
767 increasing y moves up
768 so max values along each axis are -1..+1
770 GTK uses a coordinate space that is:
772 dimension = (radius*2.0) * (radius*2.0)
773 increasing y moves down
775 const double diameter = radius*2.0;
777 c.x = diameter * ((c.x + 1.0) / 2.0);
778 /* extra subtraction inverts the y-axis to match "increasing y moves down" */
779 c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
783 Panner2d::gtk_to_cart (CartesianVector& c) const
785 const double diameter = radius*2.0;
786 c.x = ((c.x / diameter) * 2.0) - 1.0;
787 c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
791 Panner2d::sphere_project (double& x, double& y, double& z)
806 Panner2d::clamp_to_circle (double& x, double& y)
811 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
812 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
816 Panner2d::toggle_bypass ()
818 panner_shell->set_bypassed (!panner_shell->bypassed());
821 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
822 : ArdourWindow (_("Panner (2D)"))
824 , bypass_button (_("Bypass"))
825 , width_adjustment (0, -100, 100, 1, 5, 0)
826 , width_spinner (width_adjustment)
828 widget.set_name ("MixerPanZone");
830 set_title (_("Panner"));
831 widget.set_size_request (h, h);
833 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
834 width_spinner.signal_changed().connect (sigc::mem_fun (*this, &Panner2dWindow::width_changed));
836 p->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::set_bypassed, this), gui_context());
837 /* needed for the width-spinbox in the main window */
838 p->PannableChanged.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::pannable_handler, this), gui_context());
839 p->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
842 button_box.set_spacing (6);
843 button_box.pack_start (bypass_button, false, false);
845 left_side.set_spacing (6);
847 left_side.pack_start (button_box, false, false);
849 Gtk::Label* l = manage (new Label (
850 p->panner()->describe_parameter(PanWidthAutomation),
851 Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
852 spinner_box.pack_start (*l, false, false);
853 spinner_box.pack_start (width_spinner, false, false);
854 left_side.pack_start (spinner_box, false, false);
857 bypass_button.show ();
859 width_spinner.show ();
863 hpacker.set_spacing (6);
864 hpacker.set_border_width (12);
865 hpacker.pack_start (widget, false, false);
866 hpacker.pack_start (left_side, false, false);
877 Panner2dWindow::reset (uint32_t n_inputs)
879 widget.reset (n_inputs);
883 Panner2dWindow::bypass_toggled ()
885 bool view = bypass_button.get_active ();
886 bool model = widget.get_panner_shell()->bypassed ();
889 widget.get_panner_shell()->set_bypassed (view);
893 Panner2dWindow::width_changed ()
895 float model = widget.get_panner_shell()->pannable()->pan_width_control->get_value();
896 float view = width_spinner.get_value() / 100.0;
898 widget.get_panner_shell()->panner()->set_width (view);
903 Panner2dWindow::pannable_handler ()
905 panvalue_connections.drop_connections();
906 widget.get_panner_shell()->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
911 Panner2dWindow::set_bypassed ()
913 bool view = bypass_button.get_active ();
914 bool model = widget.get_panner_shell()->bypassed ();
916 bypass_button.set_active(model);
919 set<Evoral::Parameter> params = widget.get_panner_shell()->panner()->what_can_be_automated();
920 set<Evoral::Parameter>::iterator p = params.find(PanWidthAutomation);
921 if (p == params.end()) {
922 spinner_box.set_sensitive(false);
924 spinner_box.set_sensitive(true);
929 Panner2dWindow::set_width ()
931 // rounding of spinbox is different from slider -- TODO use slider
932 float model = (widget.get_panner_shell()->pannable()->pan_width_control->get_value() * 100.0);
933 float view = (width_spinner.get_value());
935 width_spinner.set_value (model);
940 Panner2dWindow::on_key_press_event (GdkEventKey* event)
942 return relay_key_press (event, &PublicEditor::instance());
946 Panner2dWindow::on_key_release_event (GdkEventKey*)