@@ -16,10 +16,12 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
1616
1717 // Basic settings every effect and audio sample has
1818 // 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 ;
2325
2426 // To smooth things out as CircuitPython is doing other tasks most audio objects have a buffer
2527 // 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
7072
7173 // Allocate the chorus buffer for the max possible delay, chorus is always 16-bit
7274 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
7476 self -> chorus_buffer = m_malloc (self -> max_chorus_buffer_len );
7577 if (self -> chorus_buffer == NULL ) {
7678 common_hal_audiodelays_chorus_deinit (self );
@@ -79,7 +81,7 @@ void common_hal_audiodelays_chorus_construct(audiodelays_chorus_obj_t *self, uin
7981 memset (self -> chorus_buffer , 0 , self -> max_chorus_buffer_len );
8082
8183 // 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 ;
8385
8486 // calculate everything needed for the current delay
8587 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
122124 f_delay_ms = MAX (f_delay_ms , self -> sample_ms );
123125
124126 // 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 ));
126128
127129 if (new_chorus_buffer_len < 0 ) { // or too short!
128130 return ;
@@ -141,18 +143,6 @@ void common_hal_audiodelays_chorus_set_voices(audiodelays_chorus_obj_t *self, mp
141143 synthio_block_assign_slot (voices , & self -> voices , MP_QSTR_voices );
142144}
143145
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-
156146void audiodelays_chorus_reset_buffer (audiodelays_chorus_obj_t * self ,
157147 bool single_channel_output ,
158148 uint8_t channel ) {
@@ -167,27 +157,7 @@ bool common_hal_audiodelays_chorus_get_playing(audiodelays_chorus_obj_t *self) {
167157}
168158
169159void 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 );
191161
192162 self -> sample = sample ;
193163 self -> loop = loop ;
@@ -196,7 +166,7 @@ void common_hal_audiodelays_chorus_play(audiodelays_chorus_obj_t *self, mp_obj_t
196166 audioio_get_buffer_result_t result = audiosample_get_buffer (self -> sample , false, 0 , (uint8_t * * )& self -> sample_remaining_buffer , & self -> sample_buffer_length );
197167
198168 // 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 );
200170 // Store if we have more data in the sample to retrieve
201171 self -> more_data = result == GET_BUFFER_MORE_DATA ;
202172
@@ -219,7 +189,7 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
219189 // If we are using 16 bit samples we need a 16 bit pointer, 8 bit needs an 8 bit pointer
220190 int16_t * word_buffer = (int16_t * )self -> buffer [self -> last_buf_idx ];
221191 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 );
223193
224194 // The chorus buffer is always stored as a 16-bit value internally
225195 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
240210 // Load another sample buffer to play
241211 audioio_get_buffer_result_t result = audiosample_get_buffer (self -> sample , false, 0 , (uint8_t * * )& self -> sample_remaining_buffer , & self -> sample_buffer_length );
242212 // 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 );
244214 self -> more_data = result == GET_BUFFER_MORE_DATA ;
245215 }
246216 }
247217
248218 // Determine how many bytes we can process to our buffer, the less of the sample we have left and our buffer remaining
249219 uint32_t n ;
250220 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 );
252222 } 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 );
254224 }
255225
256226 // 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 );
258228
259229 int32_t voices = MAX (synthio_block_slot_get (& self -> voices ), 1.0 );
260230
@@ -264,17 +234,17 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
264234 }
265235
266236 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 ));
269239 } else {
270240 // 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 )) {
272242 uint16_t * uword_buffer = (uint16_t * )word_buffer ;
273243 for (uint32_t i = 0 ; i < n ; i ++ ) {
274244 * uword_buffer ++ = 32768 ;
275245 }
276246 } else {
277- memset (hword_buffer , 128 , n * (self -> bits_per_sample / 8 ));
247+ memset (hword_buffer , 128 , n * (self -> base . bits_per_sample / 8 ));
278248 }
279249 }
280250 } else {
@@ -283,10 +253,10 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
283253
284254 for (uint32_t i = 0 ; i < n ; i ++ ) {
285255 int32_t sample_word = 0 ;
286- if (MP_LIKELY (self -> bits_per_sample == 16 )) {
256+ if (MP_LIKELY (self -> base . bits_per_sample == 16 )) {
287257 sample_word = sample_src [i ];
288258 } else {
289- if (self -> samples_signed ) {
259+ if (self -> base . samples_signed ) {
290260 sample_word = sample_hsrc [i ];
291261 } else {
292262 // 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
316286 word = synthio_mix_down_sample (word , SYNTHIO_MIX_DOWN_SCALE (2 ));
317287 }
318288
319- if (MP_LIKELY (self -> bits_per_sample == 16 )) {
289+ if (MP_LIKELY (self -> base . bits_per_sample == 16 )) {
320290 word_buffer [i ] = word ;
321- if (!self -> samples_signed ) {
291+ if (!self -> base . samples_signed ) {
322292 word_buffer [i ] ^= 0x8000 ;
323293 }
324294 } else {
325295 int8_t out = word ;
326- if (self -> samples_signed ) {
296+ if (self -> base . samples_signed ) {
327297 hword_buffer [i ] = out ;
328298 } else {
329299 hword_buffer [i ] = (uint8_t )out ^ 0x80 ;
@@ -334,7 +304,7 @@ audioio_get_buffer_result_t audiodelays_chorus_get_buffer(audiodelays_chorus_obj
334304 self -> chorus_buffer_pos = 0 ;
335305 }
336306 }
337- self -> sample_remaining_buffer += (n * (self -> bits_per_sample / 8 ));
307+ self -> sample_remaining_buffer += (n * (self -> base . bits_per_sample / 8 ));
338308 self -> sample_buffer_length -= n ;
339309 }
340310 // 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
350320 // Chorus always returns more data but some effects may return GET_BUFFER_DONE or GET_BUFFER_ERROR (see audiocore/__init__.h)
351321 return GET_BUFFER_MORE_DATA ;
352322}
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