|
6 | 6 | import os |
7 | 7 | import time |
8 | 8 |
|
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 | | -''' |
50 | 9 |
|
51 | 10 | class WAVStream: |
52 | 11 | """ |
@@ -364,12 +323,15 @@ def play(self): |
364 | 323 | ) |
365 | 324 |
|
366 | 325 | 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 |
367 | 328 | # 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 |
370 | 332 |
|
371 | 333 | 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})") |
373 | 335 |
|
374 | 336 | if data_size > file_size - data_start: |
375 | 337 | data_size = file_size - data_start |
@@ -491,14 +453,10 @@ def play(self): |
491 | 453 | raw = self._upsample_buffer(raw, upsample_factor) |
492 | 454 |
|
493 | 455 | # 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) |
502 | 460 |
|
503 | 461 | # 4. Output to I2S (blocking write is OK - we're in a separate thread) |
504 | 462 | if self._i2s: |
|
0 commit comments