semi-functioning vbap panning, still not done
authorPaul Davis <paul@linuxaudiosystems.com>
Tue, 23 Nov 2010 16:38:17 +0000 (16:38 +0000)
committerPaul Davis <paul@linuxaudiosystems.com>
Tue, 23 Nov 2010 16:38:17 +0000 (16:38 +0000)
git-svn-id: svn://localhost/ardour2/branches/3.0@8074 d708f5d6-7413-0410-9779-e7cbd77b26cf

gtk2_ardour/panner2d.cc
gtk2_ardour/panner2d.h
libs/ardour/ardour/audioengine.h
libs/ardour/ardour/vbap_speakers.h
libs/ardour/panner.cc
libs/ardour/vbap.cc
libs/ardour/vbap_speakers.cc
libs/pbd/cartesian.cc

index 84c0e1f1bc2f90bfba6752d4380bd95b1f865d1e..39501122b2e10f63fb624f036b6cfa522b77691d 100644 (file)
@@ -25,6 +25,7 @@
 #include <gtkmm/menu.h>
 
 #include "pbd/error.h"
+#include "pbd/cartesian.h"
 #include "ardour/panner.h"
 #include <gtkmm2ext/gtk_ui.h>
 
@@ -44,25 +45,20 @@ Panner2d::Target::Target (float xa, float ya, const char *txt)
        : x (xa, 0.0, 1.0, 0.01, 0.1)
        , y (ya, 0.0, 1.0, 0.01, 0.1)
        , azimuth (M_PI/2.0, 0.0, 2.0 * M_PI, 0.1, 0.5)
-       , text (txt ? strdup (txt) : 0)
+       , text (txt)
+        , _selected (false)
 {
        azimuth.set_value ((random() / (double) INT_MAX) * (2.0 * M_PI));
 }
 
 Panner2d::Target::~Target ()
 {
-       if (text) {
-               free (text);
-       }
 }
 
 void
 Panner2d::Target::set_text (const char* txt)
 {
-       if (text) {
-               free (text);
-       }
-       text = strdup (txt);
+       text = txt;
 }
 
 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
@@ -125,7 +121,7 @@ Panner2d::reset (uint32_t n_inputs)
        default:
                for (uint32_t i = 0; i < n_inputs; ++i) {
                        char buf[64];
-                       snprintf (buf, sizeof (buf), "%" PRIu32, i);
+                       snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
                        pucks[i]->set_text (buf);
                }
                break;
@@ -133,9 +129,17 @@ Panner2d::reset (uint32_t n_inputs)
 
        for (uint32_t i = existing_pucks; i < n_inputs; ++i) {
                float x, y;
+                double dx, dy;
+
                panner->streampanner (i).get_position (x, y);
-               pucks[i]->x.set_value (x);
-               pucks[i]->y.set_value (y);
+
+                dx = x;
+                dy = y;
+                clamp_to_circle (dx, dy);
+
+               pucks[i]->x.set_value (dx);
+               pucks[i]->y.set_value (dy);
+
                pucks[i]->visible = true;
        }
 
@@ -198,7 +202,14 @@ Panner2d::on_size_allocate (Gtk::Allocation& alloc)
 int
 Panner2d::add_puck (const char* text, float x, float y)
 {
-       Target* puck = new Target (x, y, text);
+        double dx, dy;
+        
+        dx = x;
+        dy = y;
+        
+        clamp_to_circle (dx, dy);
+
+       Target* puck = new Target (dx, dy, text);
        pucks.push_back (puck);
        puck->visible = true;
 
@@ -258,7 +269,7 @@ Panner2d::move_puck (int which, float x, float y)
 }
 
 Panner2d::Target *
