Repository navigation
Expand file tree
/
Copy pathaudio_buffer.hh
More file actions
146 lines (129 loc) · 4.36 KB
/
Copy pathaudio_buffer.hh
File metadata and controls
146 lines (129 loc) · 4.36 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2015 Matthias Puech.
//
// Author: Matthias Puech (matthias.puech@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
// Simple ring buffer with multiple taps
#ifndef AUDIO_BUFFER_H_
#define AUDIO_BUFFER_H_
#include "stmlib/stmlib.h"
#include "stmlib/dsp/dsp.h"
#include <algorithm>
class AudioBuffer
{
public:
void Init(short* buffer, uint32_t buffer_size) {
buffer_ = buffer;
buffer_size_ = buffer_size;
}
void Clear() {
std::fill(buffer_, buffer_ + buffer_size_, 0);
}
uint32_t size() { return buffer_size_; }
/* Write one value at cursor and increment it */
inline void Write(short value) {
buffer_[cursor_] = value;
if (cursor_ == buffer_size_) {
cursor_ = 0;
} else {
cursor_++;
}
}
/* Write [size] values at cursor and increment it. */
inline void Write(short *src, size_t size) {
size_t written = size;
if (cursor_ > buffer_size_ - size) {
written = buffer_size_ - cursor_;
}
std::copy(src, src + written, buffer_ + cursor_);
if (written < size) {
cursor_ = size - written;
std::copy(src + written, src + size, buffer_);
} else {
cursor_ += size;
}
}
/* Reads the value from [pos] writes ago */
inline short ReadShort(uint32_t pos) {
uint32_t index;// = cursor_ - pos;
if (cursor_ < pos) {
index = buffer_size_ - pos + cursor_;
} else {
index = cursor_ - pos;
}
return buffer_[index];
}
/* Assumes that buffer_size_ is 2^n */
inline float ReadLinear(float pos) {
MAKE_INTEGRAL_FRACTIONAL(pos);
int32_t x = cursor_ - pos_integral;
float a = buffer_[x & (buffer_size_-1)];
float b = buffer_[(x - 1) & (buffer_size_-1)];
return (a + (b - a) * pos_fractional) / 32768.0f;
}
/* Assumes that buffer_size_ is 2^n */
inline float ReadHermite(float pos) {
MAKE_INTEGRAL_FRACTIONAL(pos);
int32_t x = cursor_ - pos_integral;
float xm1 = buffer_[x & (buffer_size_-1)];
float x0 = buffer_[(x - 1) & (buffer_size_-1)];
float x1 = buffer_[(x - 2) & (buffer_size_-1)];
float x2 = buffer_[(x - 3) & (buffer_size_-1)];
float c = (x1 - xm1) * 0.5f;
float v = x0 - x1;
float w = c + v;
float a = w + v + (x2 - x0) * 0.5f;
float b_neg = w + a;
float t = pos_fractional;
return ((((a * t) - b_neg) * t + c) * t + x0) / 32768.0f;
}
/* Assumes that buffer_size_ is 2^n */
inline float Read(float pos) {
int32_t pos_integral = static_cast<uint32_t>(pos); \
int32_t x = cursor_ - pos_integral;
float a = buffer_[x & (buffer_size_-1)];
return a / 32768.0f;
}
/* Reads the [size] values until [pos] writes ago.
* assert (pos + size < buffer_size_) */
inline void Read(short* dest, uint32_t pos, size_t size) {
uint32_t index;
size_t read = size;
if (cursor_ < pos + size) {
index = buffer_size_ - pos - size + cursor_;
if (index > buffer_size_ - size) {
read = buffer_size_ - index;
std::copy(buffer_, buffer_ + size - read, dest + read);
}
} else {
index = cursor_ - pos - size;
}
std::copy(buffer_ + index, buffer_ + index + read, dest);
}
private:
short* buffer_;
uint32_t cursor_;
uint32_t buffer_size_;
};
#endif