|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
-
-
- #ifndef effect_delay_h_
- #define effect_delay_h_
- #include "Arduino.h"
- #include "AudioStream.h"
- #include "utility/dspinst.h"
-
- #if defined(__MK66FX1M0__)
-
- #define DELAY_QUEUE_SIZE (106496 / AUDIO_BLOCK_SAMPLES)
- #elif defined(__MK64FX512__)
-
- #define DELAY_QUEUE_SIZE (73728 / AUDIO_BLOCK_SAMPLES)
- #elif defined(__MK20DX256__)
-
- #define DELAY_QUEUE_SIZE (19826 / AUDIO_BLOCK_SAMPLES)
- #else
-
- #define DELAY_QUEUE_SIZE (6144 / AUDIO_BLOCK_SAMPLES)
- #endif
-
- class AudioEffectDelay : public AudioStream
- {
- public:
- AudioEffectDelay() : AudioStream(1, inputQueueArray) {
- activemask = 0;
- headindex = 0;
- tailindex = 0;
- maxblocks = 0;
- memset(queue, 0, sizeof(queue));
- }
- void delay(uint8_t channel, float milliseconds) {
- if (channel >= 8) return;
- if (milliseconds < 0.0) milliseconds = 0.0;
- uint32_t n = (milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0))+0.5;
- uint32_t nmax = AUDIO_BLOCK_SAMPLES * (DELAY_QUEUE_SIZE-1);
- if (n > nmax) n = nmax;
- uint32_t blks = (n + (AUDIO_BLOCK_SAMPLES-1)) / AUDIO_BLOCK_SAMPLES + 1;
- if (!(activemask & (1<<channel))) {
-
- position[channel] = n;
- if (blks > maxblocks) maxblocks = blks;
- activemask |= (1<<channel);
- } else {
- if (n > position[channel]) {
-
- if (blks > maxblocks) maxblocks = blks;
- position[channel] = n;
- } else {
-
- position[channel] = n;
- recompute_maxblocks();
- }
- }
- }
- void disable(uint8_t channel) {
- if (channel >= 8) return;
-
- activemask &= ~(1<<channel);
-
- recompute_maxblocks();
- }
- virtual void update(void);
- private:
- void recompute_maxblocks(void) {
- uint32_t max=0;
- uint32_t channel = 0;
- do {
- if (activemask & (1<<channel)) {
- uint32_t n = position[channel];
- n = (n + (AUDIO_BLOCK_SAMPLES-1)) / AUDIO_BLOCK_SAMPLES + 1;
- if (n > max) max = n;
- }
- } while(++channel < 8);
- maxblocks = max;
- }
- uint8_t activemask;
- uint16_t headindex;
- uint16_t tailindex;
- uint16_t maxblocks;
- #if DELAY_QUEUE_SIZE * AUDIO_BLOCK_SAMPLES < 65535
- uint16_t position[8];
- #else
- uint32_t position[8];
- #endif
- audio_block_t *queue[DELAY_QUEUE_SIZE];
- audio_block_t *inputQueueArray[1];
- };
-
- #endif
|