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
|
import unittest
import sh
import wave
import contextlib
def calc_audio_duration(fname):
with contextlib.closing(wave.open(fname, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
f.close()
return duration
class Qaudio(unittest.TestCase):
params = None
__resultlist = None # resultlist is a python list of python dictionaries
__dtmf_secuence = ["1", "3", "5", "7", "9"]
def __init__(self, testname, testfunc, varlist):
self.params = varlist
super(Qaudio, self).__init__(testfunc)
self._testMethodDoc = testname
self.__resultlist = []
def execute(self):
# analize audio file
recordtime = calc_audio_duration("/var/lib/hwtest-files/dtmf-13579.wav") + 0.15
# previous play to estabilise audio lines
sh.aplay("/var/lib/hwtest-files/dtmf-13579.wav")
# play and record
p1 = sh.aplay("/var/lib/hwtest-files/dtmf-13579.wav", _bg=True)
p2 = sh.arecord("-r", 8000, "-d", recordtime, "/mnt/station_ramdisk/recorded.wav", _bg=True)
p1.wait()
p2.wait()
if p1.exit_code == 0 and p2.exit_code == 0:
p = sh.multimon("-t", "wav", "-a", "DTMF", "/mnt/station_ramdisk/recorded.wav", "-q")
if p.exit_code == 0:
lines = p.stdout.decode('ascii').splitlines()
self.__dtmf_secuence_result = []
for li in lines:
if li.split(" ")[0] == "DTMF:":
self.__dtmf_secuence_result.append(li.split(" ")[1])
# compare original and processed dtmf sequence
if len(self.__dtmf_secuence) == len(self.__dtmf_secuence_result):
for a, b in zip(self.__dtmf_secuence, self.__dtmf_secuence_result):
if a != b:
self.fail("failed: sent and received DTMF sequence don't match.")
else:
print(self.__dtmf_secuence_result)
self.fail("failed: received DTMF sequence is shorter than expected.")
else:
self.fail("failed: unable to use multimon command.")
else:
self.fail("failed: unable to play/record audio.")
def getresults(self):
return self.__resultlist
def gettextresult(self):
return ""
|