-Panner2d::find_closest_object (gdouble x, gdouble y, int& which, bool& is_puck) const
+Panner2d::find_closest_object (gdouble x, gdouble y, int& which) const
 {
        gdouble efx, efy;
        gdouble cx, cy;
@@ -268,26 +279,11 @@ Panner2d::find_closest_object (gdouble x, gdouble y, int& which, bool& is_puck)
        float best_distance = FLT_MAX;
        int pwhich;
 
-       efx = x/width;
-       efy = y/height;
+       efx = x/(width-1.0);
+        efy = 1.0 - (y/(height-1.0)); /* convert from X Window origin */
+
        which = 0;
        pwhich = 0;
-       is_puck = false;
-
-       for (Targets::const_iterator i = targets.begin(); i != targets.end(); ++i, ++which) {
-               candidate = *i;
-
-               cx = candidate->x.get_value();
-               cy = candidate->y.get_value();
-
-               distance = sqrt ((cx - efx) * (cx - efx) +
-                                (cy - efy) * (cy - efy));
-
-               if (distance < best_distance) {
-                       closest = candidate;
-                       best_distance = distance;
-               }
-       }
 
        for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
                candidate = *i;
@@ -301,11 +297,14 @@ Panner2d::find_closest_object (gdouble x, gdouble y, int& which, bool& is_puck)
                if (distance < best_distance) {
                        closest = candidate;
                        best_distance = distance;
-                       is_puck = true;
                        which = pwhich;
                }
        }
 
+        if (best_distance > 0.05) { // arbitrary 
+                return 0;
+        }
+
        return closest;
 }
 
@@ -349,16 +348,22 @@ Panner2d::on_expose_event (GdkEventExpose *event)
                cairo_translate (cr, 10.0, 10.0);
        }
 
+        /* horizontal line of "crosshairs" */
+
        cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
        cairo_move_to (cr, 0.5, height/2.0+0.5);
-       cairo_line_to (cr, height+0.5, height/2+0.5);
+       cairo_line_to (cr, width+0.5, height/2+0.5);
        cairo_stroke (cr);
 
-       cairo_move_to (cr, height/2+0.5, 0.5);
-       cairo_line_to (cr, height/2+0.5, height+0.5);
+        /* vertical line of "crosshairs" */
+
+       cairo_move_to (cr, width/2+0.5, 0.5);
+       cairo_line_to (cr, width/2+0.5, height+0.5);
        cairo_stroke (cr);
 
-       cairo_arc (cr, height/2, height/2, height/2, 0, 2.0 * M_PI);
+        /* the circle on which signals live */
+
+       cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
        cairo_stroke (cr);
 
        if (!panner->bypassed()) {
@@ -385,51 +390,21 @@ Panner2d::on_expose_event (GdkEventExpose *event)
                                fx = max (fx, -1.0f);
                                x = (gint) floor (width * fx - 4);
 
-                               fy = min (puck->y.get_value(), 1.0);
+                               fy = min (fy, 1.0f);
                                fy = max (fy, -1.0f);
-                               y = (gint) floor (height * fy - 4);
 
+                                /* translate back to X Window abomination coordinates */
+                                fy = -(puck->y.get_value() - 1.0);
+
+                               y = (gint) floor (height * fy - 4);
+                                
                                cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
                                cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
                                cairo_close_path (cr);
                                cairo_fill (cr);
 
-                               /* arrow */
-
-                               if (height > 100.0f) {
-
-                                       float endx, endy;
-                                       endx = x;
-                                       endy = y;
-
-                                       cairo_save (cr);
-                                       cairo_translate (cr, x, y);
-                                       cairo_rotate (cr, puck->azimuth.get_value());
-
-                                       /* horizontal left-to-right line (rotation will rotate it, duh) */
-
-                                       endx = 30.0;
-                                       endy = 0.0;
-
-                                       /* stem */
-                                       cairo_set_line_width (cr, 4.0);
-                                       cairo_move_to (cr, 0.0, 0.0);
-                                       cairo_line_to (cr, endx, endy);
-                                       cairo_stroke (cr);
-
-                                       /* arrow head */
-
-                                       cairo_move_to (cr, endx - 10.0, endy + 10.0);
-                                       cairo_line_to (cr, endx, endy);
-                                       cairo_line_to (cr, endx - 10.0, endy - 10.0);
-                                       cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
-                                       cairo_stroke (cr);
-
-                                       cairo_restore (cr);
-                               }
-
                                cairo_move_to (cr, x + 6, y + 6);
-                               cairo_show_text (cr, puck->text);
+                               cairo_show_text (cr, puck->text.c_str());
                        }
                }
 
@@ -480,7 +455,10 @@ Panner2d::on_button_press_event (GdkEventButton *ev)
        switch (ev->button) {
        case 1:
        case 2:
-               drag_target = find_closest_object (ev->x, ev->y, drag_index, drag_is_puck);
+               if ((drag_target = find_closest_object (ev->x, ev->y, drag_index)) != 0) {
+                        drag_target->set_selected (true);
+                }
+
                drag_x = (int) floor (ev->x);
                drag_y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
@@ -508,12 +486,10 @@ Panner2d::on_button_release_event (GdkEventButton *ev)
                y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
 
-               if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
-
-
+               if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
+                        
                        for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
                                //Target* puck = i->second;
-
                                /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
                        }
 
