Skip to main content

pybela allows interfacing with Bela, the embedded audio platform, using Python. pybela provides a convenient way to stream, log, and monitor sensor data from Bela to the host machine.

Project description

pybela

pybela allows interfacing with Bela, the embedded audio platform, using Python. pybela provides a convenient way to stream, log, and monitor sensor data from your Bela device to your laptop.

This library is currently under development and has been tested with Bela at dev branch commit 69cdf75a and watcher at main commit 903573a.

Installation and set up

1. Installing the python package

Option A:

You can install this library using pip (replace pip with pipenv if you are using a pipenv environment):

pip install pybela

Option B:

You can also download the built package from the releases section and run (replace pip with pipenv if you are using a pipenv environment):

pip install pybela-<version>.tar.gz

Option C:

You can also install this library using pipenv by cloning this repository and running:

git clone  --recurse-submodules https://github.com/BelaPlatform/pybela
cd pybela
pipenv install

2. Set the Bela branch to dev

In order to use pybela, you will need to use the dev branch of the Bela code.

Option A:

If your Bela is connected to internet, you can ssh into your Bela (ssh root@bela.local) and change the branch:

# in Bela
cd Bela
git checkout dev
git pull

Option B:

If your Bela is not connected to internet, you can change the branch by cloning the Bela repository into your laptop and then pushing the dev branch to your Bela. To do that, first clone the Bela repository into your laptop:

# in laptop
git clone  --recurse-submodules https://github.com/belaPlatform/bela
cd Bela

Then add your Bela as a remote and push the dev branch to your Bela:

# in laptop
git remote add board root@bela.local:Bela/
git checkout dev
git push -f board dev:tmp

Then ssh into your Bela (ssh root@bela.local) and change the branch:

# in Bela
cd Bela
git checkout tmp

You can check the commit hash by running git rev-parse --short HEAD either on Bela or your laptop.

3. Add the watcher library to your project

For pybela to be able to communicate with your Bela device, you will need to add the watcher library to your Bela project. To do so, you will need to add the files Watcher.h and Watcher.cpp to your Bela project. You can do this by copying the files from the watcher repository into your Bela project. To do so, you can run:

scp watcher/Watcher.h watcher/Watcher.cpp root@bela.local:Bela/projects/your-project/

Getting started

Modes of operation

pybela has three different modes of operation:

  • Streaming: continuously send data from your Bela device to your laptop.
  • Logging: log data in your Bela device and then retrieve it from your laptop.
  • Monitoring: monitor the state of variables in the Bela code from your laptop.

You can check the tutorials at tutorials/ for more detailed information and usage of each of the modes.

Running the examples

The quickest way to get started is to start a jupyter notebook server and run the examples. If you haven't done it yet, install the python package as explained in the installation section. If you don't have the jupyter notebook package installed, you can installed by running (replace pip with pipenv if you are using a pipenv environment):

pip install notebook

Once installed, start a jupyter notebook server by running:

jupyter notebook # or `pipenv run jupyter notebook` if you are using a pipenv environment

This should open a window in your browser from which you can look for the tutorials/notebooks folder and open the examples.

Basic usage

pybela allows you to access variables defined in your Bela code from python. To do so, you need to define the variables you want to access in your Bela code using the Watcher library.

Bela side

For example, if you want to access the variable myvar from python, you need to define the variable in your Bela code as follows:

#include <Watcher.h>
Watcher<float> myvar("myvar");

You will also need to add the following lines to your setup loop:

bool setup(BelaContext *context, void *userData)
{
	Bela_getDefaultWatcherManager()->getGui().setup(context->projectName);
	Bela_getDefaultWatcherManager()->setup(context->audioSampleRate);
    // your code here...
}

You will also need to add the following lines to your render loop:

void render(BelaContext *context, void *userData)
{
	for(unsigned int n = 0; n < context->audioFrames; n++) {
		uint64_t frames = context->audioFramesElapsed + n;
		Bela_getDefaultWatcherManager()->tick(frames);
        // your code here...
    }
}

you can see an example here.

Python side

Once the variable is defined "in the watcher", you can stream, log and monitor its value from python. For example, to stream the value of myvar from python, you can do:

from pybela import Streamer
streamer = Streamer()
streamer.connect()
streamer.start_streaming("myvar")

to terminate the streaming, you can run:

streamer.stop_streaming()

Testing

To run pybela's tests first copy the bela-test code into your Bela, compile and run it:

rsync -rvL  test/bela-test root@bela.local:Bela/projects/
ssh root@bela.local "make -C Bela stop Bela PROJECT=bela-test run"

you can run the python tests by running:

python test/test.py # or `pipenv run python test/test.py` if you are using a pipenv environment

Building

You can build pybela using pipenv:

pipenv install -d # installs all dependencies including dev dependencies
pipenv lock && pipenv sync # updates packages hashes
pipenv run python -m build --sdist # builds the .tar.gz file

To do and known issues

  • To do: Upload to pyPI (so that the package can be installed using pip)
  • To do: Upload built package to releases (so that the package can be installed using pip install pybela-<version>.tar.gz)
  • Issue: Monitor and streamer can't be used simultaneously –  This is due to both monitor and streamer both using the same websocket connection and message format. This could be fixed by having a different message format for the monitor and the streamer (e.g., adding a header to the message)
  • Issue: The plotting routine does not work when variables are updated at different rates.
  • Issue: The plotting routine does not work for the monitor (it only works for the streamer)
  • Code refactor: There's two routines for generating filenames (for Streamer and for Logger). This should be unified.
  • Possible feature: Flexible backend buffer size for streaming: if the assign rate of variables is too slow, the buffers might not be filled and hence not sent (since the data flushed is not collected in the frontend), and there will be long delays between the variable assign and the data being sent to the frontend.
  • Issue: Flushed buffers are not collected after stop_streaming in the frontend.
  • Bug: OSError: [Errno 12] Cannot allocate memory

License

This library is distributed under LGPL, the GNU Lesser General Public License (LGPL 3.0), available here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybela-0.0.1.tar.gz (30.6 kB view details)

Uploaded Source

Built Distribution

pybela-0.0.1-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file pybela-0.0.1.tar.gz.

File metadata

  • Download URL: pybela-0.0.1.tar.gz
  • Upload date:
  • Size: 30.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for pybela-0.0.1.tar.gz
Algorithm Hash digest
SHA256 151bffcf0fbbf4f875b0134ac6d53ebef61f52468ca17067743993e3277fc9b9
MD5 9ed5fb67938affeb93d9dc9f0e37b9e8
BLAKE2b-256 1e0bf602069c39ab07f6e79429437519595dbf01acbe996fb5c76a2e51483585

See more details on using hashes here.

Provenance

File details

Details for the file pybela-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pybela-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 28.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for pybela-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 43b52c6c1c54d3c576d7b6bcc721b8916398a4b920e52dcee98d2aab1363b50a
MD5 754e8f36131fa550d70d208550108d1a
BLAKE2b-256 14a9be60e1b851582113ad03078b29284be7249e3a528fc670335932b01c3e91

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page