|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
-
-
-
- #include <Arduino.h>
- #include "AudioStream.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;
- 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_max = 0;
- uint16_t AudioStream::memory_used = 0;
- uint16_t AudioStream::memory_used_max = 0;
-
-
-
-
-
-
- void AudioStream::initialize_memory(audio_block_t *data, unsigned int num)
- {
- unsigned int i;
- unsigned int maxnum = MAX_AUDIO_MEMORY / AUDIO_BLOCK_SAMPLES / 2;
-
-
-
- if (num > maxnum) num = maxnum;
- __disable_irq();
- memory_pool = data;
- memory_pool_first_mask = 0;
- for (i=0; i < NUM_MASKS; 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;
- }
- __enable_irq();
-
- }
-
-
-
- audio_block_t * AudioStream::allocate(void)
- {
- uint32_t n, index, avail;
- uint32_t *p, *end;
- audio_block_t *block;
- uint32_t used;
-
- p = memory_pool_available_mask;
- end = p + NUM_MASKS;
- __disable_irq();
- index = memory_pool_first_mask;
- p += index;
- while (1) {
- if (p >= end) {
- __enable_irq();
-
- return NULL;
- }
- avail = *p;
- if (avail) break;
- index++;
- p++;
- }
- n = __builtin_clz(avail);
- avail &= ~(0x80000000 >> n);
- *p = avail;
- if (!avail) index++;
- memory_pool_first_mask = index;
- used = memory_used + 1;
- memory_used = used;
- __enable_irq();
- index = p - memory_pool_available_mask;
- block = memory_pool + ((index << 5) + (31 - n));
- block->ref_count = 1;
- if (used > memory_used_max) memory_used_max = used;
-
-
- return block;
- }
-
-
-
-
- void AudioStream::release(audio_block_t *block)
- {
-
- uint32_t mask = (0x80000000 >> (31 - (block->memory_pool_index & 0x1F)));
- uint32_t index = block->memory_pool_index >> 5;
-
- __disable_irq();
- if (block->ref_count > 1) {
- block->ref_count--;
- } else {
-
-
- memory_pool_available_mask[index] |= mask;
- if (index < memory_pool_first_mask) memory_pool_first_mask = index;
- memory_used--;
- }
- __enable_irq();
- }
-
-
-
-
-
-
-
-
- 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++;
- }
- }
- }
- }
-
-
-
-
- audio_block_t * AudioStream::receiveReadOnly(unsigned int index)
- {
- audio_block_t *in;
-
- if (index >= num_inputs) return NULL;
- in = inputQueue[index];
- inputQueue[index] = NULL;
- return in;
- }
-
-
-
- audio_block_t * AudioStream::receiveWritable(unsigned int index)
- {
- audio_block_t *in, *p;
-
- if (index >= num_inputs) return NULL;
- in = inputQueue[index];
- inputQueue[index] = NULL;
- if (in && in->ref_count > 1) {
- p = allocate();
- if (p) memcpy(p->data, in->data, sizeof(p->data));
- in->ref_count--;
- in = p;
- }
- return in;
- }
-
-
- void AudioConnection::connect(void)
- {
- AudioConnection *p;
-
- if (isConnected) return;
- if (dest_index > dst.num_inputs) return;
- __disable_irq();
- p = src.destination_list;
- if (p == NULL) {
- src.destination_list = this;
- } else {
- while (p->next_dest) {
- if (&p->src == &this->src && &p->dst == &this->dst
- && p->src_index == this->src_index && p->dest_index == this->dest_index) {
-
- __enable_irq();
- return;
- }
- p = p->next_dest;
- }
- p->next_dest = this;
- }
- this->next_dest = NULL;
- src.numConnections++;
- src.active = true;
-
- dst.numConnections++;
- dst.active = true;
-
- isConnected = true;
-
- __enable_irq();
- }
-
- void AudioConnection::disconnect(void)
- {
- AudioConnection *p;
-
- if (!isConnected) return;
- if (dest_index > dst.num_inputs) return;
- __disable_irq();
-
- p = src.destination_list;
- if (p == NULL) {
-
- __enable_irq();
- return;
- } else if (p == this) {
- if (p->next_dest) {
- src.destination_list = next_dest;
- } else {
- src.destination_list = NULL;
- }
- } else {
- while (p) {
- if (p == this) {
- if (p->next_dest) {
- p = next_dest;
- break;
- } else {
- p = NULL;
- break;
- }
- }
- p = p->next_dest;
- }
- }
-
-
- if(dst.inputQueue[dest_index] != NULL) {
- AudioStream::release(dst.inputQueue[dest_index]);
-
- __disable_irq();
- dst.inputQueue[dest_index] = NULL;
- }
-
-
- src.numConnections--;
- if (src.numConnections == 0) {
- src.active = false;
- }
-
- dst.numConnections--;
- if (dst.numConnections == 0) {
- dst.active = false;
- }
-
- isConnected = false;
-
- __enable_irq();
- }
-
-
-
-
-
-
-
- bool AudioStream::update_scheduled = false;
-
- bool AudioStream::update_setup(void)
- {
- if (update_scheduled) return false;
- NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208);
- NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
- update_scheduled = true;
- return true;
- }
-
- void AudioStream::update_stop(void)
- {
- NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
- update_scheduled = false;
- }
-
- AudioStream * AudioStream::first_update = NULL;
-
- void software_isr(void)
- {
- AudioStream *p;
-
- ARM_DEMCR |= ARM_DEMCR_TRCENA;
- ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
- uint32_t totalcycles = ARM_DWT_CYCCNT;
-
- for (p = AudioStream::first_update; p; p = p->next_update) {
- if (p->active) {
- uint32_t cycles = ARM_DWT_CYCCNT;
- p->update();
-
-
- cycles = (ARM_DWT_CYCCNT - cycles) >> 4;
- p->cpu_cycles = cycles;
- if (cycles > p->cpu_cycles_max) p->cpu_cycles_max = cycles;
- }
- }
-
- totalcycles = (ARM_DWT_CYCCNT - totalcycles) >> 4;;
- AudioStream::cpu_cycles_total = totalcycles;
- if (totalcycles > AudioStream::cpu_cycles_total_max)
- AudioStream::cpu_cycles_total_max = totalcycles;
- }
-
|