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

Commit d4fda33

Browse files
committed
bela cpp osc resonator example
1 parent c420ad5 commit d4fda33

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <Bela.h>
2+
#include <libraries/OscSender/OscSender.h>
3+
#include <libraries/OscReceiver/OscReceiver.h>
4+
#include "Resonator.h" // https://github.com/jarmitage/resonators
5+
6+
OscReceiver oscReceiver;
7+
const char* remoteIp = "192.168.7.1";
8+
const int localPort = 7562;
9+
10+
Resonator res;
11+
ResonatorOptions options; // will initialise to default
12+
13+
void onReceive(oscpkt::Message* msg, void* arg) {
14+
15+
if(msg->match("/resonator")) {
16+
float freq, gain, decay;
17+
msg->match("/resonator")
18+
.popFloat(freq)
19+
.popFloat(gain)
20+
.popFloat(decay)
21+
.isOkNoMoreArgs();
22+
res.setParameters(freq, gain, decay);
23+
res.update();
24+
printf("Freq: %f, Gain: %f, Decay: %f\n", freq, gain, decay);
25+
26+
} else if (msg->match("/resonator/freq")) {
27+
28+
float freq;
29+
msg->match("/resonator/freq").popFloat(freq).isOkNoMoreArgs();
30+
res.setParameter(0, freq);
31+
res.update();
32+
printf("Freq: %f\n", freq);
33+
34+
} else if (msg->match("/resonator/gain")) {
35+
36+
float gain;
37+
msg->match("/resonator/gain").popFloat(gain).isOkNoMoreArgs();
38+
res.setParameter(1, gain);
39+
res.update();
40+
printf("Gain: %f\n", gain);
41+
42+
} else if (msg->match("/resonator/decay")) {
43+
44+
float decay;
45+
msg->match("/resonator/decay").popFloat(decay).isOkNoMoreArgs();
46+
res.setParameter(2, decay);
47+
res.update();
48+
printf("Decay: %f\n", decay);
49+
50+
} else {
51+
printf("Message address not recognised\n");
52+
}
53+
}
54+
55+
bool setup (BelaContext *context, void *userData) {
56+
57+
oscReceiver.setup(localPort, onReceive);
58+
59+
res.setup(options, context->audioSampleRate, context->audioFrames);
60+
res.setParameters(440, 0.1, 0.5); // freq, gain, decay
61+
res.update(); // update the state of the resonator based on the new parameters
62+
63+
return true;
64+
}
65+
66+
void render (BelaContext *context, void *userData) {
67+
68+
for (unsigned int n = 0; n < context->audioFrames; ++n) {
69+
70+
float in = audioRead(context, n, 0); // an excitation signal
71+
float out = 0.0f;
72+
73+
out = res.render(in);
74+
75+
audioWrite(context, n, 0, out);
76+
audioWrite(context, n, 1, out);
77+
78+
}
79+
80+
}
81+
82+
void cleanup (BelaContext *context, void *userData) { }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Authors:
3+
Victor Shepardson
4+
Jack Armitage
5+
Intelligent Instruments Lab 2022
6+
"""
7+
import random
8+
9+
from iipyper import OSC, run, repeat
10+
11+
def main(host="192.168.7.1", port=7563):
12+
13+
osc = OSC(host, port)
14+
osc.create_client("bela", host="192.168.7.2", port=7562)
15+
16+
connected = False
17+
18+
count = 0
19+
freq = 440.0
20+
gain = 0.1
21+
decay = 0.5
22+
23+
@repeat(1)
24+
def _():
25+
nonlocal freq, gain, decay, count
26+
freq = random.random() * 10000 + 50
27+
gain = random.random()
28+
decay = random.random()
29+
if count==0:
30+
osc("bela", "/resonator", freq, gain, decay)
31+
elif count==1:
32+
osc("bela", "/resonator/freq", freq)
33+
elif count==2:
34+
osc("bela", "/resonator/gain", gain)
35+
elif count==3:
36+
osc("bela", "/resonator/decay", decay)
37+
count = (count + 1) % 4
38+
39+
if __name__=='__main__':
40+
run(main)

0 commit comments

Comments
 (0)