@@ -188,6 +188,26 @@ bool common_hal_audiopwmio_pwmaudioout_deinited(audiopwmio_pwmaudioout_obj_t *se
188
188
return !self -> pwm ;
189
189
}
190
190
191
+ static void free_buffers (audiopwmio_pwmaudioout_obj_t * self ) {
192
+ #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
193
+ m_free (self -> buffers [0 ], self -> buffer_size [0 ]);
194
+ self -> buffer_size [0 ] = 0 ;
195
+ #else
196
+ m_free (self -> buffers [0 ]);
197
+ #endif
198
+
199
+ self -> buffers [0 ] = NULL ;
200
+
201
+ #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
202
+ m_free (self -> buffers [1 ], self -> buffer_size [1 ]);
203
+ self -> buffer_size [1 ] = 0 ;
204
+ #else
205
+ m_free (self -> buffers [1 ]);
206
+ #endif
207
+
208
+ self -> buffers [1 ] = NULL ;
209
+ }
210
+
191
211
void common_hal_audiopwmio_pwmaudioout_deinit (audiopwmio_pwmaudioout_obj_t * self ) {
192
212
if (common_hal_audiopwmio_pwmaudioout_deinited (self )) {
193
213
return ;
@@ -209,11 +229,7 @@ void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t *self
209
229
210
230
self -> pwm = NULL ;
211
231
212
- m_free (self -> buffers [0 ]);
213
- self -> buffers [0 ] = NULL ;
214
-
215
- m_free (self -> buffers [1 ]);
216
- self -> buffers [1 ] = NULL ;
232
+ free_buffers (self );
217
233
}
218
234
219
235
void common_hal_audiopwmio_pwmaudioout_play (audiopwmio_pwmaudioout_obj_t * self , mp_obj_t sample , bool loop ) {
@@ -235,10 +251,18 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self,
235
251
236
252
mp_arg_validate_length_max (max_buffer_length , UINT16_MAX , MP_QSTR_buffer );
237
253
238
- uint16_t buffer_length = (uint16_t )max_buffer_length ;
239
- self -> buffers [0 ] = m_malloc (buffer_length * 2 * sizeof (uint16_t ));
254
+ size_t buffer_size = (size_t )max_buffer_length * 2 * sizeof (uint16_t );
255
+
256
+ self -> buffers [0 ] = m_malloc (buffer_size );
257
+ #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
258
+ self -> buffer_size [0 ] = buffer_size ;
259
+ #endif
260
+
240
261
if (!self -> single_buffer ) {
241
- self -> buffers [1 ] = m_malloc (buffer_length * 2 * sizeof (uint16_t ));
262
+ self -> buffers [1 ] = m_malloc (buffer_size );
263
+ #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
264
+ self -> buffer_size [1 ] = buffer_size ;
265
+ #endif
242
266
}
243
267
244
268
@@ -274,11 +298,7 @@ void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t *self)
274
298
self -> stopping = false;
275
299
self -> paused = false;
276
300
277
- m_free (self -> buffers [0 ]);
278
- self -> buffers [0 ] = NULL ;
279
-
280
- m_free (self -> buffers [1 ]);
281
- self -> buffers [1 ] = NULL ;
301
+ free_buffers (self );
282
302
}
283
303
284
304
bool common_hal_audiopwmio_pwmaudioout_get_playing (audiopwmio_pwmaudioout_obj_t * self ) {
0 commit comments