@@ -533,7 +509,7 @@ Panner2d::on_button_release_event (GdkEventButton *ev)
                y = (int) floor (ev->y);
                state = (GdkModifierType) ev->state;
 
-               if (drag_is_puck && (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier))) {
+               if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
                        toggle_bypass ();
                        ret = true;
                } else {
@@ -562,60 +538,29 @@ Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
                return false;
        }
 
-       int x, y;
-       bool need_move = false;
-
-       if (!drag_is_puck && !allow_target) {
-               cerr << "dip = " << drag_is_puck << " at = " << allow_target << endl;
-               return true;
-       }
 
        if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
 
-               if (allow_x || !drag_is_puck) {
-                       float new_x;
-                       x = min (evx, width - 1);
-                       x = max (x, 0);
-                       new_x = (float) x / (width - 1);
-                       if (new_x != drag_target->x.get_value()) {
-                               drag_target->x.set_value (new_x);
-                               need_move = true;
-                       }
-               }
-
-               if (allow_y || drag_is_puck) {
-                       float new_y;
-                       y = min (evy, height - 1);
-                       y = max (y, 0);
-                       new_y = (float) y / (height - 1);
-                       if (new_y != drag_target->y.get_value()) {
-                               drag_target->y.set_value (new_y);
-                               need_move = true;
-                       }
-               }
-
-               if (need_move) {
-
-                       if (drag_is_puck) {
-
-                               panner->streampanner(drag_index).set_position (
-                                               drag_target->x.get_value(), drag_target->y.get_value(), false);
+                double fx = evx;
+                double fy = evy;
+                bool need_move = false;
 
-                       } else {
+                clamp_to_circle (fx, fy);
 
-                               TargetMoved (drag_index);
-                       }
+                if ((fx != drag_target->x.get_value()) || (fy != drag_target->y.get_value())) {
+                        need_move = true;
+                }
 
+                if (need_move) {
+                        drag_target->x.set_value (fx);
+                        drag_target->y.set_value (fy);
+                        
+                        panner->streampanner (drag_index).set_position (drag_target->x.get_value(), drag_target->y.get_value(), false);
                        queue_draw ();
                }
 
-
        } else if ((state & GDK_BUTTON2_MASK) && !(state & GDK_BUTTON1_MASK)) {
 
-               if (!drag_is_puck) {
-                       return false;
-               }
-
                int xdelta = drag_x - evx;
                int ydelta = drag_x - evy;
 
@@ -626,6 +571,60 @@ Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
        return true;
 }
 
