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);
169 radius = rint(radius) + .5;
171 hoffset = max ((double) (width - height), border);
172 voffset = max ((double) (height - width), border);
174 hoffset = rint(hoffset / 2.0);
175 voffset = rint(voffset / 2.0);
177 DrawingArea::on_size_allocate (alloc);
181 Panner2d::add_signal (const char* text, const AngularVector& a)
183 Target* signal = new Target (a, text);
184 signals.push_back (signal);
185 signal->visible = true;
191 Panner2d::add_speaker (const AngularVector& a)
193 Target* speaker = new Target (a, "");
194 speakers.push_back (speaker);
195 speaker->visible = true;
198 return speakers.size() - 1;
202 Panner2d::handle_state_change ()
204 panner_connections.drop_connections();
205 if (!panner_shell->panner()) {
209 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
211 set<Evoral::Parameter> params = panner_shell->panner()->what_can_be_automated();
212 set<Evoral::Parameter>::iterator p = params.find(PanElevationAutomation);
213 bool elev = have_elevation;
214 have_elevation = (p == params.end()) ? false : true;
215 if (elev != have_elevation) {
216 handle_position_change();
222 Panner2d::label_signals ()
224 uint32_t sz = signals.size();
231 signals[0]->set_text ("");
235 signals[0]->set_text (_("L"));
236 signals[1]->set_text (_("R"));
240 for (uint32_t i = 0; i < sz; ++i) {
242 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
243 signals[i]->set_text (buf);
250 Panner2d::handle_position_change ()
253 double w = panner_shell->pannable()->pan_width_control->get_value();
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 for (uint32_t i = 0; i < signals.size(); ++i) {
259 signals[i]->position = panner_shell->panner()->signal_position (i);
262 if (w * last_width <= 0) {
269 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
271 for (n = 0; n < speakers.size(); ++n) {
272 speakers[n]->position = the_speakers[n].angles();
279 Panner2d::move_signal (int which, const AngularVector& a)
281 if (which >= int (speakers.size())) {
285 speakers[which]->position = a;
290 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
295 float best_distance = FLT_MAX;
298 /* start with the position itself */
300 PBD::AngularVector dp = position.position;
301 if (!have_elevation) dp.ele = 0;
302 dp.azi = 270 - dp.azi;
306 best_distance = sqrt ((c.x - x) * (c.x - x) +
307 (c.y - y) * (c.y - y));
310 #if 0 // TODO signal grab -> change width, not position
311 for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
314 candidate->position.cartesian (c);
317 distance = sqrt ((c.x - x) * (c.x - x) +
318 (c.y - y) * (c.y - y));
320 if (distance < best_distance) {
322 best_distance = distance;
329 if (height > large_size_threshold) {
331 if (best_distance > 30) { // arbitrary
336 if (best_distance > 10) { // arbitrary
341 /* if we didn't find a signal close by, check the speakers */
344 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
346 PBD::AngularVector sp = candidate->position;
347 sp.azi = 270 -sp.azi;
352 distance = sqrt ((c.x - x) * (c.x - x) +
353 (c.y - y) * (c.y - y));
355 if (distance < best_distance) {
357 best_distance = distance;
361 if (height > large_size_threshold) {
363 if (best_distance < 30) { // arbitrary
370 if (best_distance < 10) { // arbitrary
382 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
385 GdkModifierType state;
388 gdk_window_get_pointer (ev->window, &x, &y, &state);
390 x = (int) floor (ev->x);
391 y = (int) floor (ev->y);
392 state = (GdkModifierType) ev->state;
395 if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
399 return handle_motion (x, y, state);
403 Panner2d::on_expose_event (GdkEventExpose *event)
407 bool small = (height <= large_size_threshold);
408 const double diameter = radius*2.0;
410 cr = gdk_cairo_create (get_window()->gobj());
414 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
415 if (!panner_shell->bypassed()) {
416 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
418 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
420 cairo_fill_preserve (cr);
423 /* offset to give us some border */
425 cairo_translate (cr, hoffset, voffset);
427 cairo_set_line_width (cr, 1.0);
429 /* horizontal line of "crosshairs" */
431 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
432 cairo_move_to (cr, 0.0, radius);
433 cairo_line_to (cr, diameter, radius);
436 /* vertical line of "crosshairs" */
438 cairo_move_to (cr, radius, 0);
439 cairo_line_to (cr, radius, diameter);
442 /* the circle on which signals live */
444 cairo_set_line_width (cr, 1.5);
445 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
446 cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
449 for (uint32_t rad = 15; rad < 90; rad += 15) {
450 cairo_set_line_width (cr, .5 + (float)rad / 150.0);
452 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
454 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.8);
457 cairo_arc (cr, radius, radius, radius * sin(M_PI * (float) rad / 180.0), 0, 2.0 * M_PI);
461 if (!panner_shell->bypassed()) {
462 /* convention top == front ^= azimuth == .5 (same as stereo/mono panners) */
464 if (signals.size() > 1) {
465 /* arc to show "diffusion" */
467 double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
468 double position_angle = panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
471 cairo_translate (cr, radius, radius);
472 cairo_rotate (cr, M_PI / 2.0);
473 cairo_rotate (cr, position_angle - (width_angle/2.0));
474 cairo_move_to (cr, 0, 0);
475 cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
476 cairo_close_path (cr);
477 if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
479 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.45);
482 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
490 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
495 cairo_set_font_size (cr, 10);
501 PBD::AngularVector dp = position.position;
502 if (!have_elevation) dp.ele = 0;
503 dp.azi = 270 - dp.azi;
508 cairo_arc (cr, c.x, c.y, arc_radius + 1.0, 0, 2.0 * M_PI);
509 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
510 cairo_fill_preserve (cr);
511 cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
516 if (signals.size() > 0) {
517 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
520 if (signal->visible) {
522 /* TODO check for overlap - multiple src at same position
523 * -> visualize it properly
525 PBD::AngularVector sp = signal->position;
526 if (!have_elevation) sp.ele = 0;
532 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
533 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.75);
534 cairo_fill_preserve (cr);
535 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 0.8);
538 if (!small && !signal->text.empty()) {
539 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, .9);
540 /* the +/- adjustments are a hack to try to center the text in the circle
541 * TODO use pango get_pixel_size() -- see mono_panner.cc
544 cairo_move_to (cr, c.x - 1, c.y + 1);
546 cairo_move_to (cr, c.x - 4, c.y + 4);
548 cairo_show_text (cr, signal->text.c_str());
558 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
559 Target *speaker = *i;
563 if (speaker->visible) {
565 PBD::AngularVector sp = speaker->position;
571 snprintf (buf, sizeof (buf), "%d", n);
573 /* stroke out a speaker shape */
575 cairo_move_to (cr, c.x, c.y);
577 cairo_rotate (cr, -(sp.azi/360.0) * (2.0 * M_PI));
579 cairo_scale (cr, 0.8, 0.8);
581 cairo_scale (cr, 1.2, 1.2);
583 cairo_rel_line_to (cr, 4, -2);
584 cairo_rel_line_to (cr, 0, -7);
585 cairo_rel_line_to (cr, 5, +5);
586 cairo_rel_line_to (cr, 5, 0);
587 cairo_rel_line_to (cr, 0, 5);
588 cairo_rel_line_to (cr, -5, 0);
589 cairo_rel_line_to (cr, -5, +5);
590 cairo_rel_line_to (cr, 0, -7);
591 cairo_close_path (cr);
592 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
597 cairo_set_font_size (cr, 16);
599 /* move the text in just a bit */
601 AngularVector textpos (speaker->position.azi + 270.0, speaker->position.ele, 0.85);
603 textpos.cartesian (c);
605 cairo_move_to (cr, c.x, c.y);
606 cairo_show_text (cr, buf);
619 Panner2d::on_button_press_event (GdkEventButton *ev)
621 GdkModifierType state;
626 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
632 switch (ev->button) {
638 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
640 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
643 drag_target->set_selected (true);
647 state = (GdkModifierType) ev->state;
648 return handle_motion (ev->x, ev->y, state);
659 Panner2d::on_button_release_event (GdkEventButton *ev)
662 GdkModifierType state;
665 switch (ev->button) {
667 x = (int) floor (ev->x);
668 y = (int) floor (ev->y);
669 state = (GdkModifierType) ev->state;
670 ret = handle_motion (x, y, state);
675 x = (int) floor (ev->x);
676 y = (int) floor (ev->y);
677 state = (GdkModifierType) ev->state;
679 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
683 ret = handle_motion (x, y, state);
698 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
700 if (drag_target == 0) {
704 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
711 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
713 bool need_move = false;
715 drag_target->position.cartesian (c);
718 if ((evx != c.x) || (evy != c.y)) {
723 CartesianVector cp (evx, evy, 0.0);
727 if (!have_elevation) {
728 clamp_to_circle (cp.x, cp.y);
730 av.azi = fmod(270 - av.azi, 360);
731 if (drag_target == &position) {
732 double degree_fract = av.azi / 360.0;
733 panner_shell->panner()->set_position (degree_fract);
736 /* sphere projection */
737 sphere_project (cp.x, cp.y, cp.z);
739 double r2d = 180.0 / M_PI;
740 av.azi = r2d * atan2(cp.y, cp.x);
741 av.ele = r2d * asin(cp.z);
742 av.azi = fmod(270 - av.azi, 360);
744 if (drag_target == &position) {
745 double azi_fract = av.azi / 360.0;
746 double ele_fract = av.ele / 90.0;
747 panner_shell->panner()->set_position (azi_fract);
748 panner_shell->panner()->set_elevation (ele_fract);
758 Panner2d::on_scroll_event (GdkEventScroll* ev)
760 switch (ev->direction) {
762 case GDK_SCROLL_RIGHT:
763 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
766 case GDK_SCROLL_DOWN:
767 case GDK_SCROLL_LEFT:
768 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
775 Panner2d::cart_to_gtk (CartesianVector& c) const
777 /* cartesian coordinate space:
779 dimension = 2.0 * 2.0
780 increasing y moves up
781 so max values along each axis are -1..+1
783 GTK uses a coordinate space that is:
785 dimension = (radius*2.0) * (radius*2.0)
786 increasing y moves down
788 const double diameter = radius*2.0;
790 c.x = diameter * ((c.x + 1.0) / 2.0);
791 /* extra subtraction inverts the y-axis to match "increasing y moves down" */
792 c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
796 Panner2d::gtk_to_cart (CartesianVector& c) const
798 const double diameter = radius*2.0;
799 c.x = ((c.x / diameter) * 2.0) - 1.0;
800 c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
804 Panner2d::sphere_project (double& x, double& y, double& z)
819 Panner2d::clamp_to_circle (double& x, double& y)
824 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
825 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
829 Panner2d::toggle_bypass ()
831 panner_shell->set_bypassed (!panner_shell->bypassed());
834 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
835 : ArdourWindow (_("Panner (2D)"))
837 , bypass_button (_("Bypass"))
838 , width_adjustment (0, -100, 100, 1, 5, 0)
839 , width_spinner (width_adjustment)
841 widget.set_name ("MixerPanZone");
843 set_title (_("Panner"));
844 widget.set_size_request (h, h);
846 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
847 width_spinner.signal_changed().connect (sigc::mem_fun (*this, &Panner2dWindow::width_changed));
849 p->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::set_bypassed, this), gui_context());
850 /* needed for the width-spinbox in the main window */
851 p->PannableChanged.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::pannable_handler, this), gui_context());
852 p->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
855 button_box.set_spacing (6);
856 button_box.pack_start (bypass_button, false, false);
858 left_side.set_spacing (6);
860 left_side.pack_start (button_box, false, false);
862 Gtk::Label* l = manage (new Label (
863 p->panner()->describe_parameter(PanWidthAutomation),
864 Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
865 spinner_box.pack_start (*l, false, false);
866 spinner_box.pack_start (width_spinner, false, false);
867 left_side.pack_start (spinner_box, false, false);
870 bypass_button.show ();
872 width_spinner.show ();
876 hpacker.set_spacing (6);
877 hpacker.set_border_width (12);
878 hpacker.pack_start (widget, false, false);
879 hpacker.pack_start (left_side, false, false);
890 Panner2dWindow::reset (uint32_t n_inputs)
892 widget.reset (n_inputs);
896 Panner2dWindow::bypass_toggled ()
898 bool view = bypass_button.get_active ();
899 bool model = widget.get_panner_shell()->bypassed ();
902 widget.get_panner_shell()->set_bypassed (view);
906 Panner2dWindow::width_changed ()
908 float model = widget.get_panner_shell()->pannable()->pan_width_control->get_value();
909 float view = width_spinner.get_value() / 100.0;
911 widget.get_panner_shell()->panner()->set_width (view);
916 Panner2dWindow::pannable_handler ()
918 panvalue_connections.drop_connections();
919 widget.get_panner_shell()->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
924 Panner2dWindow::set_bypassed ()
926 bool view = bypass_button.get_active ();
927 bool model = widget.get_panner_shell()->bypassed ();
929 bypass_button.set_active(model);
932 set<Evoral::Parameter> params = widget.get_panner_shell()->panner()->what_can_be_automated();
933 set<Evoral::Parameter>::iterator p = params.find(PanWidthAutomation);
934 if (p == params.end()) {
935 spinner_box.set_sensitive(false);
937 spinner_box.set_sensitive(true);
942 Panner2dWindow::set_width ()
944 // rounding of spinbox is different from slider -- TODO use slider
945 float model = (widget.get_panner_shell()->pannable()->pan_width_control->get_value() * 100.0);
946 float view = (width_spinner.get_value());
948 width_spinner.set_value (model);
953 Panner2dWindow::on_key_press_event (GdkEventKey* event)
955 return relay_key_press (event, &PublicEditor::instance());
959 Panner2dWindow::on_key_release_event (GdkEventKey*)