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), "")
78 panner_shell->Changed.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
80 panner_shell->panner()->SignalPositionChanged.connect (panconnect, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
83 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
85 handle_position_change ();
90 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
96 Panner2d::reset (uint32_t n_inputs)
98 uint32_t nouts = panner_shell->panner()->out().n_audio();
102 while (signals.size() < n_inputs) {
103 add_signal ("", AngularVector());
106 if (signals.size() > n_inputs) {
107 for (uint32_t i = signals.size(); i < n_inputs; ++i) {
111 signals.resize (n_inputs);
116 for (uint32_t i = 0; i < n_inputs; ++i) {
117 signals[i]->position = panner_shell->panner()->signal_position (i);
120 /* add all outputs */
122 while (speakers.size() < nouts) {
123 add_speaker (AngularVector());
126 if (speakers.size() > nouts) {
127 for (uint32_t i = nouts; i < speakers.size(); ++i) {
131 speakers.resize (nouts);
134 for (Targets::iterator x = speakers.begin(); x != speakers.end(); ++x) {
135 (*x)->visible = false;
138 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
140 for (uint32_t n = 0; n < nouts; ++n) {
143 snprintf (buf, sizeof (buf), "%d", n+1);
144 speakers[n]->set_text (buf);
145 speakers[n]->position = the_speakers[n].angles();
146 speakers[n]->visible = true;
153 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
155 width = alloc.get_width();
156 height = alloc.get_height();
158 if (height > large_size_threshold) {
159 border = large_border_width;
161 border = small_border_width;
164 radius = min (width, height);
168 hoffset = max ((double) (width - height), border);
169 voffset = max ((double) (height - width), border);
174 DrawingArea::on_size_allocate (alloc);
178 Panner2d::add_signal (const char* text, const AngularVector& a)
180 Target* signal = new Target (a, text);
181 signals.push_back (signal);
182 signal->visible = true;
188 Panner2d::add_speaker (const AngularVector& a)
190 Target* speaker = new Target (a, "");
191 speakers.push_back (speaker);
192 speaker->visible = true;
195 return speakers.size() - 1;
199 Panner2d::handle_state_change ()
201 panconnect.drop_connections();
202 panner_shell->panner()->SignalPositionChanged.connect (panconnect, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
207 Panner2d::label_signals ()
209 uint32_t sz = signals.size();
216 signals[0]->set_text ("");
220 signals[0]->set_text ("L");
221 signals[1]->set_text ("R");
225 for (uint32_t i = 0; i < sz; ++i) {
227 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
228 signals[i]->set_text (buf);
235 Panner2d::handle_position_change ()
238 double w = panner_shell->pannable()->pan_width_control->get_value();
240 position.position = AngularVector (panner_shell->pannable()->pan_azimuth_control->get_value() * 360.0,
241 panner_shell->pannable()->pan_elevation_control->get_value() * 90.0);
243 for (uint32_t i = 0; i < signals.size(); ++i) {
244 signals[i]->position = panner_shell->panner()->signal_position (i);
247 if (w * last_width <= 0) {
254 vector<Speaker>& the_speakers (panner_shell->panner()->get_speakers()->speakers());
256 for (n = 0; n < speakers.size(); ++n) {
257 speakers[n]->position = the_speakers[n].angles();
264 Panner2d::move_signal (int which, const AngularVector& a)
266 if (which >= int (speakers.size())) {
270 speakers[which]->position = a;
275 Panner2d::find_closest_object (gdouble x, gdouble y, bool& is_signal)
280 float best_distance = FLT_MAX;
283 /* start with the position itself
286 position.position.cartesian (c);
288 best_distance = sqrt ((c.x - x) * (c.x - x) +
289 (c.y - y) * (c.y - y));
292 for (Targets::const_iterator i = signals.begin(); i != signals.end(); ++i) {
295 candidate->position.cartesian (c);
298 distance = sqrt ((c.x - x) * (c.x - x) +
299 (c.y - y) * (c.y - y));
301 if (distance < best_distance) {
303 best_distance = distance;
309 if (height > large_size_threshold) {
311 if (best_distance > 30) { // arbitrary
316 if (best_distance > 10) { // arbitrary
321 /* if we didn't find a signal close by, check the speakers */
324 for (Targets::const_iterator i = speakers.begin(); i != speakers.end(); ++i) {
327 candidate->position.cartesian (c);
330 distance = sqrt ((c.x - x) * (c.x - x) +
331 (c.y - y) * (c.y - y));
333 if (distance < best_distance) {
335 best_distance = distance;
339 if (height > large_size_threshold) {
341 if (best_distance < 30) { // arbitrary
348 if (best_distance < 10) { // arbitrary
360 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
363 GdkModifierType state;
366 gdk_window_get_pointer (ev->window, &x, &y, &state);
368 x = (int) floor (ev->x);
369 y = (int) floor (ev->y);
370 state = (GdkModifierType) ev->state;
373 if (ev->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) {
377 return handle_motion (x, y, state);
381 Panner2d::on_expose_event (GdkEventExpose *event)
385 bool small = (height <= large_size_threshold);
386 const double diameter = radius*2.0;
388 cr = gdk_cairo_create (get_window()->gobj());
392 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
393 if (!panner_shell->bypassed()) {
394 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
396 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
398 cairo_fill_preserve (cr);
401 /* offset to give us some border */
403 cairo_translate (cr, hoffset, voffset);
405 cairo_set_line_width (cr, 1.0);
407 /* horizontal line of "crosshairs" */
409 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
410 cairo_move_to (cr, 0.0, radius);
411 cairo_line_to (cr, diameter, radius);
414 /* vertical line of "crosshairs" */
416 cairo_move_to (cr, radius, 0);
417 cairo_line_to (cr, radius, diameter);
420 /* the circle on which signals live */
422 cairo_set_line_width (cr, 2.0);
423 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
424 cairo_arc (cr, radius, radius, radius, 0.0, 2.0 * M_PI);
427 /* 3 other circles of smaller diameter circle on which signals live */
429 cairo_set_line_width (cr, 1.0);
430 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
431 cairo_arc (cr, radius, radius, radius * 0.75, 0, 2.0 * M_PI);
433 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
434 cairo_arc (cr, radius, radius, radius * 0.50, 0, 2.0 * M_PI);
436 cairo_arc (cr, radius, radius, radius * 0.25, 0, 2.0 * M_PI);
439 if (signals.size() > 1) {
440 /* arc to show "diffusion" */
442 double width_angle = fabs (panner_shell->pannable()->pan_width_control->get_value()) * 2 * M_PI;
443 double position_angle = (2 * M_PI) - panner_shell->pannable()->pan_azimuth_control->get_value() * 2 * M_PI;
446 cairo_translate (cr, radius, radius);
447 cairo_rotate (cr, position_angle - (width_angle/2.0));
448 cairo_move_to (cr, 0, 0);
449 cairo_arc_negative (cr, 0, 0, radius, width_angle, 0.0);
450 cairo_close_path (cr);
451 if (panner_shell->pannable()->pan_width_control->get_value() >= 0.0) {
453 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.45);
456 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
462 if (!panner_shell->bypassed()) {
466 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
471 cairo_set_font_size (cr, 10);
477 if (signals.size() > 1) {
478 for (Targets::iterator i = signals.begin(); i != signals.end(); ++i) {
481 if (signal->visible) {
483 signal->position.cartesian (c);
487 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
488 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
489 cairo_fill_preserve (cr);
490 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
493 if (!small && !signal->text.empty()) {
494 cairo_set_source_rgb (cr, 0.517, 0.772, 0.882);
495 /* the +/- adjustments are a hack to try to center the text in the circle */
497 cairo_move_to (cr, c.x - 1, c.y + 1);
499 cairo_move_to (cr, c.x - 4, c.y + 4);
501 cairo_show_text (cr, signal->text.c_str());
511 for (Targets::iterator i = speakers.begin(); i != speakers.end(); ++i) {
512 Target *speaker = *i;
516 if (speaker->visible) {
520 speaker->position.cartesian (c);
523 snprintf (buf, sizeof (buf), "%d", n);
525 /* stroke out a speaker shape */
527 cairo_move_to (cr, c.x, c.y);
529 cairo_rotate (cr, -(speaker->position.azi/360.0) * (2.0 * M_PI));
531 cairo_scale (cr, 0.8, 0.8);
533 cairo_scale (cr, 1.2, 1.2);
535 cairo_rel_line_to (cr, 4, -2);
536 cairo_rel_line_to (cr, 0, -7);
537 cairo_rel_line_to (cr, 5, +5);
538 cairo_rel_line_to (cr, 5, 0);
539 cairo_rel_line_to (cr, 0, 5);
540 cairo_rel_line_to (cr, -5, 0);
541 cairo_rel_line_to (cr, -5, +5);
542 cairo_rel_line_to (cr, 0, -7);
543 cairo_close_path (cr);
544 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
549 cairo_set_font_size (cr, 16);
551 /* move the text in just a bit */
553 AngularVector textpos (speaker->position.azi, speaker->position.ele, 0.85);
554 textpos.cartesian (c);
556 cairo_move_to (cr, c.x, c.y);
557 cairo_show_text (cr, buf);
565 position.position.cartesian (c);
569 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
570 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
571 cairo_fill_preserve (cr);
572 cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
582 Panner2d::on_button_press_event (GdkEventButton *ev)
584 GdkModifierType state;
589 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
595 switch (ev->button) {
601 if ((drag_target = find_closest_object (x, y, is_signal)) != 0) {
603 panner_shell->panner()->set_position (drag_target->position.azi/360.0);
606 drag_target->set_selected (true);
612 state = (GdkModifierType) ev->state;
614 return handle_motion (drag_x, drag_y, state);
625 Panner2d::on_button_release_event (GdkEventButton *ev)
628 GdkModifierType state;
631 switch (ev->button) {
633 x = (int) floor (ev->x);
634 y = (int) floor (ev->y);
635 state = (GdkModifierType) ev->state;
636 ret = handle_motion (x, y, state);
641 x = (int) floor (ev->x);
642 y = (int) floor (ev->y);
643 state = (GdkModifierType) ev->state;
645 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
649 ret = handle_motion (x, y, state);
664 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
666 if (drag_target == 0) {
670 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
675 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
677 bool need_move = false;
679 drag_target->position.cartesian (c);
682 if ((evx != c.x) || (evy != c.y)) {
687 set<Evoral::Parameter> params = panner_shell->panner()->what_can_be_automated();
688 set<Evoral::Parameter>::iterator p = params.find(PanElevationAutomation);
691 if (height > large_size_threshold) {
694 CartesianVector cp (evx - 12.0, evy - y0, 0.0);
698 if (p == params.end()) {
699 clamp_to_circle (cp.x, cp.y);
701 if (drag_target == &position) {
702 double degree_fract = av.azi / 360.0;
703 panner_shell->panner()->set_position (degree_fract);
706 /* sphere projection */
707 sphere_project (cp.x, cp.y, cp.z);
709 double r2d = 180.0 / M_PI;
710 av.azi = r2d * atan2(cp.y, cp.x);
711 av.ele = r2d * asin(cp.z);
713 if (drag_target == &position) {
714 double azi_fract = av.azi / 360.0;
715 double ele_fract = av.ele / 90.0;
716 panner_shell->panner()->set_position (azi_fract);
717 panner_shell->panner()->set_elevation (ele_fract);
727 Panner2d::on_scroll_event (GdkEventScroll* ev)
729 switch (ev->direction) {
731 case GDK_SCROLL_RIGHT:
732 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
735 case GDK_SCROLL_DOWN:
736 case GDK_SCROLL_LEFT:
737 panner_shell->panner()->set_position (panner_shell->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
744 Panner2d::cart_to_gtk (CartesianVector& c) const
746 /* cartesian coordinate space:
748 dimension = 2.0 * 2.0
749 increasing y moves up
750 so max values along each axis are -1..+1
752 GTK uses a coordinate space that is:
754 dimension = (radius*2.0) * (radius*2.0)
755 increasing y moves down
757 const double diameter = radius*2.0;
759 c.x = diameter * ((c.x + 1.0) / 2.0);
760 /* extra subtraction inverts the y-axis to match "increasing y moves down" */
761 c.y = diameter - (diameter * ((c.y + 1.0) / 2.0));
765 Panner2d::gtk_to_cart (CartesianVector& c) const
767 const double diameter = radius*2.0;
768 c.x = ((c.x / diameter) * 2.0) - 1.0;
769 c.y = (((diameter - c.y) / diameter) * 2.0) - 1.0;
773 Panner2d::sphere_project (double& x, double& y, double& z)
788 Panner2d::clamp_to_circle (double& x, double& y)
793 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
794 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
798 Panner2d::toggle_bypass ()
800 panner_shell->set_bypassed (!panner_shell->bypassed());
803 Panner2dWindow::Panner2dWindow (boost::shared_ptr<PannerShell> p, int32_t h, uint32_t inputs)
804 : ArdourWindow (_("Panner (2D)"))
806 , bypass_button (_("Bypass"))
807 , width_adjustment (0, -100, 100, 1, 5, 0)
808 , width_spinner (width_adjustment)
810 widget.set_name ("MixerPanZone");
812 set_title (_("Panner"));
813 widget.set_size_request (h, h);
815 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
816 width_spinner.signal_changed().connect (sigc::mem_fun (*this, &Panner2dWindow::width_changed));
818 p->pannable()->pan_width_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2dWindow::set_width, this), gui_context());
819 p->Changed.connect (connections, invalidator (*this), boost::bind (&Panner2dWindow::set_bypassed, this), gui_context());
821 button_box.set_spacing (6);
822 button_box.pack_start (bypass_button, false, false);
824 left_side.set_spacing (6);
826 left_side.pack_start (button_box, false, false);
828 Gtk::Label* l = manage (new Label (
829 p->panner()->describe_parameter(PanWidthAutomation),
830 Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
831 spinner_box.pack_start (*l, false, false);
832 spinner_box.pack_start (width_spinner, false, false);
833 left_side.pack_start (spinner_box, false, false);
836 bypass_button.show ();
838 width_spinner.show ();
842 hpacker.set_spacing (6);
843 hpacker.set_border_width (12);
844 hpacker.pack_start (widget, false, false);
845 hpacker.pack_start (left_side, false, false);
856 Panner2dWindow::reset (uint32_t n_inputs)
858 widget.reset (n_inputs);
862 Panner2dWindow::bypass_toggled ()
864 bool view = bypass_button.get_active ();
865 bool model = widget.get_panner_shell()->bypassed ();
868 widget.get_panner_shell()->set_bypassed (view);
872 Panner2dWindow::width_changed ()
874 float model = widget.get_panner_shell()->pannable()->pan_width_control->get_value();
875 float view = width_spinner.get_value() / 100.0;
877 widget.get_panner_shell()->panner()->set_width (view);
882 Panner2dWindow::set_bypassed ()
884 bool view = bypass_button.get_active ();
885 bool model = widget.get_panner_shell()->bypassed ();
887 bypass_button.set_active(model);
890 set<Evoral::Parameter> params = widget.get_panner_shell()->panner()->what_can_be_automated();
891 set<Evoral::Parameter>::iterator p = params.find(PanWidthAutomation);
892 if (p == params.end()) {
893 spinner_box.set_sensitive(false);
895 spinner_box.set_sensitive(true);
900 Panner2dWindow::set_width ()
902 // rounding of spinbox is different from slider -- TODO use slider
903 float model = (widget.get_panner_shell()->pannable()->pan_width_control->get_value() * 100.0);
904 float view = (width_spinner.get_value());
906 width_spinner.set_value (model);
911 Panner2dWindow::on_key_press_event (GdkEventKey* event)
913 return relay_key_press (event, &PublicEditor::instance());
917 Panner2dWindow::on_key_release_event (GdkEventKey*)