13
0
livetrax/libs/panners/vbap/vbap.cc

466 lines
12 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2011-2017 Paul Davis <paul@linuxaudiosystems.com>
* Copyright (C) 2011 Carl Hetherington <carl@carlh.net>
* Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
* Copyright (C) 2014 Robin Gareus <robin@gareus.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <cmath>
#include <cstdio>
2020-03-23 11:46:03 -04:00
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#ifdef COMPILER_MSVC
#include <malloc.h>
#endif
#include "pbd/cartesian.h"
#include "pbd/compose.h"
#include "ardour/amp.h"
#include "ardour/audio_buffer.h"
#include "ardour/buffer_set.h"
#include "ardour/pan_controllable.h"
#include "ardour/pannable.h"
#include "ardour/speakers.h"
#include "vbap.h"
#include "vbap_speakers.h"
#include "pbd/i18n.h"
using namespace PBD;
using namespace ARDOUR;
using namespace std;
static PanPluginDescriptor _descriptor = {
2020-03-23 11:46:03 -04:00
"VBAP 2D panner",
"http://ardour.org/plugin/panner_vbap",
"http://ardour.org/plugin/panner_vbap#ui",
-1, -1,
10,
VBAPanner::factory
};
2020-03-23 11:46:03 -04:00
extern "C" ARDOURPANNER_API PanPluginDescriptor*
panner_descriptor ()
{
return &_descriptor;
}
VBAPanner::Signal::Signal (VBAPanner&, uint32_t, uint32_t n_speakers)
{
2020-03-23 11:46:03 -04:00
resize_gains (n_speakers);
2020-03-23 11:46:03 -04:00
desired_gains[0] = desired_gains[1] = desired_gains[2] = 0;
outputs[0] = outputs[1] = outputs[2] = -1;
desired_outputs[0] = desired_outputs[1] = desired_outputs[2] = -1;
}
void
VBAPanner::Signal::resize_gains (uint32_t n)
{
2020-03-23 11:46:03 -04:00
gains.assign (n, 0.0);
}
VBAPanner::VBAPanner (std::shared_ptr<Pannable> p, std::shared_ptr<Speakers> s)
: Panner (p)
, _speakers (new VBAPSpeakers (s))
{
2020-03-23 11:46:03 -04:00
_pannable->pan_azimuth_control->Changed.connect_same_thread (*this, boost::bind (&VBAPanner::update, this));
_pannable->pan_elevation_control->Changed.connect_same_thread (*this, boost::bind (&VBAPanner::update, this));
_pannable->pan_width_control->Changed.connect_same_thread (*this, boost::bind (&VBAPanner::update, this));
if (!_pannable->has_state ()) {
reset ();
}
update ();
}
VBAPanner::~VBAPanner ()
{
2020-03-23 11:46:03 -04:00
clear_signals ();
}
void
VBAPanner::clear_signals ()
{
2020-03-23 11:46:03 -04:00
for (vector<Signal*>::iterator i = _signals.begin (); i != _signals.end (); ++i) {
delete *i;
}
_signals.clear ();
}
void
VBAPanner::configure_io (ChanCount in, ChanCount /* ignored - we use Speakers */)
{
2020-03-23 11:46:03 -04:00
uint32_t n = in.n_audio ();
2020-03-23 11:46:03 -04:00
clear_signals ();
2020-03-23 11:46:03 -04:00
for (uint32_t i = 0; i < n; ++i) {
Signal* s = new Signal (*this, i, _speakers->n_speakers ());
_signals.push_back (s);
}
2020-03-23 11:46:03 -04:00
update ();
}
void
VBAPanner::update ()
{
2020-03-23 11:46:03 -04:00
_can_automate_list.clear ();
_can_automate_list.insert (Evoral::Parameter (PanAzimuthAutomation));
if (_signals.size () > 1) {
_can_automate_list.insert (Evoral::Parameter (PanWidthAutomation));
}
if (_speakers->dimension () == 3) {
_can_automate_list.insert (Evoral::Parameter (PanElevationAutomation));
}
2020-03-23 11:46:03 -04:00
/* recompute signal directions based on panner azimuth and, if relevant, width (diffusion) and elevation parameters */
double elevation = _pannable->pan_elevation_control->get_value () * 90.0;
if (_signals.size () > 1) {
double w = -(_pannable->pan_width_control->get_value ());
double signal_direction = 1.0 - (_pannable->pan_azimuth_control->get_value () + (w / 2));
double grd_step_per_signal = w / (_signals.size () - 1);
for (vector<Signal*>::iterator s = _signals.begin (); s != _signals.end (); ++s) {
Signal* signal = *s;
int over = signal_direction;
over -= (signal_direction >= 0) ? 0 : 1;
signal_direction -= (double)over;
signal->direction = AngularVector (signal_direction * 360.0, elevation);
compute_gains (signal->desired_gains, signal->desired_outputs, signal->direction.azi, signal->direction.ele);
signal_direction += grd_step_per_signal;
}
} else if (_signals.size () == 1) {
double center = (1.0 - _pannable->pan_azimuth_control->get_value ()) * 360.0;
/* width has no role to play if there is only 1 signal: VBAP does not do "diffusion" of a single channel */
Signal* s = _signals.front ();
s->direction = AngularVector (center, elevation);
compute_gains (s->desired_gains, s->desired_outputs, s->direction.azi, s->direction.ele);
}
SignalPositionChanged (); /* emit */
}
void
VBAPanner::compute_gains (double gains[3], int speaker_ids[3], int azi, int ele)
{
/* calculates gain factors using loudspeaker setup and given direction */
2020-03-23 11:46:03 -04:00
double cartdir[3];
double power;
int i, j, k;
double small_g;
double big_sm_g, gtmp[3];
const int dimension = _speakers->dimension ();
assert (dimension == 2 || dimension == 3);
spherical_to_cartesian (azi, ele, 1.0, cartdir[0], cartdir[1], cartdir[2]);
big_sm_g = -100000.0;
gains[0] = gains[1] = gains[2] = 0;
speaker_ids[0] = speaker_ids[1] = speaker_ids[2] = 0;
2020-03-23 11:46:03 -04:00
for (i = 0; i < _speakers->n_tuples (); i++) {
small_g = 10000000.0;
2014-11-19 14:38:50 -05:00
for (j = 0; j < dimension; j++) {
gtmp[j] = 0.0;
2014-11-19 14:38:50 -05:00
for (k = 0; k < dimension; k++) {
2020-03-23 11:46:03 -04:00
gtmp[j] += cartdir[k] * _speakers->matrix (i)[j * dimension + k];
}
if (gtmp[j] < small_g) {
small_g = gtmp[j];
}
}
if (small_g > big_sm_g) {
big_sm_g = small_g;
gains[0] = gtmp[0];
gains[1] = gtmp[1];
speaker_ids[0] = _speakers->speaker_for_tuple (i, 0);
speaker_ids[1] = _speakers->speaker_for_tuple (i, 1);
2020-03-23 11:46:03 -04:00
if (_speakers->dimension () == 3) {
gains[2] = gtmp[2];
speaker_ids[2] = _speakers->speaker_for_tuple (i, 2);
} else {
2020-03-23 11:46:03 -04:00
gains[2] = 0.0;
speaker_ids[2] = -1;
}
}
}
2020-03-23 11:46:03 -04:00
power = sqrt (gains[0] * gains[0] + gains[1] * gains[1] + gains[2] * gains[2]);
if (power > 0) {
gains[0] /= power;
gains[1] /= power;
gains[2] /= power;
}
}
void
VBAPanner::distribute (BufferSet& inbufs, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes)
{
2020-03-23 11:46:03 -04:00
uint32_t n;
vector<Signal*>::iterator s;
2020-03-23 11:46:03 -04:00
assert (inbufs.count ().n_audio () == _signals.size ());
2020-03-23 11:46:03 -04:00
for (s = _signals.begin (), n = 0; s != _signals.end (); ++s, ++n) {
Signal* signal (*s);
2020-03-23 11:46:03 -04:00
distribute_one (inbufs.get_audio (n), obufs, gain_coefficient, nframes, n);
2020-03-23 11:46:03 -04:00
memcpy (signal->outputs, signal->desired_outputs, sizeof (signal->outputs));
}
}
void
VBAPanner::distribute_one (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes, uint32_t which)
{
2020-03-23 11:46:03 -04:00
Sample* const src = srcbuf.data ();
Signal* signal (_signals[which]);
/* VBAP may distribute the signal across up to 3 speakers depending on
2020-03-23 11:46:03 -04:00
* the configuration of the speakers.
*
* But the set of speakers in use "this time" may be different from
* the set of speakers "the last time". So we have up to 6 speakers
* involved, and we have to interpolate so that those no longer
* in use are rapidly faded to silence and those newly in use
* are rapidly faded to their correct level. This prevents clicks
* as we change the set of speakers used to put the signal in
* a given position.
*
* However, the speakers are represented by output buffers, and other
* speakers may write to the same buffers, so we cannot use
* anything here that will simply assign new (sample) values
* to the output buffers - everything must be done via mixing
* functions and not assignment/copying.
*/
vector<double>::size_type sz = signal->gains.size ();
assert (sz == obufs.count ().n_audio ());
int8_t* outputs = (int8_t*)alloca (sz); // on the stack, no malloc
/* set initial state of each output "record"
*/
2020-03-23 11:46:03 -04:00
for (uint32_t o = 0; o < sz; ++o) {
outputs[o] = 0;
}
2020-03-23 11:46:03 -04:00
/* for all outputs used this time and last time,
* change the output record to show what has
* happened.
*/
2020-03-23 11:46:03 -04:00
for (int o = 0; o < 3; ++o) {
if (signal->outputs[o] != -1) {
/* used last time */
outputs[signal->outputs[o]] |= 1;
}
2020-03-23 11:46:03 -04:00
if (signal->desired_outputs[o] != -1) {
/* used this time */
outputs[signal->desired_outputs[o]] |= 1 << 1;
}
}
2020-03-23 11:46:03 -04:00
/* at this point, we can test a speaker's status:
*
* (*outputs[o] & 1) <= in use before
* (*outputs[o] & 2) <= in use this time
* (*outputs[o] & 3) == 3 <= in use both times
* *outputs[o] == 0 <= not in use either time
*
*/
for (int o = 0; o < 3; ++o) {
2020-03-23 11:46:03 -04:00
pan_t pan;
int output = signal->desired_outputs[o];
if (output == -1) {
2020-03-23 11:46:03 -04:00
continue;
}
2020-03-23 11:46:03 -04:00
pan = gain_coefficient * signal->desired_gains[o];
2020-03-23 11:46:03 -04:00
if (pan == 0.0 && signal->gains[output] == 0.0) {
/* nothing deing delivered to this output */
2020-03-23 11:46:03 -04:00
signal->gains[output] = 0.0;
2020-03-23 11:46:03 -04:00
} else if (fabs (pan - signal->gains[output]) > 0.00001) {
/* signal to this output but the gain coefficient has changed, so
* interpolate between them.
*/
2020-03-23 11:46:03 -04:00
AudioBuffer& buf (obufs.get_audio (output));
buf.accumulate_with_ramped_gain_from (srcbuf.data (), nframes, signal->gains[output], pan, 0);
signal->gains[output] = pan;
2020-03-23 11:46:03 -04:00
} else {
/* signal to this output, same gain as before so just copy with gain */
2020-03-23 11:46:03 -04:00
mix_buffers_with_gain (obufs.get_audio (output).data (), src, nframes, pan);
signal->gains[output] = pan;
}
}
2020-03-23 11:46:03 -04:00
/* clean up the outputs that were used last time but not this time */
2020-03-23 11:46:03 -04:00
for (uint32_t o = 0; o < sz; ++o) {
if (outputs[o] == 1) {
/* take signal and deliver with a rapid fade out */
AudioBuffer& buf (obufs.get_audio (o));
buf.accumulate_with_ramped_gain_from (srcbuf.data (), nframes, signal->gains[o], 0.0, 0);
signal->gains[o] = 0.0;
}
}
2020-03-23 11:46:03 -04:00
/* note that the output buffers were all silenced at some point
* so anything we didn't write to with this signal (or any others)
* is just as it should be.
*/
}
void
VBAPanner::distribute_one_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
samplepos_t /*start*/, samplepos_t /*end*/,
2020-03-23 11:46:03 -04:00
pframes_t /*nframes*/, pan_t** /*buffers*/, uint32_t /*which*/)
{
/* XXX to be implemented */
}
XMLNode&
VBAPanner::get_state () const
{
2020-03-23 11:46:03 -04:00
XMLNode& node (Panner::get_state ());
node.set_property (X_ ("uri"), _descriptor.panner_uri);
rework panning -- Squashed commit of the following: commit 6f4f4f161b00cb36252727f67ecc4913eb944fd7 Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 22:13:09 2014 +0100 fix panner plugin discovery (prev commit) commit 26e514f4a80af9192cae3cbd62fde0ae95474dfc Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 18:56:59 2014 +0100 update panner plugin discovery * recurse dirs in 'PANNER_PATH' and 'panner_dir_name' up to 1 level. * don't look in ardour_dll_directory() -- no panners are supposed to be in there * use .dylib on OSX exclusively. commit a514c3f1c425dccf3d42eee9d2b183b44fd26a03 Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 16:48:34 2014 +0100 remove debug/devel printf()s commit d863742ddc69af493ee6a8817bc778968d9b0800 Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 16:17:13 2014 +0100 panner-type: session backward/forward compatibility commit 25d5e4c663ada34129451b0f9045ab047d6cc2f0 Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 16:09:07 2014 +0100 update URIs -> URLs commit 00a606a43d9456cfbaf43cae4fb598549326ba71 Merge: 0f1cec1 382eb0f Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 03:29:45 2014 +0100 Merge branch 'master' into panning commit 0f1cec19babae538c9697eed4be5d6ddc851b013 Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 02:41:15 2014 +0100 switch panner ID to URI commit 575282b412c3ae1cd8219cf75f00a1a4239e2813 Author: Robin Gareus <robin@gareus.org> Date: Wed Jan 8 00:50:15 2014 +0100 prepare API for panner URI commit ea62cd049308859782a7bb16e4f18169d8638b46 Author: Robin Gareus <robin@gareus.org> Date: Tue Jan 7 19:57:06 2014 +0100 update development doc relating to panner selection commit 586d7de2392e26b9d7f597b1a00b98dfaa42ecdc Author: Robin Gareus <robin@gareus.org> Date: Tue Jan 7 19:56:24 2014 +0100 clean up PanShell::set_user_selected_panner_type() API commit 99077886a5a1cacece908d87c29c3be12903027e Author: Robin Gareus <robin@gareus.org> Date: Tue Jan 7 04:46:22 2014 +0100 panner bypass: visualize & [in]sensitivity commit 46d688d216f0e67d672376a607157af02b359fb2 Merge: 4e67573 c4cdf61 Author: Robin Gareus <robin@gareus.org> Date: Tue Jan 7 02:18:54 2014 +0100 Merge branch 'master' into panning commit 4e67573517b3d60ddf65729783687b16cfb2adb7 Author: Robin Gareus <robin@gareus.org> Date: Tue Jan 7 01:05:17 2014 +0100 don't call configure_io() for merely swapping panners commit d32a4c51f6967f48f7680554866f1f7b311ccde1 Merge: a3226d4 cec3116 Author: Robin Gareus <robin@gareus.org> Date: Mon Jan 6 23:49:55 2014 +0100 Merge branch 'master' into panning commit a3226d46b598afae54a65ac69320eca84669f347 Author: Robin Gareus <robin@gareus.org> Date: Mon Jan 6 17:52:38 2014 +0100 add notes about panner re-design commit d1ae2366024605f22b05572a81ee249e6fdbcd2f Author: Robin Gareus <robin@gareus.org> Date: Mon Jan 6 15:06:40 2014 +0100 add simple stereo-balance panner for testing commit e0ddd256ff2288b8d8cfad3ad485a916964ce5b5 Author: Robin Gareus <robin@gareus.org> Date: Mon Jan 6 17:02:52 2014 +0100 add frontend/GUI for panner selection commit 2cb8f846755eb5aea8a2620d31ea981c446c4041 Author: Robin Gareus <robin@gareus.org> Date: Mon Jan 6 17:02:20 2014 +0100 prepare backend for panner selection
2014-01-08 18:18:29 -05:00
/* this is needed to allow new sessions to load with old Ardour: */
2020-03-23 11:46:03 -04:00
node.set_property (X_ ("type"), _descriptor.name);
return node;
}
Panner*
VBAPanner::factory (std::shared_ptr<Pannable> p, std::shared_ptr<Speakers> s)
{
return new VBAPanner (p, s);
}
ChanCount
2020-03-23 11:46:03 -04:00
VBAPanner::in () const
{
2020-03-23 11:46:03 -04:00
return ChanCount (DataType::AUDIO, _signals.size ());
}
ChanCount
2020-03-23 11:46:03 -04:00
VBAPanner::out () const
{
2020-03-23 11:46:03 -04:00
return ChanCount (DataType::AUDIO, _speakers->n_speakers ());
}
string
VBAPanner::value_as_string (std::shared_ptr<const AutomationControl> ac) const
{
2020-03-23 11:46:03 -04:00
double val = ac->get_value ();
2020-03-23 11:46:03 -04:00
switch (ac->parameter ().type ()) {
case PanAzimuthAutomation: /* direction */
return string_compose (_ (u8"%1\u00B0"), (int(rint (val * 360.0)) + 180) % 360);
2020-03-23 11:46:03 -04:00
case PanWidthAutomation: /* diffusion */
return string_compose (_ ("%1%%"), (int)floor (100.0 * fabs (val)));
2020-03-23 11:46:03 -04:00
case PanElevationAutomation: /* elevation */
return string_compose (_ (u8"%1\u00B0"), (int)floor (90.0 * fabs (val)));
2020-03-23 11:46:03 -04:00
default:
return _ ("unused");
}
}
AngularVector
VBAPanner::signal_position (uint32_t n) const
{
2020-03-23 11:46:03 -04:00
if (n < _signals.size ()) {
return _signals[n]->direction;
}
2020-03-23 11:46:03 -04:00
return AngularVector ();
}
std::shared_ptr<Speakers>
VBAPanner::get_speakers () const
{
2020-03-23 11:46:03 -04:00
return _speakers->parent ();
}
void
VBAPanner::set_position (double p)
{
/* map into 0..1 range */
int over = p;
over -= (p >= 0) ? 0 : 1;
p -= (double)over;
_pannable->pan_azimuth_control->set_value (p, Controllable::NoGroup);
}
void
VBAPanner::set_width (double w)
{
_pannable->pan_width_control->set_value (min (1.0, max (-1.0, w)), Controllable::NoGroup);
}
void
VBAPanner::set_elevation (double e)
{
_pannable->pan_elevation_control->set_value (min (1.0, max (0.0, e)), Controllable::NoGroup);
}
void
VBAPanner::reset ()
{
set_position (.5);
2020-03-23 11:46:03 -04:00
if (_signals.size () > 1) {
set_width (1.0 - (1.0 / (double)_signals.size ()));
} else {
set_width (1.0);
}
set_elevation (0);
update ();
}