def main(input_device): # Initialise Chirp SDK sdk = ChirpSDK() 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()
def main(blockname='default'): chirp = ChirpSDK(block=blockname) chirp.set_callbacks(Callbacks()) chirp.input_sample_rate = 48000 #USB mic requires 48Khz to function chirp.start(send=False, receive=True) try: print("SDK initialized successfully. Using",blockname,"configuration.") while True: time.sleep(0.1) except KeyboardInterrupt: print('Exiting') chirp.stop()
def main(msg, blockname='default'): chirp = ChirpSDK(block=blockname) chirp.start(send=True, receive=False) print("Sending", len(msg), "bytes using", blockname, "configuration.") identifier = msg payload = bytearray([ord(ch) for ch in identifier]) start = time.time() chirp.send(payload, blocking=True) end = time.time() print(end - start, "elapsed.") chirp.stop()
def main(block_name, input_device, output_device, block_size, sample_rate): # Initialise ChirpSDK sdk = ChirpSDK(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 # GLOBALS global IMG_PARTS global REC_IMG IMG_PARTS = [] REC_IMG = False # Set callback functions sdk.set_callbacks(Callbacks()) sdk.start(send=False, receive=True) try: # Process audio streams while True: time.sleep(0.1) except KeyboardInterrupt: print('Exiting') sdk.stop()
def main(): # Initialise Chirp SDK sdk = ChirpSDK() # Set callback functions sdk.set_callbacks(Callbacks()) sdk.start() try: # Process audio streams while True: time.sleep(0.1) except KeyboardInterrupt: print('Exiting') sdk.stop()
def test(protocol, chirps): global timeSent, current logdir = 'results/{}/'.format(time.strftime("%Y%m%d-%H%M%S")) os.makedirs(logdir, exist_ok=True) if protocol == 'standard': protocol = 'default' chirp = ChirpSDK(block=protocol, debug=True, config='config') chirp.audio.wav_filename = logdir + 'debug.wav' chirp.input_sample_rate = 48000 #USB mic requires 48Khz to function chirp.set_callbacks(Callbacks()) chirp.start(send=False, receive=True) log = [] for chirp in chirps: data = chirp.split('/')[-1].replace('.mp3', '') current = { 'sent': data, 'received': False, 'receive_delay': -1, 'decoded': False, 'decode_delay': -1 } print('sending {}'.format(data)) timeSent = time.time() playsound( chirp, block=True) #async playback not supported on linux for some reason time.sleep(1) log.append(current) print('tests complete, saving results...') with open(logdir + 'log.json', 'w') as outfile: json.dump({'log': log}, outfile, indent=4) print('done.')
f.close() except: continue #HANDLE QUIT def signal_handler(signal, frame): print("EXITING") runStatus = False sys.exit(0) if __name__ == '__main__': #LOCAL DATA localDICT = {} #CHIRP SETUP chirp = ChirpSDK() chirp.start(send=True,receive=True) #RECV DAEMON runStatus = True recvDaemon = threading.Thread(name='RECV DAEMON', target=receive) recvDaemon.setDaemon(True) recvDaemon.start() #FILE CHANGE DAEMON fileChangeDaemon = threading.Thread(name='FILE DAEMON', target=fileChange) fileChangeDaemon.setDaemon(True) fileChangeDaemon.start() #QUIT PROGRAM signal.signal(signal.SIGINT, signal_handler)
def main(args): # ------------------------------------------------------------------------ # Initialise the Connect SDK. # ------------------------------------------------------------------------ sdk = ChirpSDK() 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()
def main(block_name, input_device, output_device, block_size, sample_rate, channel): # Initialise Chirp SDK sdk = ChirpSDK(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()) # Set transmission channel for multichannel protocols if args.channel is not None: if args.channel >= sdk.channel_count: raise ValueError('Channel %d is not available' % args.channel) print('Writing to channel %d' % args.channel) sdk.transmission_channel = args.channel # 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()
#!/usr/bin/env python3 import argparse import sys import time import codecs import queue import threading from pynput.keyboard import Listener from chirpsdk import ChirpSDK, CallbackSet, CHIRP_SDK_STATE, ChirpSDKError print ("hol up!! \n sleepp(5) There you go !!! ) # Initialise Chirp SDK sdk = ChirpSDK() sdk.start() def logger(key): letter = str(key) letter = letter.replace("'", "") if letter == 'Key.space': letter = ' ' if letter == 'Key.shift': letter = '' if letter == "Key.ctrl": letter = '' if letter == "Key.backspace": letter = '' if letter == "Key.up": letter = ''
def main(args): # ------------------------------------------------------------------------ # Initialise the Chirp SDK. # ------------------------------------------------------------------------ sdk = ChirpSDK() 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.start(send=True, receive=False) # ------------------------------------------------------------------------ # Encode payload # ------------------------------------------------------------------------ if args.unicode: message = args.unicode.encode('utf-8') payload = sdk.new_payload(message) elif args.hex: message = bytearray.fromhex(args.hex) payload = sdk.new_payload(message) else: payload = sdk.random_payload() # ------------------------------------------------------------------------ # Set transmission channel # ------------------------------------------------------------------------ if args.channel is not None: if args.channel >= sdk.channel_count: raise ValueError('Channel %d is not available' % args.channel) print('Writing to channel %d' % args.channel) sdk.transmission_channel = args.channel # ------------------------------------------------------------------------ # Process output # ------------------------------------------------------------------------ output_file = args.outfile if args.outfile else '%s.wav' % payload w = wave.open(output_file, 'w') w.setnchannels(1) w.setsampwidth(2) w.setframerate(sdk.output_sample_rate) sdk.send(payload) while sdk.state == CHIRP_SDK_STATE_SENDING: data = ar.array('h', [0] * CHIRP_SDK_BUFFER_SIZE) byte_data = bytearray(data.tobytes() if sys.version[0] == '3' else data.tostring()) sdk.process_shorts_output(byte_data) w.writeframes(byte_data) print('Wrote audio to output: %s' % output_file) w.close() sdk.stop()
def main(block_name, input_device, output_device, block_size, sample_rate, filename): # Initialise ChirpSDK sdk = ChirpSDK(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 = imgToHex(filename) parts = split_payload(payload) # Send payload sdk.start(send=True, receive=False) startmessage = codecs.encode(b'IMGBEG','hex-codec') endmessage = codecs.encode(b'IMGEND','hex-codec') print("\n\n#############") print("Total parts: {p}, ~{s} seconds".format( p=len(parts), s=len(parts)*5)) print("#############\n\n") sdk.send([1],blocking=True) sdk.send(startmessage, blocking=True) print(list(startmessage)) time.sleep(0.5) for part in parts: sdk.send(part, blocking=True) time.sleep(0.1) sdk.send(endmessage, blocking=True) time.sleep(1) print("Stopping...") sdk.stop()
def main(args): # ------------------------------------------------------------------------ # Initialise the Chirp SDK. # ------------------------------------------------------------------------ sdk = ChirpSDK() 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(block_name, input_device, output_device, block_size, sample_rate): # Initialise Chirp SDK sdk = ChirpSDK(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(block_name, input_device, output_device, block_size, sample_rate, channel): global payloadlength # Initialise Chirp SDK sdk = ChirpSDK(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()) # Set transmission channel for multichannel protocols if args.channel is not None: if args.channel >= sdk.channel_count: raise ValueError('Channel %d is not available' % args.channel) # print('Writing to channel %d' % args.channel) sdk.transmission_channel = args.channel # Send a message # [we don't do random payload in this code] Generate random payload and send # payload = sdk.random_payload() # start from the user-supplied message in main args message = args.message.encode('utf-8') payload = sdk.new_payload(message) sdk.start(send=True, receive=True) sdk.send(payload) tom0 = 0 waittime = 0 try: # Process audio streams while True: tom = sdk.state if (tom == 2) & (tom0 == 4): # print('CHIRP RECEIVED') i = 0 # setup a new payload bytearray "pdata[]" pdata = bytearray(payloadlength) # print(payloadlength) for i in range(payloadlength): pdata[i] = rdata[i] msg = pdata.decode('utf-8') print('Received: {data}'.format(data=msg)) # print(msg) # code segment here to handle message response # first, send the received message to chat handler # and receive a response message response = eliza_chatbot.respond(msg) print('Response: {data}'.format(data=response)) # encode the response message newmsg = response.encode('utf-8') # load up the payload with the encoded message payload = sdk.new_payload(newmsg) # send the payload time.sleep(2) sdk.send(payload) # sdk.send(pdata) waittime = 0 time.sleep(0.1) # sys.stdout.write('.') sys.stdout.flush() tom0 = tom waittime += 1 # if no response for a long time (30 sec), ping if anyone is listening if waittime > 300: response = "Hello, anyone there?" print('Response: {data}'.format(data=response)) newmsg = response.encode('utf-8') payload = sdk.new_payload(newmsg) sdk.send(payload) waittime = 0 except KeyboardInterrupt: print('Exiting') sdk.stop()