2
2
3
3
void BoxAudioBuffer::init (uint8_t * buffer, uint16_t size) {
4
4
_bufferStart = buffer;
5
- _bufferEnd = buffer+size;
5
+ _bufferEnd = buffer+size- 1 ;
6
6
_bufferSize = size;
7
7
8
8
_readPointer = _bufferStart;
9
9
_writePointer = _bufferStart;
10
10
11
- Log.info (" BoxAudioBuffer init bufferStart=%X, _bufferEnd=%X, _bufferSize=%X, _readPointer=%X, _writePointer=%X" , _bufferStart, _bufferEnd, _bufferSize, _readPointer, _writePointer);
11
+ _isFull = false ;
12
+ _isEmpty = true ;
13
+ }
14
+
15
+ void BoxAudioBuffer::logState () {
16
+ Log.info (" BoxAudioBuffer bufferStart=%X, _bufferEnd=%X, _bufferSize=%X, _readPointer=%X, _writePointer=%X" , _bufferStart, _bufferEnd, _bufferSize, _readPointer, _writePointer);
12
17
Log.info (" getBytesReadable()=%X, getBytesReadableBlock()=%X, getBytesWritable()=%X, getBytesWritableBlock()=%X" , getBytesReadable (), getBytesReadableBlock (), getBytesWritable (), getBytesWritableBlock ());
13
18
}
14
19
@@ -17,10 +22,22 @@ bool BoxAudioBuffer::readPointerEqualsWritePointer() {
17
22
}
18
23
19
24
bool BoxAudioBuffer::isEmpty (uint16_t threshold) {
20
- return (getBytesReadable () <= threshold);
25
+ if (getBytesReadable () <= threshold) {
26
+ if (threshold == 0 ) {
27
+ return _isEmpty;
28
+ }
29
+ return true ;
30
+ }
31
+ return false ;
21
32
}
22
33
bool BoxAudioBuffer::isFull (uint16_t threshold) {
23
- return (getBytesWritable () <= threshold);
34
+ if (getBytesWritable () <= threshold) {
35
+ if (threshold == 0 ) {
36
+ return _isFull;
37
+ }
38
+ return true ;
39
+ }
40
+ return false ;
24
41
}
25
42
26
43
uint8_t * BoxAudioBuffer::getReadPointer () {
@@ -31,7 +48,11 @@ void BoxAudioBuffer::updateReadPointer(uint16_t readLength) {
31
48
if (readLength < readableBlock) {
32
49
_readPointer += readLength;
33
50
} else {
34
- _readPointer = _bufferStart + (readLength - readableBlock - 1 );
51
+ _readPointer = _bufferStart + (readLength - readableBlock);
52
+ }
53
+ if (readPointerEqualsWritePointer ()) {
54
+ _isFull = false ;
55
+ _isEmpty = true ;
35
56
}
36
57
}
37
58
@@ -53,36 +74,48 @@ void BoxAudioBuffer::write(uint8_t* buffer, uint16_t size) {
53
74
memcpy (_bufferStart, buffer + writableBlock, size - writableBlock);
54
75
_writePointer = _bufferStart + (size - writableBlock);
55
76
}
77
+ if (readPointerEqualsWritePointer ()) {
78
+ _isEmpty = false ;
79
+ _isFull = true ;
80
+ }
56
81
isWriting = false ;
57
82
}
58
83
59
84
60
85
uint16_t BoxAudioBuffer::getBytesReadable () {
86
+ if (readPointerEqualsWritePointer () && _isEmpty)
87
+ return 0 ;
61
88
return _measureDistance (_writePointer, _readPointer);
62
89
}
63
90
uint16_t BoxAudioBuffer::getBytesReadableBlock () {
91
+ if (readPointerEqualsWritePointer () && _isEmpty)
92
+ return 0 ;
64
93
return _measureDistanceBlock (_writePointer, _readPointer);
65
94
}
66
95
67
96
uint16_t BoxAudioBuffer::getBytesWritable () {
97
+ if (readPointerEqualsWritePointer () && _isFull)
98
+ return 0 ;
68
99
return _measureDistance (_readPointer, _writePointer);
69
100
}
70
101
uint16_t BoxAudioBuffer::getBytesWritableBlock () {
102
+ if (readPointerEqualsWritePointer () && _isFull)
103
+ return 0 ;
71
104
return _measureDistanceBlock (_readPointer, _writePointer);
72
105
}
73
106
74
107
uint16_t BoxAudioBuffer::_measureDistance (uint8_t * p1, uint8_t * p2) {
75
108
int32_t offset = p1 - p2;
76
109
77
- if (offset >= 0 )
110
+ if (offset > 0 )
78
111
return offset;
79
112
80
113
return (_bufferEnd - p2 + 1 ) + (p1 - _bufferStart);
81
114
}
82
115
uint16_t BoxAudioBuffer::_measureDistanceBlock (uint8_t * p1, uint8_t * p2) {
83
116
int32_t offset = p1 - p2;
84
117
85
- if (offset >= 0 )
118
+ if (offset > 0 )
86
119
return offset;
87
120
88
121
return _bufferEnd - p2 + 1 ;
0 commit comments