change to 16khz, add audio filter, refactor to use DMA

This commit is contained in:
zyphlar
2026-07-04 03:28:13 -07:00
parent 8a6d096635
commit 0697c3d6f3
4 changed files with 333 additions and 201 deletions
+6 -6
View File
@@ -44,12 +44,12 @@ def convert_to_pcm(path: str) -> bytes:
sample_rate = struct.unpack_from("<I", data, 24)[0]
bits_per_samp = struct.unpack_from("<H", data, 34)[0]
needs_convert = (audio_format != 1 or num_channels != 1
or sample_rate != 8000 or bits_per_samp != 8)
or sample_rate != 16000 or bits_per_samp != 8)
except Exception:
needs_convert = True
if not needs_convert:
print("WAV is already 8-bit mono 8kHz — stripping header")
print("WAV is already 8-bit mono 16kHz — stripping header")
return data[44:]
print("WAV needs resampling — converting via ffmpeg")
# fall through to ffmpeg conversion
@@ -64,14 +64,14 @@ def convert_to_pcm(path: str) -> bytes:
print(f"Converting {os.path.basename(path)} via ffmpeg...")
result = subprocess.run(
['ffmpeg', '-y', '-i', path,
'-ar', '8000', '-ac', '1', '-f', 'u8', '-acodec', 'pcm_u8', 'pipe:1'],
'-ar', '16000', '-ac', '1', '-f', 'u8', '-acodec', 'pcm_u8', 'pipe:1'],
capture_output=True
)
if result.returncode != 0:
sys.exit(f"ERROR: ffmpeg failed:\n{result.stderr.decode(errors='replace')[-400:]}")
pcm = result.stdout
duration = len(pcm) / 8000
duration = len(pcm) / 16000
print(f" Converted: {len(pcm):,} bytes ({duration:.1f}s)")
return pcm
@@ -133,10 +133,10 @@ def upload(port: str, track_num: int, pcm: bytes) -> None:
if "at=" in err_line:
accepted = int(err_line.split("at=")[1].split()[0])
safe = int(accepted * 0.9) # 10% headroom
max_s = safe / 8000
max_s = safe / 16000
print(f" LittleFS accepted {accepted} bytes before full.")
print(f" Safe clip length: ~{max_s:.1f}s")
print(f" Trim with: ffmpeg -i input.mp3 -t {max_s:.0f} -ar 8000 -ac 1 -f u8 out.raw")
print(f" Trim with: ffmpeg -i input.mp3 -t {max_s:.0f} -ar 16000 -ac 1 -f u8 out.raw")
sys.exit(1)
# Wait for OK