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.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:
        if args.channel >= sdk.channel_count:
            raise ValueError('Channel %d is not available' % 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(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(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()