23
23
24
24
#define MAX_BUFFER_LEN (MAX_NSAMP * MAX_NGRAN * MAX_NCHAN * sizeof(int16_t))
25
25
26
+ #define DO_DEBUG (0)
27
+
26
28
#if defined(MICROPY_UNIX_COVERAGE )
27
29
#define background_callback_prevent () ((void)0)
28
30
#define background_callback_allow () ((void)0)
@@ -38,6 +40,9 @@ STATIC bool stream_readable(void *stream) {
38
40
}
39
41
40
42
mp_int_t ret = stream_p -> ioctl (stream , MP_STREAM_POLL , MP_STREAM_POLL_RD | MP_STREAM_POLL_ERR | MP_STREAM_POLL_HUP , & errcode );
43
+ if (DO_DEBUG ) {
44
+ mp_printf (& mp_plat_print , "stream_readable ioctl() -> %d [errcode=%d]\n" , ret , errcode );
45
+ }
41
46
return ret != 0 ;
42
47
}
43
48
@@ -51,7 +56,13 @@ STATIC ssize_t stream_read(void *stream, void *buf, size_t len) {
51
56
return - EINVAL ;
52
57
}
53
58
mp_uint_t out_sz = stream_p -> read (MP_OBJ_FROM_PTR (stream ), buf , len , & errcode );
59
+ if (DO_DEBUG ) {
60
+ mp_printf (& mp_plat_print , "stream_read(%d) -> %d\n" , (int )len , (int )out_sz );
61
+ }
54
62
if (out_sz == MP_STREAM_ERROR ) {
63
+ if (DO_DEBUG ) {
64
+ mp_printf (& mp_plat_print , "errcode=%d\n" , errcode );
65
+ }
55
66
return - errcode ; // CIRCUITPY-CHANGE: returns negative errcode value
56
67
} else {
57
68
return out_sz ;
@@ -127,6 +138,10 @@ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self) {
127
138
self -> inbuf .write_off += n_read ;
128
139
}
129
140
141
+ if (DO_DEBUG ) {
142
+ mp_printf (& mp_plat_print , "new avail=%d eof=%d\n" , (int )INPUT_BUFFER_AVAILABLE (self -> inbuf ), self -> eof );
143
+ }
144
+
130
145
// Return true iff there are at least some useful bytes in the buffer
131
146
return INPUT_BUFFER_AVAILABLE (self -> inbuf ) > 0 ;
132
147
}
@@ -395,18 +410,25 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *
395
410
uint32_t * buffer_length ) {
396
411
if (!self -> inbuf .buf ) {
397
412
* buffer_length = 0 ;
413
+ if (DO_DEBUG ) {
414
+ mp_printf (& mp_plat_print , "%s:%d\n" , __FILE__ , __LINE__ );
415
+ }
398
416
return GET_BUFFER_ERROR ;
399
417
}
400
418
if (!single_channel_output ) {
401
419
channel = 0 ;
402
420
}
403
421
404
- * buffer_length = self -> frame_buffer_size ;
422
+ size_t frame_buffer_size_bytes = self -> frame_buffer_size ;
423
+ * buffer_length = frame_buffer_size_bytes ;
405
424
406
425
if (channel == self -> other_channel ) {
407
426
* bufptr = (uint8_t * )(self -> pcm_buffer [self -> other_buffer_index ] + channel );
408
427
self -> other_channel = -1 ;
409
428
self -> samples_decoded += * buffer_length / sizeof (int16_t );
429
+ if (DO_DEBUG ) {
430
+ mp_printf (& mp_plat_print , "%s:%d\n" , __FILE__ , __LINE__ );
431
+ }
410
432
return GET_BUFFER_MORE_DATA ;
411
433
}
412
434
@@ -428,15 +450,26 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *
428
450
CONSUME (self , BYTES_LEFT (self ) - bytes_left );
429
451
430
452
if (err ) {
431
- * buffer_length = 0 ;
432
- return GET_BUFFER_DONE ;
453
+ memset (buffer , 0 , frame_buffer_size_bytes );
454
+ if (DO_DEBUG ) {
455
+ mp_printf (& mp_plat_print , "%s:%d err=%d\n" , __FILE__ , __LINE__ , err );
456
+ }
457
+ if (err != ERR_MP3_INDATA_UNDERFLOW && err != ERR_MP3_MAINDATA_UNDERFLOW ) {
458
+ memset (buffer , 0 , self -> frame_buffer_size );
459
+ * buffer_length = 0 ;
460
+ self -> eof = true;
461
+ return GET_BUFFER_ERROR ;
462
+ }
433
463
}
434
464
435
- self -> samples_decoded += * buffer_length / sizeof (int16_t );
465
+ self -> samples_decoded += frame_buffer_size_bytes / sizeof (int16_t );
436
466
437
467
mp3file_skip_id3v2 (self );
438
468
int result = mp3file_find_sync_word (self ) ? GET_BUFFER_MORE_DATA : GET_BUFFER_DONE ;
439
469
470
+ if (DO_DEBUG ) {
471
+ mp_printf (& mp_plat_print , "%s:%d result=%d\n" , __FILE__ , __LINE__ , result );
472
+ }
440
473
if (INPUT_BUFFER_SPACE (self -> inbuf ) > 512 ) {
441
474
background_callback_add (
442
475
& self -> inbuf_fill_cb ,
0 commit comments