-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc-info.py
85 lines (72 loc) · 2.64 KB
/
calc-info.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
82
83
84
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Yunzhao,Luke
import argparse
import time
import threading
#import _thread
import sys
import json
import time
import os
import codecs
import subprocess
import numpy as np
# subprocess.check_call("soxi -d *.wav", shell=True)
def main():
# ./calc-info.py -h
parser = argparse.ArgumentParser(description='Command line client for kaldigstserver')
parser.add_argument('-f', '--file', default="", dest="file", help="audio file")
parser.add_argument('-o', '--output', default="temp.txt", dest="output", help="Output of calculation")
parser.add_argument('-d', '--directory', default=".", dest="directory", help="directory to process")
parser.add_argument('-p', '--parallel', default=4, type=int, dest="parallel", help="parallel of threads")
args = parser.parse_args()
def chunks(lst, n):
#"""Yield successive n-sized chunks from lst."""
#for i in range(0, len(lst), n):
# yield lst[i:i + n]
n = max(1, n)
return (lst[i:i+n] for i in range(0, len(lst), n))
def doCalculate(result, wavfile):
wavinfo = subprocess.check_output("soxi -d "+wavfile, shell=True)
arr = wavinfo.strip().decode().split(":")
farr = np.array(arr).astype(np.float)
result.append(farr)
def runThread(result, wfiles):
for wf in wfiles:
doCalculate(result, wf)
result=[]
if args.file != "":
doCalculate(result, args.file)
else:
tmpfiles = os.listdir(args.directory)
wvfiles = []
for tf in tmpfiles:
if tf.endswith(".wav"):
wvfiles.append(os.path.join(args.directory, tf))
wl = len(wvfiles)
if wl == 0:
print("No wav files")
sys.exit()
#wlist = chunks(wvfiles,args.parallel)
wlist = np.array_split(wvfiles,args.parallel)
threads = []
for ti in range(args.parallel):
t = threading.Thread(target=runThread, args=(result, wlist[ti]))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
#print(result)
final = np.sum(result, axis=0)
#print("axis=0:",final)
num_secs = final[0]*3600 + final[1]*60.0 + final[2]
print("%d hh %d mm %0.2f ss"%(int(num_secs/3600),int((num_secs%3600)/60),num_secs%60.0))
#print("axis=1:",np.sum(result, axis=1))
if args.output:
with codecs.open(args.output, "wb", encoding="utf-8") as fw:
fw.write("%d hh %d mm %0.2f ss"%(int(num_secs/3600),int((num_secs%3600)/60),num_secs%60.0))
#fw.write(str(int(num_secs/3600))+"hh"+str(int((num_secs%3600)/60)+"mm"+(num_secs%60)+"ss")
if __name__ == "__main__":
main()