@@ -75,25 +75,36 @@ void common_hal_audioio_wavefile_construct(audioio_wavefile_obj_t *self,
7575 self -> channel_count = format .num_channels ;
7676 self -> bits_per_sample = format .bits_per_sample ;
7777
78- // TODO(tannewt): Skip any extra chunks that occur before the data section.
78+ uint8_t chunk_tag [4 ];
79+ uint32_t chunk_length ;
80+ bool found_data_chunk = false;
7981
80- uint8_t data_tag [4 ];
81- if (f_read (& self -> file -> fp , & data_tag , 4 , & bytes_read ) != FR_OK ) {
82- mp_raise_OSError (MP_EIO );
83- }
84- if (bytes_read != 4 ||
85- memcmp ((uint8_t * )data_tag , "data" , 4 ) != 0 ) {
86- mp_raise_ValueError (MP_ERROR_TEXT ("Data chunk must follow fmt chunk" ));
87- }
82+ while (!found_data_chunk ) {
83+ if (f_read (& self -> file -> fp , & chunk_tag , 4 , & bytes_read ) != FR_OK ) {
84+ mp_raise_OSError (MP_EIO );
85+ }
86+ if (bytes_read != 4 ) {
87+ mp_raise_OSError (MP_EIO );
88+ }
89+ if (memcmp ((uint8_t * )chunk_tag , "data" , 4 ) == 0 ) {
90+ found_data_chunk = true;
91+ }
8892
89- uint32_t data_length ;
90- if (f_read (& self -> file -> fp , & data_length , 4 , & bytes_read ) != FR_OK ) {
91- mp_raise_OSError (MP_EIO );
92- }
93- if (bytes_read != 4 ) {
94- mp_arg_error_invalid (MP_QSTR_file );
93+ if (f_read (& self -> file -> fp , & chunk_length , 4 , & bytes_read ) != FR_OK ) {
94+ mp_raise_OSError (MP_EIO );
95+ }
96+ if (bytes_read != 4 ) {
97+ mp_raise_OSError (MP_EIO );
98+ }
99+
100+ if (!found_data_chunk ) {
101+ if (f_lseek (& self -> file -> fp , f_tell (& self -> file -> fp ) + chunk_length ) != FR_OK ) {
102+ mp_raise_OSError (MP_EIO );
103+ }
104+ }
95105 }
96- self -> file_length = data_length ;
106+
107+ self -> file_length = chunk_length ;
97108 self -> data_start = self -> file -> fp .fptr ;
98109
99110 // Try to allocate two buffers, one will be loaded from file and the other
0 commit comments