AuroraOpenALSoft/OpenAL32/Include/alu.h

253 lines
6.8 KiB
C
Raw Normal View History

2007-11-14 02:02:18 +00:00
#ifndef _ALU_H_
#define _ALU_H_
#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
#ifdef HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
2009-05-17 06:26:39 +00:00
#include "alMain.h"
#include "alBuffer.h"
#include "alFilter.h"
2014-04-19 09:11:04 +00:00
#include "hrtf.h"
2014-04-19 09:11:04 +00:00
#include "align.h"
2009-05-17 06:26:39 +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)
#ifndef FLT_EPSILON
#define FLT_EPSILON (1.19209290e-07f)
#endif
#define DEG2RAD(x) ((ALfloat)(x) * (F_PI/180.0f))
#define RAD2DEG(x) ((ALfloat)(x) * (180.0f/F_PI))
#define SRC_HISTORY_BITS (6)
#define SRC_HISTORY_LENGTH (1<<SRC_HISTORY_BITS)
#define SRC_HISTORY_MASK (SRC_HISTORY_LENGTH-1)
#define MAX_PITCH (10)
2007-11-14 02:02:18 +00:00
#ifdef __cplusplus
extern "C" {
#endif
struct ALsource;
2014-08-21 10:24:48 +00:00
struct ALvoice;
enum ActiveFilters {
AF_None = 0,
AF_LowPass = 1,
AF_HighPass = 2,
AF_BandPass = AF_LowPass | AF_HighPass
};
typedef struct HrtfState {
2014-05-04 01:59:26 +00:00
alignas(16) ALfloat History[SRC_HISTORY_LENGTH];
alignas(16) ALfloat Values[HRIR_LENGTH][2];
} HrtfState;
typedef struct HrtfParams {
2014-05-04 01:59:26 +00:00
alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
alignas(16) ALfloat CoeffStep[HRIR_LENGTH][2];
ALuint Delay[2];
ALint DelayStep[2];
} HrtfParams;
typedef struct MixGains {
ALfloat Current;
ALfloat Step;
ALfloat Target;
2014-06-13 18:42:04 +00:00
} MixGains;
typedef struct DirectParams {
ALfloat (*OutBuffer)[BUFFERSIZE];
/* If not 'moving', gain/coefficients are set directly without fading. */
ALboolean Moving;
/* Stepping counter for gain/coefficient fading. */
ALuint Counter;
struct {
enum ActiveFilters ActiveType;
ALfilterState LowPass;
ALfilterState HighPass;
} Filters[MAX_INPUT_CHANNELS];
union {
struct {
2014-05-04 01:59:26 +00:00
HrtfParams Params[MAX_INPUT_CHANNELS];
HrtfState State[MAX_INPUT_CHANNELS];
ALuint IrSize;
ALfloat Gain;
ALfloat Dir[3];
} Hrtf;
2014-06-13 18:42:04 +00:00
MixGains Gains[MAX_INPUT_CHANNELS][MaxChannels];
} Mix;
} DirectParams;
typedef struct SendParams {
ALfloat (*OutBuffer)[BUFFERSIZE];
ALboolean Moving;
ALuint Counter;
struct {
enum ActiveFilters ActiveType;
ALfilterState LowPass;
ALfilterState HighPass;
} Filters[MAX_INPUT_CHANNELS];
/* Gain control, which applies to all input channels to a single (mono)
* output buffer. */
2014-06-13 18:42:04 +00:00
MixGains Gain;
} SendParams;
typedef const ALfloat* (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
ALfloat *restrict dst, ALuint dstlen);
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);
typedef void (*HrtfMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
ALuint Counter, ALuint Offset, ALuint OutPos,
const ALuint IrSize, const HrtfParams *hrtfparams,
HrtfState *hrtfstate, ALuint BufferSize);
2010-11-27 01:47:43 +00:00
#define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
2013-10-07 00:25:47 +00:00
#define SPEEDOFSOUNDMETRESPERSEC (343.3f)
#define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
#define FRACTIONBITS (14)
#define FRACTIONONE (1<<FRACTIONBITS)
#define FRACTIONMASK (FRACTIONONE-1)
2013-11-04 21:44:46 +00:00
inline ALfloat minf(ALfloat a, ALfloat b)
{ return ((a > b) ? b : a); }
2013-11-04 21:44:46 +00:00
inline ALfloat maxf(ALfloat a, ALfloat b)
{ return ((a > b) ? a : b); }
2013-11-04 21:44:46 +00:00
inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
{ return minf(max, maxf(min, val)); }
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)
{ return ((a > b) ? b : a); }
2013-11-04 21:44:46 +00:00
inline ALuint maxu(ALuint a, ALuint b)
{ return ((a > b) ? a : b); }
2013-11-04 21:44:46 +00:00
inline ALuint clampu(ALuint val, ALuint min, ALuint max)
{ return minu(max, maxu(min, val)); }
2013-11-04 21:44:46 +00:00
inline ALint mini(ALint a, ALint b)
{ return ((a > b) ? b : a); }
2013-11-04 21:44:46 +00:00
inline ALint maxi(ALint a, ALint b)
{ return ((a > b) ? a : b); }
2013-11-04 21:44:46 +00:00
inline ALint clampi(ALint val, ALint min, ALint max)
{ return mini(max, maxi(min, val)); }
2013-11-04 21:44:46 +00:00
inline ALint64 mini64(ALint64 a, ALint64 b)
{ return ((a > b) ? b : a); }
2013-11-04 21:44:46 +00:00
inline ALint64 maxi64(ALint64 a, ALint64 b)
{ return ((a > b) ? a : b); }
2013-11-04 21:44:46 +00:00
inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
{ return mini64(max, maxi64(min, val)); }
2013-11-04 21:44:46 +00:00
inline ALuint64 minu64(ALuint64 a, ALuint64 b)
{ return ((a > b) ? b : a); }
2013-11-04 21:44:46 +00:00
inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
{ return ((a > b) ? a : b); }
2013-11-04 21:44:46 +00:00
inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
{ return minu64(max, maxu64(min, val)); }
2013-11-04 21:44:46 +00:00
inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
{
2010-11-29 06:50:27 +00:00
return val1 + (val2-val1)*mu;
}
2013-11-04 21:44:46 +00:00
inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
{
ALfloat mu2 = mu*mu;
ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
ALfloat a2 = -0.5f*val0 + 0.5f*val2;
ALfloat a3 = val1;
return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
}
2010-04-08 22:58:11 +00:00
ALvoid aluInitPanning(ALCdevice *Device);
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
*
* Sets channel gains based on the given source's direction. The direction must
* be a 3-component vector no longer than 1 unit.
*/
void ComputeDirectionalGains(const ALCdevice *device, const ALfloat dir[3], ALfloat ingain, ALfloat gains[MaxChannels]);
/**
* ComputeAngleGains
*
* Sets channel gains based on a given source's angle and its half-width. The
* angle and hwidth parameters are in radians.
*/
void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
/**
* SetGains
*
* Helper to set the appropriate channels to the specified gain.
*/
2013-11-04 21:44:46 +00:00
inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
{
ALuint i;
for(i = 0;i < MaxChannels;i++)
gains[i] = 0.0f;
for(i = 0;i < device->NumSpeakers;i++)
gains[device->Speaker[i].ChanName] = ingain;
}
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);
2014-08-21 10:24:48 +00:00
ALvoid MixSource(struct ALvoice *voice, struct ALsource *source, ALCdevice *Device, ALuint SamplesToDo);
2009-09-16 02:30:27 +00:00
ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
/* Caller must lock the device. */
ALvoid aluHandleDisconnect(ALCdevice *device);
2007-11-14 02:02:18 +00:00
extern ALfloat ConeScale;
extern ALfloat ZScale;
2007-11-14 02:02:18 +00:00
#ifdef __cplusplus
}
#endif
#endif