Created
January 28, 2022 23:13
-
-
Save akarnokd/fdba4f814a342377482918e9d5dbb63b to your computer and use it in GitHub Desktop.
Python script to convert Imperium Galactica's SMP files into WAV files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example