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"
36 #include "canvas/colors.h"
38 #include "ardour_ui.h"
41 #include "gui_thread.h"
43 #include "public_editor.h"
49 using namespace ARDOUR;
50 using namespace ARDOUR_UI_UTILS;
52 using Gtkmm2ext::Keyboard;
54 static const int large_size_threshold = 100;
55 static const int large_border_width = 25;
56 static const int small_border_width = 8;
58 Panner2d::Target::Target (const AngularVector& a, const char *txt)
65 Panner2d::Target::~Target ()
70 Panner2d::Target::set_text (const char* txt)
75 Panner2d::Panner2d (boost::shared_ptr<PannerShell> p, int32_t h)
77 , position (AngularVector (0.0, 0.0), "")
81 , have_elevation (false)
84 panner_shell->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
86 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
89 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
91 handle_state_change ();
92 handle_position_change ();
97 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
103 Panner2d::reset (uint32_t n_inputs)
105 uint32_t nouts = panner_shell->panner()->out().n_audio();
109 while (signals.size() < n_inputs) {
110 add_signal ("", AngularVector());
113 if (signals.size() > n_inputs) {
114 for (uint32_t i = signals.size(); i < n_inputs; ++i) {
118 signals.resize (n_inputs);
123 for (uint32_t i = 0; i < n_inputs; ++i) {
124 signals[i]->position = panner_shell->panner()->signal_position (i);
127 /* add all outputs */
129 while (speakers.size() < nouts) {
130 add_speaker (AngularVector());
133 if (speakers.size() > nouts) {
134 for (uint32_t i = nouts; i < speakers.size(); ++i) {
138 speakers.resize (nouts);
141 for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
142 (*x)->visible = false;
145 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
147 for (uint32_t n = 0; n < nouts; ++n) {
150 snprintf (buf, sizeof (buf), "%d", n+1);
151 speakers[n]->set_text (buf);
152 speakers[n]->position = the_speakers[n].angles();
153 speakers[n]->visible = true;
160 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
162 width = alloc.get_width();
163 height = alloc.get_height();
165 if (height > large_size_threshold) {
166 border = large_border_width;
168 border = small_border_width;
171 radius = min (width, height);
174 radius = rint(radius) + .5;
176 hoffset = max ((double) (width - height), border);
177 voffset = max ((double) (height - width), border);
179 hoffset = rint(hoffset / 2.0);
180 voffset = rint(voffset / 2.0);
182 DrawingArea::on_size_allocate (alloc);
186 Panner2d::add_signal (const char* text, const AngularVector& a)
188 Target* signal = new Target (a, text);
189 signals.push_back (signal);
190 signal->visible = true;
196 Panner2d::add_speaker (const AngularVector& a)
198 Target* speaker = new Target (a, "");
199 speakers.push_back (speaker);
200 speaker->visible = true;
203 return speakers.size() - 1;
207 Panner2d::handle_state_change ()
209 panner_connections.drop_connections();
210 if (!panner_shell->panner()) {
214 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
216 set<Evoral::Parameter> params = panner_shell->panner()->what_can_be_automated();
217 set<Evoral::Parameter>::iterator p = params.find(PanElevationAutomation);
218 bool elev = have_elevation;
219 have_elevation = (p == params.end()) ? false : true;
220 if (elev != have_elevation) {
221 handle_position_change();
227 Panner2d::label_signals ()
229 uint32_t sz = signals.size();
236 signals[0]->set_text ("");
240 signals[0]->set_text (S_("Panner|L"));
241 signals[1]->set_text (S_("Panner|R"));
245 for (uint32_t i = 0; i < sz; ++i) {
247 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
248 signals[i]->set_text (buf);
255 Panner2d::handle_position_change ()
258 double w = panner_shell->pannable()->pan_width_control->get_value();
260 position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0,
261 panner_shell->pannable()->pan_elevation_control->get_value() * 90.0);
263 for (uint32_t i = 0; i < signals.size(); ++i) {
264 signals[i]->position = panner_shell->panner()->signal_position (i);
267 if (w * last_width <= 0) {
274 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
276 for (n = 0; n < speakers.size(); ++n) {
277 speakers[n]->position = the_speakers[n].angles();
284 Panner2d::move_signal (int which, const AngularVector& a)
286 if (which >= int (speakers.size())) {
290 speakers[which]->position = a;
295 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
300 float best_distance = FLT_MAX;
303 /* start with the position itself */
305 PBD::AngularVector dp = position.position;
306 if (!have_elevation) dp.ele = 0;
307 dp.azi = 270 - dp.azi;
311 best_distance = sqrt ((c.x - x) * (c.x - x) +
312 (c.y - y) * (c.y - y));
315 #if 0 // TODO signal grab -> change width, not position
316 for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
319 candidate->position.cartesian (c);
322 distance = sqrt ((c.x - x) * (c.x - x) +
323 (c.y - y) * (c.y - y));
325 if (distance < best_distance) {
327 best_distance = distance;
334 if (height > large_size_threshold) {
336 if (best_distance > 30) { // arbitrary
341 if (best_distance > 10) { // arbitrary
346 /* if we didn't find a signal close by, check the speakers */
349 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
351 PBD::AngularVector sp = candidate->position;
352 sp.azi = 270 -sp.azi;
357 distance = sqrt ((c.x - x) * (c.x - x) +
358 (c.y - y) * (c.y - y));
360 if (distance < best_distance) {
362 best_distance = distance;
366 if (height > large_size_threshold) {
368 if (best_distance < 30) { // arbitrary
375 if (best_distance < 10) { // arbitrary
387 Panner2d::set_send_drawing_mode (bool onoff)
389 if (_send_mode != onoff) {
396 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
399 GdkModifierType state;
402 gdk_window_get_pointer (ev->window, &x, &y, &state);
404 x = (int) floor (ev->x);
405 y = (int) floor (ev->y);
406 state = (GdkModifierType) ev->state;
409 if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
413 return handle_motion (x, y, state);
417 Panner2d::on_expose_event (GdkEventExpose *event)
421 bool xsmall = (height <= large_size_threshold);
422 const double diameter = radius*2.0;
424 cr = gdk_cairo_create (get_window()->gobj());
428 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
432 ArdourCanvas::color_to_rgba (ARDOUR_UI::config()->color ("send bg"), r, g, b, a);
434 if (!panner_shell->bypassed()) {
435 cairo_set_source_rgba (cr, r, g, b, 1.0);
437 cairo_set_source_rgba (cr, r, g, b , 0.2);
439 cairo_fill_preserve (cr);
442 /* offset to give us some border */
444 cairo_translate (cr, hoffset, voffset);
446 cairo_set_line_width (cr, 1.0);
448 /* horizontal line of "crosshairs" */
450 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
451 cairo_move_to (cr, 0.0, radius);
452 cairo_line_to (cr, diameter, radius);
455 /* vertical line of "crosshairs" */
457 cairo_move_to (cr, radius, 0);
458 cairo_line_to (cr, radius, diameter);
461 /* the circle on which signals live */
463 cairo_set_line_width (cr, 1.5);
464 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
465 cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
468 for (uint32_t rad = 15; rad < 90; rad += 15) {
469 cairo_set_line_width (cr, .5 + (float)rad / 150.0);
471 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
473 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.8);
476 cairo_arc (cr, radius, radius, radius * sin(M_PI * (float) rad / 180.0), 0, 2.0 * M_PI);
480 if (!panner_shell->bypassed()) {
481 /* convention top == front ^= azimuth == .5 (same as stereo/mono panners) */
483 if (signals.size() > 1) {
484 /* arc to show "diffusion" */
486 double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
487 double position_angle = panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
490 cairo_translate (cr, radius, radius);
491 cairo_rotate (cr, M_PI / 2.0);
492 cairo_rotate (cr, position_angle - (width_angle/2.0));
493 cairo_move_to (cr, 0, 0);
494 cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
495 cairo_close_path (cr);
496 if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
498 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.45);
501 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
509 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
514 cairo_set_font_size (cr, 10);
520 PBD::AngularVector dp = position.position;
521 if (!have_elevation) dp.ele = 0;
522 dp.azi = 270 - dp.azi;
527 cairo_arc (cr, c.x, c.y, arc_radius + 1.0, 0, 2.0 * M_PI);
528 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
529 cairo_fill_preserve (cr);
530 cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
535 if (signals.size() > 0) {
536 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
539 if (signal->visible) {
541 /* TODO check for overlap - multiple src at same position
542 * -> visualize it properly
544 PBD::AngularVector sp = signal->position;
545 if (!have_elevation) sp.ele = 0;
551 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
552 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.75);
553 cairo_fill_preserve (cr);
554 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 0.8);
557 if (!xsmall && !signal->text.empty()) {
558 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, .9);
559 /* the +/- adjustments are a hack to try to center the text in the circle
560 * TODO use pango get_pixel_size() -- see mono_panner.cc
563 cairo_move_to (cr, c.x - 1, c.y + 1);
565 cairo_move_to (cr, c.x - 4, c.y + 4);
567 cairo_show_text (cr, signal->text.c_str());
577 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
578 Target *speaker = *i;
582 if (speaker->visible) {
584 PBD::AngularVector sp = speaker->position;
590 snprintf (buf, sizeof (buf), "%d", n);
592 /* stroke out a speaker shape */
594 cairo_move_to (cr, c.x, c.y);
596 cairo_rotate (cr, -(sp.azi/360.0) * (2.0 * M_PI));
598 cairo_scale (cr, 0.8, 0.8);
600 cairo_scale (cr, 1.2, 1.2);
602 cairo_rel_line_to (cr, 4, -2);
603 cairo_rel_line_to (cr, 0, -7);
604 cairo_rel_line_to (cr, 5, +5);
605 cairo_rel_line_to (cr, 5, 0);
606 cairo_rel_line_to (cr, 0, 5);
607 cairo_rel_line_to (cr, -5, 0);
608 cairo_rel_line_to (cr, -5, +5);
609 cairo_rel_line_to (cr, 0, -7);
610 cairo_close_path (cr);
611 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
616 cairo_set_font_size (cr, 16);
618 /* move the text in just a bit */
620 AngularVector textpos (speaker->position.azi + 270.0, speaker->position.ele, 0.85);
622 textpos.cartesian (c);
624 cairo_move_to (cr, c.x, c.y);
625 cairo_show_text (cr, buf);
638 Panner2d::on_button_press_event (GdkEventButton *ev)
640 GdkModifierType state;
645 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
651 switch (ev->button) {
657 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
659 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
662 drag_target->set_selected (true);
666 state = (GdkModifierType) ev->state;
667 return handle_motion (ev->x, ev->y, state);
678 Panner2d::on_button_release_event (GdkEventButton *ev)
681 GdkModifierType state;
684 switch (ev->button) {
686 x = (int) floor (ev->x);
687 y = (int) floor (ev->y);
688 state = (GdkModifierType) ev->state;
689 ret = handle_motion (x, y, state);
694 x = (int) floor (ev->x);
695 y = (int) floor (ev->y);
696 state = (GdkModifierType) ev->state;
698 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
702 ret = handle_motion (x, y, state);
717 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
719 if (drag_target == 0) {
723 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
730 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
732 bool need_move = false;
734 drag_target->position.cartesian (c);
737 if ((evx != c.x) || (evy != c.y)) {
742 CartesianVector cp (evx, evy, 0.0);
746 if (!have_elevation) {
747 clamp_to_circle (cp.x, cp.y);
749 av.azi = fmod(270 - av.azi, 360);
750 if (drag_target == &position) {
751 double degree_fract = av.azi / 360.0;
752 panner_shell->panner()->set_position (degree_fract);
755 /* sphere projection */
756 sphere_project (cp.x, cp.y, cp.z);
758 double r2d = 180.0 / M_PI;
759 av.azi = r2d * atan2(cp.y, cp.x);
760 av.ele = r2d * asin(cp.z);
761 av.azi = fmod(270 - av.azi, 360);
763 if (drag_target == &position) {
764 double azi_fract = av.azi / 360.0;
765 double ele_fract = av.ele / 90.0;
766 panner_shell->panner()->set_position (azi_fract);
767 panner_shell->panner()->set_elevation (ele_fract);
777 Panner2d::on_scroll_event (GdkEventScroll* ev)
779 switch (ev->direction) {
781 case GDK_SCROLL_RIGHT:
782 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
785 case GDK_SCROLL_DOWN:
786 case GDK_SCROLL_LEFT:
787 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
794 Panner2d::cart_to_gtk (CartesianVector& c) const
796 /* cartesian coordinate space:
798 dimension = 2.0 * 2.0
799 increasing y moves up
800 so max values along each axis are -1..+1
802 GTK uses a coordinate space that is:
804 dimension = (radius*2.0) * (radius*2.0)
805 increasing y moves down
807 const double diameter = radius*2.0;
809 c.x = diameter * ((c.x + 1.0) / 2.0);
810 /* extra subtraction inverts the y-axis to match "increasing y moves down" */
811 c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
815 Panner2d::gtk_to_cart (CartesianVector& c) const
817 const double diameter = radius*2.0;
818 c.x = ((c.x / diameter) * 2.0) - 1.0;
819 c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
823 Panner2d::sphere_project (double& x, double& y, double& z)
838 Panner2d::clamp_to_circle (double& x, double& y)
843 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
844 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
848 Panner2d::toggle_bypass ()
850 panner_shell->set_bypassed (!panner_shell->bypassed());
853 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
854 : ArdourWindow (_("Panner (2D)"))
856 , bypass_button (_("Bypass"))
857 , width_adjustment (0, -100, 100, 1, 5, 0)
858 , width_spinner (width_adjustment)
860 widget.set_name ("MixerPanZone");
862 set_title (_("Panner"));
863 widget.set_size_request (h, h);
865 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
866 width_spinner.signal_changed().connect (sigc::mem_fun (*this, &Panner2dWindow::width_changed));
868 p->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::set_bypassed, this), gui_context());
869 /* needed for the width-spinbox in the main window */
870 p->PannableChanged.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::pannable_handler, this), gui_context());
871 p->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
874 button_box.set_spacing (6);
875 button_box.pack_start (bypass_button, false, false);
877 left_side.set_spacing (6);
879 left_side.pack_start (button_box, false, false);
881 Gtk::Label* l = manage (new Label (
882 p->panner()->describe_parameter(PanWidthAutomation),
883 Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
884 spinner_box.pack_start (*l, false, false);
885 spinner_box.pack_start (width_spinner, false, false);
886 left_side.pack_start (spinner_box, false, false);
889 bypass_button.show ();
891 width_spinner.show ();
895 hpacker.set_spacing (6);
896 hpacker.set_border_width (12);
897 hpacker.pack_start (widget, false, false);
898 hpacker.pack_start (left_side, false, false);
909 Panner2dWindow::reset (uint32_t n_inputs)
911 widget.reset (n_inputs);
915 Panner2dWindow::bypass_toggled ()
917 bool view = bypass_button.get_active ();
918 bool model = widget.get_panner_shell()->bypassed ();
921 widget.get_panner_shell()->set_bypassed (view);
925 Panner2dWindow::width_changed ()
927 float model = widget.get_panner_shell()->pannable()->pan_width_control->get_value();
928 float view = width_spinner.get_value() / 100.0;
930 widget.get_panner_shell()->panner()->set_width (view);
935 Panner2dWindow::pannable_handler ()
937 panvalue_connections.drop_connections();
938 widget.get_panner_shell()->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
943 Panner2dWindow::set_bypassed ()
945 bool view = bypass_button.get_active ();
946 bool model = widget.get_panner_shell()->bypassed ();
948 bypass_button.set_active(model);
951 set<Evoral::Parameter> params = widget.get_panner_shell()->panner()->what_can_be_automated();
952 set<Evoral::Parameter>::iterator p = params.find(PanWidthAutomation);
953 if (p == params.end()) {
954 spinner_box.set_sensitive(false);
956 spinner_box.set_sensitive(true);
961 Panner2dWindow::set_width ()
963 // rounding of spinbox is different from slider -- TODO use slider
964 float model = (widget.get_panner_shell()->pannable()->pan_width_control->get_value() * 100.0);
965 float view = (width_spinner.get_value());
967 width_spinner.set_value (model);
972 Panner2dWindow::on_key_press_event (GdkEventKey* event)
974 return relay_key_press (event, &PublicEditor::instance());
978 Panner2dWindow::on_key_release_event (GdkEventKey*)