forked from bobsayshilol/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudio_buffer.cpp
More file actions
44 lines (35 loc) · 1.01 KB
/
audio_buffer.cpp
File metadata and controls
44 lines (35 loc) · 1.01 KB
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
#include "../include/audio_buffer.h"
#include <assert.h>
AudioBuffer::AudioBuffer() {
m_writePointer = 0;
m_sampleRate = 0;
m_samples = nullptr;
m_bufferSize = 0;
m_offsetToSeconds = 0;
}
AudioBuffer::~AudioBuffer() {
assert(m_samples == nullptr);
}
void AudioBuffer::initialize(int sampleRate, int bufferSize) {
m_writePointer = 0;
m_sampleRate = sampleRate;
m_samples = new int16_t[bufferSize];
memset(m_samples, 0, sizeof(int16_t) * bufferSize);
m_bufferSize = bufferSize;
m_offsetToSeconds = 1 / (double)sampleRate;
}
void AudioBuffer::destroy() {
delete[] m_samples;
m_samples = nullptr;
m_bufferSize = 0;
}
bool AudioBuffer::checkForDiscontinuitiy(int threshold) const {
for (int i = 0; i < m_bufferSize - 1; ++i) {
const int i0 = getBufferIndex(i + m_writePointer);
const int i1 = getBufferIndex(i0 + 1);
if (std::abs(m_samples[i0] - m_samples[i1]) >= threshold) {
return true;
}
}
return false;
}