Audacity 3.2.0
SamplesFloat.h
Go to the documentation of this file.
1#pragma once
2
3#include <complex>
4#include <vector>
5
6#include "SimdTypes.h"
7#include "VectorOps.h"
8
9namespace staffpad {
10
11template <typename T = float>
13{
14public:
16 {
17 for (int ch = 0; ch < num_channels; ch++)
18 dealloc(ch);
19 }
20
21 void setSize(int32_t numChannels, int32_t samples)
22 {
23 for (int ch = 0; ch < num_channels; ch++)
24 dealloc(ch);
25
26 num_channels = numChannels;
27 num_samples = samples;
28 data.resize(num_channels);
29 for (int ch = 0; ch < num_channels; ch++)
30 alloc(ch, num_samples);
31 }
32
33 int32_t getNumChannels() const
34 {
35 return num_channels;
36 }
37
38 int32_t getNumSamples() const
39 {
40 return num_samples;
41 }
42
43 float** getPtrs()
44 {
45 return data.data();
46 }
47
48 T* getPtr(int32_t channel)
49 {
50 assert(channel < num_channels);
51 assert(data[channel]);
52 return data[channel];
53 }
54
55 const T* getPtr(int32_t channel) const
56 {
57 assert(channel < num_channels);
58 assert(data[channel]);
59 return data[channel];
60 }
61
62 void assignSamples(int32_t channel, const T* input)
63 {
64 assert(channel < num_channels);
65 assert(data[channel]);
66 vo::copy(input, data[channel], num_samples);
67 }
68
69 void assignSamples(const SamplesFloat& rhs)
70 {
71 assert(num_channels == rhs.num_channels);
72 assert(num_samples == rhs.num_samples);
73 for (int ch = 0; ch < num_channels; ch++)
74 {
75 assert(data[ch]);
76 vo::copy(rhs.getPtr(ch), getPtr(ch), num_samples);
77 }
78 }
79
80 void zeroOut()
81 {
82 for (int ch = 0; ch < num_channels; ch++)
84 }
85
86private:
87 int32_t num_channels{0};
88 int32_t num_samples{0};
89 std::vector<T*> data;
90
91 void alloc(int32_t channel, int32_t samples)
92 {
93 assert(channel < num_channels);
94 if (data[channel])
95 {
96 dealloc(channel);
97 }
98 data[channel] = (T*)audio::simd::aligned_malloc(samples * sizeof(T), 64);
99 }
100
101 void dealloc(int32_t channel)
102 {
103 assert(channel < num_channels);
104 if (data[channel])
105 {
107 data[channel] = nullptr;
108 }
109 }
110};
111
114
115} // namespace staffpad
void alloc(int32_t channel, int32_t samples)
Definition: SamplesFloat.h:91
std::vector< T * > data
Definition: SamplesFloat.h:89
void setSize(int32_t numChannels, int32_t samples)
Definition: SamplesFloat.h:21
void dealloc(int32_t channel)
Definition: SamplesFloat.h:101
void assignSamples(const SamplesFloat &rhs)
Definition: SamplesFloat.h:69
int32_t getNumSamples() const
Definition: SamplesFloat.h:38
void assignSamples(int32_t channel, const T *input)
Definition: SamplesFloat.h:62
T * getPtr(int32_t channel)
Definition: SamplesFloat.h:48
const T * getPtr(int32_t channel) const
Definition: SamplesFloat.h:55
int32_t getNumChannels() const
Definition: SamplesFloat.h:33
void aligned_free(void *p)
free memory allocated with aligned_malloc
Definition: SimdTypes.h:43
void * aligned_malloc(size_t required_bytes, size_t alignment)
reserve aligned memory. Needs to be freed with aligned_free()
Definition: SimdTypes.h:29
void setToZero(T *dst, int32_t n)
Definition: VectorOps.h:81
void copy(const T *src, T *dst, int32_t n)
Definition: VectorOps.h:40
SamplesFloat< float > SamplesReal
Definition: SamplesFloat.h:112
SamplesFloat< std::complex< float > > SamplesComplex
Definition: SamplesFloat.h:113