@@ -16,10 +16,12 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
16
16
17
17
// Basic settings every effect and audio sample has
18
18
// These are the effects values, not the source sample(s)
19
- self -> bits_per_sample = bits_per_sample ; // Most common is 16, but 8 is also supported in many places
20
- self -> samples_signed = samples_signed ; // Are the samples we provide signed (common is true)
21
- self -> channel_count = channel_count ; // Channels can be 1 for mono or 2 for stereo
22
- self -> sample_rate = sample_rate ; // Sample rate for the effect, this generally needs to match all audio objects
19
+ self -> base .bits_per_sample = bits_per_sample ; // Most common is 16, but 8 is also supported in many places
20
+ self -> base .samples_signed = samples_signed ; // Are the samples we provide signed (common is true)
21
+ self -> base .channel_count = channel_count ; // Channels can be 1 for mono or 2 for stereo
22
+ self -> base .sample_rate = sample_rate ; // Sample rate for the effect, this generally needs to match all audio objects
23
+ self -> base .single_buffer = false;
24
+ self -> base .max_buffer_length = buffer_size ;
23
25
24
26
// To smooth things out as CircuitPython is doing other tasks most audio objects have a buffer
25
27
// A double buffer is set up here so the audio output can use DMA on buffer 1 while we
@@ -70,7 +72,7 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
70
72
71
73
// Allocate the chorus buffer for the max possible delay, chorus is always 16-bit
72
74
self -> max_delay_ms = max_delay_ms ;
73
- self -> max_chorus_buffer_len = self -> sample_rate / MICROPY_FLOAT_CONST (1000.0 ) * max_delay_ms * (self -> channel_count * sizeof (uint16_t )); // bytes
75
+ self -> max_chorus_buffer_len = self -> base . sample_rate / MICROPY_FLOAT_CONST (1000.0 ) * max_delay_ms * (self -> base . channel_count * sizeof (uint16_t )); // bytes
74
76
self -> chorus_buffer = m_malloc (self -> max_chorus_buffer_len );
75
77
if (self -> chorus_buffer == NULL ) {
76
78
common_hal_audiodelays_chorus_deinit (self );
@@ -79,7 +81,7 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
79
81
memset (self -> chorus_buffer , 0 , self -> max_chorus_buffer_len );
80
82
81
83
// calculate the length of a single sample in milliseconds
82
- self -> sample_ms = MICROPY_FLOAT_CONST (1000.0 ) / self -> sample_rate ;
84
+ self -> sample_ms = MICROPY_FLOAT_CONST (1000.0 ) / self -> base . sample_rate ;
83
85
84
86
// calculate everything needed for the current delay
85
87
mp_float_t f_delay_ms = synthio_block_slot_get (& self -> delay_ms );
@@ -122,7 +124,7 @@ void chorus_recalculate_delay(audiodelays_chorus_obj_t *self, mp_float_t f_delay
122
124
f_delay_ms = MAX (f_delay_ms , self -> sample_ms );
123
125
124
126
// Calculate the current chorus buffer length in bytes
125
- uint32_t new_chorus_buffer_len = (uint32_t )(self -> sample_rate / MICROPY_FLOAT_CONST (1000.0 ) * f_delay_ms ) * (self -> channel_count * sizeof (uint16_t ));
127
+ uint32_t new_chorus_buffer_len = (uint32_t )(self -> base . sample_rate / MICROPY_FLOAT_CONST (1000.0 ) * f_delay_ms ) * (self -> base . channel_count * sizeof (uint16_t ));
126
128
127
129
if (new_chorus_buffer_len < 0 ) { // or too short!
128
130
return ;
@@ -141,18 +143,6 @@ void common_hal_audiodelays_chorus_set_voices(audiodelays_chorus_obj_t *self, mp
141
143
synthio_block_assign_slot (voices , & self -> voices , MP_QSTR_voices );
142
144
}
143
145
144
- uint32_t common_hal_audiodelays_chorus_get_sample_rate (audiodelays_chorus_obj_t * self ) {
145
- return self -> sample_rate ;
146
- }
147
-
148
- uint8_t common_hal_audiodelays_chorus_get_channel_count (audiodelays_chorus_obj_t * self ) {
149
- return self -> channel_count ;
150
- }
151
-
152
- uint8_t common_hal_audiodelays_chorus_get_bits_per_sample (audiodelays_chorus_obj_t * self ) {
153
- return self -> bits_per_sample ;
154
- }
155
-
156
146
void audiodelays_chorus_reset_buffer (audiodelays_chorus_obj_t * self ,
157
147
bool single_channel_output ,
158
148
uint8_t channel ) {
@@ -167,27 +157,7 @@ bool common_hal_audiodelays_chorus_get_playing(audiodelays_chorus_obj_t *self) {
167
157
}
168
158
169
159
void common_hal_audiodelays_chorus_play (audiodelays_chorus_obj_t * self , mp_obj_t sample , bool loop ) {
170
- // When a sample is to be played we must ensure the samples values matches what we expect
171
- // Then we reset the sample and get the first buffer to play
172
- // The get_buffer function will actually process that data
173
-
174
- if (audiosample_sample_rate (sample ) != self -> sample_rate ) {
175
- mp_raise_ValueError_varg (MP_ERROR_TEXT ("The sample's %q does not match" ), MP_QSTR_sample_rate );
176
- }
177
- if (audiosample_channel_count (sample ) != self -> channel_count ) {
178
- mp_raise_ValueError_varg (MP_ERROR_TEXT ("The sample's %q does not match" ), MP_QSTR_channel_count );
179
- }
180
- if (audiosample_bits_per_sample (sample ) != self -> bits_per_sample ) {
181
- mp_raise_ValueError_varg (MP_ERROR_TEXT ("The sample's %q does not match" ), MP_QSTR_bits_per_sample );
182
- }
183
- bool single_buffer ;
184
- bool samples_signed ;
185
- uint32_t max_buffer_length ;
186
- uint8_t spacing ;
187
- audiosample_get_buffer_structure (sample , false, & single_buffer , & samples_signed , & max_buffer_length , & spacing );
188
- if (samples_signed != self -> samples_signed ) {
189
- mp_raise_ValueError_varg (MP_ERROR_TEXT ("The sample's %q does not match" ), MP_QSTR_signedness );
190
- }
160
+ audiosample_must_match (& self -> base , sample );
191
161
192
162
self -> sample = sample ;
193
163
self -> loop = loop ;
@@ -196,7 +166,7 @@ void common_hal_audiodelays_chorus_play(audiodelays_chorus_obj_t *self, mp_obj_t
196
166
audioio_get_buffer_result_t result = audiosample_get_buffer (self -> sample , false, 0 , (uint8_t * * )& self -> sample_remaining_buffer , & self -> sample_buffer_length );
197
167
198
168
// Track remaining sample length in terms of bytes per sample
199
- self -> sample_buffer_length /= (self -> bits_per_sample / 8 );
169
+ self -> sample_buffer_length /= (self -> base . bits_per_sample / 8 );
200
170
// Store if we have more data in the sample to retrieve
201
171
self -> more_data = result == GET_BUFFER_MORE_DATA ;
202
172
@@ -219,7 +189,7 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
219
189
// If we are using 16 bit samples we need a 16 bit pointer, 8 bit needs an 8 bit pointer
220
190
int16_t * word_buffer = (int16_t * )self -> buffer [self -> last_buf_idx ];
221
191
int8_t * hword_buffer = self -> buffer [self -> last_buf_idx ];
222
- uint32_t length = self -> buffer_len / (self -> bits_per_sample / 8 );
192
+ uint32_t length = self -> buffer_len / (self -> base . bits_per_sample / 8 );
223
193
224
194
// The chorus buffer is always stored as a 16-bit value internally
225
195
int16_t * chorus_buffer = (int16_t * )self -> chorus_buffer ;
@@ -240,21 +210,21 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
240
210
// Load another sample buffer to play
241
211
audioio_get_buffer_result_t result = audiosample_get_buffer (self -> sample , false, 0 , (uint8_t * * )& self -> sample_remaining_buffer , & self -> sample_buffer_length );
242
212
// Track length in terms of words.
243
- self -> sample_buffer_length /= (self -> bits_per_sample / 8 );
213
+ self -> sample_buffer_length /= (self -> base . bits_per_sample / 8 );
244
214
self -> more_data = result == GET_BUFFER_MORE_DATA ;
245
215
}
246
216
}
247
217
248
218
// Determine how many bytes we can process to our buffer, the less of the sample we have left and our buffer remaining
249
219
uint32_t n ;
250
220
if (self -> sample == NULL ) {
251
- n = MIN (length , SYNTHIO_MAX_DUR * self -> channel_count );
221
+ n = MIN (length , SYNTHIO_MAX_DUR * self -> base . channel_count );
252
222
} else {
253
- n = MIN (MIN (self -> sample_buffer_length , length ), SYNTHIO_MAX_DUR * self -> channel_count );
223
+ n = MIN (MIN (self -> sample_buffer_length , length ), SYNTHIO_MAX_DUR * self -> base . channel_count );
254
224
}
255
225
256
226
// get the effect values we need from the BlockInput. These may change at run time so you need to do bounds checking if required
257
- shared_bindings_synthio_lfo_tick (self -> sample_rate , n / self -> channel_count );
227
+ shared_bindings_synthio_lfo_tick (self -> base . sample_rate , n / self -> base . channel_count );
258
228
259
229
int32_t voices = MAX (synthio_block_slot_get (& self -> voices ), 1.0 );
260
230
@@ -264,17 +234,17 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
264
234
}
265
235
266
236
if (self -> sample == NULL ) {
267
- if (self -> samples_signed ) {
268
- memset (word_buffer , 0 , n * (self -> bits_per_sample / 8 ));
237
+ if (self -> base . samples_signed ) {
238
+ memset (word_buffer , 0 , n * (self -> base . bits_per_sample / 8 ));
269
239
} else {
270
240
// For unsigned samples set to the middle which is "quiet"
271
- if (MP_LIKELY (self -> bits_per_sample == 16 )) {
241
+ if (MP_LIKELY (self -> base . bits_per_sample == 16 )) {
272
242
uint16_t * uword_buffer = (uint16_t * )word_buffer ;
273
243
for (uint32_t i = 0 ; i < n ; i ++ ) {
274
244
* uword_buffer ++ = 32768 ;
275
245
}
276
246
} else {
277
- memset (hword_buffer , 128 , n * (self -> bits_per_sample / 8 ));
247
+ memset (hword_buffer , 128 , n * (self -> base . bits_per_sample / 8 ));
278
248
}
279
249
}
280
250
} else {
@@ -283,10 +253,10 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
283
253
284
254
for (uint32_t i = 0 ; i < n ; i ++ ) {
285
255
int32_t sample_word = 0 ;
286
- if (MP_LIKELY (self -> bits_per_sample == 16 )) {
256
+ if (MP_LIKELY (self -> base . bits_per_sample == 16 )) {
287
257
sample_word = sample_src [i ];
288
258
} else {
289
- if (self -> samples_signed ) {
259
+ if (self -> base . samples_signed ) {
290
260
sample_word = sample_hsrc [i ];
291
261
} else {
292
262
// Be careful here changing from an 8 bit unsigned to signed into a 32-bit signed
@@ -316,14 +286,14 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
316
286
word = synthio_mix_down_sample (word , SYNTHIO_MIX_DOWN_SCALE (2 ));
317
287
}
318
288
319
- if (MP_LIKELY (self -> bits_per_sample == 16 )) {
289
+ if (MP_LIKELY (self -> base . bits_per_sample == 16 )) {
320
290
word_buffer [i ] = word ;
321
- if (!self -> samples_signed ) {
291
+ if (!self -> base . samples_signed ) {
322
292
word_buffer [i ] ^= 0x8000 ;
323
293
}
324
294
} else {
325
295
int8_t out = word ;
326
- if (self -> samples_signed ) {
296
+ if (self -> base . samples_signed ) {
327
297
hword_buffer [i ] = out ;
328
298
} else {
329
299
hword_buffer [i ] = (uint8_t )out ^ 0x80 ;
@@ -334,7 +304,7 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
334
304
self -> chorus_buffer_pos = 0 ;
335
305
}
336
306
}
337
- self -> sample_remaining_buffer += (n * (self -> bits_per_sample / 8 ));
307
+ self -> sample_remaining_buffer += (n * (self -> base . bits_per_sample / 8 ));
338
308
self -> sample_buffer_length -= n ;
339
309
}
340
310
// Update the remaining length and the buffer positions based on how much we wrote into our buffer
@@ -350,18 +320,3 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
350
320
// Chorus always returns more data but some effects may return GET_BUFFER_DONE or GET_BUFFER_ERROR (see audiocore/__init__.h)
351
321
return GET_BUFFER_MORE_DATA ;
352
322
}
353
-
354
- void audiodelays_chorus_get_buffer_structure (audiodelays_chorus_obj_t * self , bool single_channel_output ,
355
- bool * single_buffer , bool * samples_signed , uint32_t * max_buffer_length , uint8_t * spacing ) {
356
-
357
- // Return information about the effect's buffer (not the sample's)
358
- // These are used by calling audio objects to determine how to handle the effect's buffer
359
- * single_buffer = false;
360
- * samples_signed = self -> samples_signed ;
361
- * max_buffer_length = self -> buffer_len ;
362
- if (single_channel_output ) {
363
- * spacing = self -> channel_count ;
364
- } else {
365
- * spacing = 1 ;
366
- }
367
- }
0 commit comments