码迷,mamicode.com
首页 > 编程语言 > 详细

python分割8KHz16Bit44Header的Wave文件

时间:2014-10-23 20:41:33      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   os   ar   for   sp   文件   on   

#encoding=GBK

import os

import sys

import string

from struct import pack, unpack

‘‘‘

[52 49 46 46]   [char ]  [4bytes]‘RIFF‘       –RIFF file identification

[E4 6C 00 00]   [int  ]  [4bytes]<length>     –File length field

[57 41 56 45]   [char ]  [4bytes]‘WAVE‘       –WAVE chunk identification

[66 6D 74 20]   [char ]  [4bytes]‘fmt‘        –format sub-chunk identification

[10 00 00 00]   [int  ]  [4bytes]flength      –length of format sub-chunk

[01 00]         [short]  [2bytes]format       –format specifier

[01 00]         [short]  [2bytes]chans        –number of channels

[40 1F 00 00]   [int  ]  [4bytes]sampsRate    –sample rate in Hz

[80 3E 00 00]   [int  ]  [4bytes]bpsec        –bytes per second

[02 00]         [short]  [2bytes]bpsample     –bytes per sample

[10 00]         [short]  [2bytes]bpchan       –bits per channel

[64 61 74 61]   [char ]  [4bytes]‘data‘       –data sub-chunk identification

[C0 6C 00 00]   [int  ]  [4bytes]dlength      –length of data sub-chuk

 

#30 00 00 00 -- 00 00 00 30 -- 0 * 16^0 + 3 * 16^1                        = 48

#40 1F 00 00 -- 00 00 1F 40 -- 0 * 16^0 + 4 * 16^1 + 15 * 16^2 + 1 * 16^3 = 8000

 

wavheader = ‘4si4s4sihhiihh4si‘

https://docs.python.org/release/2.6.6/library/struct.html?highlight=pack#struct.pack

‘‘‘

class normalWave():

headerIde = ‘4si4s4sihhiihh4si‘

wavename  = ""

waveobj   = ""

wavelen   = 0 

 

def __init__(self, wavename):

self.wavename = wavename

self.waveobj = open(self.wavename,"rb")

 

def getheaderinfo(self):

self.waveobj.seek(0)

sequ = list(unpack(self.headerIde, self.waveobj.read(44)))

self.wavelen = round(sequ[-1] * 1.0 / sequ[-6] / 2.0, 8)

return sequ

 

def getchildwave(self, headerdata, name, cutstime, cutetime, expandTime):

expandTime = expandTime / 1000.0

 

if cutstime > 0:

cutstime = cutstime - expandTime if cutstime - expandTime >= 0 else cutstime

 

if cutetime <= 0 or cutetime <= cutstime:

return

else:

cutetime = cutetime + expandTime if cutetime + expandTime <= self.wavelen else cutetime

 

dataLen = (int)(round((cutetime - cutstime) * 8000 * 2, 8))

sBytes = (int)(round(cutstime * 8000 * 2, 8)) + 44

 

if sBytes % 2 != 0:

sBytes += 1

if dataLen % 2 != 0:

dataLen -= 1

 

#print cutstime,cutetime, self.wavelen, sBytes - 44,sBytes, dataLen

 

if dataLen <= 0: return

 

headerdata[1] = dataLen + 36

headerdata[-1] = dataLen

 

child = open(name,"w")

child.write(pack(self.headerIde, *headerdata))

self.waveobj.seek(sBytes)

child.write(self.waveobj.read(dataLen))

child.close()

 

def close(self):

self.waveobj.close()

 

def cutwavbyInfo(wavename, cutinfolist, expandTime, dir):

for line in cutinfolist:

childname = ""

stime     = 0.0

etime     = 0.0

childArr = line.strip().split("\t")

if len(childArr) < 2:continue

childname = dir + "/" + childArr[0]

timeArr = childArr[1].replace("[","").replace("]","").split(",")

if len(timeArr) < 2:continue

stime = string.atof(timeArr[0])

etime = string.atof(timeArr[1])

 

wave = normalWave(wavename)

header = wave.getheaderinfo()

wave.getchildwave(header, childname, stime, etime, expandTime)

wave.close()

 

def readCutInfoFile(file, dir, expandTime):

os.system("rm -rf " + dir + "; mkdir " + dir)

fileObj = open(file,"r")

index = 0

wavpath = ""

childlist = []

for line in fileObj:

line = line.strip()

if line == ".":

cutwavbyInfo(wavpath, childlist, expandTime, dir)

index = 0

wavpath = ""

childlist = []

else:

index += 1

if index == 1:

wavpath = line

else:

childlist.append(line)

if len(wavpath) > 0 and len(childlist) > 0:

cutwavbyInfo(wavpath, childlist, expandTime, dir)

 

if __name__ == ‘__main__‘:

if len(sys.argv) != 4:

print "Usage cutInfoFile[in] cutWavDir[in] expandTime[in]"

print " cutInfoFile: x_1.wav\t[0,2.23]"

print " expandTime : 50/100/200(ms)"

sys.exit(-1)

 

readCutInfoFile(sys.argv[1],sys.argv[2],string.atoi(sys.argv[3]))

 

python分割8KHz16Bit44Header的Wave文件

标签:style   http   io   os   ar   for   sp   文件   on   

原文地址:http://www.cnblogs.com/ariesblogs/p/4046594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!