non-crashing (but also non-functional) integration of VBAP with panner "architecture"
[ardour.git] / libs / ardour / ardour / vbap_speakers.h
1 /*
2     Copyright (C) 2010 Paul Davis
3
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.
8
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.
13
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.
17 */
18
19 #ifndef __libardour_vbap_speakers_h__
20 #define __libardour_vbap_speakers_h__
21
22 #include <string>
23 #include <map>
24
25 #include <pbd/signals.h>
26
27 #include "ardour/panner.h"
28
29 namespace ARDOUR {
30
31 class VBAPSpeakers {
32   public:
33         struct cart_vec {
34             float x;
35             float y;
36             float z;
37         };
38         
39         struct ang_vec {
40             float azi;
41             float ele;
42             float length;
43         };
44
45         static const int MAX_TRIPLET_AMOUNT = 60;
46         typedef std::vector<double> dvector;
47
48         VBAPSpeakers ();
49         ~VBAPSpeakers ();
50         
51         int  add_speaker (double direction, double elevation = 0.0);
52         void remove_speaker (int id);
53         void move_speaker (int id, double direction, double elevation = 0.0);
54         void clear_speakers ();
55
56         const dvector matrix (int tuple) const  { return _matrices[tuple]; }
57         int speaker_for_tuple (int tuple, int which) const { return _speaker_tuples[tuple][which]; }
58
59         int           n_tuples () const  { return _matrices.size(); }
60         int           dimension() const { return _dimension; }
61
62         static void angle_to_cart(ang_vec *from, cart_vec *to);
63
64         PBD::Signal0<void> Changed;
65
66   private:
67         static const double MIN_VOL_P_SIDE_LGTH = 0.01;
68         int   _dimension;  
69
70         /* A struct for a loudspeaker instance */
71         struct Speaker { 
72             int id;
73             cart_vec coords;
74             ang_vec angles;
75             
76             Speaker (int, double azimuth, double elevation);
77
78             void move (double azimuth, double elevation);
79         };
80
81         struct twoDmatrix : public dvector {
82           twoDmatrix() : dvector (4, 0.0) {}
83         };
84
85         struct threeDmatrix : public dvector {
86           threeDmatrix() : dvector (9, 0.0) {}
87         };
88         
89         struct tmatrix : public dvector {
90           tmatrix() : dvector (3, 0.0) {}
91         };
92
93         std::vector<Speaker>  _speakers;
94         std::vector<dvector>  _matrices;       /* holds matrices for a given speaker combinations */
95         std::vector<tmatrix>  _speaker_tuples; /* holds speakers IDs for a given combination */
96
97         /* A struct for all loudspeakers */
98         struct ls_triplet_chain {
99             int ls_nos[3];
100             float inv_mx[9];
101             struct ls_triplet_chain *next;
102         };
103
104         static float vec_angle(cart_vec v1, cart_vec v2);
105         static float vec_length(cart_vec v1);
106         static float vec_prod(cart_vec v1, cart_vec v2);
107         static float vol_p_side_lgth(int i, int j,int k, const std::vector<Speaker>&);
108         static void  cross_prod(cart_vec v1,cart_vec v2, cart_vec *res);
109
110         void update ();
111         int  any_ls_inside_triplet (int a, int b, int c);
112         void add_ldsp_triplet (int i, int j, int k, struct ls_triplet_chain **ls_triplets);
113         int  lines_intersect (int i,int j,int k,int l);
114         void calculate_3x3_matrixes (struct ls_triplet_chain *ls_triplets);
115         void choose_speaker_triplets (struct ls_triplet_chain **ls_triplets);
116         void choose_speaker_pairs ();
117         void sort_2D_lss (int* sorted_lss);
118         int  calc_2D_inv_tmatrix (double azi1,double azi2, double* inv_mat);
119 };
120
121 } /* namespace */
122
123 #endif /* __libardour_vbap_speakers_h__ */