Wav file generation from numeric text with python

1.First of all

This is a python script that converts 44kSa/s voice data output as text into a wav file.

2. Overview

Use numpy and wave modules. Read the numerical data of the text with loadtxt of numpy and output it as a file with Wave_write of wave. On the way, DC offset removal, normalization and clipping processing are performed to make a signed 16-bit integer.

3. Script

Normalization is done with the maximum value. The volume will be louder with RMS, but this is to avoid distortion due to clipping. Since the argument is used at the time of execution, import sys is used.

txt2wav44k.py
import wave
import numpy as np
import sys

#usage:
#python3 txt2wav44k.py dat01.txt

aryArg = sys.argv
if len(aryArg) > 1:
	fnamel = aryArg[1]
else:
	fnamel ='dat00.txt'

fnames= fnamel[0:-3]+'wav'
fs=44100

strmOrg=np.loadtxt(fnamel)
N=len(strmOrg)
timlen=N/fs #sec

factScl = 0.90
strmOrg = strmOrg - np.mean(strmOrg)
myRms = np.sqrt(np.mean(strmOrg*strmOrg))
myMax = np.max(np.abs(strmOrg))


#streamNrm = strmOrg / myRms * factScl
streamNrm = strmOrg / myMax * factScl

streamPeakSup = np.where( streamNrm < 1.0 , streamNrm , 1.0)
streamPeakSup = np.where( streamPeakSup > -1.0 , streamPeakSup , -1.0)
streamOut = streamPeakSup

scaleFixedPoint = 32767
stream= np.int16(streamOut * scaleFixedPoint)

objw = wave.Wave_write(fnames)
objw.setnchannels(1)
objw.setsampwidth(2)
objw.setframerate(fs)
objw.writeframes(stream)
objw.close()

#from playsound import playsound
#playsound(fnames)

Recommended Posts