-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvu-meter
executable file
·72 lines (64 loc) · 2 KB
/
vu-meter
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
#!/usr/bin/env python
#
# Adapted from code available here:
# https://blog.blinkenlight.net/experiments/basic-effects/vu-meter/
#
# Copyright 2016 Next Thing Co
# Copyright 2011 Udo Klein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/
import CHIP_IO.GPIO as gpio
import alsaaudio
import audioop
import math
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, 'default')
inp.setchannels(2)
inp.setrate(44100)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(1024)
lo = math.log(2000)
hi = math.log(20000)
pinmap = [3,2,1,0, 4,5,6,7]
val_max = len(pinmap)/2
val_min = 0
pinid = "CSID{0}"
retry=10
for x in pinmap:
gpio.setup(pinid.format(x), gpio.OUT)
while True:
l,data = inp.read()
if l:
try:
pins = []
lchannel=audioop.tomono(data, 2, 1, 0)
rchannel=audioop.tomono(data, 2, 0, 1)
# transform data to logarithmic scale
lvu = (math.log(float(max(audioop.max(lchannel, 2),1)))-lo)/(hi-lo)
rvu = (math.log(float(max(audioop.max(rchannel, 2),1)))-lo)/(hi-lo)
lval = min(max(int(lvu*val_max),val_min),val_max)
rval = min(max(int(rvu*val_max),val_min),val_max)
pins.extend( pinmap[0:lval] )
pins.extend( pinmap[val_max:val_max+rval] )
for x in pinmap:
if x in pins:
gpio.output(pinid.format(x), gpio.HIGH)
else:
gpio.output(pinid.format(x), gpio.LOW)
except:
if retry<=0:
print("Program Closed")
break
else:
retry -= 1
gpio.cleanup()