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)
80 panner_shell->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
82 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
85 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
87 handle_state_change ();
88 handle_position_change ();
93 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
99 Panner2d::reset (uint32_t n_inputs)
101 uint32_t nouts = panner_shell->panner()->out().n_audio();
105 while (signals.size() < n_inputs) {
106 add_signal ("", AngularVector());
109 if (signals.size() > n_inputs) {
110 for (uint32_t i = signals.size(); i < n_inputs; ++i) {
114 signals.resize (n_inputs);
119 for (uint32_t i = 0; i < n_inputs; ++i) {
120 signals[i]->position = panner_shell->panner()->signal_position (i);
123 /* add all outputs */
125 while (speakers.size() < nouts) {
126 add_speaker (AngularVector());
129 if (speakers.size() > nouts) {
130 for (uint32_t i = nouts; i < speakers.size(); ++i) {
134 speakers.resize (nouts);
137 for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
138 (*x)->visible = false;
141 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
143 for (uint32_t n = 0; n < nouts; ++n) {
146 snprintf (buf, sizeof (buf), "%d", n+1);
147 speakers[n]->set_text (buf);
148 speakers[n]->position = the_speakers[n].angles();
149 speakers[n]->visible = true;
156 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
158 width = alloc.get_width();
159 height = alloc.get_height();
161 if (height > large_size_threshold) {
162 border = large_border_width;
164 border = small_border_width;
167 radius = min (width, height);
170 radius = rint(radius) + .5;
172 hoffset = max ((double) (width - height), border);
173 voffset = max ((double) (height - width), border);
175 hoffset = rint(hoffset / 2.0);
176 voffset = rint(voffset / 2.0);
178 DrawingArea::on_size_allocate (alloc);
182 Panner2d::add_signal (const char* text, const AngularVector& a)
184 Target* signal = new Target (a, text);
185 signals.push_back (signal);
186 signal->visible = true;
192 Panner2d::add_speaker (const AngularVector& a)
194 Target* speaker = new Target (a, "");
195 speakers.push_back (speaker);
196 speaker->visible = true;
199 return speakers.size() - 1;
203 Panner2d::handle_state_change ()
205 panner_connections.drop_connections();
206 if (!panner_shell->panner()) {
210 panner_shell->panner()->SignalPositionChanged.connect (panner_connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
212 set<Evoral::Parameter> params = panner_shell->panner()->what_can_be_automated();
213 set<Evoral::Parameter>::iterator p = params.find(PanElevationAutomation);
214 bool elev = have_elevation;
215 have_elevation = (p == params.end()) ? false : true;
216 if (elev != have_elevation) {
217 handle_position_change();
223 Panner2d::label_signals ()
225 uint32_t sz = signals.size();
232 signals[0]->set_text ("");
236 signals[0]->set_text (_("L"));
237 signals[1]->set_text (_("R"));
241 for (uint32_t i = 0; i < sz; ++i) {
243 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
244 signals[i]->set_text (buf);
251 Panner2d::handle_position_change ()
254 double w = panner_shell->pannable()->pan_width_control->get_value();
256 position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0,
257 panner_shell->pannable()->pan_elevation_control->get_value() * 90.0);
259 for (uint32_t i = 0; i < signals.size(); ++i) {
260 signals[i]->position = panner_shell->panner()->signal_position (i);
263 if (w * last_width <= 0) {
270 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
272 for (n = 0; n < speakers.size(); ++n) {
273 speakers[n]->position = the_speakers[n].angles();
280 Panner2d::move_signal (int which, const AngularVector& a)
282 if (which >= int (speakers.size())) {
286 speakers[which]->position = a;
291 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
296 float best_distance = FLT_MAX;
299 /* start with the position itself */
301 PBD::AngularVector dp = position.position;
302 if (!have_elevation) dp.ele = 0;
303 dp.azi = 270 - dp.azi;
307 best_distance = sqrt ((c.x - x) * (c.x - x) +
308 (c.y - y) * (c.y - y));
311 #if 0 // TODO signal grab -> change width, not position
312 for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
315 candidate->position.cartesian (c);
318 distance = sqrt ((c.x - x) * (c.x - x) +
319 (c.y - y) * (c.y - y));
321 if (distance < best_distance) {
323 best_distance = distance;
330 if (height > large_size_threshold) {
332 if (best_distance > 30) { // arbitrary
337 if (best_distance > 10) { // arbitrary
342 /* if we didn't find a signal close by, check the speakers */
345 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
347 PBD::AngularVector sp = candidate->position;
348 sp.azi = 270 -sp.azi;
353 distance = sqrt ((c.x - x) * (c.x - x) +
354 (c.y - y) * (c.y - y));
356 if (distance < best_distance) {
358 best_distance = distance;
362 if (height > large_size_threshold) {
364 if (best_distance < 30) { // arbitrary
371 if (best_distance < 10) { // arbitrary
383 Panner2d::set_send_drawing_mode (bool onoff)
385 if (_send_mode != onoff) {
392 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
395 GdkModifierType state;
398 gdk_window_get_pointer (ev->window, &x, &y, &state);
400 x = (int) floor (ev->x);
401 y = (int) floor (ev->y);
402 state = (GdkModifierType) ev->state;
405 if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
409 return handle_motion (x, y, state);
413 Panner2d::on_expose_event (GdkEventExpose *event)
417 bool small = (height <= large_size_threshold);
418 const double diameter = radius*2.0;
420 cr = gdk_cairo_create (get_window()->gobj());
424 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
429 rgba_p_from_style("SendStripBase", &r, &g, &b, "fg");
431 if (!panner_shell->bypassed()) {
432 cairo_set_source_rgba (cr, r, g, b, 1.0);
434 cairo_set_source_rgba (cr, r, g, b , 0.2);
436 cairo_fill_preserve (cr);
439 /* offset to give us some border */
441 cairo_translate (cr, hoffset, voffset);
443 cairo_set_line_width (cr, 1.0);
445 /* horizontal line of "crosshairs" */
447 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
448 cairo_move_to (cr, 0.0, radius);
449 cairo_line_to (cr, diameter, radius);
452 /* vertical line of "crosshairs" */
454 cairo_move_to (cr, radius, 0);
455 cairo_line_to (cr, radius, diameter);
458 /* the circle on which signals live */
460 cairo_set_line_width (cr, 1.5);
461 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
462 cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
465 for (uint32_t rad = 15; rad < 90; rad += 15) {
466 cairo_set_line_width (cr, .5 + (float)rad / 150.0);
468 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
470 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.8);
473 cairo_arc (cr, radius, radius, radius * sin(M_PI * (float) rad / 180.0), 0, 2.0 * M_PI);
477 if (!panner_shell->bypassed()) {
478 /* convention top == front ^= azimuth == .5 (same as stereo/mono panners) */
480 if (signals.size() > 1) {
481 /* arc to show "diffusion" */
483 double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
484 double position_angle = panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
487 cairo_translate (cr, radius, radius);
488 cairo_rotate (cr, M_PI / 2.0);
489 cairo_rotate (cr, position_angle - (width_angle/2.0));
490 cairo_move_to (cr, 0, 0);
491 cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
492 cairo_close_path (cr);
493 if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
495 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.45);
498 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
506 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
511 cairo_set_font_size (cr, 10);
517 PBD::AngularVector dp = position.position;
518 if (!have_elevation) dp.ele = 0;
519 dp.azi = 270 - dp.azi;
524 cairo_arc (cr, c.x, c.y, arc_radius + 1.0, 0, 2.0 * M_PI);
525 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
526 cairo_fill_preserve (cr);
527 cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
532 if (signals.size() > 0) {
533 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
536 if (signal->visible) {
538 /* TODO check for overlap - multiple src at same position
539 * -> visualize it properly
541 PBD::AngularVector sp = signal->position;
542 if (!have_elevation) sp.ele = 0;
548 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
549 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.75);
550 cairo_fill_preserve (cr);
551 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 0.8);
554 if (!small && !signal->text.empty()) {
555 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, .9);
556 /* the +/- adjustments are a hack to try to center the text in the circle
557 * TODO use pango get_pixel_size() -- see mono_panner.cc
560 cairo_move_to (cr, c.x - 1, c.y + 1);
562 cairo_move_to (cr, c.x - 4, c.y + 4);
564 cairo_show_text (cr, signal->text.c_str());
574 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
575 Target *speaker = *i;
579 if (speaker->visible) {
581 PBD::AngularVector sp = speaker->position;
587 snprintf (buf, sizeof (buf), "%d", n);
589 /* stroke out a speaker shape */
591 cairo_move_to (cr, c.x, c.y);
593 cairo_rotate (cr, -(sp.azi/360.0) * (2.0 * M_PI));
595 cairo_scale (cr, 0.8, 0.8);
597 cairo_scale (cr, 1.2, 1.2);
599 cairo_rel_line_to (cr, 4, -2);
600 cairo_rel_line_to (cr, 0, -7);
601 cairo_rel_line_to (cr, 5, +5);
602 cairo_rel_line_to (cr, 5, 0);
603 cairo_rel_line_to (cr, 0, 5);
604 cairo_rel_line_to (cr, -5, 0);
605 cairo_rel_line_to (cr, -5, +5);
606 cairo_rel_line_to (cr, 0, -7);
607 cairo_close_path (cr);
608 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
613 cairo_set_font_size (cr, 16);
615 /* move the text in just a bit */
617 AngularVector textpos (speaker->position.azi + 270.0, speaker->position.ele, 0.85);
619 textpos.cartesian (c);
621 cairo_move_to (cr, c.x, c.y);
622 cairo_show_text (cr, buf);
635 Panner2d::on_button_press_event (GdkEventButton *ev)
637 GdkModifierType state;
642 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
648 switch (ev->button) {
654 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
656 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
659 drag_target->set_selected (true);
663 state = (GdkModifierType) ev->state;
664 return handle_motion (ev->x, ev->y, state);
675 Panner2d::on_button_release_event (GdkEventButton *ev)
678 GdkModifierType state;
681 switch (ev->button) {
683 x = (int) floor (ev->x);
684 y = (int) floor (ev->y);
685 state = (GdkModifierType) ev->state;
686 ret = handle_motion (x, y, state);
691 x = (int) floor (ev->x);
692 y = (int) floor (ev->y);
693 state = (GdkModifierType) ev->state;
695 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
699 ret = handle_motion (x, y, state);
714 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
716 if (drag_target == 0) {
720 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
727 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
729 bool need_move = false;
731 drag_target->position.cartesian (c);
734 if ((evx != c.x) || (evy != c.y)) {
739 CartesianVector cp (evx, evy, 0.0);
743 if (!have_elevation) {
744 clamp_to_circle (cp.x, cp.y);
746 av.azi = fmod(270 - av.azi, 360);
747 if (drag_target == &position) {
748 double degree_fract = av.azi / 360.0;
749 panner_shell->panner()->set_position (degree_fract);
752 /* sphere projection */
753 sphere_project (cp.x, cp.y, cp.z);
755 double r2d = 180.0 / M_PI;
756 av.azi = r2d * atan2(cp.y, cp.x);
757 av.ele = r2d * asin(cp.z);
758 av.azi = fmod(270 - av.azi, 360);
760 if (drag_target == &position) {
761 double azi_fract = av.azi / 360.0;
762 double ele_fract = av.ele / 90.0;
763 panner_shell->panner()->set_position (azi_fract);
764 panner_shell->panner()->set_elevation (ele_fract);
774 Panner2d::on_scroll_event (GdkEventScroll* ev)
776 switch (ev->direction) {
778 case GDK_SCROLL_RIGHT:
779 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
782 case GDK_SCROLL_DOWN:
783 case GDK_SCROLL_LEFT:
784 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
791 Panner2d::cart_to_gtk (CartesianVector& c) const
793 /* cartesian coordinate space:
795 dimension = 2.0 * 2.0
796 increasing y moves up
797 so max values along each axis are -1..+1
799 GTK uses a coordinate space that is:
801 dimension = (radius*2.0) * (radius*2.0)
802 increasing y moves down
804 const double diameter = radius*2.0;
806 c.x = diameter * ((c.x + 1.0) / 2.0);
807 /* extra subtraction inverts the y-axis to match "increasing y moves down" */
808 c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
812 Panner2d::gtk_to_cart (CartesianVector& c) const
814 const double diameter = radius*2.0;
815 c.x = ((c.x / diameter) * 2.0) - 1.0;
816 c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
820 Panner2d::sphere_project (double& x, double& y, double& z)
835 Panner2d::clamp_to_circle (double& x, double& y)
840 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
841 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
845 Panner2d::toggle_bypass ()
847 panner_shell->set_bypassed (!panner_shell->bypassed());
850 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
851 : ArdourWindow (_("Panner (2D)"))
853 , bypass_button (_("Bypass"))
854 , width_adjustment (0, -100, 100, 1, 5, 0)
855 , width_spinner (width_adjustment)
857 widget.set_name ("MixerPanZone");
859 set_title (_("Panner"));
860 widget.set_size_request (h, h);
862 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
863 width_spinner.signal_changed().connect (sigc::mem_fun (*this, &Panner2dWindow::width_changed));
865 p->Changed.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::set_bypassed, this), gui_context());
866 /* needed for the width-spinbox in the main window */
867 p->PannableChanged.connect (panshell_connections, invalidator (*this), boost::bind (&Panner2dWindow::pannable_handler, this), gui_context());
868 p->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
871 button_box.set_spacing (6);
872 button_box.pack_start (bypass_button, false, false);
874 left_side.set_spacing (6);
876 left_side.pack_start (button_box, false, false);
878 Gtk::Label* l = manage (new Label (
879 p->panner()->describe_parameter(PanWidthAutomation),
880 Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
881 spinner_box.pack_start (*l, false, false);
882 spinner_box.pack_start (width_spinner, false, false);
883 left_side.pack_start (spinner_box, false, false);
886 bypass_button.show ();
888 width_spinner.show ();
892 hpacker.set_spacing (6);
893 hpacker.set_border_width (12);
894 hpacker.pack_start (widget, false, false);
895 hpacker.pack_start (left_side, false, false);
906 Panner2dWindow::reset (uint32_t n_inputs)
908 widget.reset (n_inputs);
912 Panner2dWindow::bypass_toggled ()
914 bool view = bypass_button.get_active ();
915 bool model = widget.get_panner_shell()->bypassed ();
918 widget.get_panner_shell()->set_bypassed (view);
922 Panner2dWindow::width_changed ()
924 float model = widget.get_panner_shell()->pannable()->pan_width_control->get_value();
925 float view = width_spinner.get_value() / 100.0;
927 widget.get_panner_shell()->panner()->set_width (view);
932 Panner2dWindow::pannable_handler ()
934 panvalue_connections.drop_connections();
935 widget.get_panner_shell()->pannable()->pan_width_control->Changed.connect (panvalue_connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
940 Panner2dWindow::set_bypassed ()
942 bool view = bypass_button.get_active ();
943 bool model = widget.get_panner_shell()->bypassed ();
945 bypass_button.set_active(model);
948 set<Evoral::Parameter> params = widget.get_panner_shell()->panner()->what_can_be_automated();
949 set<Evoral::Parameter>::iterator p = params.find(PanWidthAutomation);
950 if (p == params.end()) {
951 spinner_box.set_sensitive(false);
953 spinner_box.set_sensitive(true);
958 Panner2dWindow::set_width ()
960 // rounding of spinbox is different from slider -- TODO use slider
961 float model = (widget.get_panner_shell()->pannable()->pan_width_control->get_value() * 100.0);
962 float view = (width_spinner.get_value());
964 width_spinner.set_value (model);
969 Panner2dWindow::on_key_press_event (GdkEventKey* event)
971 return relay_key_press (event, &PublicEditor::instance());
975 Panner2dWindow::on_key_release_event (GdkEventKey*)