博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python分割8KHz16Bit44Header的Wave文件
阅读量:5123 次
发布时间:2019-06-13

本文共 3618 字,大约阅读时间需要 12 分钟。

#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]))

 

转载于:https://www.cnblogs.com/ariesblogs/p/4046594.html

你可能感兴趣的文章
Java同步锁——lock与synchronized 的区别【转】
查看>>
洛谷-校门外的树-数组
查看>>
Python--网络编程-----文件传输简单版本
查看>>
CF 208E. Blood Cousins [dsu on tree 倍增]
查看>>
趣谈面试(一)
查看>>
Quart2D setNeedsDisplay
查看>>
设计模式之策略设计模式
查看>>
Sql server 从一个表中获取数据更新到另一个表中
查看>>
JS继承的实现方式 原型 原型链 prototype和_proto_的区别
查看>>
[bzoj3622] 已经没有什么好害怕的了
查看>>
Objective-c 中 nil, Nil, NULL和NSNull的区别
查看>>
解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot
查看>>
NOIP2018退役记
查看>>
Oracle 11g Release 1 (11.1) SQL_层级查询(概)
查看>>
被查封7周之后,全球最大BT网站“海盗湾”又重新活过来了【36kr】
查看>>
partition by的用法
查看>>
消息传递
查看>>
第三次作业-功能测试
查看>>
(C++)浅谈using namespace std
查看>>
Http协议与生命周期
查看>>