Skip to content
This repository was archived by the owner on Nov 23, 2023. It is now read-only.

Commit c420ad5

Browse files
committed
Bela cpp OSC arrays example
1 parent bb0d8e3 commit c420ad5

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <Bela.h>
2+
#include <libraries/OscSender/OscSender.h>
3+
#include <libraries/OscReceiver/OscReceiver.h>
4+
#include <cmath>
5+
6+
OscSender oscSender;
7+
OscReceiver oscReceiver;
8+
9+
const char* remoteIp = "192.168.7.1";
10+
const int remotePort = 7563;
11+
const int localPort = 7562;
12+
13+
const int OSC_PACKET_LEN = 300;
14+
const int OUT_CHANNELS = 2;
15+
16+
std::vector<float> oscInBuffer;
17+
std::vector<float> oscOutBuffer;
18+
19+
float gIn1, gIn2;
20+
21+
int writePointer = 0;
22+
int packetsSent = 0;
23+
int gAudioFramesPerAnalogFrame = 0;
24+
25+
void on_receive(oscpkt::Message* msg, void* arg) {
26+
if(msg->match("/bela")) {
27+
auto argReader = msg->match("/bela");
28+
for (int i=0; i<OUT_CHANNELS * OSC_PACKET_LEN; i++)
29+
argReader.popFloat(oscInBuffer[i]);
30+
argReader.isOkNoMoreArgs();
31+
}
32+
printf("Printing oscInBuffer: ");
33+
for(auto f : oscInBuffer)
34+
printf("%f ", f);
35+
printf("\n");
36+
}
37+
38+
bool setup(BelaContext *context, void *userData) {
39+
oscSender.setup(remotePort, remoteIp);
40+
oscReceiver.setup(localPort, on_receive);
41+
42+
oscInBuffer.resize(OUT_CHANNELS * OSC_PACKET_LEN);
43+
oscOutBuffer.resize(OUT_CHANNELS * OSC_PACKET_LEN);
44+
45+
if (context->analogFrames)
46+
gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
47+
48+
return true;
49+
}
50+
51+
void render(BelaContext *context, void *userData) {
52+
for (unsigned int n = 0; n < context->audioFrames; ++n) {
53+
if (gAudioFramesPerAnalogFrame && !(n % gAudioFramesPerAnalogFrame)) {
54+
gIn1 = analogRead(context, n / gAudioFramesPerAnalogFrame, 0);
55+
gIn2 = analogRead(context, n / gAudioFramesPerAnalogFrame, 1);
56+
}
57+
58+
oscOutBuffer[writePointer] = gIn1;
59+
oscOutBuffer[OSC_PACKET_LEN+writePointer] = gIn2;
60+
61+
if (writePointer + 1 == OSC_PACKET_LEN) {
62+
oscSender.newMessage("/bela");
63+
oscSender.add(packetsSent);
64+
for (auto v : oscOutBuffer)
65+
oscSender.add(v);
66+
oscSender.send();
67+
packetsSent += 1;
68+
}
69+
writePointer = (writePointer+1)%OSC_PACKET_LEN;
70+
}
71+
}
72+
73+
void cleanup(BelaContext*context, void *userData){}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Authors:
3+
Victor Shepardson
4+
Jack Armitage
5+
Intelligent Instruments Lab 2022
6+
"""
7+
8+
import numpy as np
9+
from iipyper import OSC, run, repeat
10+
11+
OSC_PACKET_LEN = 300
12+
OUT_CHANNELS = 2
13+
14+
def main(host="192.168.7.1", port=7563):
15+
16+
osc = OSC(host, port, verbose=False)
17+
osc.create_client("bela", host="192.168.7.2", port=7562)
18+
19+
@osc.args("/*")
20+
def _(address, *args):
21+
print(f"{address} {args}")
22+
23+
@repeat(1)
24+
def _():
25+
arr = np.random.rand(OSC_PACKET_LEN * OUT_CHANNELS)
26+
osc("bela", "/bela", *arr)
27+
28+
if __name__=='__main__':
29+
run(main)

0 commit comments

Comments
 (0)