From 1b2b21169c78fcc02759e4556c4d582f605490d4 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Tue, 23 Nov 2010 16:38:17 +0000 Subject: semi-functioning vbap panning, still not done git-svn-id: svn://localhost/ardour2/branches/3.0@8074 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/ardour/audioengine.h | 6 ++- libs/ardour/ardour/vbap_speakers.h | 20 +++++--- libs/ardour/panner.cc | 16 +++++-- libs/ardour/vbap.cc | 26 +++++----- libs/ardour/vbap_speakers.cc | 98 +++++++++++++++++++++----------------- libs/pbd/cartesian.cc | 28 ++++++----- 6 files changed, 113 insertions(+), 81 deletions(-) (limited to 'libs') diff --git a/libs/ardour/ardour/audioengine.h b/libs/ardour/ardour/audioengine.h index 7060e68331..b288de12ef 100644 --- a/libs/ardour/ardour/audioengine.h +++ b/libs/ardour/ardour/audioengine.h @@ -31,7 +31,6 @@ #include #include - #include #include "pbd/rcu.h" @@ -63,6 +62,11 @@ class AudioEngine : public SessionHandlePtr public: typedef std::set 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 (); diff --git a/libs/ardour/ardour/vbap_speakers.h b/libs/ardour/ardour/vbap_speakers.h index 054079f739..51430398c0 100644 --- a/libs/ardour/ardour/vbap_speakers.h +++ b/libs/ardour/ardour/vbap_speakers.h @@ -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 */ diff --git a/libs/ardour/panner.cc b/libs/ardour/panner.cc index 55d1e5f751..c44ab96a59 100644 --- a/libs/ardour/panner.cc +++ b/libs/ardour/panner.cc @@ -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::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); } } diff --git a/libs/ardour/vbap.cc b/libs/ardour/vbap.cc index 57957b086d..bb5bed4963 100644 --- a/libs/ardour/vbap.cc +++ b/libs/ardour/vbap.cc @@ -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; } diff --git a/libs/ardour/vbap_speakers.cc b/libs/ardour/vbap_speakers.cc index 8d03fb3a9a..3881c971d8 100644 --- a/libs/ardour/vbap_speakers.cc +++ b/libs/ardour/vbap_speakers.cc @@ -32,8 +32,10 @@ */ #include +#include #include +#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::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::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 tmp = _speakers; + vector::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; } } diff --git a/libs/pbd/cartesian.cc b/libs/pbd/cartesian.cc index 0a84fa6bbc..fe7bf29acf 100644 --- a/libs/pbd/cartesian.cc +++ b/libs/pbd/cartesian.cc @@ -17,13 +17,18 @@ */ #include -#include +#include #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); } -- cgit v1.2.3