@@ -62,6 +62,7 @@ def __init__(self, id, sck_pin, ws_pin, sd_pin, ibuf_len=INTERNAL_BUFFER_LENGTH,
62
62
self .__tone_samples = None
63
63
self .__queued_samples = None
64
64
65
+
65
66
def set_root (self , root ):
66
67
self .__root = root .rstrip ("/" ) + "/"
67
68
@@ -75,7 +76,10 @@ def play_wav(self, wav_file, loop=False):
75
76
self .__loop_wav = loop # Record if the user wants the file to loop
76
77
77
78
# Parse the WAV file, returning the necessary parameters to initialise I2S communication
78
- format , sample_rate , bits_per_sample , self .__first_sample_offset = WavPlayer .__parse_wav (self .__wav_file )
79
+ format , sample_rate , bits_per_sample , self .__first_sample_offset , self .sample_size = WavPlayer .__parse_wav (self .__wav_file )
80
+
81
+ # Keep a track of total bytes read from WAV File
82
+ self .total_bytes_read = 0
79
83
80
84
self .__wav_file .seek (self .__first_sample_offset ) # Advance to first byte of sample data
81
85
@@ -179,7 +183,7 @@ def __i2s_callback(self, arg):
179
183
if self .__state == WavPlayer .PLAY :
180
184
if self .__mode == WavPlayer .MODE_WAV :
181
185
num_read = self .__wav_file .readinto (self .__wav_samples_mv ) # Read the next section of the WAV file
182
-
186
+ self . total_bytes_read += num_read
183
187
# Have we reached the end of the file?
184
188
if num_read == 0 :
185
189
# Do we want to loop the WAV playback?
@@ -191,6 +195,8 @@ def __i2s_callback(self, arg):
191
195
192
196
self .__audio_out .write (self .__silence_samples ) # In both cases play silence to end this callback
193
197
else :
198
+ if num_read > 0 and num_read < self .WAV_BUFFER_LENGTH :
199
+ num_read = num_read - (self .total_bytes_read - self .sample_size )
194
200
self .__audio_out .write (self .__wav_samples_mv [: num_read ]) # We are within the file, so write out the next audio samples
195
201
else :
196
202
if self .__queued_samples is not None :
@@ -255,4 +261,7 @@ def __parse_wav(wav_file):
255
261
if offset == - 1 :
256
262
raise ValueError ("WAV sub chunk 2 ID not found" )
257
263
258
- return (format , sample_rate , bits_per_sample , 44 + offset )
264
+ wav_file .seek (40 )
265
+ sub_chunk2_size = struct .unpack ("<I" , wav_file .read (4 ))[0 ]
266
+
267
+ return (format , sample_rate , bits_per_sample , 44 + offset , sub_chunk2_size )
0 commit comments