Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #converts uncompressed pcm .wav files to mode 0 .kpm
- if __name__ != "__main__": exit(0) #lol
- from time import time
- from sys import argv
- from os import listdir, system as cmd
- from os.path import isfile, join
- import wave
- #beeg line
- def getFilenames(fldr = "."): return [f for f in listdir(fldr) if isfile(join(fldr,f)) and f != argv[0].split("\\")[-1] and f.split(".")[-1] != "kpm"]
- def getArgPath(indx):
- try:
- return argv[indx]
- except IndexError:
- return "." #current directory
- def tryInt(string):
- try:
- return int(string)
- except ValueError:
- return None
- except TypeError:
- return None
- def replaceExtension(filename,extension):
- split = filename.split(".")[:-1]
- if len(split) == 0: return filename + extension
- else: return (".").join(split) + extension
- folderin = getArgPath(1)
- folderout = getArgPath(2)
- filenames = None
- try:
- filenames = getFilenames(folderin)
- except FileNotFoundError as err:
- print(err)
- exit(1)
- if len(filenames) == 0:
- print("folder \"{}\" is empty!".format(folderin))
- cmd("pause")
- exit(1)
- which = None
- while which == None:
- cmd("cls")
- print("input files in \"{}\":\n".format(folderin))
- for i in range(len(filenames)):
- print("{:2}: \"{}\"".format(i,filenames[i]))
- result = input("\npick a .wav to convert (0 -> {}): ".format(len(filenames)-1))
- which = tryInt(result)
- if which == None: continue
- if which<0 or which>=len(filenames): which = None
- startTime = time()
- #supported input SDL_AudioFormat types
- AUDIO_U8 = b'\x08\x00' #unsigned 8-bit samples
- AUDIO_S16 = b'\x10\x80' #signed 16-bit samples
- input_types = (None,AUDIO_U8,AUDIO_S16) #sorted by byte width
- input_types_str = (None,"AUDIO_U8","AUDIO_S16")
- fileinPath = join(folderin,filenames[which])
- filein = None
- try:
- filein = wave.open(fileinPath,"rb") #https://docs.python.org/3/library/wave.html
- except Exception as err:
- print(err)
- cmd("pause")
- exit(1)
- sampwidth = filein.getsampwidth()
- nframes = filein.getnframes()
- framerate = filein.getframerate()
- nchannels = filein.getnchannels()
- sample_data = filein.readframes(-1)
- filein.close()
- if sampwidth < 1 or sampwidth > 2:
- print("sample data is not of type AUDIO_U8 or AUDIO_S16!")
- cmd("pause")
- exit(1)
- magic = "kPCM" #4B
- format = input_types[sampwidth] #2B
- headerSize = 72 #2B
- dataSize = len(sample_data) #8B
- loopStart = 0 #8B
- loopEnd = nframes #8B
- numSamples = nframes #8B
- sampleRate = framerate #4B
- bitRate = framerate * sampwidth * nchannels * 8 #4B
- loopCount = 0 #2B
- channels = nchannels #2B
- bitRemainder = 0 #1B
- userflags = 0 #1B
- mode = 0 #2B
- userdata_a = 0 #8B
- userdata_b = 0 #8B
- #iirc byte objects are immutable, but the header is only 72 bytes anyway
- le = "little" #[l]ittle [e]ndian (i wish python had macros)
- header_data = bytes(magic,"ascii") # 0x00: magic
- header_data += format # 0x04: format
- header_data += headerSize.to_bytes( 2,le) # 0x06: headerSize
- header_data += dataSize.to_bytes( 8,le) # 0x08: dataSize
- header_data += loopStart.to_bytes( 8,le) # 0x10: loopStart
- header_data += loopEnd.to_bytes( 8,le) # 0x18: loopEnd
- header_data += numSamples.to_bytes( 8,le) # 0x20: numSamples
- header_data += sampleRate.to_bytes( 4,le) # 0x28: sampleRate
- header_data += bitRate.to_bytes( 4,le) # 0x2C: bitRate
- header_data += loopCount.to_bytes( 2,le) # 0x30: loopCount
- header_data += channels.to_bytes( 2,le) # 0x32: channels
- header_data += bitRemainder.to_bytes(1,le) # 0x34: bitRemainder
- header_data += userflags.to_bytes( 1,le) # 0x35: userflags
- header_data += mode.to_bytes( 2,le) # 0x36: mode
- header_data += userdata_a.to_bytes( 8,le) # 0x38: userdata_a
- header_data += userdata_b.to_bytes( 8,le) # 0x40: userdata_b
- fileoutPath = join(folderout,filenames[which])
- fileoutPath = replaceExtension(fileoutPath,".kpm")
- fileout = open(fileoutPath,"wb")
- fileout.write(header_data)
- fileout.write(sample_data)
- fileout.close()
- timeTakenMS = (time()-startTime)*1000
- print("\noutput file info:")
- print((" magic = \"{}\"").format(magic))
- print((" format = {}").format(input_types_str[sampwidth]))
- print((" headerSize = {}").format(headerSize))
- print((" dataSize = {}").format(dataSize))
- print((" loopStart = {}").format(loopStart))
- print((" loopEnd = {}").format(loopEnd))
- print((" numSamples = {}").format(numSamples))
- print((" sampleRate = {}").format(sampleRate))
- print((" bitRate = {}").format(bitRate))
- print((" loopCount = {}").format(loopCount))
- print((" channels = {}").format(channels))
- print((" bitRemainder = {}").format(bitRemainder))
- print((" userflags = {}").format(userflags))
- print((" mode = {}").format(mode))
- print((" userdata_a = {}").format(userdata_a))
- print((" userdata_b = {}").format(userdata_b))
- print("\nsaved to: \"{}\"".format(fileoutPath))
- print("time taken: {:.4}ms".format(timeTakenMS))
- print("\npress any key to exit")
- cmd("pause >nul")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement