Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Created January 28, 2022 23:13
Show Gist options
  • Save akarnokd/fdba4f814a342377482918e9d5dbb63b to your computer and use it in GitHub Desktop.
Save akarnokd/fdba4f814a342377482918e9d5dbb63b to your computer and use it in GitHub Desktop.
Python script to convert Imperium Galactica's SMP files into WAV files
import sys
import os.path
from os import listdir
from os.path import isfile, join
def int_to_bytes(uint):
return bytearray([
(uint & 0xFF),
((uint >> 8) & 0xFF),
((uint >> 16) & 0xFF),
((uint >> 24) & 0xFF),
])
def short_to_bytes(uint):
return bytearray([
(uint & 0xFF),
((uint >> 8) & 0xFF),
])
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage\r\nsmp_to_wav.py source_path")
exit(1)
inputDir = sys.argv[1]
onlyFiles = [f for f in listdir(inputDir) if isfile(join(sys.argv[1], f)) & f.lower().endswith(".smp")]
for inf in onlyFiles:
inputFile = inputDir + os.path.sep + inf
outputFile = inputFile + ".wav"
print("Input file " + inputFile)
print("Output file " + outputFile)
os.makedirs(os.path.split(outputFile)[0], exist_ok=True)
inputLen = os.path.getsize(inputFile)
if inputLen % 2 != 0:
inputLen = inputLen + 1
handle_input = open(inputFile, "rb")
data_input = handle_input.read()
handle_input.close()
handle_output = open(outputFile, "wb")
handle_output.write("RIFF".encode("latin1"))
handle_output.write(int_to_bytes(36 + inputLen))
handle_output.write("WAVE".encode("latin1"))
handle_output.write("fmt ".encode("latin1"))
handle_output.write(int_to_bytes(16))
handle_output.write(short_to_bytes(1))
handle_output.write(short_to_bytes(1))
handle_output.write(int_to_bytes(22050))
handle_output.write(int_to_bytes(22050))
handle_output.write(short_to_bytes(1))
handle_output.write(short_to_bytes(8))
handle_output.write("data".encode("latin1"))
handle_output.write(int_to_bytes(inputLen))
for b in data_input:
handle_output.write(bytearray([(b + 128) & 0xFF]))
if len(data_input) != inputLen:
handle_output.write(bytearray([128]))
handle_output.close()
print("Done.\n")
@akarnokd
Copy link
Author

Usage example

python main.py "c:\\Games\\Imperium Galactica\\SOUND"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment