Browse Source

Scale audio memory management with chip & block size

main
PaulStoffregen 7 years ago
parent
commit
2b616722cc
2 changed files with 49 additions and 24 deletions
  1. +43
    -18
      teensy3/AudioStream.cpp
  2. +6
    -6
      teensy3/AudioStream.h

+ 43
- 18
teensy3/AudioStream.cpp View File



#include <string.h> // for memcpy #include <string.h> // for memcpy
#include "AudioStream.h" #include "AudioStream.h"

#include <Arduino.h>

#if defined(__MKL26Z64__)
#define MAX_AUDIO_MEMORY 6144
#elif defined(__MK20DX128__)
#define MAX_AUDIO_MEMORY 12288
#elif defined(__MK20DX256__)
#define MAX_AUDIO_MEMORY 49152
#elif defined(__MK64FX512__)
#define MAX_AUDIO_MEMORY 163840
#elif defined(__MK66FX1M0__)
#define MAX_AUDIO_MEMORY 229376
#endif

#define NUM_MASKS (((MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2) + 31) / 32)


audio_block_t * AudioStream::memory_pool; audio_block_t * AudioStream::memory_pool;
uint32_t AudioStream::memory_pool_available_mask[6];
uint32_t AudioStream::memory_pool_available_mask[NUM_MASKS];
uint16_t AudioStream::memory_pool_first_mask;


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;







// Set up the pool of audio data blocks // Set up the pool of audio data blocks
// 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; unsigned int i;
unsigned int maxnum = MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2;


//Serial.println("AudioStream initialize_memory"); //Serial.println("AudioStream initialize_memory");
//delay(10); //delay(10);
if (num > 192) num = 192;
if (num > maxnum) num = maxnum;
__disable_irq(); __disable_irq();
memory_pool = data; memory_pool = data;
for (i=0; i < 6; i++) {
memory_pool_first_mask = 0;
for (i=0; i < NUM_MASKS; i++) {
memory_pool_available_mask[i] = 0; memory_pool_available_mask[i] = 0;
} }
for (i=0; i < num; i++) { for (i=0; i < num; i++) {
audio_block_t * AudioStream::allocate(void) audio_block_t * AudioStream::allocate(void)
{ {
uint32_t n, index, avail; uint32_t n, index, avail;
uint32_t *p;
uint32_t *p, *end;
audio_block_t *block; audio_block_t *block;
uint8_t used; uint8_t used;


p = memory_pool_available_mask; p = memory_pool_available_mask;
end = p + NUM_MASKS;
__disable_irq(); __disable_irq();
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();
//Serial.println("alloc:null");
return NULL;
} while (0);
index = memory_pool_first_mask;
p += index;
while (1) {
if (p >= end) {
__enable_irq();
//Serial.println("alloc:null");
return NULL;
}
avail = *p;
if (avail) break;
index++;
p++;
}
n = __builtin_clz(avail); n = __builtin_clz(avail);
*p = avail & ~(0x80000000 >> n);
avail &= ~(0x80000000 >> n);
*p = avail;
if (!avail) index++;
memory_pool_first_mask = index;
used = memory_used + 1; used = memory_used + 1;
memory_used = used; memory_used = used;
__enable_irq(); __enable_irq();
// returned to the free pool // returned to the free pool
void AudioStream::release(audio_block_t *block) void AudioStream::release(audio_block_t *block)
{ {
//if (block == NULL) return;
uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F))); uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
uint32_t index = block->memory_pool_index >> 5; 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--;
//Serial.print("reles:"); //Serial.print("reles:");
//Serial.println((uint32_t)block, HEX); //Serial.println((uint32_t)block, HEX);
memory_pool_available_mask[index] |= mask; memory_pool_available_mask[index] |= mask;
if (index < memory_pool_first_mask) memory_pool_first_mask = index;
memory_used--; memory_used--;
} }
__enable_irq(); __enable_irq();

+ 6
- 6
teensy3/AudioStream.h View File

class AudioConnection; class AudioConnection;


typedef struct audio_block_struct { typedef struct audio_block_struct {
unsigned char ref_count;
unsigned char memory_pool_index;
unsigned char reserved1;
unsigned char reserved2;
int16_t data[AUDIO_BLOCK_SAMPLES];
uint8_t ref_count;
uint8_t reserved1;
uint16_t memory_pool_index;
int16_t data[AUDIO_BLOCK_SAMPLES];
} audio_block_t; } audio_block_t;




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 uint32_t memory_pool_available_mask[6];
static uint32_t memory_pool_available_mask[];
static uint16_t memory_pool_first_mask;
}; };


#endif #endif

Loading…
Cancel
Save