-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsilenciator.py
More file actions
executable file
·121 lines (103 loc) · 3.5 KB
/
Copy pathsilenciator.py
File metadata and controls
executable file
·121 lines (103 loc) · 3.5 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
## This is an example of a simple sound capture script.
##
## The script opens an ALSA pcm for sound capture. Set
## various attributes of the capture, and reads in a loop,
## Then prints the volume.
##
## To test it out, run it and shout at your microphone:
import alsaaudio, time, audioop
import datetime
from threading import Timer
from subprocess import Popen, STDOUT
from random import randint
import yaml
import sys
import os
import signal
import logging
LOG_FILENAME = 'silenciator.log'
logger = logging.getLogger(__name__)
logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO)
# Open the device in nonblocking capture mode. The last argument could
# just as well have been zero for blocking mode. Then we could have
# left out the sleep call in the bottom of the loop
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
# Set attributes: Mono, 8000 Hz, 16 bit little endian samples
inp.setchannels(1)
inp.setrate(8000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
# The period size controls the internal number of frames per period.
# The significance of this parameter is documented in the ALSA api.
# For our purposes, it is suficcient to know that reads from the device
# will return this many frames. Each frame being 2 bytes long.
# This means that the reads below will return either 320 bytes of data
# or 0 bytes of data. The latter is possible because we are in nonblocking
# mode.
#inp.setperiodsize(160)
inp.setperiodsize(3600)
##### Fin audio ######
aviso_t = datetime.datetime.now()
avisos_n = 0
config = None
def read_config(sgn=None, frame=None):
global config
logger.info("Leyendo config.yml")
with open("config.yml") as f:
config = yaml.load(f)
def clean_avisos():
global avisos_n
logger.info("avisos_n = 0")
avisos_n = 0
global hilo
hilo = Timer(config['clean_time'], clean_avisos)
hilo.start()
def play(audio):
ran = randint(0,len(audio)-1)
FNULL = open(os.devnull, 'w')
Popen([config['play'], os.path.join("sonidos",audio[ran])], stdout=FNULL, stderr=STDOUT)
def avisa_nivel():
logger.info("callarse! [%s]" % avisos_n)
if avisos_n <= 2:
play(config['n1_wav'])
elif avisos_n <= 4:
play(config['n2_wav'])
else:
play(config['n3_wav'])
def avisador():
"""
Se van contando avisos durante un periodo 'clean_time'
Se avisa como rapido cada 'wait_warns'
Segun se reciban mas avisos en un 'clean_time' se va subiendo el nivel de callarse
"""
global aviso_t
if (datetime.datetime.now() - aviso_t).seconds > config['wait_warns']:
aviso_t = datetime.datetime.now()
global avisos_n
avisos_n += 1
avisa_nivel()
if __name__ == '__main__':
read_config()
signal.signal(signal.SIGUSR1, read_config) # kill -10 <PID>
global hilo
hilo = Timer(config['clean_time'], clean_avisos)
hilo.start()
try:
counter = 1
while True:
# Read data from device
l,data = inp.read()
if l:
# Return the maximum of the absolute value of all samples in a fragment.
max_v = audioop.max(data, 2)
if counter % config['show'] == 0:
logger.info("Value: %s",max_v)
counter = 1
else:
counter += 1
if max_v > config['threshold']:
avisador()
time.sleep(.001)
except (KeyboardInterrupt, SystemExit):
hilo.cancel()
sys.exit(0)