-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavetree.cc
More file actions
171 lines (133 loc) · 4.7 KB
/
Copy pathwavetree.cc
File metadata and controls
171 lines (133 loc) · 4.7 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "wavetree.h"
#include <assert.h>
#include <iostream>
using namespace std;
class WaveTreeNode {
protected:
// The position of the wave node relative to the parent measured in
// samples.
int pos;
// The number of samples covered by the node.
int size;
WaveTreeNode(int pos, int size) : pos(pos), size(size) {}
public:
virtual ~WaveTreeNode() {}
// Returns the wave buffer at the given position or null if that
// position is empty, unless "create" is true, in which case a new
// buffer will be created at the position if none exists.
virtual WaveBuf *get(int pos, bool create = false) = 0;
// Make a new root node. This should be called only on an existing
// root node. It converts the position of the receiver to be
// relative to the new parent.
WaveTreeNode *makeNewRoot();
int getPos() const { return pos; }
int getSize() const { return size; }
};
namespace {
const int slotsPerNode = 10;
// Number of frames in an input buffer. Should be set by
int framesPerBuffer = 1024;
class WaveTreeLeaf : public WaveTreeNode {
private:
WaveBuf *buf;
public:
WaveTreeLeaf(int pos, int size) :
WaveTreeNode(pos, size),
buf(new WaveBuf(size * 2)) {
}
virtual ~WaveTreeLeaf() {
delete buf;
}
virtual WaveBuf *get(int pos, bool create) {
if (pos != this->pos) {
cerr << "leaf reference is not the leaf pos. Leaf pos = " <<
this->pos << " reference is " << pos << endl;
return 0;
}
return buf;
}
};
class WaveTreeInner : public WaveTreeNode {
public:
// We're making children "public" so it can be assigned from
// WaveTreeNode.
WaveTreeNode *children[slotsPerNode];
WaveTreeInner(int pos, int size) : WaveTreeNode(pos, size) {
for (WaveTreeNode *&child : children)
child = 0;
}
~WaveTreeInner() {
for (WaveTreeNode *child : children)
delete child;
}
virtual WaveBuf *get(int pos, bool create) {
int index = (pos - this->pos) / (size / slotsPerNode);
if (index < 0 || index > slotsPerNode) {
cerr << "slot reference out of range: " << index << endl;
return 0;
}
WaveTreeNode *child = children[index];
if (!child) {
int childSize = size / 10;
if (create) {
// Allocate either an inner node or a leaf depending on
// size/10 is the size of a single buffer.
int childPos = index * childSize;
children[index] = child =
(childSize == framesPerBuffer) ?
static_cast<WaveTreeNode *>(
new WaveTreeLeaf(childPos, childSize)
) :
static_cast<WaveTreeNode *>(
new WaveTreeInner(index * childSize, childSize)
);
} else {
return 0;
}
}
return child->get(pos - this->pos, create);
}
};
} // anonymous namespace
WaveTreeNode *WaveTreeNode::makeNewRoot() {
// create a new inner node.
int parentSize = size * 10;
int newRelPos = pos % parentSize;
int newRootPos = pos - newRelPos;
assert(newRootPos >= 0);
// Create a new root node with an absolute position. Add this
// node as its child.
WaveTreeInner *newRoot =
new WaveTreeInner(newRootPos, parentSize);
newRoot->children[newRelPos / size] = this;
// Convert the position to relative to the new parent.
pos = newRelPos;
return newRoot;
}
WaveBuf *WaveTree::get(int pos, bool create) {
if (!root) {
if (create)
root = new WaveTreeLeaf(pos, framesPerBuffer);
else
return 0;
} else {
int rootPos = root->getPos();
// If the position is out of range, continue to create intermediate
// nodes until we have one big enough to hold both the new child and
// the existing one.
while (pos < rootPos || pos >= rootPos + root->getSize()) {
root = root->makeNewRoot();
rootPos = root->getPos();
}
}
return root->get(pos, create);
}
WaveTree::~WaveTree() {
delete root;
}
void WaveTree::setBufferSize(int nframes) {
framesPerBuffer = nframes;
}
int WaveTree::getBufferSize() {
return framesPerBuffer;
}