-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAudioEffectDelay_OA_F32.h
152 lines (146 loc) · 6.1 KB
/
AudioEffectDelay_OA_F32.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* AudioEffectDelay_OA_F32.h
*
* Audio Library for Teensy 3.X
* Copyright (c) 2014, Paul Stoffregen, [email protected]
* Extended by Chip Audette, Open Audio, Apr 2018
* Isolated names for Open Audio (F32) RSL June 2020
*
* Development of this audio library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef AudioEffecDelay_OA_F32_h_
#define AudioEffecDelay_OA_F32_h_
#include "Arduino.h"
#include "AudioStream_F32.h"
#include "utility/dspinst.h"
#define AUDIO_BLOCK_SIZE_F32 AUDIO_BLOCK_SAMPLES //what is the maximum length of the F32 audio blocks
// Are these too big??? I think the same as I16---half as much? <<<<<<<<<<<<<<<<
#if defined(__IMXRT1062__)
// 4.00 second maximum on Teensy 4.0
#define DELAY_QUEUE_SIZE_OA (176512 / AUDIO_BLOCK_SAMPLES)
#elif defined(__MK66FX1M0__)
// 2.41 second maximum on Teensy 3.6
#define DELAY_QUEUE_SIZE_OA (106496 / AUDIO_BLOCK_SIZE_F32)
#elif defined(__MK64FX512__)
// 1.67 second maximum on Teensy 3.5
#define DELAY_QUEUE_SIZE_OA (73728 / AUDIO_BLOCK_SIZE_F32)
#elif defined(__MK20DX256__)
// 0.45 second maximum on Teensy 3.1 & 3.2
#define DELAY_QUEUE_SIZE_OA (19826 / AUDIO_BLOCK_SIZE_F32)
#else
// 0.14 second maximum on Teensy 3.0
#define DELAY_QUEUE_SIZE_OA (6144 / AUDIO_BLOCK_SIZE_F32)
#endif
class AudioEffectDelay_OA_F32 : public AudioStream_F32
{
//GUI: inputs:1, outputs:1 //this line used for automatic generation of GUI node
//GUI: shortName:delay
public:
AudioEffectDelay_OA_F32() : AudioStream_F32(1, inputQueueArray) {
activemask = 0;
headindex = 0;
tailindex = 0;
maxblocks = 0;
memset(queue, 0, sizeof(queue));
}
AudioEffectDelay_OA_F32(const AudioSettings_F32 &settings) :
AudioStream_F32(1,inputQueueArray) {
activemask = 0;
headindex = 0;
tailindex = 0;
maxblocks = 0;
memset(queue, 0, sizeof(queue));
setSampleRate_Hz(settings.sample_rate_Hz);
}
void setSampleRate_Hz(float _fs_Hz) {
//Serial.print("AudioEffectDelay_OA_F32: setSampleRate_Hz to ");
//Serial.println(_fs_Hz);
sampleRate_Hz = _fs_Hz;
//audio_block_len_samples = block_size; //this is the actual size that is being used in each audio_block_f32
}
void delay(uint8_t channel, float milliseconds) {
if (channel >= 8) return;
if (milliseconds < 0.0) milliseconds = 0.0;
uint32_t n = (milliseconds*(sampleRate_Hz/1000.0))+0.5;
uint32_t nmax = AUDIO_BLOCK_SIZE_F32 * (DELAY_QUEUE_SIZE_OA-1);
if (n > nmax) n = nmax;
uint32_t blks = (n + (AUDIO_BLOCK_SIZE_F32-1)) / AUDIO_BLOCK_SIZE_F32 + 1;
if (!(activemask & (1<<channel))) {
// enabling a previously disabled channel
delay_samps[channel] = n;
if (blks > maxblocks) maxblocks = blks;
activemask |= (1<<channel);
} else {
if (n > delay_samps[channel]) {
// new delay is greater than previous setting
if (blks > maxblocks) maxblocks = blks;
delay_samps[channel] = n;
} else {
// new delay is less than previous setting
delay_samps[channel] = n;
recompute_maxblocks();
}
}
}
void disable(uint8_t channel) {
if (channel >= 8) return;
// diable this channel
activemask &= ~(1<<channel);
// recompute maxblocks for remaining enabled channels
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 = delay_samps[channel];
n = (n + (AUDIO_BLOCK_SIZE_F32-1)) / AUDIO_BLOCK_SIZE_F32 + 1;
if (n > max) max = n;
}
} while(++channel < 8);
maxblocks = max;
}
uint8_t activemask; // which output channels are active
uint16_t headindex; // head index (incoming) data in queue
uint16_t tailindex; // tail index (outgoing) data from queue
uint16_t maxblocks; // number of blocks needed in queue
//#if DELAY_QUEUE_SIZE_OA * AUDIO_BLOCK_SAMPLES < 65535
// uint16_t writeposition;
// uint16_t delay_samps[8]; // # of samples to delay for each channel
//#else
int writeposition; //position within current head buffer in the queue
uint32_t delay_samps[8]; // # of samples to delay for each channel
//#endif
audio_block_f32_t *queue[DELAY_QUEUE_SIZE_OA];
audio_block_f32_t *inputQueueArray[1];
float sampleRate_Hz = AUDIO_SAMPLE_RATE_EXACT; //default. from AudioStream.h??
//int audio_block_len_samples = AUDIO_BLOCK_SAMPLES;
void receiveIncomingData(void);
void discardUnneededBlocksFromQueue(void);
void transmitOutgoingData(void);
unsigned long last_received_block_id = 0;
};
#endif