|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
-
-
- #ifndef AudioStream_h
- #define AudioStream_h
-
- #ifndef __ASSEMBLER__
- #include <stdio.h> // for NULL
- #include <string.h> // for memcpy
-
- #endif
-
-
-
-
-
-
-
-
-
-
-
- #ifndef AUDIO_BLOCK_SAMPLES
- #define AUDIO_BLOCK_SAMPLES 128
- #endif
-
- #ifndef AUDIO_SAMPLE_RATE_EXACT
- #define AUDIO_SAMPLE_RATE_EXACT 44100.0f
- #endif
-
- #define AUDIO_SAMPLE_RATE AUDIO_SAMPLE_RATE_EXACT
-
- #ifndef __ASSEMBLER__
- class AudioStream;
- class AudioConnection;
-
- typedef struct audio_block_struct {
- uint8_t ref_count;
- uint8_t reserved1;
- uint16_t memory_pool_index;
- int16_t data[AUDIO_BLOCK_SAMPLES];
- } audio_block_t;
-
-
- class AudioConnection
- {
- public:
- AudioConnection(AudioStream &source, AudioStream &destination) :
- src(source), dst(destination), src_index(0), dest_index(0),
- next_dest(NULL)
- { isConnected = false;
- connect(); }
- AudioConnection(AudioStream &source, unsigned char sourceOutput,
- AudioStream &destination, unsigned char destinationInput) :
- src(source), dst(destination),
- src_index(sourceOutput), dest_index(destinationInput),
- next_dest(NULL)
- { isConnected = false;
- connect(); }
- friend class AudioStream;
- ~AudioConnection() {
- disconnect();
- }
- void disconnect(void);
- void connect(void);
- protected:
- AudioStream &src;
- AudioStream &dst;
- unsigned char src_index;
- unsigned char dest_index;
- AudioConnection *next_dest;
- bool isConnected;
- };
-
-
- #define AudioMemory(num) ({ \
- static DMAMEM audio_block_t data[num]; \
- AudioStream::initialize_memory(data, num); \
- })
-
- #define CYCLE_COUNTER_APPROX_PERCENT(n) (((n) + (F_CPU_ACTUAL / 32 / AUDIO_SAMPLE_RATE * AUDIO_BLOCK_SAMPLES / 100)) / (F_CPU_ACTUAL / 16 / AUDIO_SAMPLE_RATE * AUDIO_BLOCK_SAMPLES / 100))
-
- #define AudioProcessorUsage() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total))
- #define AudioProcessorUsageMax() (CYCLE_COUNTER_APPROX_PERCENT(AudioStream::cpu_cycles_total_max))
- #define AudioProcessorUsageMaxReset() (AudioStream::cpu_cycles_total_max = AudioStream::cpu_cycles_total)
- #define AudioMemoryUsage() (AudioStream::memory_used)
- #define AudioMemoryUsageMax() (AudioStream::memory_used_max)
- #define AudioMemoryUsageMaxReset() (AudioStream::memory_used_max = AudioStream::memory_used)
-
- class AudioStream
- {
- public:
- AudioStream(unsigned char ninput, audio_block_t **iqueue) :
- num_inputs(ninput), inputQueue(iqueue) {
- active = false;
- destination_list = NULL;
- for (int i=0; i < num_inputs; i++) {
- inputQueue[i] = NULL;
- }
-
-
- if (first_update == NULL) {
- first_update = this;
- } else {
- AudioStream *p;
- for (p=first_update; p->next_update; p = p->next_update) ;
- p->next_update = this;
- }
- next_update = NULL;
- cpu_cycles = 0;
- cpu_cycles_max = 0;
- numConnections = 0;
- }
- static void initialize_memory(audio_block_t *data, unsigned int num);
- int processorUsage(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles); }
- int processorUsageMax(void) { return CYCLE_COUNTER_APPROX_PERCENT(cpu_cycles_max); }
- void processorUsageMaxReset(void) { cpu_cycles_max = cpu_cycles; }
- bool isActive(void) { return active; }
- uint16_t cpu_cycles;
- uint16_t cpu_cycles_max;
- static uint16_t cpu_cycles_total;
- static uint16_t cpu_cycles_total_max;
- static uint16_t memory_used;
- static uint16_t memory_used_max;
- protected:
- bool active;
- unsigned char num_inputs;
- static audio_block_t * allocate(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);
- audio_block_t * receiveWritable(unsigned int index = 0);
- static bool update_setup(void);
- static void update_stop(void);
- static void update_all(void) { NVIC_SET_PENDING(IRQ_SOFTWARE); }
- friend void software_isr(void);
- friend class AudioConnection;
- uint8_t numConnections;
- private:
- AudioConnection *destination_list;
- audio_block_t **inputQueue;
- static bool update_scheduled;
- virtual void update(void) = 0;
- static AudioStream *first_update;
- AudioStream *next_update;
- static audio_block_t *memory_pool;
- static uint32_t memory_pool_available_mask[];
- static uint16_t memory_pool_first_mask;
- };
-
- #endif
- #endif
|