+void
+Panner2d::cart_to_azi_ele (double x, double y, double& azi, double& ele)
+{
+        x = min (x, (double) width);
+        x = max (x, 0.0);
+        x = x / (width-1.0);
+
+        y = min (y, (double) height);
+        y = max (y, 0.0);
+        y = y / (height-1.0);
+
+        /* at this point, new_x and new_y are in the range [ 0.0 .. 1.0 ], with
+           (0,0) at the upper left corner (thank you, X Window)
+           
+           we need to translate to (0,0) at center
+        */
+        
+        x -= 0.5;
+        y  = (1.0 - y) - 0.5;
+
+        PBD::cart_to_azi_ele (x, y, 0.0, azi, ele);
+}
+
+void
+Panner2d::azi_ele_to_cart (double azi, double ele, double& x, double& y)
+{
+        double z;
+
+        PBD::azi_ele_to_cart (azi, ele, x, y, z);
+        
+        /* xp,yp,zp use a (0,0) == center and 2.0 unit dimension. so convert
+           back to (0,0) and 1.0 unit dimension
+        */
+        
+        x /= 2.0;
+        y /= 2.0;
+        z /= 2.0;
+        
+        /* and now convert back to (0,0) == upper left corner */
+        
+        x += 0.5;
+        y += 0.5;
+        z += 0.5;
+}
+
+void
+Panner2d::clamp_to_circle (double& x, double& y)
+{
+        double azi, ele;
+
+        cart_to_azi_ele (x, y, azi, ele);
+        azi_ele_to_cart (azi, ele, x, y);
+}
+
 void
 Panner2d::toggle_bypass ()
 {
index 5825faf80a3ed9ca0c2f68b787fe1b7732b4cd77..fe8305343e7b4e9d227ea1b7175b44793803f0e5 100644 (file)
@@ -77,17 +77,27 @@ class Panner2d : public Gtk::DrawingArea
        void on_size_allocate (Gtk::Allocation& alloc);
 
   private:
-       struct Target {
+       class Target {
+          public:
            Gtk::Adjustment x;
            Gtk::Adjustment y;
            Gtk::Adjustment azimuth;
            bool visible;
-           char* text;
+            std::string text;
 
            Target (float xa, float ya, const char* txt = 0);
            ~Target ();
 
            void set_text (const char*);
+            void set_selected (bool yn) {
+                    _selected = yn;
+            }
+            bool selected() const { 
+                    return _selected;
+            }
+
+          private:
+            bool _selected;
        };
 
        boost::shared_ptr<ARDOUR::Panner> panner;
@@ -101,7 +111,6 @@ class Panner2d : public Gtk::DrawingArea
        int drag_x;
        int drag_y;
        int     drag_index;
-       bool    drag_is_puck;
        bool  allow_x;
        bool  allow_y;
        bool  allow_target;
@@ -113,7 +122,7 @@ class Panner2d : public Gtk::DrawingArea
        gint compute_x (float);
        gint compute_y (float);
 
-       Target *find_closest_object (gdouble x, gdouble y, int& which, bool& is_puck) const;
+       Target *find_closest_object (gdouble x, gdouble y, int& which) const;
 
        gint handle_motion (gint, gint, GdkModifierType);
 
@@ -123,6 +132,17 @@ class Panner2d : public Gtk::DrawingArea
 
        PBD::ScopedConnection state_connection;
        PBD::ScopedConnection change_connection;
+
+        /* cartesian coordinates in GTK units ; return azimuth & elevation in degrees */
+        void cart_to_azi_ele (double x, double y, double& azi, double& eli);
+
+        /* azimuth & elevation in degrees; return cartesian coordinates in GTK units */
+        void azi_ele_to_cart (double azi, double eli, double& x, double& y);
+
+        /* cartesian coordinates in GTK units ; adjust to same but on a circle of radius 1.0
+           and centered in the middle of our area
+        */
+        void clamp_to_circle (double& x, double& y);
 };
 
 class Panner2dWindow : public Gtk::Window
index 7060e683317b346663b21744894cfb4145a0584b..b288de12ef4786f686be9bd6f7240b2fef55b287 100644 (file)
@@ -31,7 +31,6 @@
 #include <exception>
 #include <string>
 
-
 #include <glibmm/thread.h>
 
 #include "pbd/rcu.h"
@@ -63,6 +62,11 @@ class AudioEngine : public SessionHandlePtr
    public:
        typedef std::set<Port*> Ports;
 
+        class disconnected_exception : public std::exception {
+          public:
+                virtual const char *what() const throw() { return "AudioEngine is disconnected"; }
+        };
+
        AudioEngine (std::string client_name, std::string session_uuid);
        virtual ~AudioEngine ();
 
index 054079f739fc0c5aa166d5e89ccbe75b8971f2f6..51430398c0a1c75ad6baa4bba973a68a7f499bfb 100644 (file)
@@ -31,15 +31,15 @@ namespace ARDOUR {
 class VBAPSpeakers {
   public:
         struct cart_vec {
-            float x;
-            float y;
-            float z;
+            double x;
+            double y;
+            double z;
         };
         
         struct ang_vec {
-            float azi;
-            float ele;
-            float length;
+            double azi;
+            double ele;
+            double length;
         };
 
         static const int MAX_TRIPLET_AMOUNT = 60;
@@ -78,6 +78,12 @@ class VBAPSpeakers {
             void move (double azimuth, double elevation);
         };
 
+        struct azimuth_sorter {
+            bool operator() (const Speaker& s1, const Speaker& s2) {
+                    return s1.angles.azi < s2.angles.azi;
+            }
+        };
+
         struct twoDmatrix : public dvector {
           twoDmatrix() : dvector (4, 0.0) {}
         };
@@ -116,6 +122,8 @@ class VBAPSpeakers {
         void choose_speaker_pairs ();
         void sort_2D_lss (int* sorted_lss);
         int  calc_2D_inv_tmatrix (double azi1,double azi2, double* inv_mat);
+        
+        void dump_speakers (std::ostream&);
 };
 
 } /* namespace */
index 55d1e5f751ecb622f7eb9bb82bc0380693774235..c44ab96a59f6551586e5db6d9ad04aa221657a13 100644 (file)
@@ -1643,10 +1643,11 @@ Panner::setup_speakers (uint32_t nouts)
                outputs.push_back (Output  (1.0, 1.0));
                 break;
         case 4:
-               outputs.push_back (Output  (0, 0));
-               outputs.push_back (Output  (1.0, 0));
-               outputs.push_back (Output  (1.0, 1.0));
+                /* clockwise from top left */
                outputs.push_back (Output  (0, 1.0));
+               outputs.push_back (Output  (1.0, 1.0));
+               outputs.push_back (Output  (1.0, 0));
+               outputs.push_back (Output  (0, 0));
                 break;
 
        case 5: //square+offcenter center
@@ -1671,8 +1672,13 @@ Panner::setup_speakers (uint32_t nouts)
         for (vector<Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
                 double azimuth;
                 double elevation;
-                
-                cart_to_azi_ele ((*o).x + 1.0, (*o).y + 1.0, (*o).z, azimuth, elevation);
+                double tx, ty, tz;
+
+                tx = (*o).x - 0.5;
+                ty = (*o).y - 0.5;
+                tz = 0.0; // XXX change this if we ever do actual 3D
+
+                cart_to_azi_ele (tx, ty, tz, azimuth, elevation);
                 speakers.add_speaker (azimuth, elevation);
         }
 }
index 57957b086d2297a00d91daef81f235d55e8684ce..bb5bed4963d7f4eae475b92e64bfcb8bf63fcf64 100644 (file)
@@ -72,6 +72,14 @@ VBAPanner::mark_dirty ()
 void
 VBAPanner::update ()
 {
+        /* convert from coordinate space with (0,0) at upper left to (0,0) at center and dimensions of 1 unit */
+        _x -= 0.5;
+        _y -= 0.5;
+
+
+        /* we're 2D for now */
+        _z = 0.0;
+
         cart_to_azi_ele (_x, _y, _z, _azimuth, _elevation);
         _dirty = true;
 }
@@ -86,8 +94,6 @@ VBAPanner::compute_gains (double gains[3], int speaker_ids[3], int azi, int ele)
         double small_g;
         double big_sm_g, gtmp[3];
 
-        cerr << "COMPUTE GAINS with " << _speakers.n_tuples() << endl;
-
         azi_ele_to_cart (azi,ele, cartdir[0], cartdir[1], cartdir[2]);  
         big_sm_g = -100000.0;
 
@@ -105,7 +111,6 @@ VBAPanner::compute_gains (double gains[3], int speaker_ids[3], int azi, int ele)
 
                         if (gtmp[j] < small_g) {
                                 small_g = gtmp[j];
-                                cerr << "For triplet " << i << " g = " << small_g << endl;
                         }
                 }
 
