10
10
#include "shared-module/audiomixer/utils.h"
11
11
12
12
13
- void common_hal_audiodelays_echo_construct (audiodelays_echo_obj_t * self , uint32_t delay_ms , mp_float_t decay , mp_float_t mix ,
13
+ void common_hal_audiodelays_echo_construct (audiodelays_echo_obj_t * self , uint32_t delay_ms , mp_float_t decay , mp_obj_t mix ,
14
14
uint32_t buffer_size , uint8_t bits_per_sample ,
15
15
bool samples_signed , uint8_t channel_count , uint32_t sample_rate ) {
16
16
@@ -29,7 +29,11 @@ void common_hal_audiodelays_echo_construct(audiodelays_echo_obj_t *self, uint32_
29
29
memset (self -> buffer , 0 , self -> buffer_len );
30
30
31
31
self -> decay = (uint16_t )(decay * (1 << 15 ));
32
- self -> mix = (uint16_t )(mix * (1 << 15 ));
32
+
33
+ if (mix == MP_OBJ_NULL ) {
34
+ mix = mp_obj_new_float (0.5 );
35
+ }
36
+ synthio_block_assign_slot (mix , & self -> mix , MP_QSTR_mix );
33
37
34
38
// calculate buffer size for the set delay
35
39
self -> delay_ms = delay_ms ;
@@ -103,13 +107,12 @@ void common_hal_audiodelays_echo_set_decay(audiodelays_echo_obj_t *self, mp_floa
103
107
self -> decay = (uint16_t )(decay * (1 << 15 ));
104
108
}
105
109
106
- mp_float_t common_hal_audiodelays_echo_get_mix (audiodelays_echo_obj_t * self ) {
107
- return ( mp_float_t ) self -> mix / ( 1 << 15 ) ;
110
+ mp_obj_t common_hal_audiodelays_echo_get_mix (audiodelays_echo_obj_t * self ) {
111
+ return self -> mix . obj ;
108
112
}
109
113
110
- void common_hal_audiodelays_echo_set_mix (audiodelays_echo_obj_t * self , mp_float_t mix ) {
111
- self -> mix = (uint16_t )(mix * (1 << 15 ));
112
- mp_printf (& mp_plat_print , "mix %d\n" , self -> mix );
114
+ void common_hal_audiodelays_echo_set_mix (audiodelays_echo_obj_t * self , mp_obj_t arg ) {
115
+ synthio_block_assign_slot (arg , & self -> mix , MP_QSTR_mix );
113
116
}
114
117
115
118
uint32_t common_hal_audiodelays_echo_get_sample_rate (audiodelays_echo_obj_t * self ) {
@@ -177,6 +180,13 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
177
180
uint32_t * word_buffer = (uint32_t * )self -> buffer ;
178
181
uint32_t length = self -> buffer_len / sizeof (uint32_t );
179
182
uint32_t echo_buf_len = self -> echo_buffer_len / sizeof (uint32_t );
183
+ mp_float_t f_mix = synthio_block_slot_get (& self -> mix );
184
+ if (f_mix > 1.0 ) {
185
+ f_mix = 1.0 ;
186
+ } else if (f_mix < 0.0 ) {
187
+ f_mix = 0.0 ;
188
+ }
189
+ uint16_t mix = (uint16_t )(f_mix * (1 << 15 ));
180
190
181
191
while (length != 0 ) {
182
192
if (self -> sample_buffer_length == 0 ) {
@@ -201,7 +211,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
201
211
// If we have no sample keep the echo echoing
202
212
if (self -> sample == NULL ) {
203
213
if (MP_LIKELY (self -> bits_per_sample == 16 )) {
204
- if (self -> mix == 0 ) { // no effect and no sample sound
214
+ if (mix == 0 ) { // no effect and no sample sound
205
215
for (uint32_t i = 0 ; i < length ; i ++ ) {
206
216
word_buffer [i ] = 0 ;
207
217
}
@@ -212,7 +222,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
212
222
word_buffer [i ] = mult16signed (echo , self -> decay );
213
223
self -> echo_buffer [self -> echo_buffer_write_pos ++ ] = word_buffer [i ];
214
224
215
- word_buffer [i ] = mult16signed (word_buffer [i ], self -> mix );
225
+ word_buffer [i ] = mult16signed (word_buffer [i ], mix );
216
226
217
227
if (self -> echo_buffer_read_pos >= echo_buf_len ) {
218
228
self -> echo_buffer_read_pos = 0 ;
@@ -246,7 +256,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
246
256
uint32_t * sample_src = self -> sample_remaining_buffer ;
247
257
248
258
if (MP_LIKELY (self -> bits_per_sample == 16 )) {
249
- if (self -> mix == 0 ) { // sample only
259
+ if (mix == 0 ) { // sample only
250
260
for (uint32_t i = 0 ; i < n ; i ++ ) {
251
261
word_buffer [i ] = sample_src [i ];
252
262
}
@@ -260,7 +270,7 @@ audioio_get_buffer_result_t audiodelays_echo_get_buffer(audiodelays_echo_obj_t *
260
270
word_buffer [i ] = add16signed (mult16signed (echo , self -> decay ), sample_word );
261
271
self -> echo_buffer [self -> echo_buffer_write_pos ++ ] = word_buffer [i ];
262
272
263
- word_buffer [i ] = add16signed (mult16signed (sample_word , 32768 - self -> mix ), mult16signed (word_buffer [i ], self -> mix ));
273
+ word_buffer [i ] = add16signed (mult16signed (sample_word , 32768 - mix ), mult16signed (word_buffer [i ], mix ));
264
274
265
275
if (self -> echo_buffer_read_pos >= echo_buf_len ) {
266
276
self -> echo_buffer_read_pos = 0 ;
0 commit comments