Exemplo n.º 1
0
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(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()
Exemplo n.º 3
0
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()
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
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()
Exemplo n.º 7
0
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()
Exemplo n.º 8
0
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()
Exemplo n.º 9
0
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.')
Exemplo n.º 10
0
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()
Exemplo n.º 11
0
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()