Skip to content

Commit

Permalink
Fix for issue PaulStoffregen#403 AudioRecordQueue behaves incorrectly…
Browse files Browse the repository at this point in the history
… with no input

record_queue.cpp - don't release() NULL or flag queue entries in ::clear(); allocate and blank a new audio block (if available) in ::readBuffer(), if we have a flag to say NULL (silence)  was received; also keep returning same block until it's discarded by calling ::freeBuffer(); put flag in queue if record is activated by ::begin() but receiving only NULL pointers
record_queue.h - define the "silent data" flag pointer
  • Loading branch information
Jonathan Oakley authored and Jonathan Oakley committed Aug 22, 2021
1 parent 1c5426e commit b935359
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
45 changes: 33 additions & 12 deletions record_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ void AudioRecordQueue::clear(void)
{
uint32_t t;

if (userblock) {
if (NULL != userblock) {
release(userblock);
userblock = NULL;
}
t = tail;
while (t != head) {
if (++t >= max_buffers) t = 0;
release(queue[t]);
if (SILENT_BLOCK_FLAG != queue[t]) // real block?
release(queue[t]);
}
tail = t;
}
Expand All @@ -59,12 +60,24 @@ int16_t * AudioRecordQueue::readBuffer(void)
{
uint32_t t;

if (userblock) return NULL;
t = tail;
if (t == head) return NULL;
if (++t >= max_buffers) t = 0;
userblock = queue[t];
tail = t;
if (NULL == userblock) // no block passed to foreground yet
{
t = tail;
if (t == head) return NULL;
if (++t >= max_buffers) t = 0;
userblock = queue[t];
if (SILENT_BLOCK_FLAG == userblock) // got a NULL block, i.e. silence
{
userblock = allocate(); // allocate a block now, rather than one for every silent entry
if (NULL != userblock) // if we got one...
memset(userblock->data,0,sizeof userblock->data); // ...set it to silent
}
tail = t;
}
// otherwise return data address previously sent,
// rather than NULL, which according to the
// documentation says no data available

return userblock->data;
}

Expand All @@ -81,16 +94,24 @@ void AudioRecordQueue::update(void)
uint32_t h;

block = receiveReadOnly();
if (!block) return;
//if (!block) return;
if (!enabled) {
release(block);
if (NULL != block)
release(block);
return;
}

h = head + 1;
if (h >= max_buffers) h = 0;
if (h == tail) {
release(block);
if (h == tail) { // no room in queue
if (NULL != block)
release(block);
} else {
// No block supplied, but we are active: make a place-holder
if (NULL == block)
block = SILENT_BLOCK_FLAG;

// queue the block or place-holder
queue[h] = block;
head = h;
}
Expand Down
2 changes: 2 additions & 0 deletions record_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "Arduino.h"
#include "AudioStream.h"

#define SILENT_BLOCK_FLAG ((audio_block_t*) 1)

class AudioRecordQueue : public AudioStream
{
private:
Expand Down

0 comments on commit b935359

Please sign in to comment.