Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
pelinski committed Jan 15, 2025
2 parents c77445b + e6a3a82 commit e0d5a33
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dev/dev.test-dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ echo "\nCreating test-env..."
python -m venv test-env
source test-env/bin/activate
echo "\nInstalling pybela from dist..."
pip install ../dist/pybela-2.0.0-py3-none-any.whl
pip install ../dist/pybela-2.0.1-py3-none-any.whl
echo "\nRunning test.py..."
python test.py
deactivate
Expand Down
16 changes: 13 additions & 3 deletions pybela/Watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,19 @@ def _parse_binary_data(self, binary_data, timestamp_mode, _type):

# --- utils --- #

def wait(self, time_in_seconds):
"""Wait for a given amount of time. Can't be used in async functions."""
self.loop.run_until_complete(asyncio.sleep(time_in_seconds))
def wait(self, time_in_seconds=0):
"""Wait for a given amount of time. Can't be used in async functions.
Args:
time_in_seconds (float, optional): Time to wait in seconds. If 0, it waits forever. Defaults to 0.
"""
if time_in_seconds < 0:
raise ValueError("Time in seconds should be greater than 0.")
elif time_in_seconds > 0:
self.loop.run_until_complete(asyncio.sleep(time_in_seconds))
else:
async def wait_forever():
await asyncio.Future()
self.loop.run_until_complete(wait_forever())

async def _async_get_latest_timestamp(self):
"""Get latest timestamp. Async version of get_latest_timestamp."""
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jupyter-bokeh==3.0.5
nest-asyncio==1.6.0
notebook==7.2.2
numpy==1.26.0
pandas==2.2.3
panel==0.14.4
paramiko==3.5.0
websockets==14.1
pandas==2.2.3
websockets==14.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def load_requirements(filename):

setuptools.setup(
name="pybela",
version="2.0.0",
version="2.0.1",
author="Teresa Pelinski",
author_email="teresapelinski@gmail.com",
description="pybela allows interfacing with Bela, the embedded audio platform, using python. It offers a convenient way to stream data between Bela and python, in both directions. In addition to data streaming, pybela supports data logging, as well as variable monitoring and control functionalities.",
Expand Down
33 changes: 33 additions & 0 deletions test/bela2python2bela-benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from pybela import Streamer
import numpy as np

diffs = []

async def callback(buffer,streamer):
diffs.append(buffer['buffer']['data'][0])

buffer_id, buffer_type, buffer_length = 0, 'i', 1024
data_list = np.ones(buffer_length, dtype=int)*buffer['buffer']['ref_timestamp']
streamer.send_buffer(buffer_id, buffer_type,
buffer_length, data_list)

if __name__ == "__main__":
streamer = Streamer()
streamer.connect()
vars = ['diffFramesElapsed']

streamer.start_streaming(
vars, on_buffer_callback=callback, callback_args=(streamer))
buffer_id, buffer_type, buffer_length = 0, 'i', 1024
data_list = np.zeros(buffer_length, dtype=int)

streamer.send_buffer(buffer_id, buffer_type,
buffer_length, data_list) # send zeros buffer to get it started
streamer.send_buffer(buffer_id, buffer_type,
buffer_length, data_list) # send zeros buffer to get it started

streamer.wait(1)
streamer.stop_streaming()

avg_roundtrip = np.round(np.average(diffs[4:])/streamer.sample_rate, 3)*1000
print("average roundtrip ", avg_roundtrip) # discard first measurements
1 change: 1 addition & 0 deletions test/bela2python2bela-benchmark/Watcher.cpp
1 change: 1 addition & 0 deletions test/bela2python2bela-benchmark/Watcher.h
66 changes: 66 additions & 0 deletions test/bela2python2bela-benchmark/render.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <Bela.h>
#include <Watcher.h>
#include <cmath>

Watcher<int> diffFramesElapsed("diffFramesElapsed");

unsigned int counter = 0;
unsigned int gFramesElapsed = 0;
struct ReceivedBuffer {
uint32_t bufferId;
char bufferType[4];
uint32_t bufferLen;
uint32_t empty;
std::vector<int> bufferData;
};
ReceivedBuffer receivedBuffer;
uint receivedBufferHeaderSize;
uint64_t totalReceivedCount;


bool binaryDataCallback(const std::string& addr, const WSServerDetails* id, const unsigned char* data, size_t size, void* arg) {

totalReceivedCount++;

std::memcpy(&receivedBuffer, data, receivedBufferHeaderSize);
receivedBuffer.bufferData.resize(receivedBuffer.bufferLen);
std::memcpy(receivedBuffer.bufferData.data(), data + receivedBufferHeaderSize, receivedBuffer.bufferLen * sizeof(float)); // data is a pointer to the beginning of the data

printf("\ntotal received count: %llu, total data size: %zu, bufferId: %d, bufferType: %s, bufferLen: %d \n", totalReceivedCount, size, receivedBuffer.bufferId, receivedBuffer.bufferType,
receivedBuffer.bufferLen);

for (size_t i = 0; i < receivedBuffer.bufferLen; ++i) {
diffFramesElapsed = gFramesElapsed-receivedBuffer.bufferData[0];
Bela_getDefaultWatcherManager()->tick(gFramesElapsed);
}

return true;
}

bool setup(BelaContext* context, void* userData) {

Bela_getDefaultWatcherManager()->getGui().setup(context->projectName);
Bela_getDefaultWatcherManager()->setup(context->audioSampleRate); // set sample rate in watcher

Bela_getDefaultWatcherManager()->getGui().setBuffer('i',1024);


Bela_getDefaultWatcherManager()->getGui().setBinaryDataCallback(binaryDataCallback);

receivedBufferHeaderSize = sizeof(receivedBuffer.bufferId) + sizeof(receivedBuffer.bufferType) + sizeof(receivedBuffer.bufferLen) + sizeof(receivedBuffer.empty);
totalReceivedCount = 0;
Bela_getDefaultWatcherManager()->tick(totalReceivedCount); // init the watcher

return true;
}

void render(BelaContext* context, void* userData) {

for (unsigned int n = 0; n < context->audioFrames; n++) {
gFramesElapsed = context->audioFramesElapsed + n;
}

}

void cleanup(BelaContext* context, void* userData) {
}

0 comments on commit e0d5a33

Please sign in to comment.