gist-404217398b4f35263b5af3.../pkg2wav.py
2022-04-06 04:00:53 -05:00

106 lines
3.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
"""
import argparse
import struct
from collections import namedtuple
from os import system, remove
from os.path import join
from sys import exit
swavHeader = namedtuple("swavHeader",
"magic byteOrder version fileSize headerSize numBlocks data dataSize waveType loopFlag samplingRate time loopOffset soundLen"
)
swavFormat = "<LHHLHHLLBBHHHL"
def pkg2png(args):
for input in args.inputs:
print(input.name)
type, fileCount = struct.unpack("<LL", input.read(8))
if type == 0:
print("Error! This is a graphics pkg")
exit()
elif type != 1:
print(f"Error! This is not an audio pkg (type {type})")
exit()
# File names
files = []
for _ in range(fileCount):
strLen = struct.unpack("B", input.read(1))[0]
files.append(input.read(strLen).decode("utf-8"))
# Sound data
for output in files:
# Music or Effects
strLen = struct.unpack("B", input.read(1))[0]
type = input.read(strLen).decode("utf-8")
print(type)
outputName = f"{type}.{output}.raw"
print(outputName)
_, size = struct.unpack("<LL", input.read(8))
print(_) # always 1?
# SWAV header
header = swavHeader._make(struct.unpack(swavFormat, input.read(36)))
outputPath = join(args.output, outputName) if args.output else outputName
with open(outputPath, "wb") as f:
f.write(input.read(header.dataSize - 0x14))
if header.waveType == 0:
format = "s8"
codec = "pcm_s8"
elif header.waveType == 1:
format = "s16le"
codec = "pcm_s16le"
elif header.waveType == 2:
format = "s16le"
codec = "adpcm_ima_alp"
else:
print("Error: Invalid wave type")
exit()
system(f"ffmpeg -y -loglevel error -f {format} -acodec {codec} -ar {header.samplingRate} -i '{outputPath}' '{outputPath[:outputPath.rfind('.')]}.wav'")
remove(outputPath)
if __name__ == "__main__":
pkg2pngarg = argparse.ArgumentParser(description="Converts a PKG file to SWAVs")
pkg2pngarg.add_argument("inputs", metavar="in.pkg", nargs="*", type=argparse.FileType("rb"), help="input file(s)")
pkg2pngarg.add_argument("--output", "-o", metavar="out", type=str, help="output directory")
exit(pkg2png(pkg2pngarg.parse_args()))