-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
BoxAudioBuffer.cpp
124 lines (107 loc) · 3.77 KB
/
BoxAudioBuffer.cpp
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
#include "BoxAudioBuffer.h"
void BoxAudioBuffer::init(uint8_t* buffer, uint16_t size) {
_bufferStart = buffer;
_bufferEnd = buffer+size-1;
_bufferSize = size;
_readPointer = _bufferStart;
_writePointer = _bufferStart;
_isFull = false;
_isEmpty = true;
}
void BoxAudioBuffer::logState() {
//noIRQ = true;
Log.info("BoxAudioBuffer bufferStart=%X, _bufferEnd=%X, _bufferSize=%X, _readPointer=%X, _writePointer=%X", _bufferStart, _bufferEnd, _bufferSize, _readPointer, _writePointer);
Log.info(" getBytesReadable()=%X, getBytesReadableBlock()=%X, getBytesWritable()=%X, getBytesWritableBlock()=%X", getBytesReadable(), getBytesReadableBlock(), getBytesWritable(), getBytesWritableBlock());
//noIRQ = false;
}
bool BoxAudioBuffer::readPointerEqualsWritePointer() {
return _readPointer == _writePointer;
}
bool BoxAudioBuffer::isEmpty(uint16_t threshold) {
if (getBytesReadable() <= threshold) {
if (threshold == 0) {
return _isEmpty;
}
return true;
}
return false;
}
bool BoxAudioBuffer::isFull(uint16_t threshold) {
if (getBytesWritable() <= threshold) {
if (threshold == 0) {
return _isFull;
}
return true;
}
return false;
}
uint8_t* BoxAudioBuffer::getReadPointer() {
return _readPointer;
}
void BoxAudioBuffer::updateReadPointer(uint16_t readLength) {
uint16_t readableBlock = getBytesReadableBlock();
if (readLength < readableBlock) {
_readPointer += readLength;
} else {
_readPointer = _bufferStart + (readLength - readableBlock);
}
if (readPointerEqualsWritePointer()) {
_isFull = false;
_isEmpty = true;
}
}
void BoxAudioBuffer::write(uint8_t* buffer, uint16_t size) {
noIRQ = true;
uint16_t writableBlock = getBytesWritableBlock();
//Log.info("write *buffer=%X, size=%i, writableBlock=%i", buffer, size, writableBlock);
if (size <= writableBlock) {
//Log.info(" _memcpy(%X, %X, %i);", _writePointer, buffer, size);
memcpy(_writePointer, buffer, size);
_writePointer += size;
if (_writePointer > _bufferEnd)
_writePointer = _bufferStart;
} else {
//Log.info(" _memcpy(%X, %X, %i);", _writePointer, buffer, writableBlock);
memcpy(_writePointer, buffer, writableBlock);
//Log.info(" _memcpy(%X, %X, %i);", _bufferStart, buffer + writableBlock, size - writableBlock);
memcpy(_bufferStart, buffer + writableBlock, size - writableBlock);
_writePointer = _bufferStart + (size - writableBlock);
}
if (readPointerEqualsWritePointer()) {
_isEmpty = false;
_isFull = true;
}
noIRQ = false;
}
uint16_t BoxAudioBuffer::getBytesReadable() {
if (readPointerEqualsWritePointer() && _isEmpty)
return 0;
return _measureDistance(_writePointer, _readPointer);
}
uint16_t BoxAudioBuffer::getBytesReadableBlock() {
if (readPointerEqualsWritePointer() && _isEmpty)
return 0;
return _measureDistanceBlock(_writePointer, _readPointer);
}
uint16_t BoxAudioBuffer::getBytesWritable() {
if (readPointerEqualsWritePointer() && _isFull)
return 0;
return _measureDistance(_readPointer, _writePointer);
}
uint16_t BoxAudioBuffer::getBytesWritableBlock() {
if (readPointerEqualsWritePointer() && _isFull)
return 0;
return _measureDistanceBlock(_readPointer, _writePointer);
}
uint16_t BoxAudioBuffer::_measureDistance(uint8_t* p1, uint8_t* p2) {
int32_t offset = p1 - p2;
if (offset > 0)
return offset;
return (_bufferEnd - p2 + 1) + (p1 - _bufferStart);
}
uint16_t BoxAudioBuffer::_measureDistanceBlock(uint8_t* p1, uint8_t* p2) {
int32_t offset = p1 - p2;
if (offset > 0)
return offset;
return _bufferEnd - p2 + 1;
}