def main(block_name, input_device, output_device, block_size, sample_rate): # Initialise ConnectSDK sdk = ChirpConnect(block=block_name) print(str(sdk)) print('Protocol: {protocol} [v{version}]'.format( protocol=sdk.protocol_name, version=sdk.protocol_version)) print(sdk.audio.query_devices()) # Configure audio sdk.audio.input_device = input_device sdk.audio.output_device = output_device sdk.audio.block_size = block_size sdk.input_sample_rate = sample_rate sdk.output_sample_rate = sample_rate # Set callback functions sdk.set_callbacks(Callbacks()) # Generate random payload and send payload = sdk.random_payload() sdk.start(send=True, receive=True) sdk.send(payload) try: # Process audio streams while True: time.sleep(0.1) sys.stdout.write('.') sys.stdout.flush() except KeyboardInterrupt: print('Exiting') sdk.stop()
def main(args): # ------------------------------------------------------------------------ # Initialise the Connect SDK. # ------------------------------------------------------------------------ sdk = ChirpConnect() print(str(sdk)) if args.network_config: sdk.set_config_from_network() print('Protocol: {protocol} [v{version}]'.format( protocol=sdk.protocol_name, version=sdk.protocol_version)) # ------------------------------------------------------------------------ # Disable audio playback. # ------------------------------------------------------------------------ sdk.audio = None sdk.set_callbacks(Callbacks(args)) sdk.start(send=False, receive=True) w = wave.open(args.infile, 'r') data = w.readframes(w.getnframes()) sdk.input_sample_rate = w.getframerate() for f in range(0, len(data), CHIRP_SDK_BUFFER_SIZE): if w.getsampwidth() == 2: sdk.process_shorts_input(data[f: f + CHIRP_SDK_BUFFER_SIZE]) elif w.getsampwidth() == 4: sdk.process_input(data[f: f + CHIRP_SDK_BUFFER_SIZE]) sdk.stop()
def main(args): # ------------------------------------------------------------------------ # Initialise the Connect SDK. # ------------------------------------------------------------------------ sdk = ChirpConnect() print(sdk.audio.query_devices()) print(str(sdk)) sdk.audio.output_device = args.o if args.network_config: sdk.set_config_from_network() if sdk.protocol_name != '16khz-mono': raise RuntimeError('Must use the 16khz-mono protocol ' + 'to be compatible with other Chirp Messenger apps.') # ------------------------------------------------------------------------ # Parse unicode and send as a chirp payload # ------------------------------------------------------------------------ message = args.message.encode('utf-8') payload = sdk.new_payload(message) sdk.volume = args.volume sdk.set_callbacks(Callbacks()) sdk.start() sdk.send(payload) try: # Process audio streams while True: time.sleep(0.1) except KeyboardInterrupt: print('Exiting') sdk.stop() sdk.close()
def main(input_device): # Initialise Connect SDK sdk = ChirpConnect() print(str(sdk)) print(sdk.audio.query_devices()) if sdk.protocol_name != 'standard': raise RuntimeError('Must use the standard protocol ' + 'to be compatible with other Chirp Messenger apps.') # Configure audio sdk.audio.frame_size = 4096 sdk.audio.input_device = input_device # Set callbacks and start SDK sdk.set_callbacks(Callbacks()) sdk.start(send=False, receive=True) try: # Process audio streams while True: time.sleep(1) except KeyboardInterrupt: print('Exiting') sdk.stop() sdk.close()
class Grillo: """ Tool to send data to a different computer or receive it, just using audio and mic. """ HEADER_SEPARATOR = b"|" FILE_NAME_SEPARATOR = b"<NAME>" def __init__(self, send=False, receive=False): """ Return an instance of a chirp thingymagic ready to be used. """ self.chirp = ChirpConnect( key=config.CHIRP_APP_KEY, secret=config.CHIRP_APP_SECRET, config=config.CHIRP_APP_CONFIG, ) self.chirp.set_callbacks(ChirpCallbacks(self)) self.listening = receive self.chirp.start(send=send, receive=receive) def send_message(self, kind, payload): """ Build a serialized message to send over audio. """ message = kind.value.encode( "utf-8") + Grillo.HEADER_SEPARATOR + payload if len(message) > 32: raise MessageTooLongException() self.chirp.send(message, blocking=True) def read_message(self, message): """ Read a serialized message received over audio. """ parts = message.split(Grillo.HEADER_SEPARATOR) kind = MessageKind(parts[0].decode("utf-8")) payload = Grillo.HEADER_SEPARATOR.join(parts[1:]) return kind, payload def send_text(self, text): """ Send text via audio. """ self.send_message(MessageKind.TEXT, text.encode("utf-8")) def send_clipboard(self): """ Send clipboard contents via audio. """ self.send_message(MessageKind.CLIPBOARD, pyperclip.paste().encode("utf-8")) def send_file(self, file_path): """ Send file contents via audio. """ if isinstance(file_path, str): file_path = Path(file_path) with file_path.open('rb') as file: file_contents = file.read() payload = (file_path.name.encode("utf-8") + Grillo.FILE_NAME_SEPARATOR + file_contents) self.send_message(MessageKind.FILE, payload) def listen(self, forever=False): """ Receive whatever data is being sent from the source computer. """ while self.listening or forever: time.sleep(1) def receive_message(self, message): """ Process an incoming message. """ kind, payload = self.read_message(message) if kind == MessageKind.TEXT: self.receive_text(payload) elif kind == MessageKind.CLIPBOARD: self.receive_clipboard(payload) elif kind == MessageKind.FILE: self.receive_file(payload) self.listening = False def receive_text(self, payload): """ Receive text via audio. """ text = payload.decode("utf-8") print("Received text:") print(text) def receive_clipboard(self, payload): """ Receive clipboard contents via audio. """ clipboard_contents = payload.decode("utf-8") pyperclip.copy(clipboard_contents) print("Received clipboard contents, copied to your own clipboard :)") def receive_file(self, payload): """ Receive file contents via audio. """ parts = payload.split(Grillo.FILE_NAME_SEPARATOR) name = parts[0].decode("utf-8") file_contents = Grillo.FILE_NAME_SEPARATOR.join(parts[1:]) file_path = Path(".") / name copy_counter = 0 while file_path.exists(): copy_counter += 1 file_path = Path(".") / str(copy_counter) + "_" + name with file_path.open('wb') as file: file.write(file_contents) print("Received a file, saved to", str(file_path))
import sys import time import json from chirpsdk import ChirpConnect, CallbackSet class Callbacks(CallbackSet): def on_received(self, payload, channel): if payload is not None: hex = payload.decode('utf-8') print(json.dumps({"data": hex, "type": "hex"})) else: print(json.dumps({"data": "Decode Failed", "type": "error"})) def on_receiving(self, channel): print(json.dumps({"data": "Listening...", "type": "listening"})) return super().on_receiving(channel) chirp = ChirpConnect(block='ultrasonic-multi-channel') chirp.start(receive=True) chirp.set_callbacks(Callbacks()) try: while True: time.sleep(0.1) sys.stdout.flush() except KeyboardInterrupt: print('Exiting')
def main(block_name, input_device, output_device, block_size, sample_rate, string): # Initialise ConnectSDK sdk = ChirpConnect(block=block_name) print(str(sdk)) print('Protocol: {protocol} [v{version}]'.format( protocol=sdk.protocol_name, version=sdk.protocol_version)) print(sdk.audio.query_devices()) # Configure audio sdk.audio.input_device = input_device sdk.audio.output_device = output_device sdk.audio.block_size = block_size sdk.input_sample_rate = sample_rate sdk.output_sample_rate = 44100 # Set callback functions sdk.set_callbacks(Callbacks()) # print("type your message") #msg = "" print(string) divideMsg8(string) #print(divMsg) # window = Tk() # messages = Text(window) # messages.pack() # # input_user = StringVar() # # input_field = Entry(window, text=input_user) # input_field.pack(side=BOTTOM, fill=X) # def Enter_pressed(event): # input_get = input_field.get() # global msg, divMsg # msg = input_get # divMsg = divideMsg8(string) # #print(input_get) # messages.insert(INSERT, '%s\n' % input_get) # # label = Label(window, text=input_get) # input_user.set('') # # label.pack() # return "break" # frame = Frame(window) # , width=300, height=300) # input_field.bind("<Return>", Enter_pressed) # frame.pack() # window.mainloop() #initialize SDK to SEND ONLY sdk.start(send=True, receive=True) timeLapse = 0 # numMsgSent = 0; try: # Process audio streams while True: if string != "xS$9!a6@": time.sleep(0.1) # sys.stdout.write('.') sys.stdout.flush() timeLapse += 1 if timeLapse % 50 == 0: timeLapse = 0 # if numMsgSent <= len(divMsg): if len(divMsg) > 0: # identifier = divMsg[numMsgSent] identifier = divMsg[0] payload = bytearray([ord(ch) for ch in identifier]) sdk.send(payload) divMsg.pop(0) # numMsgSent += 1 else: identifier = "@6a!9$Sx" payload = bytearray([ord(ch) for ch in identifier]) sdk.send(payload) time.sleep(5) break else: time.sleep(0.1) # sys.stdout.write('.') sys.stdout.flush() break except KeyboardInterrupt: print('Exiting') sdk.stop()
class Modem: """ An audio modem able to encode and decode data from/to audio. Internally uses chirp for the modulation/demodulation and error correction, but adding a layer that allows for messages longer than 32 bytes (sending multiple chirp messages for every grillo message). """ DATA_LEN = 30 PACKET_DURATION = timedelta(seconds=4.66) def __init__(self, with_confirmation=False): self.chirp = ChirpConnect( key=config.CHIRP_APP_KEY, secret=config.CHIRP_APP_SECRET, config=config.CHIRP_APP_CONFIG, ) self.chirp.start(send=True, receive=True) self.with_confirmation = with_confirmation self.reset_chained_status() def reset_chained_status(self): """ Reset the status of the chained message that is being received. """ self.chained_total_parts = None self.chained_parts = {} def send_message(self, message): """ Send a message as multiple packets. """ chain_len = self._get_chain_len(len(message)) if chain_len > 255: raise MessageTooLongException() packets_to_send = range(chain_len) while len(packets_to_send) > 0: self._send_packets(message, packets_to_send, chain_len) if self.with_confirmation: packets_to_send = self._get_packets_to_retry() else: break def _get_packets_to_retry(self): """ Wait for the other end to inform which parts of a message it didn't receive. """ packets_to_retry = [] ack_msg = self.receive_packet(self.PACKET_DURATION * 2) if ack_msg is None: return [] header = ack_msg[0] if header == 0: packets_to_retry = ack_msg[1:] return packets_to_retry else: raise MessageAckIsBroken() def _send_packets(self, message, packet_list, chain_len): """ Send a message as multiple packets, one after the other. """ for i in packet_list: packet = (bytes([chain_len, i]) + message[self.DATA_LEN * i:self.DATA_LEN * (i + 1)]) self.send_packet(packet) def send_packet(self, packet): """ Send a single packet. """ self.chirp.send(packet, blocking=True) def send_ack(self, missing_parts=None): """ Send a packet informing the missing parts of a chained message. """ if missing_parts is None: missing_parts = [] self.send_packet(bytes([0] + missing_parts)) def _get_chain_len(self, size): return size // self.DATA_LEN + 1 def receive_packet(self, timeout=None): """ Wait (blocking) for a single packet, and return it when received. """ receiver = SinglePacketReceiver() self.chirp.set_callbacks(receiver) start = datetime.now() while receiver.packet is None: time.sleep(0.1) if timeout: now = datetime.now() if now - start > timeout: break self.stop_listening() return receiver.packet def receive_message(self, timeout=300): """ Wait (blocking) for a single message, and return it when received. """ self.reset_chained_status() receiver = SinglePacketReceiver(callback=self.on_chained_part_received) self.chirp.set_callbacks(receiver) self.timeout_start = datetime.now() if timeout: self.timeout_delta = timedelta(seconds=timeout) chained_message = None last_expected_part = None while chained_message is None: time.sleep(0.1) if self.chained_total_parts is not None: if last_expected_part is None: last_expected_part = self.chained_total_parts - 1 if last_expected_part in self.chained_parts or self._timeout_expired( ): # finished receiving all the parts or should have finished and we didn't missing_parts = self.chained_missing_parts() if missing_parts: # we didn't get all the parts, ask for the missing ones parts_to_resend = missing_parts[:self.DATA_LEN] last_expected_part = parts_to_resend[-1] self.send_ack(parts_to_resend) self._reset_timeout() else: # stop the chained building loop, we got all the parts chained_message = self.chained_combine() self.send_ack() break if timeout and self._timeout_expired(): break self.stop_listening() self.reset_chained_status() return chained_message def _timeout_expired(self): now = datetime.now() return (now - self.timeout_start) > self.timeout_delta def on_chained_part_received(self, packet): """ Executed when chirp receives data that is part of a chained message. """ if packet is not None: total_parts = packet[0] part_number = packet[1] message_part = packet[2:] if self.chained_total_parts is None: # first part received! self.chained_total_parts = total_parts self.chained_parts[part_number] = message_part self._reset_timeout() def _reset_timeout(self): self.timeout_delta = self.PACKET_DURATION * 1.5 self.timeout_start = datetime.now() def chained_missing_parts(self): """ Which parts of the message are missing? """ return [ part_number for part_number in range(self.chained_total_parts) if part_number not in self.chained_parts ] def chained_combine(self): """ Concatenate all the message parts. """ return b''.join(self.chained_parts[part_number] for part_number in range(self.chained_total_parts)) def listen_for_packets(self, callback): """ Start listening for packets, calling a callback whenever a packet is received. """ receiver = SinglePacketReceiver(callback) self.chirp.set_callbacks(receiver) def listen_for_messages(self, callback): """ Start listening for messages, calling a callback whenever a packet is received. """ while True: message = self.receive_message() callback(message) self.reset_chained_status() def stop_listening(self): """ Stop using chirp to listen for packets. """ self.chirp.set_callbacks(NoCallbacks())