aboutsummaryrefslogtreecommitdiffstats
path: root/core/converter.h
blob: 7aeb6cad9907c2a9ec0536af0b965a6b5a7268f4 (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
#ifndef CORE_CONVERTER_H
#define CORE_CONVERTER_H

#include <chrono>
#include <cstddef>
#include <memory>

#include "almalloc.h"
#include "devformat.h"
#include "flexarray.h"
#include "mixer/defs.h"

using uint = unsigned int;


struct SampleConverter {
    DevFmtType mSrcType{};
    DevFmtType mDstType{};
    uint mSrcTypeSize{};
    uint mDstTypeSize{};

    uint mSrcPrepCount{};

    uint mFracOffset{};
    uint mIncrement{};
    InterpState mState{};
    ResamplerFunc mResample{};

    alignas(16) float mSrcSamples[BufferLineSize]{};
    alignas(16) float mDstSamples[BufferLineSize]{};

    struct ChanSamples {
        alignas(16) float PrevSamples[MaxResamplerPadding];
    };
    al::FlexArray<ChanSamples> mChan;

    SampleConverter(size_t numchans) : mChan{numchans} { }

    uint convert(const void **src, uint *srcframes, void *dst, uint dstframes);
    uint convertPlanar(const void **src, uint *srcframes, void *const*dst, uint dstframes);
    uint availableOut(uint srcframes) const;

    using SampleOffset = std::chrono::duration<int64_t, std::ratio<1,MixerFracOne>>;
    SampleOffset currentInputDelay() const noexcept
    {
        const int64_t prep{int64_t{mSrcPrepCount} - MaxResamplerEdge};
        return SampleOffset{(prep<<MixerFracBits) + mFracOffset};
    }

    static std::unique_ptr<SampleConverter> Create(DevFmtType srcType, DevFmtType dstType,
        size_t numchans, uint srcRate, uint dstRate, Resampler resampler);

    DEF_FAM_NEWDEL(SampleConverter, mChan)
};
using SampleConverterPtr = std::unique_ptr<SampleConverter>;

struct ChannelConverter {
    DevFmtType mSrcType{};
    uint mSrcStep{};
    uint mChanMask{};
    DevFmtChannels mDstChans{};

    bool is_active() const noexcept { return mChanMask != 0; }

    void convert(const void *src, float *dst, uint frames) const;
};

#endif /* CORE_CONVERTER_H */