10template <
typename SampleType>
15 m_numChannels (numChannels),
16 m_numFrames (numFrames),
17 m_frames (m_numChannels * m_numFrames),
18 m_channelPointers (m_numChannels)
20 updateChannelPointers();
24 m_numChannels (other.m_numChannels),
25 m_numFrames (other.m_numFrames),
26 m_frames (other.m_frames),
27 m_channelPointers (m_numChannels)
29 updateChannelPointers();
33 m_numChannels (other.m_numChannels),
34 m_numFrames (other.m_numFrames),
35 m_frames (std::move (other.m_frames)),
36 m_channelPointers (std::move (other.m_channelPointers))
48 if (m_numChannels != other.m_numChannels)
51 m_numFrames = other.m_numFrames;
52 m_frames = other.m_frames;
53 m_channelPointers.resize (m_numChannels);
54 updateChannelPointers();
64 if (m_numChannels != other.m_numChannels)
67 m_numFrames = other.m_numFrames;
68 m_frames = std::move (other.m_frames);
69 m_channelPointers = std::move (other.m_channelPointers);
77 return static_cast<
const SampleType*
const*> (m_channelPointers.data());
82 return m_channelPointers.data();
87 return AudioBuffer (other.getNumChannels(), other.getNumFrames());
95 return m_channelPointers[channel];
98 const SampleType*
operator[] (size_t channel)
const
100 return m_channelPointers[channel];
105 if (otherBuffer.getNumChannels() != m_numChannels)
108 const size_t thisNumFrames = m_numFrames;
109 const size_t otherNumFrames = otherBuffer.getNumFrames();
111 std::vector<SampleType> combinedFrames (m_numChannels * (thisNumFrames + otherNumFrames));
113 for (size_t channel = 0; channel < m_numChannels; ++channel)
115 SampleType* newChannelStart = &combinedFrames[channel * (thisNumFrames + otherNumFrames)];
116 std::copy (m_channelPointers[channel], m_channelPointers[channel] + thisNumFrames, newChannelStart);
117 std::copy (otherBuffer[channel], otherBuffer[channel] + otherNumFrames, newChannelStart + thisNumFrames);
120 m_frames = std::move (combinedFrames);
121 m_numFrames += otherNumFrames;
123 updateChannelPointers();
132 if (m_channelPointers.size() != m_numChannels)
133 m_channelPointers.resize (m_numChannels);
135 updateChannelPointers();
138 SampleType
getMagnitude (size_t channel, size_t startFrame, size_t numFrames)
const
140 if (channel >= m_numChannels)
143 if (startFrame + numFrames > m_numFrames || numFrames == 0)
146 const SampleType* start = m_channelPointers[channel] + startFrame;
147 const SampleType* peakSample = std::max_element (
150 [] (SampleType a, SampleType b) {
return std::abs (a) < std::abs (b); }
153 return std::abs (*peakSample);
158 if (startFrame + numFrames > m_numFrames || numFrames == 0)
161 SampleType peakSampleAcrossAllChannels = (SampleType) 0;
163 for (size_t channel = 0; channel < m_numChannels; ++channel)
165 const SampleType* start = m_channelPointers[channel] + startFrame;
166 const SampleType* peakSample = std::max_element (
169 [] (SampleType a, SampleType b) {
return std::abs (a) < std::abs (b); }
171 peakSampleAcrossAllChannels = std::max (peakSampleAcrossAllChannels, std::abs (*peakSample));
174 return peakSampleAcrossAllChannels;
180 const size_t m_numChannels = 0;
181 size_t m_numFrames = 0;
182 std::vector<SampleType> m_frames;
183 std::vector<SampleType*> m_channelPointers;
185 void updateChannelPointers()
187 for (size_t channel = 0; channel < m_numChannels; ++channel)
188 m_channelPointers[channel] = m_numFrames > 0 ? &m_frames[channel * m_numFrames] :
nullptr;
SampleType * operator[](size_t channel)
static AudioBuffer emptyLike(const AudioBuffer &other)
AudioBuffer & operator=(AudioBuffer &&other)
size_t getNumFrames() const
AudioBuffer & operator=(const AudioBuffer &other)
void appendFrom(const AudioBuffer< SampleType > &otherBuffer)
const SampleType *const * getArrayOfReadPointers() const
SampleType getMagnitude(size_t startFrame, size_t numFrames) const
SampleType *const * getArrayOfWritePointers()
AudioBuffer(const AudioBuffer &other)
const SampleType * operator[](size_t channel) const
AudioBuffer(AudioBuffer &&other)
SampleType getMagnitude(size_t channel, size_t startFrame, size_t numFrames) const
size_t getNumChannels() const
AudioBuffer(size_t numChannels=0, size_t numFrames=0)
#define HART_THROW_OR_RETURN_VOID(ExceptionType, message)
#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue)