Beispiel #1
0
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(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(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()
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.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_CONNECT_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()
Beispiel #6
0
class TestConnectSDK(unittest.TestCase):
    BUFFER_SIZE = 1024
    TEST_PAYLOAD_LENGTH = 3

    @classmethod
    def setUpClass(cls):
        config = configparser.ConfigParser()
        config.read(os.path.expanduser('~/.chirprc'))

        try:
            cls.app_key = config.get('test', 'app_key')
            cls.app_secret = config.get('test', 'app_secret')
            cls.app_config = config.get('test', 'app_config')
        except configparser.NoSectionError:
            raise Exception(
                "Couldn't find test credentials. Please add a [test] section to your ~/.chirprc."
            )

        cls.is2 = sys.version[0] == '2'

    def setUp(self):
        self.sdk = ChirpConnect(self.app_key, self.app_secret, self.app_config)
        self.sdk.audio = None

        self.async_request = patch(
            'requests_futures.sessions.FuturesSession.post')
        self.async_patch = self.async_request.start()
        self.addCleanup(self.async_request.stop)

        self.length = None
        self.channel = None

    def test_version(self):
        version = self.sdk.version
        for data in ['name', 'version', 'build']:
            self.assertIn(data, version)
            self.assertTrue(len(version[data]) > 0)

    def test_read_chirprc(self):
        self.sdk = ChirpConnect()
        self.sdk.read_chirprc('test')
        self.assertIsNotNone(self.sdk.config)

    # -- Getters & Setters

    def test_volume(self):
        self.assertEqual(self.sdk.volume, 1.0)

    def test_set_volume(self):
        self.sdk.volume = 0.33
        self.assertEqual(self.sdk.volume, 0.33)

    def test_input_sample_rate(self):
        self.assertEqual(self.sdk.input_sample_rate, 44100)

    def test_output_sample_rate(self):
        self.assertEqual(self.sdk.output_sample_rate, 44100)

    def test_set_input_sample_rate(self):
        self.sdk.input_sample_rate = 48000
        self.assertEqual(self.sdk.input_sample_rate, 48000)
        with self.assertRaises(ConnectError):
            self.sdk.input_sample_rate = 0

    def test_set_output_sample_rate(self):
        self.sdk.output_sample_rate = 48000
        self.assertEqual(self.sdk.output_sample_rate, 48000)
        with self.assertRaises(ConnectError):
            self.sdk.output_sample_rate = 0

    def test_default_state(self):
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_STOPPED)

    def test_get_auto_mute(self):
        self.assertTrue(self.sdk.auto_mute)

    def test_set_auto_mute(self):
        self.sdk.auto_mute = False
        self.assertFalse(self.sdk.auto_mute)

    def test_protocol_name(self):
        self.assertEqual(self.sdk.protocol_name, 'standard')

    def test_protocol_version(self):
        self.assertIsInstance(self.sdk.protocol_version, int)

    def test_protocol_duration(self):
        self.assertEqual(self.sdk.get_duration(10), 2.04)

    def test_expiry(self):
        self.assertIsInstance(self.sdk.expiry, datetime)

    def test_channel_count(self):
        self.assertEqual(self.sdk.channel_count, 1)

    def test_transmission_channel(self):
        self.assertEqual(self.sdk.transmission_channel, 0)

    def test_get_state_for_channel(self):
        self.sdk.start()
        self.assertEqual(self.sdk.get_state_for_channel(0),
                         CHIRP_CONNECT_STATE_RUNNING)
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.sdk.send(payload)
        self.assertEqual(self.sdk.get_state_for_channel(0),
                         CHIRP_CONNECT_STATE_SENDING)

    # -- States

    def test_not_created(self):
        with self.assertRaises(ConnectError):
            sdk = ChirpConnect(self.app_key, self.app_secret, '/not/real/path')
            self.assertEqual(sdk.state, CHIRP_CONNECT_STATE_NOT_CREATED)

    def test_start(self):
        self.sdk.start()
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_RUNNING)

    def test_already_started(self):
        self.sdk.start()
        with self.assertRaises(ConnectError):
            self.sdk.start()

    def test_pause(self):
        self.sdk.start()
        self.sdk.pause(True)
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_PAUSED)

    def test_unpause(self):
        self.sdk.start()
        self.sdk.pause(True)
        self.sdk.pause(False)
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_RUNNING)

    def test_already_paused(self):
        with self.assertRaises(ConnectError):
            self.sdk.pause(True)

    def test_stop(self):
        self.sdk.start()
        self.sdk.stop()
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_STOPPED)

    def test_already_stopped(self):
        with self.assertRaises(ConnectError):
            self.sdk.stop()

    # -- Callbacks

    def stub_connect_state_callback(self, old, new):
        self.old = old
        self.new = new

    def stub_connect_callback(self, payload, channel):
        self.length = len(payload)
        self.channel = channel

    def stub_receiving_callback(self, channel):
        self.recv = True
        self.channel = channel

    def test_state_changed_callback(self):
        self.sdk.callbacks.on_state_changed = self.stub_connect_state_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.old)
        self.assertIsNotNone(self.new)

    def test_sending_callback(self):
        self.sdk.callbacks.on_sending = self.stub_connect_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.length)
        self.assertIsNotNone(self.channel)

    def test_sent_callback(self):
        self.sdk.callbacks.on_sent = self.stub_connect_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.length)
        self.assertIsNotNone(self.channel)

    def test_receiving_callback(self):
        self.sdk.callbacks.on_receiving = self.stub_receiving_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertTrue(self.recv)
        self.assertIsNotNone(self.channel)

    def test_received_callback(self):
        self.sdk.callbacks.on_received = self.stub_connect_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.length)
        self.assertIsNotNone(self.channel)

    # -- Processing

    def test_process_input(self):
        indata = ar.array('f', [0.025] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_input(
            getattr(indata, 'tostring' if self.is2 else 'tobytes')())

    def test_process_input_not_started(self):
        indata = ar.array('f', [0.025] * self.BUFFER_SIZE)
        with self.assertRaises(ConnectError):
            self.sdk.process_input(
                getattr(indata, 'tostring' if self.is2 else 'tobytes')())

    def test_process_output(self):
        outdata = ar.array('f', [0.05] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_output(outdata)

    def test_process_output_not_started(self):
        outdata = ar.array('f', [0.05] * self.BUFFER_SIZE)
        with self.assertRaises(ConnectError):
            self.sdk.process_output(outdata)

    def test_process_shorts_input(self):
        indata = ar.array('h', [128] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_shorts_input(
            getattr(indata, 'tostring' if self.is2 else 'tobytes')())

    def test_process_shorts_output(self):
        outdata = ar.array('h', [-128] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_shorts_output(outdata)

    # -- Payload

    def test_get_max_payload_length(self):
        self.assertIsInstance(self.sdk.max_payload_length, int)
        self.assertTrue(self.sdk.max_payload_length > 0)

    def test_new_payload_string(self):
        payload = self.sdk.new_payload('test'.encode())
        self.assertIsInstance(payload, bytearray)

    def test_new_payload_array(self):
        payload = self.sdk.new_payload([64, 27, 33, 27])
        self.assertIsInstance(payload, bytearray)

    def test_random_payload(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertIsInstance(payload, bytearray)
        for byte in range(0, len(payload)):
            self.assertIsInstance(payload[byte], int)

    def test_pseudo_random_payload(self):
        self.sdk._set_seed(0)
        self.assertEqual(
            self.sdk.random_payload(0),
            [47, 117, 192, 67, 251, 195, 103, 9, 211, 21, 242, 36, 87])

    def test_is_valid(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertTrue(self.sdk.is_valid(payload))

    def test_payload_is_valid(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertTrue(payload.isvalid())

    def test_as_string(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertIsInstance(self.sdk.as_string(payload), str)

    def test_payload_as_string(self):
        payload = self.sdk.new_payload(b'hello')
        self.assertEqual(str(payload), '68656c6c6f')

    def test_send(self):
        self.sdk.start()
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertIsNone(self.sdk.send(payload))

    def test_null_payload(self):
        with self.assertRaises(ValueError):
            self.sdk.new_payload([])

    def test_payload_too_long(self):
        payload = self.sdk.new_payload('hello'.encode('ascii'))
        with self.assertRaises(ValueError):
            payload.extend('this-is-wayyyyy-toooooo-long')
Beispiel #7
0
from chirpsdk import ChirpConnect, CallbackSet
from resources import send_credential


class Callbacks(CallbackSet):
    def on_received(self, payload, channel):
        if payload is None:
            print('Decode failed!')
        else:
            payload = ''.join([chr(tmp) for tmp in payload])
            data = payload.split(':')
            send_audio_credential(data[0], data[1])


chirp = ChirpConnect()
chirp.set_callbacks(Callbacks())

chirp.start(send=False, receive=True)
try:
    while (True):
        pass
except KeyboardInterrupt:
    chirp.stop()
    print('Exiting...')
Beispiel #8
0
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()