Skip to content

Instantly share code, notes, and snippets.

@Bigjango13
Created May 20, 2024 02:53
Show Gist options
  • Save Bigjango13/298626fc6f4acadffb0ae333c8e42b84 to your computer and use it in GitHub Desktop.
Save Bigjango13/298626fc6f4acadffb0ae333c8e42b84 to your computer and use it in GitHub Desktop.
PCM/Wav converters
import sys, wave, struct
import lief
if len(sys.argv) < 2:
print("Usage: <file to find sounds in>")
sys.exit()
elf = lief.parse(sys.argv[1])
for sym in elf.dynamic_symbols:
if sym.name.startswith("PCM_"):
# Print name
print(sym, bool(elf.get_dynamic_symbol(sym.name)))
# Dump
data = bytes(elf.get_content_from_virtual_address(sym.value, sym.size))
wf = wave.open('dump/' + sym.name + ".wav", 'wb')
chan, samp, fr, nf = struct.unpack("iiii", data[:16])
wf.setnchannels(chan)
wf.setsampwidth(samp)
wf.setframerate(fr)
wf.setnframes(nf)
wf.writeframesraw(data[16:])
wf.close()
import os, sys, struct, wave
# Audio file to wav
os.system(f"ffmpeg -i {sys.argv[1]} output.wav")
# Wav to metadata + PCM data
w = wave.Wave_read("output.wav")
metadata = struct.pack(
"iiii",
w.getnchannels(),
w.getsampwidth(),
w.getframerate(),
w.getnframes()
)
body = metadata + w.readframes(w.getnframes())
# PCM to C
with open("output.c", "w+") as f:
print(f"unsigned char PCM_{sys.argv[2]}[] = {{", file = f, flush = 0)
for c in body:
print(c, file = f, end = ",", flush = 0)
print("0\n};", file = f);
# C to obj
print("Compiling...")
os.system(f"arm-linux-gnueabihf-gcc output.c -shared -fPIC -o PCM_{sys.argv[2]}")
# Remove everthing but the obj
os.system("rm output.wav output.c")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment