-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffect_modulated_delay_F32.cpp
121 lines (95 loc) · 3.81 KB
/
effect_modulated_delay_F32.cpp
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
/*
Copyright (c) 2014, Pete (El Supremo)
Copyright (c) 2019-2021 H. Wirtz
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 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.
*/
#include "effect_modulated_delay_F32.h"
/******************************************************************/
// Based on; A u d i o E f f e c t D e l a y
// Written by Pete (El Supremo) Jan 2014
// 140529 - change to handle mono stream - change modify() to voices()
// 140219 - correct storage class (not static)
// 190527 - added modulation input (by Holger Wirtz)
// ported by Marc Paquette to the Teensy OpenAudio F32 library
// https://github.com/chipaudette/OpenAudio_ArduinoLibrary
boolean AudioEffectModulatedDelay_F32::begin(float *delayline, uint16_t d_length)
{
_delayline = NULL;
_delay_length = 0;
_cb_index = 0;
_delay_offset = 0;
if (delayline == NULL)
return (false);
if (d_length < 10)
return (false);
_delayline = delayline;
_delay_length = d_length;
memset(_delayline, 0, _delay_length * sizeof(float));
_delay_offset = _delay_length >> 1 ;
return (true);
}
uint16_t AudioEffectModulatedDelay_F32::get_delay_length(void)
{
return (_delay_length);
}
void AudioEffectModulatedDelay_F32::update(void)
{
audio_block_f32_t *block;
audio_block_f32_t *modulation;
if (_delayline == NULL) return;
block = AudioStream_F32::receiveWritable_f32(0);
modulation = AudioStream_F32::receiveReadOnly_f32(1);
if (block && modulation)
{
float *bp;
int16_t cb_mod_index_neighbor;
float *mp;
float mod_index;
float mod_number;
float mod_fraction;
bp = block->data;
mp = modulation->data;
for (uint16_t i = 0; i < block->length; i++)
{
// write data into circular buffer (delayline)
if (_cb_index >= _delay_length) _cb_index = 0;
_delayline[_cb_index] = *bp;
// calculate the modulation-index as a floating point number for interpolation
mod_index = *mp * _delay_offset;
mod_fraction = modff(mod_index, &mod_number); // split float of mod_index into integer (= mod_number) and fraction part
// calculate modulation index into circular buffer
cb_mod_index = _cb_index - (_delay_offset + mod_number);
if (cb_mod_index < 0) // check for negative offsets and correct them
cb_mod_index += _delay_length;
if (cb_mod_index == _delay_length - 1)
cb_mod_index_neighbor = 0;
else
cb_mod_index_neighbor = cb_mod_index + 1;
*bp = _delayline[cb_mod_index] * mod_fraction + _delayline[cb_mod_index_neighbor] * (1.0f - mod_fraction);
// push the pointers forward
bp++; // next audio data
mp++; // next modulation data
_cb_index++; // next circular buffer index
}
}
if (modulation) AudioStream_F32::release(modulation);
if (block)
{
AudioStream_F32::transmit(block, 0);
AudioStream_F32::release(block);
}
}