-
Notifications
You must be signed in to change notification settings - Fork 0
/
waverms.py
98 lines (85 loc) · 2.91 KB
/
waverms.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#! /usr/bin/python3
# -*- coding: utf-8 -*-
# Fabien Marteau <mail@fabienm.eu>
""" waverms
"""
import numpy as np
import sqlite3
import audioop
import getopt
import time
import wave
import sys
import os
class waverms(object):
"""
"""
def usage():
print("Usage:")
print("$ python3 waverms [options]")
print("-h, --help print this help message")
print("-n, --name give prefix name of records(default rec)")
print("-d, --dir give directory to record wavefiles (default hdata)")
print("-c, --csvname give the csv name file (default rms.csv)")
print("-s, --sqlitefile give sqlite3 filename")
if __name__ == "__main__":
print("waverms class test\n")
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
try:
opts, args = getopt.getopt(sys.argv[1:], "hn:t:d:s:",
["help", "name=",
"time=", "dir=",
"sqlitefile="])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
wavename = "rec"
hdata = "hdata"
csvname = "rms.csv"
sqlitefile = "housepower.db"
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-n", "--name"):
wavename = arg
elif opt in ("-t", "--time"):
wavetime = int(arg)
elif opt in ("-d", "--dir"):
hdata = arg
elif opt in ("-s", "--sqlitefile"):
sqlitefile = arg
while True:
wavelist = [value for value in os.listdir(hdata) if wavename == value.split("-")[0]]
if wavelist == []:
print("No new file, waiting")
time.sleep(30)
continue
fsql = sqlite3.connect(sqlitefile)
cursor = fsql.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS rms
(id INTEGER PRIMARY KEY AUTOINCREMENT,
recordtype VARCHAR(20), rmsvalue INTEGER,
recordtime INTEGER);""")
if not os.path.exists(csvname):
fcsv = open(csvname, "w")
fcsv.write("epoch_time, rms\n")
else:
fcsv = open(csvname, "a+")
for filename in wavelist:
pathfilename = "/".join([hdata,filename])
with wave.open(pathfilename, "r") as spf:
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int16')
rms = audioop.rms(signal, 2)
epoch_time = filename.split("-")[1].split(".")[0]
fcsv.write("{}, {}\n".format(epoch_time, rms))
cursor.execute("INSERT INTO rms VALUES (NULL, 'house', {}, {});"
.format(rms, epoch_time))
print("{} -> {}".format(epoch_time, rms))
os.remove(pathfilename)
fsql.commit()
fsql.close()
fcsv.close()