@@ -116,11 +121,9 @@ VBAPanner::compute_gains (double gains[3], int speaker_ids[3], int azi, int ele)
                         gains[0] = gtmp[0]; 
                         gains[1] = gtmp[1]; 
 
-                        cerr << "Best triplet = " << i << endl;
-
-                        speaker_ids[0]= _speakers.speaker_for_tuple (i, 0);
-                        speaker_ids[1]= _speakers.speaker_for_tuple (i, 1);
-
+                        speaker_ids[0] = _speakers.speaker_for_tuple (i, 0);
+                        speaker_ids[1] = _speakers.speaker_for_tuple (i, 1);
+                        
                         if (_speakers.dimension() == 3) {
                                 gains[2] = gtmp[2];
                                 speaker_ids[2] = _speakers.speaker_for_tuple (i, 2);
@@ -155,16 +158,13 @@ VBAPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coe
 
         if ((was_dirty = _dirty)) {
                 compute_gains (desired_gains, desired_outputs, _azimuth, _elevation);
-
                 cerr << " @ " << _azimuth << " /= " << _elevation
                      << " Outputs: "
-                     << desired_outputs[0] << ' '
-                     << desired_outputs[1] << ' '
-                     << desired_outputs[2] 
+                     << desired_outputs[0] + 1 << ' '
+                     << desired_outputs[1] + 1 << ' '
                      << " Gains "
                      << desired_gains[0] << ' '
                      << desired_gains[1] << ' '
-                     << desired_gains[2] 
                      << endl;
         }
 
index 8d03fb3a9a0bb3b3d9e3fe9a0f9b6b580ea18806..3881c971d8d503103ce6394a5713009f7f8d70d6 100644 (file)
 */
 
 #include <cmath>
+#include <algorithm>
 #include <stdlib.h>
 
+#include "pbd/cartesian.h"
 #include "ardour/vbap_speakers.h"
 
 using namespace ARDOUR;
@@ -43,6 +45,7 @@ VBAPSpeakers::Speaker::Speaker (int i, double azimuth, double elevation)
         : id (i)
 {
         move (azimuth, elevation);
+        cerr << setprecision (5) << "%%%%%%%%%% New speaker @ " << angles.azi << ", " << angles.ele << endl;
 }
 
 void
@@ -63,6 +66,19 @@ VBAPSpeakers::~VBAPSpeakers ()
 {
 }
 
+void
+VBAPSpeakers::dump_speakers (ostream& o)
+{
+        for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
+                o << "Speaker " << (*i).id << " @ " 
+                  << (*i).coords.x << ", " << (*i).coords.y << ", " << (*i).coords.z
+                  << " azimuth " << (*i).angles.azi
+                  << " elevation " << (*i).angles.ele
+                  << " distance " << (*i).angles.length
+                  << endl;
+        }
+}
+
 void
 VBAPSpeakers::clear_speakers ()
 {
@@ -80,6 +96,8 @@ VBAPSpeakers::add_speaker (double azimuth, double elevation)
         _speakers.push_back (Speaker (id, azimuth, elevation));
         update ();
 
+        dump_speakers (cerr);
+
         return id;
 }        
 
