RavEngine
Loading...
Searching...
No Matches
AudioRenderBuffer.hpp
1#pragma once
2#include "AudioTypes.hpp"
3#include "SpinLock.hpp"
4
5#define RVE_DEBUG_AUDIO_ACCESS 0
6
7namespace RavEngine {
8
10#if RVE_DEBUG_AUDIO_ACCESS
11 mutable uint32_t readerCount = 0; // fake const is a code smell, but required (this class is not public API)
12 mutable bool writersActive = false;
13 mutable SpinLock mtx;
14#endif
15
16 void AcquireRead() const;
17 void ReleaseRead() const;
18 void AcquireWrite() const;
19 void ReleaseWrite() const;
20 };
21
22 template<bool isReader>
24 const SingleAudioRenderBuffer_t_readwritetrack* owner = nullptr;
25
26 void OnConstruct() {
27 if constexpr (isReader) {
28 owner->AcquireRead();
29 }
30 else {
31 owner->AcquireWrite();
32 }
33 }
34
35 PlanarSampleBufferInlineViewAcessControlled(decltype(owner) owner, decltype(combined_buffers)::value_type* data, const decltype(combined_buffers)::size_type nframestotal, decltype(sizeOfOneChannelInFrames) sizeOfOneChannelInFrames) : owner(owner),
36 PlanarSampleBufferInlineView(data, nframestotal, sizeOfOneChannelInFrames){
37 OnConstruct();
38 }
39
41 if constexpr (isReader) {
42 owner->ReleaseRead();
43 }
44 else {
45 owner->ReleaseWrite();
46 }
47 }
48 PlanarSampleBufferInlineViewAcessControlled(const PlanarSampleBufferInlineViewAcessControlled& other) = delete; // these represent ownership now and so can't be copied
49 };
50
55 template<bool allocateScratchBuffer = true>
57 protected:
58
59 float* data_impl = nullptr;
60 float* scratch_impl = nullptr;
61 const uint32_t totalBufferSize;
62 uint8_t nchannels = 0;
63
64 SingleAudioRenderBuffer_t_readwritetrack dataTracker, scratchTracker;
65 public:
66
67
68 SingleAudioRenderBuffer_t(uint32_t nsamples, uint8_t nchannels) : nchannels(nchannels), totalBufferSize(nsamples* nchannels) {
69 data_impl = new float[totalBufferSize] {0};
70 if constexpr (allocateScratchBuffer) {
71 scratch_impl = new float[totalBufferSize] {0};
72 }
73 }
75 if (data_impl) {
76 delete[] data_impl;
77 }
78 if (scratch_impl) {
79 delete[] scratch_impl;
80 }
81 }
82 SingleAudioRenderBuffer_t(SingleAudioRenderBuffer_t&& other) : nchannels(other.nchannels), data_impl(other.data_impl), scratch_impl(other.scratch_impl) {
83 other.data_impl = nullptr;
84 other.scratch_impl = nullptr;
85 }
86 PlanarSampleBufferInlineViewAcessControlled<false> GetWritableDataBufferView() {
87 return { &dataTracker, data_impl, totalBufferSize, GetSizeOneChannel()};
88 }
89 PlanarSampleBufferInlineViewAcessControlled<false> GetWritableScratchBufferView() {
90 return { &scratchTracker, scratch_impl, totalBufferSize, GetSizeOneChannel() };
91 }
92
93 const PlanarSampleBufferInlineViewAcessControlled<true> GetReadonlyDataBufferView() const {
94 return { &dataTracker, data_impl, totalBufferSize, GetSizeOneChannel() };
95 }
96
97 const PlanarSampleBufferInlineViewAcessControlled<true> GetReadonlyScratchBufferView() const {
98 return { &scratchTracker, scratch_impl, totalBufferSize, GetSizeOneChannel() };
99 }
100
101 auto GetSizeOneChannel() const {
102 return totalBufferSize / nchannels;
103 }
104 };
105
108}
Definition AudioTypes.hpp:20
Definition SpinLock.hpp:9
Definition concurrentqueue.h:747
Definition Animation.hpp:6
Definition AudioRenderBuffer.hpp:9
Definition AudioRenderBuffer.hpp:56