Skip to content

Commit 156646e

Browse files
stream_wav: tweak audio input buffer
1 parent 47035ae commit 156646e

1 file changed

Lines changed: 10 additions & 52 deletions

File tree

internal_filesystem/lib/mpos/audio/stream_wav.py

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,6 @@
66
import os
77
import time
88

9-
'''
10-
@micropython.viper
11-
def _scale_audio_optimized(buf: ptr8, num_bytes: int, scale_fixed: int):
12-
if scale_fixed >= 32768:
13-
return
14-
if scale_fixed <= 0:
15-
for i in range(num_bytes):
16-
buf[i] = 0
17-
return
18-
19-
mask: int = scale_fixed
20-
21-
for i in range(0, num_bytes, 2):
22-
s: int = int(buf[i]) | (int(buf[i+1]) << 8)
23-
if s >= 0x8000:
24-
s -= 0x10000
25-
26-
r: int = 0
27-
if mask & 0x8000: r += s
28-
if mask & 0x4000: r += s>>1
29-
if mask & 0x2000: r += s>>2
30-
if mask & 0x1000: r += s>>3
31-
if mask & 0x0800: r += s>>4
32-
if mask & 0x0400: r += s>>5
33-
if mask & 0x0200: r += s>>6
34-
if mask & 0x0100: r += s>>7
35-
if mask & 0x0080: r += s>>8
36-
if mask & 0x0040: r += s>>9
37-
if mask & 0x0020: r += s>>10
38-
if mask & 0x0010: r += s>>11
39-
if mask & 0x0008: r += s>>12
40-
if mask & 0x0004: r += s>>13
41-
if mask & 0x0002: r += s>>14
42-
if mask & 0x0001: r += s>>15
43-
44-
if r > 32767: r = 32767
45-
if r < -32768: r = -32768
46-
47-
buf[i] = r & 0xFF
48-
buf[i+1] = (r >> 8) & 0xFF
49-
'''
509

5110
class WAVStream:
5211
"""
@@ -364,12 +323,15 @@ def play(self):
364323
)
365324

366325
self._playback_rate = playback_rate
326+
327+
# This influences how long it takes for the audio to start; low values: more responsive, high values: slow start but smoother
367328
# ibuf = playback_rate # doesnt account for stereo vs mono...
368-
ibuf = 8192
369-
#ibuf = 32000 # old setting
329+
#ibuf = 8192 * 1 # more jitters when playing audio and doing other things like updating the UI
330+
ibuf = 8192 * 4 # good balance between smooth audio and still responsive to volume changes
331+
#ibuf = 8192 * 8 # 64KiB seems to help for reducing stutter while playing music + QuasiBird
370332

371333
print(f"WAVStream: {original_rate} Hz, {bits_per_sample}-bit, {channels}-ch")
372-
print(f"WAVStream: Playback at {playback_rate} Hz (factor {upsample_factor})")
334+
print(f"WAVStream: Playback at {playback_rate} Hz (upsample factor {upsample_factor})")
373335

374336
if data_size > file_size - data_start:
375337
data_size = file_size - data_start
@@ -491,14 +453,10 @@ def play(self):
491453
raw = self._upsample_buffer(raw, upsample_factor)
492454

493455
# 3. Volume scaling via I2S native right-shift.
494-
volume_shift = self._volume_percent_to_shift(self.volume)
495-
if self._i2s and volume_shift > 0:
496-
self._i2s.shift(buf=raw, bits=16, shift=-volume_shift)
497-
498-
# The old volume scaling method, left here for comparison purposes:
499-
#scale = self.volume / 100.0
500-
#scale_fixed = int(scale * 32768)
501-
#_scale_audio_optimized(raw, len(raw), scale_fixed)
456+
if self.volume < 100:
457+
volume_shift = self._volume_percent_to_shift(self.volume)
458+
if self._i2s and volume_shift > 0:
459+
self._i2s.shift(buf=raw, bits=16, shift=-volume_shift)
502460

503461
# 4. Output to I2S (blocking write is OK - we're in a separate thread)
504462
if self._i2s:

0 commit comments

Comments
 (0)