@@ -114,6 +132,7 @@ VBAPSpeakers::update ()
 
         for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
                 if ((*i).angles.ele != 0.0) {
+                        cerr << "\n\n\nSPEAKER " << (*i).id << " has ele = " << (*i).angles.ele << "\n\n\n\n";
                         dim = 3;
                         break;
                 }
@@ -145,15 +164,7 @@ VBAPSpeakers::update ()
 void 
 VBAPSpeakers::angle_to_cart(ang_vec *from, cart_vec *to)
 {
-        /* from angular to cartesian coordinates*/
-
-        float ang2rad = 2 * M_PI / 360;
-
-        to->x = (float) (cos((double)(from->azi * ang2rad)) 
-                        * cos((double) (from->ele * ang2rad)));
-        to->y = (float) (sin((double)(from->azi * ang2rad)) 
-                        * cos((double) (from->ele * ang2rad)));
-        to->z = (float) (sin((double) (from->ele * ang2rad)));
+        PBD::azi_ele_to_cart (from->azi, from->ele, to->x, to->y, to->z);
 }  
 
 void 
@@ -500,6 +511,8 @@ VBAPSpeakers::calculate_3x3_matrixes(struct ls_triplet_chain *ls_triplets)
                 tr_ptr = tr_ptr->next;
         }
 
+        cerr << "@@@ triplets generate " << triplet_count << " of speaker tuples\n";
+
         triplet = 0;
 
         _matrices.clear ();
@@ -547,6 +560,11 @@ VBAPSpeakers::calculate_3x3_matrixes(struct ls_triplet_chain *ls_triplets)
                 _speaker_tuples[triplet][1] = tr_ptr->ls_nos[1];
                 _speaker_tuples[triplet][2] = tr_ptr->ls_nos[2];
 
+                cerr << "Triplet[" << triplet << "] = " 
+                     << tr_ptr->ls_nos[0] << " + " 
+                     << tr_ptr->ls_nos[1] << " + " 
+                     << tr_ptr->ls_nos[2] << endl;
+
                 triplet++;
 
                 tr_ptr = tr_ptr->next;
