MCPcopy Create free account
hub / github.com/LabSound/LabSound / AudioArray

Class AudioArray

include/LabSound/core/AudioArray.h:15–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14template <typename T>
15class AudioArray
16{
17 T * _allocation = nullptr;
18 T * _data = nullptr;
19 int _size = 0;
20
21public:
22 AudioArray() = default;
23 explicit AudioArray(int n)
24 : _allocation(nullptr)
25 , _data(nullptr)
26 , _size(-1)
27 {
28 allocate(n);
29 }
30
31 ~AudioArray()
32 {
33 if (_allocation)
34 free(_allocation);
35 }
36
37 // allocation will realloc if necessary.
38 // the buffer will be zeroed whether reallocated or not
39 //
40 void allocate(int n)
41 {
42 const uintptr_t alignment = 0x10;
43 const uintptr_t mask = ~0xf;
44 size_t initialSize = sizeof(T) * n + alignment;
45 if (_size != n) {
46 if (_allocation)
47 free(_allocation);
48
49 if (n) {
50 _allocation = static_cast<T*>(calloc(initialSize, 1));
51 _data = (T*)((((intptr_t)_allocation) + (alignment-1)) & mask);
52 }
53 else {
54 _allocation = nullptr;
55 _data = nullptr;
56 }
57
58 _size = n;
59 }
60 }
61
62 T * data() { return _data; }
63 const T * data() const { return _data; }
64
65 // size in samples, not bytes
66 int size() const { return _size; }
67
68 T & operator[](size_t i) { return data()[i]; }
69
70 void zero()
71 {
72 memset(data(), 0, sizeof(T) * size());

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected