2007-11-14 02:02:18 +00:00
|
|
|
#ifndef _ALU_H_
|
|
|
|
#define _ALU_H_
|
|
|
|
|
2010-08-07 12:43:16 +00:00
|
|
|
#include <limits.h>
|
2009-11-19 17:24:35 +00:00
|
|
|
#include <math.h>
|
2009-05-17 06:26:39 +00:00
|
|
|
#ifdef HAVE_FLOAT_H
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
2011-07-23 12:53:53 +00:00
|
|
|
#ifdef HAVE_IEEEFP_H
|
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
2009-05-17 06:26:39 +00:00
|
|
|
|
2014-03-22 09:39:57 +00:00
|
|
|
#include "alMain.h"
|
|
|
|
#include "alBuffer.h"
|
|
|
|
#include "alFilter.h"
|
2014-04-19 09:11:04 +00:00
|
|
|
|
2014-03-22 09:39:57 +00:00
|
|
|
#include "hrtf.h"
|
2014-04-19 09:11:04 +00:00
|
|
|
#include "align.h"
|
2014-03-22 09:39:57 +00:00
|
|
|
|
2009-05-17 06:26:39 +00:00
|
|
|
|
2013-10-08 15:29:14 +00:00
|
|
|
#define F_PI (3.14159265358979323846f)
|
|
|
|
#define F_PI_2 (1.57079632679489661923f)
|
2013-10-08 23:31:23 +00:00
|
|
|
#define F_2PI (6.28318530717958647692f)
|
2011-09-22 18:17:01 +00:00
|
|
|
|
2012-10-12 14:10:51 +00:00
|
|
|
#ifndef FLT_EPSILON
|
|
|
|
#define FLT_EPSILON (1.19209290e-07f)
|
|
|
|
#endif
|
|
|
|
|
2013-10-08 15:29:14 +00:00
|
|
|
#define DEG2RAD(x) ((ALfloat)(x) * (F_PI/180.0f))
|
|
|
|
#define RAD2DEG(x) ((ALfloat)(x) * (180.0f/F_PI))
|
|
|
|
|
2012-10-12 14:10:51 +00:00
|
|
|
|
2014-06-03 02:19:22 +00:00
|
|
|
#define MAX_PITCH (10)
|
|
|
|
|
2014-03-22 09:39:57 +00:00
|
|
|
|
2007-11-14 02:02:18 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-08-21 04:35:18 +00:00
|
|
|
struct ALsource;
|
2014-08-21 10:24:48 +00:00
|
|
|
struct ALvoice;
|
2014-08-21 04:35:18 +00:00
|
|
|
|
|
|
|
|
2014-05-17 14:17:48 +00:00
|
|
|
enum ActiveFilters {
|
|
|
|
AF_None = 0,
|
|
|
|
AF_LowPass = 1,
|
2014-05-17 14:54:25 +00:00
|
|
|
AF_HighPass = 2,
|
|
|
|
AF_BandPass = AF_LowPass | AF_HighPass
|
2014-05-17 14:17:48 +00:00
|
|
|
};
|
|
|
|
|
2014-05-18 17:24:07 +00:00
|
|
|
|
|
|
|
typedef struct MixGains {
|
2014-05-18 17:35:11 +00:00
|
|
|
ALfloat Current;
|
|
|
|
ALfloat Step;
|
|
|
|
ALfloat Target;
|
2014-06-13 18:42:04 +00:00
|
|
|
} MixGains;
|
2014-05-18 17:35:11 +00:00
|
|
|
|
2014-05-18 17:24:07 +00:00
|
|
|
|
2014-03-22 09:39:57 +00:00
|
|
|
typedef struct DirectParams {
|
|
|
|
ALfloat (*OutBuffer)[BUFFERSIZE];
|
2014-11-23 00:23:08 +00:00
|
|
|
ALuint OutChannels;
|
2014-03-22 09:39:57 +00:00
|
|
|
|
2014-05-17 14:17:48 +00:00
|
|
|
/* If not 'moving', gain/coefficients are set directly without fading. */
|
|
|
|
ALboolean Moving;
|
|
|
|
/* Stepping counter for gain/coefficient fading. */
|
|
|
|
ALuint Counter;
|
2014-11-24 09:31:38 +00:00
|
|
|
/* Last direction (relative to listener) and gain of a moving source. */
|
|
|
|
ALfloat LastDir[3];
|
|
|
|
ALfloat LastGain;
|
2014-05-17 14:17:48 +00:00
|
|
|
|
2014-05-19 09:24:31 +00:00
|
|
|
struct {
|
|
|
|
enum ActiveFilters ActiveType;
|
|
|
|
ALfilterState LowPass;
|
|
|
|
ALfilterState HighPass;
|
|
|
|
} Filters[MAX_INPUT_CHANNELS];
|
|
|
|
|
2014-11-23 18:49:54 +00:00
|
|
|
struct {
|
|
|
|
HrtfParams Params[MAX_INPUT_CHANNELS];
|
|
|
|
HrtfState State[MAX_INPUT_CHANNELS];
|
|
|
|
} Hrtf;
|
2014-11-22 20:58:54 +00:00
|
|
|
MixGains Gains[MAX_INPUT_CHANNELS][MAX_OUTPUT_CHANNELS];
|
2014-03-22 09:39:57 +00:00
|
|
|
} DirectParams;
|
|
|
|
|
|
|
|
typedef struct SendParams {
|
|
|
|
ALfloat (*OutBuffer)[BUFFERSIZE];
|
|
|
|
|
2014-05-17 14:17:48 +00:00
|
|
|
ALboolean Moving;
|
|
|
|
ALuint Counter;
|
|
|
|
|
2014-05-19 09:24:31 +00:00
|
|
|
struct {
|
|
|
|
enum ActiveFilters ActiveType;
|
|
|
|
ALfilterState LowPass;
|
|
|
|
ALfilterState HighPass;
|
|
|
|
} Filters[MAX_INPUT_CHANNELS];
|
|
|
|
|
2014-03-22 09:39:57 +00:00
|
|
|
/* Gain control, which applies to all input channels to a single (mono)
|
|
|
|
* output buffer. */
|
2014-06-13 18:42:04 +00:00
|
|
|
MixGains Gain;
|
2014-03-22 09:39:57 +00:00
|
|
|
} SendParams;
|
|
|
|
|
2011-06-25 07:08:05 +00:00
|
|
|
|
2014-05-19 12:46:01 +00:00
|
|
|
typedef const ALfloat* (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
|
|
|
|
ALfloat *restrict dst, ALuint dstlen);
|
2012-09-14 11:13:18 +00:00
|
|
|
|
2014-06-13 20:34:19 +00:00
|
|
|
typedef void (*MixerFunc)(const ALfloat *data, ALuint OutChans,
|
|
|
|
ALfloat (*restrict OutBuffer)[BUFFERSIZE], struct MixGains *Gains,
|
|
|
|
ALuint Counter, ALuint OutPos, ALuint BufferSize);
|
2014-05-18 16:31:08 +00:00
|
|
|
typedef void (*HrtfMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
|
2014-11-23 18:49:54 +00:00
|
|
|
ALuint Counter, ALuint Offset, ALuint OutPos,
|
|
|
|
const ALuint IrSize, const HrtfParams *hrtfparams,
|
2014-05-18 18:05:38 +00:00
|
|
|
HrtfState *hrtfstate, ALuint BufferSize);
|
2011-06-25 07:08:05 +00:00
|
|
|
|
2010-11-27 01:47:43 +00:00
|
|
|
|
2014-05-04 00:51:06 +00:00
|
|
|
#define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
|
2013-10-07 00:25:47 +00:00
|
|
|
|
2012-09-14 09:52:37 +00:00
|
|
|
#define SPEEDOFSOUNDMETRESPERSEC (343.3f)
|
|
|
|
#define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
|
|
|
|
|
2014-12-15 20:23:28 +00:00
|
|
|
#define FRACTIONBITS (12)
|
2012-09-09 05:09:34 +00:00
|
|
|
#define FRACTIONONE (1<<FRACTIONBITS)
|
|
|
|
#define FRACTIONMASK (FRACTIONONE-1)
|
|
|
|
|
2010-08-07 12:43:16 +00:00
|
|
|
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALfloat minf(ALfloat a, ALfloat b)
|
2011-08-16 11:21:58 +00:00
|
|
|
{ return ((a > b) ? b : a); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALfloat maxf(ALfloat a, ALfloat b)
|
2011-08-16 11:21:58 +00:00
|
|
|
{ return ((a > b) ? a : b); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
|
2011-08-17 01:40:21 +00:00
|
|
|
{ return minf(max, maxf(min, val)); }
|
2011-08-16 11:21:58 +00:00
|
|
|
|
2013-11-27 08:30:13 +00:00
|
|
|
inline ALdouble mind(ALdouble a, ALdouble b)
|
|
|
|
{ return ((a > b) ? b : a); }
|
|
|
|
inline ALdouble maxd(ALdouble a, ALdouble b)
|
|
|
|
{ return ((a > b) ? a : b); }
|
|
|
|
inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
|
|
|
|
{ return mind(max, maxd(min, val)); }
|
|
|
|
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALuint minu(ALuint a, ALuint b)
|
2011-08-17 01:33:10 +00:00
|
|
|
{ return ((a > b) ? b : a); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALuint maxu(ALuint a, ALuint b)
|
2011-08-17 01:33:10 +00:00
|
|
|
{ return ((a > b) ? a : b); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALuint clampu(ALuint val, ALuint min, ALuint max)
|
2011-08-17 01:33:10 +00:00
|
|
|
{ return minu(max, maxu(min, val)); }
|
|
|
|
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALint mini(ALint a, ALint b)
|
2011-08-17 01:33:10 +00:00
|
|
|
{ return ((a > b) ? b : a); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALint maxi(ALint a, ALint b)
|
2011-08-17 01:33:10 +00:00
|
|
|
{ return ((a > b) ? a : b); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALint clampi(ALint val, ALint min, ALint max)
|
2011-08-17 01:33:10 +00:00
|
|
|
{ return mini(max, maxi(min, val)); }
|
|
|
|
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALint64 mini64(ALint64 a, ALint64 b)
|
2011-10-05 05:39:35 +00:00
|
|
|
{ return ((a > b) ? b : a); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALint64 maxi64(ALint64 a, ALint64 b)
|
2011-10-05 05:39:35 +00:00
|
|
|
{ return ((a > b) ? a : b); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
|
2011-10-05 05:39:35 +00:00
|
|
|
{ return mini64(max, maxi64(min, val)); }
|
|
|
|
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALuint64 minu64(ALuint64 a, ALuint64 b)
|
2012-08-17 20:38:52 +00:00
|
|
|
{ return ((a > b) ? b : a); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
|
2012-08-17 20:38:52 +00:00
|
|
|
{ return ((a > b) ? a : b); }
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
|
2012-08-17 20:38:52 +00:00
|
|
|
{ return minu64(max, maxu64(min, val)); }
|
|
|
|
|
2011-08-16 11:21:58 +00:00
|
|
|
|
2014-12-15 20:23:28 +00:00
|
|
|
extern ALfloat CubicLUT[FRACTIONONE][4];
|
|
|
|
|
|
|
|
|
2013-11-04 21:44:46 +00:00
|
|
|
inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
|
2010-11-26 09:07:54 +00:00
|
|
|
{
|
2010-11-29 06:50:27 +00:00
|
|
|
return val1 + (val2-val1)*mu;
|
2010-11-26 09:07:54 +00:00
|
|
|
}
|
2014-12-15 20:23:28 +00:00
|
|
|
inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALuint frac)
|
2010-11-26 09:07:54 +00:00
|
|
|
{
|
2014-12-15 20:23:28 +00:00
|
|
|
const ALfloat *k = CubicLUT[frac];
|
|
|
|
return k[0]*val0 + k[1]*val1 + k[2]*val2 + k[3]*val3;
|
2010-11-26 09:07:54 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 11:03:18 +00:00
|
|
|
|
2014-12-15 20:23:28 +00:00
|
|
|
void aluInitResamplers(void);
|
|
|
|
|
2010-04-08 22:58:11 +00:00
|
|
|
ALvoid aluInitPanning(ALCdevice *Device);
|
2010-08-04 06:10:00 +00:00
|
|
|
|
Use an ambisonics-based panning method
For mono sources, third-order ambisonics is utilized to generate panning gains.
The general idea is that a panned mono sound can be encoded into b-format
ambisonics as:
w[i] = sample[i] * 0.7071;
x[i] = sample[i] * dir[0];
y[i] = sample[i] * dir[1];
...
and subsequently rendered using:
output[chan][i] = w[i] * w_coeffs[chan] +
x[i] * x_coeffs[chan] +
y[i] * y_coeffs[chan] +
...;
By reordering the math, channel gains can be generated by doing:
gain[chan] = 0.7071 * w_coeffs[chan] +
dir[0] * x_coeffs[chan] +
dir[1] * y_coeffs[chan] +
...;
which then get applied as normal:
output[chan][i] = sample[i] * gain[chan];
One of the reasons to use ambisonics for panning is that it provides arguably
better reproduction for sounds emanating from between two speakers. As well,
this makes it easier to pan in all 3 dimensions, with for instance a "3D7.1" or
8-channel cube speaker configuration by simply providing the necessary
coefficients (this will need some work since some methods still use angle-based
panpot, particularly multi-channel sources).
Unfortunately, the math to reliably generate the coefficients for a given
speaker configuration is too costly to do at run-time. They have to be pre-
generated based on a pre-specified speaker arangement, which means the config
options for tweaking speaker angles are no longer supportable. Eventually I
hope to provide config options for custom coefficients, which can either be
generated and written in manually, or via alsoft-config from user-specified
speaker positions.
The current default set of coefficients were generated using the MATLAB scripts
(compatible with GNU Octave) from the excellent Ambisonic Decoder Toolbox, at
https://bitbucket.org/ambidecodertoolbox/adt/
2014-09-30 14:33:13 +00:00
|
|
|
/**
|
|
|
|
* ComputeDirectionalGains
|
|
|
|
*
|
2014-10-03 01:05:42 +00:00
|
|
|
* Sets channel gains based on a direction. The direction must be a 3-component
|
|
|
|
* vector no longer than 1 unit.
|
Use an ambisonics-based panning method
For mono sources, third-order ambisonics is utilized to generate panning gains.
The general idea is that a panned mono sound can be encoded into b-format
ambisonics as:
w[i] = sample[i] * 0.7071;
x[i] = sample[i] * dir[0];
y[i] = sample[i] * dir[1];
...
and subsequently rendered using:
output[chan][i] = w[i] * w_coeffs[chan] +
x[i] * x_coeffs[chan] +
y[i] * y_coeffs[chan] +
...;
By reordering the math, channel gains can be generated by doing:
gain[chan] = 0.7071 * w_coeffs[chan] +
dir[0] * x_coeffs[chan] +
dir[1] * y_coeffs[chan] +
...;
which then get applied as normal:
output[chan][i] = sample[i] * gain[chan];
One of the reasons to use ambisonics for panning is that it provides arguably
better reproduction for sounds emanating from between two speakers. As well,
this makes it easier to pan in all 3 dimensions, with for instance a "3D7.1" or
8-channel cube speaker configuration by simply providing the necessary
coefficients (this will need some work since some methods still use angle-based
panpot, particularly multi-channel sources).
Unfortunately, the math to reliably generate the coefficients for a given
speaker configuration is too costly to do at run-time. They have to be pre-
generated based on a pre-specified speaker arangement, which means the config
options for tweaking speaker angles are no longer supportable. Eventually I
hope to provide config options for custom coefficients, which can either be
generated and written in manually, or via alsoft-config from user-specified
speaker positions.
The current default set of coefficients were generated using the MATLAB scripts
(compatible with GNU Octave) from the excellent Ambisonic Decoder Toolbox, at
https://bitbucket.org/ambidecodertoolbox/adt/
2014-09-30 14:33:13 +00:00
|
|
|
*/
|
2014-11-07 10:18:24 +00:00
|
|
|
void ComputeDirectionalGains(const ALCdevice *device, const ALfloat dir[3], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
|
Use an ambisonics-based panning method
For mono sources, third-order ambisonics is utilized to generate panning gains.
The general idea is that a panned mono sound can be encoded into b-format
ambisonics as:
w[i] = sample[i] * 0.7071;
x[i] = sample[i] * dir[0];
y[i] = sample[i] * dir[1];
...
and subsequently rendered using:
output[chan][i] = w[i] * w_coeffs[chan] +
x[i] * x_coeffs[chan] +
y[i] * y_coeffs[chan] +
...;
By reordering the math, channel gains can be generated by doing:
gain[chan] = 0.7071 * w_coeffs[chan] +
dir[0] * x_coeffs[chan] +
dir[1] * y_coeffs[chan] +
...;
which then get applied as normal:
output[chan][i] = sample[i] * gain[chan];
One of the reasons to use ambisonics for panning is that it provides arguably
better reproduction for sounds emanating from between two speakers. As well,
this makes it easier to pan in all 3 dimensions, with for instance a "3D7.1" or
8-channel cube speaker configuration by simply providing the necessary
coefficients (this will need some work since some methods still use angle-based
panpot, particularly multi-channel sources).
Unfortunately, the math to reliably generate the coefficients for a given
speaker configuration is too costly to do at run-time. They have to be pre-
generated based on a pre-specified speaker arangement, which means the config
options for tweaking speaker angles are no longer supportable. Eventually I
hope to provide config options for custom coefficients, which can either be
generated and written in manually, or via alsoft-config from user-specified
speaker positions.
The current default set of coefficients were generated using the MATLAB scripts
(compatible with GNU Octave) from the excellent Ambisonic Decoder Toolbox, at
https://bitbucket.org/ambidecodertoolbox/adt/
2014-09-30 14:33:13 +00:00
|
|
|
|
2013-10-03 12:02:16 +00:00
|
|
|
/**
|
|
|
|
* ComputeAngleGains
|
|
|
|
*
|
2014-10-03 01:05:42 +00:00
|
|
|
* Sets channel gains based on angle and elevation. The angle and elevation
|
|
|
|
* parameters are in radians, going right and up respectively.
|
2013-10-03 12:02:16 +00:00
|
|
|
*/
|
2014-11-07 10:18:24 +00:00
|
|
|
void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat elevation, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
|
2013-10-03 12:02:16 +00:00
|
|
|
|
2014-11-04 11:33:35 +00:00
|
|
|
/**
|
|
|
|
* ComputeAmbientGains
|
|
|
|
*
|
|
|
|
* Sets channel gains for ambient, omni-directional sounds.
|
|
|
|
*/
|
2014-11-07 10:18:24 +00:00
|
|
|
void ComputeAmbientGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
|
2014-11-04 11:33:35 +00:00
|
|
|
|
2014-11-01 00:18:45 +00:00
|
|
|
/**
|
|
|
|
* ComputeBFormatGains
|
|
|
|
*
|
2014-11-01 05:43:13 +00:00
|
|
|
* Sets channel gains for a given (first-order) B-Format channel. The matrix is
|
|
|
|
* a 1x4 'slice' of the rotation matrix for a given channel used to orient the
|
|
|
|
* coefficients.
|
2014-11-01 00:18:45 +00:00
|
|
|
*/
|
2014-11-07 10:18:24 +00:00
|
|
|
void ComputeBFormatGains(const ALCdevice *device, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
|
2014-11-01 00:18:45 +00:00
|
|
|
|
2012-04-28 14:28:36 +00:00
|
|
|
|
2014-08-21 10:24:48 +00:00
|
|
|
ALvoid CalcSourceParams(struct ALvoice *voice, const struct ALsource *source, const ALCcontext *ALContext);
|
|
|
|
ALvoid CalcNonAttnSourceParams(struct ALvoice *voice, const struct ALsource *source, const ALCcontext *ALContext);
|
2010-08-04 06:10:00 +00:00
|
|
|
|
2014-08-21 10:24:48 +00:00
|
|
|
ALvoid MixSource(struct ALvoice *voice, struct ALsource *source, ALCdevice *Device, ALuint SamplesToDo);
|
2010-09-26 08:15:27 +00:00
|
|
|
|
2009-09-16 02:30:27 +00:00
|
|
|
ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
|
2012-12-02 19:20:20 +00:00
|
|
|
/* Caller must lock the device. */
|
2009-08-27 02:15:17 +00:00
|
|
|
ALvoid aluHandleDisconnect(ALCdevice *device);
|
2007-11-14 02:02:18 +00:00
|
|
|
|
2011-09-24 05:33:37 +00:00
|
|
|
extern ALfloat ConeScale;
|
|
|
|
extern ALfloat ZScale;
|
|
|
|
|
2007-11-14 02:02:18 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|