Browse Source

Increase AudioStream memory capacity

teensy4-core
PaulStoffregen 10 years ago
parent
commit
b80d569f92
2 changed files with 42 additions and 18 deletions
  1. +40
    -15
      teensy3/AudioStream.cpp
  2. +2
    -3
      teensy3/AudioStream.h

+ 40
- 15
teensy3/AudioStream.cpp View File





audio_block_t * AudioStream::memory_pool; audio_block_t * AudioStream::memory_pool;
uint8_t AudioStream::memory_pool_size = 0;
uint32_t AudioStream::memory_pool_available_mask;
uint32_t AudioStream::memory_pool_available_mask[6];


uint16_t AudioStream::cpu_cycles_total = 0; uint16_t AudioStream::cpu_cycles_total = 0;
uint16_t AudioStream::cpu_cycles_total_max = 0; uint16_t AudioStream::cpu_cycles_total_max = 0;
// placing them all onto the free list // placing them all onto the free list
void AudioStream::initialize_memory(audio_block_t *data, unsigned int num) void AudioStream::initialize_memory(audio_block_t *data, unsigned int num)
{ {
unsigned int i;

//Serial.println("AudioStream initialize_memory"); //Serial.println("AudioStream initialize_memory");
//delay(10);
if (num > 192) num = 192;
__disable_irq();
memory_pool = data; memory_pool = data;
if (num > 31) num = 31;
memory_pool_size = num;
memory_pool_available_mask = 0xFFFFFFFF;
for (unsigned int i=0; i < num; i++) {
for (i=0; i < 6; i++) {
memory_pool_available_mask[i] = 0;
}
for (i=0; i < num; i++) {
memory_pool_available_mask[i >> 5] |= (1 << (i & 0x1F));
}
for (i=0; i < num; i++) {
data[i].memory_pool_index = i; data[i].memory_pool_index = i;
} }
__enable_irq();

} }


// Allocate 1 audio data block. If successful // Allocate 1 audio data block. If successful
// the caller is the only owner of this new block // the caller is the only owner of this new block
audio_block_t * AudioStream::allocate(void) audio_block_t * AudioStream::allocate(void)
{ {
uint32_t n, avail;
uint32_t n, index, avail;
uint32_t *p;
audio_block_t *block; audio_block_t *block;
uint8_t used; uint8_t used;


p = memory_pool_available_mask;
__disable_irq(); __disable_irq();
avail = memory_pool_available_mask;
n = __builtin_clz(avail);
if (n >= memory_pool_size) {
do {
avail = *p; if (avail) break;
p++; avail = *p; if (avail) break;
p++; avail = *p; if (avail) break;
p++; avail = *p; if (avail) break;
p++; avail = *p; if (avail) break;
p++; avail = *p; if (avail) break;
__enable_irq(); __enable_irq();
//Serial.println("alloc:null");
return NULL; return NULL;
}
memory_pool_available_mask = avail & ~(0x80000000 >> n);
} while (0);
n = __builtin_clz(avail);
*p = avail & ~(0x80000000 >> n);
used = memory_used + 1; used = memory_used + 1;
memory_used = used; memory_used = used;
__enable_irq(); __enable_irq();
block = memory_pool + n;
index = p - memory_pool_available_mask;
block = memory_pool + ((index << 5) + (31 - n));
block->ref_count = 1; block->ref_count = 1;
if (used > memory_used_max) memory_used_max = used; if (used > memory_used_max) memory_used_max = used;
//Serial.print("alloc:");
//Serial.println((uint32_t)block, HEX);
return block; return block;
} }


// returned to the free pool // returned to the free pool
void AudioStream::release(audio_block_t *block) void AudioStream::release(audio_block_t *block)
{ {
uint32_t mask = (0x80000000 >> block->memory_pool_index);
uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
uint32_t index = block->memory_pool_index >> 5;


__disable_irq(); __disable_irq();
if (block->ref_count > 1) { if (block->ref_count > 1) {
block->ref_count--; block->ref_count--;
} else { } else {
memory_pool_available_mask |= mask;
//Serial.print("reles:");
//Serial.println((uint32_t)block, HEX);
memory_pool_available_mask[index] |= mask;
memory_used--; memory_used--;
} }
__enable_irq(); __enable_irq();

+ 2
- 3
teensy3/AudioStream.h View File

#include "Arduino.h" #include "Arduino.h"


#define AUDIO_BLOCK_SAMPLES 128 #define AUDIO_BLOCK_SAMPLES 128
#define AUDIO_SAMPLE_RATE 44100
#define AUDIO_SAMPLE_RATE 44117.64706
#define AUDIO_SAMPLE_RATE_EXACT 44117.64706 // 48 MHz / 1088, or 96 MHz * 2 / 17 / 256 #define AUDIO_SAMPLE_RATE_EXACT 44117.64706 // 48 MHz / 1088, or 96 MHz * 2 / 17 / 256


class AudioStream; class AudioStream;
static AudioStream *first_update; // for update_all static AudioStream *first_update; // for update_all
AudioStream *next_update; // for update_all AudioStream *next_update; // for update_all
static audio_block_t *memory_pool; static audio_block_t *memory_pool;
static uint8_t memory_pool_size;
static uint32_t memory_pool_available_mask;
static uint32_t memory_pool_available_mask[6];
}; };


#endif #endif

Loading…
Cancel
Save