Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an audio block pre-filled with silence #746

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions teensy3/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)

audio_block_t * AudioStream::memory_pool;
static const audio_block_t silent_block = {0,0,0,{0}};
uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
uint16_t AudioStream::memory_pool_first_mask;

Expand Down Expand Up @@ -136,12 +137,25 @@ audio_block_t * AudioStream::allocate(void)
return block;
}


// "Allocate" the silent block. The caller doesn't
// actually own it, and must NOT write to it! However,
// it may be transmitted and released as usual.
audio_block_t* AudioStream::allocate_silent(void)
{
return (audio_block_t*) &silent_block;
}


// Release ownership of a data block. If no
// other streams have ownership, the block is
// returned to the free pool
// returned to the free pool.
//
// An attempt to release a nullptr or the silent
// block is ignored.
void AudioStream::release(audio_block_t *block)
{
//if (block == NULL) return;
if (nullptr == block || &silent_block == block) return;
uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
uint32_t index = block->memory_pool_index >> 5;

Expand Down Expand Up @@ -169,9 +183,15 @@ void AudioStream::transmit(audio_block_t *block, unsigned char index)
{
for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
if (c->src_index == index) {
if (c->dst->inputQueue[c->dest_index] == NULL) {
c->dst->inputQueue[c->dest_index] = block;
block->ref_count++;
if (c->dst->inputQueue[c->dest_index] == NULL)
{
if (&silent_block != block) // it's a real block
{
c->dst->inputQueue[c->dest_index] = block;
block->ref_count++; // only real blocks have reference counts
}
// if we "transmit" the silent block, then leave
// the inputQueue entry as nullptr, which means "silence"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions teensy3/AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class AudioStream
bool active;
unsigned char num_inputs;
static audio_block_t * allocate(void);
static audio_block_t* allocate_silent(void);
static void release(audio_block_t * block);
void transmit(audio_block_t *block, unsigned char index = 0);
audio_block_t * receiveReadOnly(unsigned int index = 0);
Expand Down
31 changes: 26 additions & 5 deletions teensy4/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)

audio_block_t * AudioStream::memory_pool;
PROGMEM static const audio_block_t silent_block = {0,0,0,{0}};
extern "C" { audio_block_t* silent_block_ptr = (audio_block_t*) &silent_block; }
uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
uint16_t AudioStream::memory_pool_first_mask;

Expand Down Expand Up @@ -128,12 +130,25 @@ audio_block_t * AudioStream::allocate(void)
return block;
}


// "Allocate" the silent block. The caller doesn't
// actually own it, and must NOT write to it! However,
// it may be transmitted and released as usual.
audio_block_t* AudioStream::allocate_silent(void)
{
return (audio_block_t*) &silent_block;
}


// Release ownership of a data block. If no
// other streams have ownership, the block is
// returned to the free pool
// returned to the free pool.
//
// An attempt to release a nullptr or the silent
// block is ignored.
void AudioStream::release(audio_block_t *block)
{
//if (block == NULL) return;
if (nullptr == block || &silent_block == block) return;
uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
uint32_t index = block->memory_pool_index >> 5;

Expand Down Expand Up @@ -161,9 +176,15 @@ void AudioStream::transmit(audio_block_t *block, unsigned char index)
{
for (AudioConnection *c = destination_list; c != NULL; c = c->next_dest) {
if (c->src_index == index) {
if (c->dst->inputQueue[c->dest_index] == NULL) {
c->dst->inputQueue[c->dest_index] = block;
block->ref_count++;
if (c->dst->inputQueue[c->dest_index] == NULL)
{
if (&silent_block != block) // it's a real block
{
c->dst->inputQueue[c->dest_index] = block;
block->ref_count++; // only real blocks have reference counts
}
// if we "transmit" the silent block, then leave
// the inputQueue entry as nullptr, which means "silence"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions teensy4/AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class AudioStream
bool active;
unsigned char num_inputs;
static audio_block_t * allocate(void);
static audio_block_t* allocate_silent(void);
static void release(audio_block_t * block);
void transmit(audio_block_t *block, unsigned char index = 0);
audio_block_t * receiveReadOnly(unsigned int index = 0);
Expand Down