-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwav.py
82 lines (72 loc) · 2.16 KB
/
wav.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
##
# Custom wave module,limited support
##
from struct import *
class Wave():
""" Custom wave class """
def __init__(self,filename):
self.information = None
self.error = False
self.filename = filename
f = open(self.filename,'r')
data = f.read(12)
riffchunk = unpack('<4sI4s',data)
if riffchunk[0] != "RIFF":
self.error = True
elif riffchunk[2] != "WAVE":
self.error = True
if self.error is False:
self.chunksize = riffchunk[1]
while(True):
data = f.read(8)
subchunk = unpack('<4sI',data)
if subchunk[0] == "fmt ":
data = f.read(subchunk[1])
self.information = unpack('<HHIIHHH',data)
elif subchunk[0] == "data":
self.nframes = subchunk[1]
break
else:
f.seek(subchunk[1],1)
f.close()
def getnframes(self):
if self.error is False:
return self.nframes
else:
return 0
def getchannels(self):
if self.error is False:
return self.information[1]
else:
return 0
def getsamplerate(self):
if self.error is False:
return self.information[2]
else:
return 0
def getbitrate(self):
if self.error is False:
return self.information[3]
else:
return 0
def getsamplewidth(self):
if self.error is False:
return self.information[5]
else:
return 0
def getdata(self):
data = None
if self.error is False:
f = open(self.filename,'r')
f.seek(self.chunksize + 8 - self.nframes,1)
data = f.read()
f.close()
return data
def getDuration(self):
"""" Duration of the wav-file in seconds"""
frames = self.getnframes()
framesInSecond = float(self.getsamplerate())
if framesInSecond != 0:
return int(round(frames/framesInSecond))
else:
return 0