Created
April 4, 2017 03:36
-
-
Save jordam/98961c9ad0d384b71743c595bd2e24e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Stupid simple buggy radio signal location tool in python | |
import pyshark | |
import numpy | |
import pyaudio | |
import math | |
import time | |
class ToneGenerator(object): | |
def __init__(self, samplerate=44100, frames_per_buffer=4410): | |
self.p = pyaudio.PyAudio() | |
self.samplerate = samplerate | |
self.frames_per_buffer = frames_per_buffer | |
self.streamOpen = False | |
def sinewave(self): | |
if self.buffer_offset + self.frames_per_buffer - 1 > self.x_max: | |
# We don't need a full buffer or audio so pad the end with 0's | |
xs = numpy.arange(self.buffer_offset, | |
self.x_max) | |
tmp = self.amplitude * numpy.sin(xs * self.omega) | |
out = numpy.append(tmp, | |
numpy.zeros(self.frames_per_buffer - len(tmp))) | |
else: | |
xs = numpy.arange(self.buffer_offset, | |
self.buffer_offset + self.frames_per_buffer) | |
out = self.amplitude * numpy.sin(xs * self.omega) | |
self.buffer_offset += self.frames_per_buffer | |
return out | |
def callback(self, in_data, frame_count, time_info, status): | |
if self.buffer_offset < self.x_max: | |
data = self.sinewave().astype(numpy.float32) | |
return (data.tostring(), pyaudio.paContinue) | |
else: | |
return (None, pyaudio.paComplete) | |
def is_playing(self): | |
if self.stream.is_active(): | |
return True | |
else: | |
if self.streamOpen: | |
self.stream.stop_stream() | |
self.stream.close() | |
self.streamOpen = False | |
return False | |
def play(self, frequency, duration, amplitude): | |
self.omega = float(frequency) * (math.pi * 2) / self.samplerate | |
self.amplitude = amplitude | |
self.buffer_offset = 0 | |
self.streamOpen = True | |
self.x_max = math.ceil(self.samplerate * duration) - 1 | |
self.stream = self.p.open(format=pyaudio.paFloat32, | |
channels=1, | |
rate=self.samplerate, | |
output=True, | |
frames_per_buffer=self.frames_per_buffer, | |
stream_callback=self.callback) | |
#generator = ToneGenerator() | |
lasthit = time.time() | |
def packet_captured(packet): | |
global lasthit | |
if time.time() - lasthit > 0.1: | |
lasthit = time.time() | |
global generator | |
x = packet.wlan_radio.signal_dbm | |
ical = int(x)*20 | |
ifreq = 2000 + ical | |
print ifreq, x | |
try: | |
generator.play(ifreq, 0.5, 1) | |
except: | |
generator = ToneGenerator() | |
generator.play(20, 0.2, 1) | |
pass | |
#print 'Just arrived:', packet | |
want = "00:80:92:c3:e9:46" | |
want = want.lower() | |
print want | |
def main(): | |
#generator.play(1000, 0.1, 1) | |
capture = pyshark.LiveCapture(interface='mon0', display_filter='wlan.sa == ' + want) | |
#while 1: | |
# for packet in capture.sniff_continuously(packet_count=1): | |
# packet_captured(packet) | |
capture.apply_on_packets(packet_captured) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment