-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingpong.rb
executable file
·136 lines (101 loc) · 2.79 KB
/
pingpong.rb
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
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/home/pi/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
require 'rubygems'
require 'wav-file'
require 'sqlite3'
require 'spectrum-analyzer'
def get_value(rawSound, index)
return (rawSound[index].unpack('H*')[0].to_i(8) + (rawSound[index + 1].unpack('H*')[0].to_i(8) << 8)).to_f
end
def record_audio
`arecord -d 3 -f S16_LE -D plug:default /rss/out.wav`
end
def load_audio
if File.exists?('/rss/out.wav')
f = open('/rss/out.wav')
format = WavFile::readFormat(f)
data = WavFile::readDataChunk(f).data
f.close
return data
end
end
def delete_old_file
return system('rm /rss/out.wav') if File.exists?('/rss/out.wav')
end
def write_to_db
db_name = "/www/rails-sound-spy/db/production.sqlite3"
#Record Ping
begin
db = SQLite3::Database.open db_name
db.execute "INSERT INTO pings(time) VALUES (datetime('now'))"
rescue SQLite3::Exception => e
system("echo Pings error >> /rss/log")
system("echo #{e}>> /rss/log")
puts "Exception occured"
puts e
ensure
db.close if db
end
end
def detect_pings(rawSound)
amplitude_threshold = get_amplitude_threshold
for index in 0..((rawSound.length - 1) / 2)
if (get_value(rawSound, index * 2) > amplitude_threshold)
if (SpectrumAnalyzer.quick_analyze)
puts "#{Time.now} Ping Detected :: Amplitude: #{get_value(rawSound,index * 2)}"
system("echo #{Time.now} :: Amplitude: #{get_value(rawSound,index * 2)} >> /rss/log")
#Write it to the DB!
write_to_db
end
return
end
end
end
def main_loop
record_audio
rawSound = load_audio
detect_pings(rawSound) unless rawSound.nil?
delete_old_file
end
def get_amplitude_threshold
amplitude_threshold = 7000
set_configuration
begin
file = File.new '/www/rails-sound-spy/sensitivity.var', 'r'
rescue
#File doesn't exist.
end
unless file.nil?
while (line = file.gets)
begin
amplitude_threshold = line.to_i
rescue
#We don't have a number in here.
end
end
file.close
end
amplitude_threshold
end
def set_configuration
conf = load_configuration
unless conf == {}
SpectrumAnalyzer.configuration.window_size = conf[:window_size] unless conf[:window_size].blank?
SpectrumAnalyzer.configuration.window_function = conf[:window_function] unless conf[:window_function].blank?
SpectrumAnalyzer.configuration.analysis_range = conf[:analysis_range] unless conf[:analysis_range].blank?
SpectrumAnalyzer.configuration.file_name = conf[:file_name] unless conf[:file_name].blank?
end
end
def load_configuration
begin
return YAML.load_file('/rss/ping.yml')
rescue
return {}
end
end
#Create Log file if none
system('touch /rss/log')
SpectrumAnalyzer.configuration(load_configuration)
# RUN
while true
main_loop
end