1212#include "py/obj.h"
1313#include "py/objnamedtuple.h"
1414#include "py/runtime.h"
15- #include "extmod/vfs_fat.h"
16- #include "extmod/vfs_posix.h"
15+ #include "py/stream.h"
1716
1817#include "shared-bindings/synthio/__init__.h"
1918#include "shared-bindings/synthio/Biquad.h"
@@ -211,16 +210,22 @@ static mp_obj_t synthio_from_file(size_t n_args, const mp_obj_t *pos_args, mp_ma
211210 };
212211 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
213212 mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
214- if (!mp_obj_is_type (args [ARG_file ].u_obj , & mp_type_vfs_fat_fileio )) {
213+ mp_obj_t file_obj = args [ARG_file ].u_obj ;
214+ // Any readable, seekable binary stream works, regardless of which
215+ // filesystem it came from.
216+ const mp_stream_p_t * stream_p = mp_get_stream_raise (file_obj , MP_STREAM_OP_READ | MP_STREAM_OP_IOCTL );
217+ if (stream_p -> is_text ) {
215218 mp_raise_TypeError (MP_ERROR_TEXT ("file must be a file opened in byte mode" ));
216219 }
217- pyb_file_obj_t * file = MP_OBJ_TO_PTR (args [ARG_file ].u_obj );
218220
219221 uint8_t chunk_header [14 ];
220- f_rewind (& file -> fp );
221- UINT bytes_read ;
222- if (f_read (& file -> fp , chunk_header , sizeof (chunk_header ), & bytes_read ) != FR_OK ) {
223- mp_raise_OSError (MP_EIO );
222+ int errcode ;
223+ if (mp_stream_seek (file_obj , 0 , MP_SEEK_SET , & errcode ) == MP_STREAM_ERROR ) {
224+ mp_raise_OSError (errcode );
225+ }
226+ mp_uint_t bytes_read = mp_stream_rw (file_obj , chunk_header , sizeof (chunk_header ), & errcode , MP_STREAM_RW_READ );
227+ if (bytes_read == MP_STREAM_ERROR ) {
228+ mp_raise_OSError (errcode );
224229 }
225230 if (bytes_read != sizeof (chunk_header ) ||
226231 memcmp (chunk_header , "MThd\0\0\0\6\0\0\0\1" , 12 )) {
@@ -235,17 +240,19 @@ static mp_obj_t synthio_from_file(size_t n_args, const mp_obj_t *pos_args, mp_ma
235240 tempo = 2 * ((chunk_header [12 ] << 8 ) | chunk_header [13 ]);
236241 }
237242
238- if (f_read (& file -> fp , chunk_header , 8 , & bytes_read ) != FR_OK ) {
239- mp_raise_OSError (MP_EIO );
243+ bytes_read = mp_stream_rw (file_obj , chunk_header , 8 , & errcode , MP_STREAM_RW_READ );
244+ if (bytes_read == MP_STREAM_ERROR ) {
245+ mp_raise_OSError (errcode );
240246 }
241247 if (bytes_read != 8 || memcmp (chunk_header , "MTrk" , 4 )) {
242248 mp_arg_error_invalid (MP_QSTR_file );
243249 }
244250 uint32_t track_size = (chunk_header [4 ] << 24 ) |
245251 (chunk_header [5 ] << 16 ) | (chunk_header [6 ] << 8 ) | chunk_header [7 ];
246252 uint8_t * buffer = m_malloc_without_collect (track_size );
247- if (f_read (& file -> fp , buffer , track_size , & bytes_read ) != FR_OK ) {
248- mp_raise_OSError (MP_EIO );
253+ bytes_read = mp_stream_rw (file_obj , buffer , track_size , & errcode , MP_STREAM_RW_READ );
254+ if (bytes_read == MP_STREAM_ERROR ) {
255+ mp_raise_OSError (errcode );
249256 }
250257 if (bytes_read != track_size ) {
251258 mp_arg_error_invalid (MP_QSTR_file );
0 commit comments