aboutsummaryrefslogtreecommitdiffstats
path: root/alc/hrtf.h
blob: 3298b11f3ffcbd9caca03bf6c3484fa88d7f289c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef ALC_HRTF_H
#define ALC_HRTF_H

#include <array>
#include <cstddef>
#include <memory>
#include <string>

#include "AL/al.h"

#include "almalloc.h"
#include "alspan.h"
#include "ambidefs.h"
#include "atomic.h"
#include "vector.h"


#define HRTF_HISTORY_BITS   (6)
#define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
#define HRTF_HISTORY_MASK   (HRTF_HISTORY_LENGTH-1)

#define HRIR_BITS   (7)
#define HRIR_LENGTH (1<<HRIR_BITS)
#define HRIR_MASK   (HRIR_LENGTH-1)

#define MIN_IR_LENGTH (8)

using float2 = std::array<float,2>;
using HrirArray = std::array<float2,HRIR_LENGTH>;
using ubyte2 = std::array<ALubyte,2>;


struct HrtfStore {
    RefCount mRef;

    ALuint sampleRate;
    ALuint irSize;

    struct Field {
        ALfloat distance;
        ALubyte evCount;
    };
    /* NOTE: Fields are stored *backwards*. field[0] is the farthest field, and
     * field[fdCount-1] is the nearest.
     */
    ALuint fdCount;
    const Field *field;

    struct Elevation {
        ALushort azCount;
        ALushort irOffset;
    };
    Elevation *elev;
    const HrirArray *coeffs;
    const ubyte2 *delays;

    void IncRef();
    void DecRef();

    DEF_PLACE_NEWDEL()
};


struct HrtfState {
    alignas(16) std::array<float,HRTF_HISTORY_LENGTH> History;
};

struct HrtfFilter {
    alignas(16) HrirArray Coeffs;
    ALuint Delay[2];
    float Gain;
};

struct DirectHrtfState {
    /* HRTF filter state for dry buffer content */
    ALuint IrSize{0};
    al::FlexArray<HrirArray,16> Coeffs;

    DirectHrtfState(size_t numchans) : Coeffs{numchans} { }

    static std::unique_ptr<DirectHrtfState> Create(size_t num_chans);

    DEF_FAM_NEWDEL(DirectHrtfState, Coeffs)
};

struct EvRadians { float value; };
struct AzRadians { float value; };
struct AngularPoint {
    EvRadians Elev;
    AzRadians Azim;
};


al::vector<std::string> EnumerateHrtf(const char *devname);
HrtfStore *GetLoadedHrtf(const std::string &name, const char *devname, const ALuint devrate);

void GetHrtfCoeffs(const HrtfStore *Hrtf, float elevation, float azimuth, float distance,
    float spread, HrirArray &coeffs, ALuint (&delays)[2]);

/**
 * Produces HRTF filter coefficients for decoding B-Format, given a set of
 * virtual speaker positions, a matching decoding matrix, and per-order high-
 * frequency gains for the decoder. The calculated impulse responses are
 * ordered and scaled according to the matrix input.
 */
void BuildBFormatHrtf(const HrtfStore *Hrtf, DirectHrtfState *state,
    const al::span<const AngularPoint> AmbiPoints, const float (*AmbiMatrix)[MAX_AMBI_CHANNELS],
    const al::span<const float,MAX_AMBI_ORDER+1> AmbiOrderHFGain);

#endif /* ALC_HRTF_H */