Skip to content

Commit

Permalink
Add the Normalization as a standalone script
Browse files Browse the repository at this point in the history
The wt-tool normalization is useful for raw i16 .wav files also so
add it as a standalone scrpit (albeit with a bit of copy and paste)
  • Loading branch information
baconpaul committed Sep 12, 2019
1 parent 0d338d8 commit 08cde89
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions scripts/wt-tool/normalize-i16-to-i15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Just the normalization path of the wave table code
# A bit of copy and paste going on here from wt-tool. Should fix that one day

from optparse import OptionParser
import wave


def main():
parser = OptionParser(usage="usage: % prog [options] infile outfile")
parser.add_option("-n", "--normalize", dest="normalize", default="half", metavar="MODE",
help="""(Create only) how to normalize input.
'half' input wav are divided by 2 (so 2^16 range becomes 2^15 range);
'peak' input wav are scanned and re-peaked to 2^15.
""")
(options, args) = parser.parse_args()
if(len(args) != 2):
print("Please specify an input file and output file\n")
parser.print_help()
return

inf = args[0]
outf = args[1]

with wave.open(inf, "rb") as wf:
c0 = wf.getnchannels()
fr = wf.getframerate()
sw = wf.getsampwidth()
nf = wf.getnframes()
wavdata = wf.readframes(nf * sw * c0)

if (c0 != 1):
raise RuntimeError("wt-tool only processes mono inputs")
if (fr != 44100):
raise RuntimeError("Please use 44.1k mono 16bit PCM wav files")
if (sw != 2):
raise RuntimeError("Please use 44.1k mono 16bit PCM wav files")

newrec = bytearray(len(wavdata))

if(options.normalize == "half"):
for i in range(int(len(wavdata) / 2)):
ls = wavdata[2 * i]
ms = wavdata[2 * i + 1]
if(ms >= 128):
ms -= 256
r = int((ls + ms * 256) / 2)

nls = int(r % 256)
nms = int(r / 256)

if(r < 0 and nls != 0):
nms -= 1
if(nms < 0):
nms += 256
newrec[2 * i] = nls
newrec[2 * i + 1] = nms

elif (options.normalize == "peak"):
peakp = 0
peakm = 0
d = wavdata
for i in range(int(len(d) / 2)):
ls = d[2 * i]
ms = d[2 * i + 1]
if(ms >= 128):
ms -= 256
r = int(ls + ms * 256)
if(r > peakp):
peakp = r
if(r < peakm):
peakm = r
if(-peakm > peakp):
peakp = -peakm
print("Peak value is ", peakp)
for i in range(int(len(d) / 2)):
ls = d[2 * i]
ms = d[2 * i + 1]
if(ms >= 128):
ms -= 256
r = int((ls + ms * 256) * 16384.0 / peakp)

nls = int(r % 256)
nms = int(r / 256)
if(r < 0 and nls != 0):
nms -= 1
if(nms < 0):
nms += 256
newrec[2 * i] = nls
newrec[2 * i + 1] = nms
else:
print("Unknown normalization ", options.normalize)
parser.print_help()

with wave.open(outf, "wb") as wav_file:
wav_file.setnchannels(1)
wav_file.setframerate(44100)
wav_file.setsampwidth(2)
wav_file.setnframes(int(len(newrec) / 2))

wav_file.writeframes(newrec)


if __name__ == "__main__":
main()

0 comments on commit 08cde89

Please sign in to comment.