-
Notifications
You must be signed in to change notification settings - Fork 0
/
realTimeLogAudio.py
85 lines (72 loc) · 2.09 KB
/
realTimeLogAudio.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
#!/usr/bin/python
import pyaudio
import numpy as np
from time import time
import os
import signal
import sys
CHANNELS = 2
RATE = 44100
FREQ = 100
NEWFREQ = 100
PHASE = 0
def signal_handler(sig, frame):
'''
Process control + C
'''
stream.stop_stream()
stream.close()
p.terminate()
print "Exiting program upon Control + C"
sys.exit(0)
def callback(in_data, frame_count, time_info, status):
'''
Callback function refreshes frequency audio stream
'''
global TT,PHASE,FREQ,NEWFREQ
if NEWFREQ != FREQ:
PHASE = 2*np.pi*TT*(FREQ-NEWFREQ)+PHASE
FREQ=NEWFREQ
left = (np.sin(PHASE+2*np.pi*FREQ*(TT+np.arange(frame_count)/float(RATE))))
data = np.zeros((left.shape[0]*2,),np.float32)
data[0::2] = left
data[1::2] = left
TT+=frame_count/float(RATE)
return (data, pyaudio.paContinue)
if __name__=="__main__":
if len(sys.argv) < 2:
print("Generated Audio Tone for variation in given log file .\n\nUsage: %s logfile.log" % sys.argv[0])
sys.exit(-1)
#LOG_FILE = "probemon.log"
LOG_FILE = sys.argv[1]
TT = time()
#Handle SIGINT
signal.signal(signal.SIGINT, signal_handler)
#Initialize pyaudio instance
p = pyaudio.PyAudio()
# define and start stream
stream = p.open(format=pyaudio.paFloat32,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
stream_callback=callback)
stream.start_stream()
#Logic to refresh global NEWFREQ var by probing LOG_FILE
tmphold = ""
try:
while True:
line = os.popen('tail -n 1 {}'.format(LOG_FILE)).read()
try:
key, val = line.split()
except:
key, val = "default", 0.0
f = abs(int(val))
NEWFREQ = f * 10 #update freq per log
if NEWFREQ != tmphold:
tmphold = NEWFREQ
print "mac:{} , rssi:{} , freq:{} Hz".format(key,val,NEWFREQ)
finally:
stream.stop_stream()
stream.close()
p.terminate()