@@ -560,6 +578,7 @@ VBAPSpeakers::choose_speaker_pairs (){
            matrices and stores the data to a global array
         */
         const int n_speakers = _speakers.size();
+        const double AZIMUTH_DELTA_THRESHOLD_DEGREES = (180.0/M_PI) * (M_PI - 0.175);
         int sorted_speakers[n_speakers];
         bool exists[n_speakers];
         double inverse_matrix[n_speakers][4]; 
@@ -582,8 +601,17 @@ VBAPSpeakers::choose_speaker_pairs (){
         
         /* adjacent loudspeakers are the loudspeaker pairs to be used.*/
         for (speaker = 0; speaker < n_speakers-1; speaker++) {
+
+                cerr << "Looking at " 
+                     << _speakers[sorted_speakers[speaker]].id << " @ " << _speakers[sorted_speakers[speaker]].angles.azi  
+                     << " and "
+                     << _speakers[sorted_speakers[speaker+1]].id << " @ " << _speakers[sorted_speakers[speaker+1]].angles.azi  
+                     << " delta = " 
+                     << _speakers[sorted_speakers[speaker+1]].angles.azi - _speakers[sorted_speakers[speaker]].angles.azi
+                     << endl;
+
                 if ((_speakers[sorted_speakers[speaker+1]].angles.azi - 
-                     _speakers[sorted_speakers[speaker]].angles.azi) <= (M_PI - 0.175)){
+                     _speakers[sorted_speakers[speaker]].angles.azi) <= AZIMUTH_DELTA_THRESHOLD_DEGREES) {
                         if (calc_2D_inv_tmatrix( _speakers[sorted_speakers[speaker]].angles.azi, 
                                                  _speakers[sorted_speakers[speaker+1]].angles.azi, 
                                                  inverse_matrix[speaker]) != 0){
@@ -594,8 +622,8 @@ VBAPSpeakers::choose_speaker_pairs (){
         }
         
         if (((6.283 - _speakers[sorted_speakers[n_speakers-1]].angles.azi) 
-            +_speakers[sorted_speakers[0]].angles.azi) <= (M_PI - 0.175)) {
-                if(calc_2D_inv_tmatrix(_speakers[sorted_speakers[n_speakers-1]].angles.azi, 
+             +_speakers[sorted_speakers[0]].angles.azi) <= AZIMUTH_DELTA_THRESHOLD_DEGREES) {
+                if (calc_2D_inv_tmatrix(_speakers[sorted_speakers[n_speakers-1]].angles.azi, 
                                        _speakers[sorted_speakers[0]].angles.azi, 
                                        inverse_matrix[n_speakers-1]) != 0) { 
                         exists[n_speakers-1] = true;
@@ -614,7 +642,6 @@ VBAPSpeakers::choose_speaker_pairs (){
         }
 
         for (speaker = 0; speaker < n_speakers - 1; speaker++) {
-                cerr << "exists[" << speaker << "] = " << exists[speaker] << endl;
                 if (exists[speaker]) {
                         _matrices[pair][0] = inverse_matrix[speaker][0];
                         _matrices[pair][1] = inverse_matrix[speaker][1];
@@ -624,6 +651,8 @@ VBAPSpeakers::choose_speaker_pairs (){
                         _speaker_tuples[pair][0] = sorted_speakers[speaker];
                         _speaker_tuples[pair][1] = sorted_speakers[speaker+1];
 
+                        cerr << "PAIR[" << pair << "] = " << sorted_speakers[speaker] << " + " << sorted_speakers[speaker+1] << endl;
+
                         pair++;
                 }
         }
@@ -636,46 +665,25 @@ VBAPSpeakers::choose_speaker_pairs (){
 
                 _speaker_tuples[pair][0] = sorted_speakers[n_speakers-1];
                 _speaker_tuples[pair][1] = sorted_speakers[0];
-        }
 
-        cerr << "PAIRS done, tuples == " << n_tuples() << " pair = " << pair << endl;
+                cerr << "PAIR[" << pair << "] = " << sorted_speakers[n_speakers-1] << " + " << sorted_speakers[0] << endl;
+
+        }
 }
 
 void 
 VBAPSpeakers::sort_2D_lss (int* sorted_speakers)
 {
-        int speaker, other_speaker, index;
-        float tmp, tmp_azi;
-        int n_speakers = _speakers.size();
-
-        /* Transforming angles between -180 and 180 */
-        for (speaker = 0; speaker < n_speakers; speaker++) {
-                angle_to_cart(&_speakers[speaker].angles, &_speakers[speaker].coords);
-                _speakers[speaker].angles.azi = (float) acos((double) _speakers[speaker].coords.x);
-                if (fabsf(_speakers[speaker].coords.y) <= 0.001) {
-                        tmp = 1.0;
-                } else {
-                        tmp = _speakers[speaker].coords.y / fabsf(_speakers[speaker].coords.y);
-                }
-                _speakers[speaker].angles.azi *= tmp;
-        }
+        vector<Speaker> tmp = _speakers;
+        vector<Speaker>::iterator s;
+        azimuth_sorter sorter;
+        int n;
 
-        for (speaker = 0; speaker < n_speakers; speaker++){
-                tmp = 2000;
-                for (other_speaker = 0 ; other_speaker < n_speakers; other_speaker++){
-                        if (_speakers[other_speaker].angles.azi <= tmp){
-                                tmp=_speakers[other_speaker].angles.azi;
-                                index = other_speaker;
-                        }
-                }
-                sorted_speakers[speaker] = index;
-                tmp_azi = (_speakers[index].angles.azi);
-                _speakers[index].angles.azi = (tmp_azi + (float) 4000.0);
-        }
+        sort (tmp.begin(), tmp.end(), sorter);
 
-        for (speaker = 0 ; speaker < n_speakers; ++speaker) {
-                tmp_azi = _speakers[speaker].angles.azi;
-                _speakers[speaker].angles.azi = (tmp_azi - (float) 4000.0);
+        for (n = 0, s = tmp.begin(); s != tmp.end(); ++s, ++n) {
+                sorted_speakers[n] = (*s).id;
+                cerr << "Sorted[" << n << "] = " << (*s).id << endl;
         }
 }
 
index 0a84fa6bbc5263774815b3a74be95db644a6618f..fe7bf29acfc512b3e17d36d7a2f31e2145cf2f99 100644 (file)
 */
 
 #include <iostream>
-#include <cmath>
+#include <math.h>
 #include "pbd/cartesian.h"
 
+using namespace std;
+
 void
 PBD::azi_ele_to_cart (double azi, double ele, double& x, double& y, double& z)
 {
+        /* convert from cylindrical coordinates in degrees to cartesian */
+
         static const double atorad = 2.0 * M_PI / 360.0 ;
+
         x = cos (azi * atorad) * cos (ele * atorad);
         y = sin (azi * atorad) * cos (ele * atorad);
         z = sin (ele * atorad);
@@ -32,7 +37,8 @@ PBD::azi_ele_to_cart (double azi, double ele, double& x, double& y, double& z)
 void 
 PBD::cart_to_azi_ele (double x, double y, double z, double& azimuth, double& elevation)
 {
-        /* converts cartesian coordinates to angular */
+        /* converts cartesian coordinates to cylindrical in degrees*/
+
         const double atorad = 2.0 * M_PI / 360.0;
         double atan_y_per_x, atan_x_pl_y_per_z;
         double distance;
@@ -40,13 +46,15 @@ PBD::cart_to_azi_ele (double x, double y, double z, double& azimuth, double& ele
         if(x == 0.0) {
                 atan_y_per_x = M_PI / 2;
         } else {
-                atan_y_per_x = atan (y/x);
+                atan_y_per_x = atan2 (y,x);
         }
 
-        azimuth = atan_y_per_x / atorad;
-
-        if (x < 0.0) {
-                azimuth += 180.0;
+        if (y < 0.0) {
+                /* below x-axis: atan2 returns 0 .. -PI (negative) so convert to degrees and ADD to 180 */
+                azimuth = 180.0 + (atan_y_per_x / (M_PI/180.0) + 180.0);
+        } else {
+                /* above x-axis: atan2 returns 0 .. +PI so convert to degrees */
+                azimuth = atan_y_per_x / atorad;
         }
 
         distance = sqrt (x*x + y*y);
@@ -54,21 +62,19 @@ PBD::cart_to_azi_ele (double x, double y, double z, double& azimuth, double& ele
         if (z == 0.0) {
                 atan_x_pl_y_per_z = 0.0;
         } else {
-                atan_x_pl_y_per_z = atan (z/distance);
+                atan_x_pl_y_per_z = atan2 (z,distance);
         }
 
         if (distance == 0.0) {
                 if (z < 0.0) {
                         atan_x_pl_y_per_z = -M_PI/2.0;
-                } else {
+                } else if (z > 0.0) {
                         atan_x_pl_y_per_z = M_PI/2.0;
                 }
         }
 
         elevation = atan_x_pl_y_per_z / atorad;
 
-        std::cerr << x << ", " << y << ", " << z << " = " << azimuth << " /= " << elevation << std::endl;
-
         // distance = sqrtf (x*x + y*y + z*z);
 }