# How to pass python BytesIO() stream to Precise Runner?

**URL:** https://community.openconversational.ai/t/how-to-pass-python-bytesio-stream-to-precise-runner/11811
**Category:** General Discussion
**Created:** [January 22, 2022, 5:32pm UTC](https://community.openconversational.ai/t/how-to-pass-python-bytesio-stream-to-precise-runner/11811 "2022-01-22T17:32:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ScottMonaghan](https://community.openconversational.ai/user_avatar/community.openconversational.ai/scottmonaghan/32/3516_2.png) [@ScottMonaghan](https://community.openconversational.ai/u/ScottMonaghan)
#### Post date: [January 22, 2022, 5:32pm UTC](https://community.openconversational.ai/t/how-to-pass-python-bytesio-stream-to-precise-runner/11811/1 "2022-01-22T17:32:39Z")

</div>

Hello,

I’ve been testing Precise as a wake-word engine for my companion robot project ([GitHub - ScottMonaghan/robud: Loveable Accessible Autonomous Robot](https://github.com/scottmonaghan/robud)).

If I create a stream object using pyaudio.open, I’m able to specify my input device, pass it into the precise runner in python and it works great!

The problem is, I can’t allow the precise python script to take full control of my audio card because it’s used by several other processes.

The way that I handle this is that I publish the raw input bytestream with the Moquitto MQTT messaging system.

With other tools I’m able to subscribe to that stream and then use it the audio however I wish, but with precise I’ve been having trouble.

I’m able to subscribe and write the stream to a BytesIO object, and I can specify the BytesIO object as the stream when initializing my runner.

But when I start the runner, I get an error stating there are 0 frames.

Can anyone offer any advice on how to achieve this?

Here are the related python snippets:

```auto
def on_message_audio_input_data(client:mqtt.Client, userdata, message):
        #print("audio recieived")
        audio_buffer:BytesIO = userdata["audio_buffer"]
        audio_buffer.write(message.payload)

    #Create audio buffer
    audio_buffer = BytesIO()

    client_userdata = {"audio_buffer":audio_buffer}
    mqtt_client = mqtt.Client(client_id=MQTT_CLIENT_NAME,userdata=client_userdata)
    mqtt_client.connect(MQTT_BROKER_ADDRESS)
    logger.info('MQTT Client Connected')
    mqtt_client.subscribe(TOPIC_AUDIO_INPUT_DATA)
    mqtt_client.message_callback_add(TOPIC_AUDIO_INPUT_DATA, on_message_audio_input_data)
    logger.info('Subscribed to ' + TOPIC_AUDIO_INPUT_DATA)
    #stream.start_stream()
    logger.info('Waiting for messages...')
    mqtt_client.loop_start()

    #Create audio buffer
    #audio_buffer = BytesIO()

    engine = PreciseEngine('/home/robud/Downloads/precise-engine/precise-engine', '/home/robud/src/precise-data/hey-mycroft.pb')

    runner = PreciseRunner(engine=engine, stream=audio_buffer, on_activation=lambda: print('activated!'))
    #runner.start()

    # Sleep forever
    from time import sleep
    while True:
        if audio_buffer.getbuffer().nbytes > CHUNK and not(runner.running): #2 bytes for each 16bit frame
            logger.info('runner started')
            runner.start()
        sleep(0.1)

```

---

<div class="post-metadata">

### Author: ![ScottMonaghan](https://community.openconversational.ai/user_avatar/community.openconversational.ai/scottmonaghan/32/3516_2.png) [@ScottMonaghan](https://community.openconversational.ai/u/ScottMonaghan)
#### Post date: [January 24, 2022, 11:38pm UTC](https://community.openconversational.ai/t/how-to-pass-python-bytesio-stream-to-precise-runner/11811/2 "2022-01-24T23:38:42Z")

</div>

Is this the correct place to ask this question?

---

<div class="post-metadata">

### Author: ![baconator](https://community.openconversational.ai/letter_avatar_proxy/v4/letter/b/d07c76/32.png) [@baconator](https://community.openconversational.ai/u/baconator)
#### Post date: [January 25, 2022, 12:17am UTC](https://community.openconversational.ai/t/how-to-pass-python-bytesio-stream-to-precise-runner/11811/3 "2022-01-25T00:17:22Z")

</div>

probably better on github.

---

<div class="post-metadata">

### Author: ![ScottMonaghan](https://community.openconversational.ai/user_avatar/community.openconversational.ai/scottmonaghan/32/3516_2.png) [@ScottMonaghan](https://community.openconversational.ai/u/ScottMonaghan)
#### Post date: [January 25, 2022, 4:11am UTC](https://community.openconversational.ai/t/how-to-pass-python-bytesio-stream-to-precise-runner/11811/4 "2022-01-25T04:11:32Z")

</div>

For anyone interested, I was able to get this working.

By reviewing the code in runner.py ([mycroft-precise/runner.py at dev · MycroftAI/mycroft-precise · GitHub](https://github.com/MycroftAI/mycroft-precise/blob/dev/runner/precise_runner/runner.py)) I realized I could bypass the PreciseRunner object altogether.

Here is the snippet of code that shows how it all comes together. Let me know if anyone has any questions:

```auto
    client_userdata = {}
    mqtt_client = mqtt.Client(client_id=MQTT_CLIENT_NAME,userdata=client_userdata)
 
    def on_message_audio_input_data(client:mqtt.Client, userdata, message):
        if engine.proc: #this is the Popen external process run for the precise engine. It takes a bit to fully open so this prevents errors before it fully starts.
            chunk = message.payload
            handle_predictions(chunk)

    def handle_predictions(chunk):
        """Check Precise process output"""
        prob = engine.get_prediction(chunk)
        if detector.update(prob):
           mqtt_client.publish(topic=TOPIC_WAKEWORD_DETECTED, payload="True",qos=2)
           logger.info("Wakeword Detected!")

    #initialize mqtt client
    mqtt_client.connect(MQTT_BROKER_ADDRESS)
    logger.info('MQTT Client Connected')
    mqtt_client.subscribe(TOPIC_AUDIO_INPUT_DATA)
    mqtt_client.message_callback_add(TOPIC_AUDIO_INPUT_DATA, on_message_audio_input_data)   
    logger.info('Subscribed to ' + TOPIC_AUDIO_INPUT_DATA)
    logger.info('Waiting for messages...')
    
    #initialize precise engine
    engine = PreciseEngine('/home/robud/Downloads/precise-engine/precise-engine', '/home/robud/src/precise-data/hey-mycroft.pb')
    detector = TriggerDetector(CHUNK, SENSITIVITY, TRIGGER_LEVEL)
    engine.start()

    mqtt_client.loop_forever()

```

---

<div class="post-metadata">

### Author: ![ScottMonaghan](https://community.openconversational.ai/user_avatar/community.openconversational.ai/scottmonaghan/32/3516_2.png) [@ScottMonaghan](https://community.openconversational.ai/u/ScottMonaghan)
#### Post date: [January 25, 2022, 4:13am UTC](https://community.openconversational.ai/t/how-to-pass-python-bytesio-stream-to-precise-runner/11811/5 "2022-01-25T04:13:31Z")

</div>

Next step, train my own wakeword!
