aboutsummaryrefslogtreecommitdiffstats
path: root/core/hrtf.h
blob: c5dc64756d5e8c942cea2f072194de5aa932b86d (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
#ifndef CORE_HRTF_H
#define CORE_HRTF_H

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

#include "almalloc.h"
#include "alspan.h"
#include "atomic.h"
#include "ambidefs.h"
#include "bufferline.h"
#include "flexarray.h"
#include "intrusive_ptr.h"
#include "mixer/hrtfdefs.h"


struct HrtfStore {
    std::atomic<uint> mRef;

    uint mSampleRate : 24;
    uint mIrSize : 8;

    struct Field {
        float distance;
        ubyte evCount;
    };
    /* NOTE: Fields are stored *backwards*. field[0] is the farthest field, and
     * field[fdCount-1] is the nearest.
     */
    al::span<const Field> mFields;

    struct Elevation {
        ushort azCount;
        ushort irOffset;
    };
    Elevation *mElev;
    const HrirArray *mCoeffs;
    const ubyte2 *mDelays;

    void getCoeffs(float elevation, float azimuth, float distance, float spread, HrirArray &coeffs,
        const al::span<uint,2> delays);

    void add_ref();
    void dec_ref();

    DEF_PLACE_NEWDEL()
};
using HrtfStorePtr = al::intrusive_ptr<HrtfStore>;


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


struct DirectHrtfState {
    std::array<float,BufferLineSize> mTemp;

    /* HRTF filter state for dry buffer content */
    uint mIrSize{0};
    al::FlexArray<HrtfChannelState> mChannels;

    DirectHrtfState(size_t numchans) : mChannels{numchans} { }
    /**
     * 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 build(const HrtfStore *Hrtf, const uint irSize, const bool perHrirMin,
        const al::span<const AngularPoint> AmbiPoints,
        const al::span<const std::array<float,MaxAmbiChannels>> AmbiMatrix,
        const float XOverFreq, const al::span<const float,MaxAmbiOrder+1> AmbiOrderHFGain);

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

    DEF_FAM_NEWDEL(DirectHrtfState, mChannels)
};


std::vector<std::string> EnumerateHrtf(std::optional<std::string> pathopt);
HrtfStorePtr GetLoadedHrtf(const std::string &name, const uint devrate);

#endif /* CORE_HRTF_H */