Skip to content

Commit e6fef98

Browse files
committed
Re-added support for signals
1 parent e8abdad commit e6fef98

File tree

19 files changed

+854
-80
lines changed

19 files changed

+854
-80
lines changed

Resources/Xcode/ofelia.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
AB88A8602A32A803006E17E1 /* ofxOfeliaPdBindings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB88A8512A32A803006E17E1 /* ofxOfeliaPdBindings.cpp */; };
2727
ABA2C9ED2A3B66A000C1D766 /* TcpSocket.h in Headers */ = {isa = PBXBuildFile; fileRef = ABA2C9E92A3B66A000C1D766 /* TcpSocket.h */; };
2828
ABA2C9EE2A3B66A000C1D766 /* TcpSocket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ABA2C9EA2A3B66A000C1D766 /* TcpSocket.cpp */; };
29-
ABCF244D2A32AF4B00EB1126 /* ofxRingBuffer.h in Headers */ = {isa = PBXBuildFile; fileRef = ABCF244A2A32AF4B00EB1126 /* ofxRingBuffer.h */; };
3029
ABCF247D2A35416600EB1126 /* ofxPdInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = ABCF247C2A35416600EB1126 /* ofxPdInterface.h */; };
3130
FDED156022B4CCD4005D9D62 /* ofxLuaFileWriter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FDED155C22B4CCD3005D9D62 /* ofxLuaFileWriter.cpp */; };
3231
FDED156122B4CCD4005D9D62 /* ofxLuaFileWriter.h in Headers */ = {isa = PBXBuildFile; fileRef = FDED155D22B4CCD3005D9D62 /* ofxLuaFileWriter.h */; };
@@ -135,7 +134,6 @@
135134
AB88A8512A32A803006E17E1 /* ofxOfeliaPdBindings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxOfeliaPdBindings.cpp; sourceTree = "<group>"; };
136135
ABA2C9E92A3B66A000C1D766 /* TcpSocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TcpSocket.h; sourceTree = "<group>"; };
137136
ABA2C9EA2A3B66A000C1D766 /* TcpSocket.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TcpSocket.cpp; sourceTree = "<group>"; };
138-
ABCF244A2A32AF4B00EB1126 /* ofxRingBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxRingBuffer.h; sourceTree = "<group>"; };
139137
ABCF247C2A35416600EB1126 /* ofxPdInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ofxPdInterface.h; sourceTree = "<group>"; };
140138
ABCF247F2A3541E100EB1126 /* ofxOfeliaStream.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ofxOfeliaStream.h; sourceTree = "<group>"; };
141139
E4328143138ABC890047C5CB /* openFrameworksLib.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = openFrameworksLib.xcodeproj; path = ../../Libraries/openFrameworks; sourceTree = SOURCE_ROOT; };
@@ -249,7 +247,6 @@
249247
AB88A8482A32A803006E17E1 /* ofxOfeliaSignal.cpp */,
250248
AB88A84C2A32A803006E17E1 /* ofxOfeliaSignal.h */,
251249
ABCF247C2A35416600EB1126 /* ofxPdInterface.h */,
252-
ABCF244A2A32AF4B00EB1126 /* ofxRingBuffer.h */,
253250
);
254251
name = Runner;
255252
path = ../../Source/Runner;
@@ -466,7 +463,6 @@
466463
FDED15C822B4CD9F005D9D62 /* lua.hpp in Headers */,
467464
FDED15B822B4CD9F005D9D62 /* lmem.h in Headers */,
468465
AB88A8562A32A803006E17E1 /* ofxOfeliaLog.h in Headers */,
469-
ABCF244D2A32AF4B00EB1126 /* ofxRingBuffer.h in Headers */,
470466
FDED15C922B4CD9F005D9D62 /* lzio.h in Headers */,
471467
AB88A85B2A32A803006E17E1 /* ofxOfeliaSignal.h in Headers */,
472468
AB88A8582A32A803006E17E1 /* ofxOfeliaMessageManager.h in Headers */,

Resources/Xcode/ofelia.xcodeproj/xcuserdata/timschoen.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 622 additions & 0 deletions
Large diffs are not rendered by default.

Source/Objects/ofxOfeliaClient.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,9 @@ void ofxOfeliaClient::setVariableByArgs(t_symbol *s, int argc, t_atom *argv)
114114

115115
void ofxOfeliaClient::dspMethod(t_signal **sp)
116116
{
117-
// TODO: Implement this!
118-
/*
119117
if (!data.isSignalObject) return;
120118
if (!isDataValid()) return;
121-
data.signal.addDsp(sp); */
119+
data.signal.addDsp(sp);
122120
}
123121

124122
void ofxOfeliaClient::freeMethod()

Source/Objects/ofxOfeliaData.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ofxOfeliaData::ofxOfeliaData()
3030
//,signal(this)
3131
,lua(std::make_unique<ofxOfeliaLua>(this))
3232
,io(this)
33+
,signal(this)
3334
,textBuf(this){};
3435

3536

@@ -108,12 +109,11 @@ void ofxOfeliaData::argParse(t_symbol *s, int argc, t_atom *argv, bool define)
108109
isDirectMode = true;
109110
goto directMode;
110111
}
111-
/*
112112
if (isSignalObject)
113113
{
114114
signal.f = atom_getfloat(argv);
115115
argc--; argv++;
116-
} */
116+
}
117117
else
118118
{
119119
for (int i = 0; i < io.numInlets - 1; ++i)

Source/Objects/ofxOfeliaData.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "m_pd.h"
44
#include "g_canvas.h"
5-
//#include "ofxOfeliaSignal.h"
5+
#include "ofxOfeliaSignal.h"
66
#include "ofxOfeliaIO.h"
77
#include "ofxOfeliaTextBuf.h"
88
#include <memory>
@@ -36,7 +36,7 @@ class ofxOfeliaData
3636
bool hasUniqueSym; /* whether the object has a unique symbol */
3737

3838
std::unique_ptr<ofxOfeliaLua> lua;
39-
//ofxOfeliaSignal signal;
39+
ofxOfeliaSignal signal;
4040
ofxOfeliaIO io;
4141
ofxOfeliaTextBuf textBuf;
4242
};

Source/Objects/ofxOfeliaDefine.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ void ofxOfeliaDefine::saveMethod(t_object *ob, t_binbuf *bb)
144144

145145
void ofxOfeliaDefine::dspMethod(t_signal **sp)
146146
{
147-
// TODO: Implement this!
148-
//if (!data.isSignalObject) return;
149-
//data.signal.addDsp(sp);
147+
if (!data.isSignalObject) return;
148+
data.signal.addDsp(sp);
150149
}
151150

152151
/* notification from GUI that we've been updated */
@@ -267,8 +266,7 @@ void ofxOfeliaDefine::setup()
267266
reinterpret_cast<t_method>(freeWrapper),
268267
sizeof(ofxOfeliaDefine), 0, A_GIMME, 0);
269268
class_setsavefn(pdClass, saveWrapper);
270-
// TODO: Implement this!
271-
//CLASS_MAINSIGNALIN(pdClass, ofxOfeliaDefine, data.signal.f);
269+
CLASS_MAINSIGNALIN(pdClass, ofxOfeliaDefine, data.signal.f);
272270
class_addbang(pdClass, bangWrapper);
273271
class_addfloat(pdClass, floatWrapper);
274272
class_addsymbol(pdClass, symbolWrapper);

Source/Objects/ofxOfeliaGet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ void ofxOfeliaGet::setup()
106106
reinterpret_cast<t_newmethod>(newWrapper),
107107
reinterpret_cast<t_method>(freeWrapper),
108108
sizeof(ofxOfeliaGet), 0, A_GIMME, 0);
109-
// TODO: Implement this!
110-
//CLASS_MAINSIGNALIN(pdClass, ofxOfeliaGet, client.data.signal.f);
109+
110+
CLASS_MAINSIGNALIN(pdClass, ofxOfeliaGet, client.data.signal.f);
111111
class_addbang(pdClass, bangWrapper);
112112
class_addfloat(pdClass, floatWrapper);
113113
class_addsymbol(pdClass, symbolWrapper);

Source/Objects/ofxOfeliaIO.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ void ofxOfeliaIO::newControlIO(int numInlets, int numOutlets)
3131

3232
void ofxOfeliaIO::newSignalIO(int numInlets, int numOutlets)
3333
{
34-
/* TODO: Implement this!
3534
this->numInlets = numInlets;
3635
this->numOutlets = numOutlets;
3736
dataPtr->signal.w = static_cast<t_int **>(getbytes(sizeof(t_int *) * (numInlets + numOutlets + 2)));
3837
newIO(--numInlets, numOutlets);
3938
dataPtr->signal.f = 0;
40-
dataPtr->isSignalObject = true; */
39+
dataPtr->isSignalObject = true;
4140
}
4241

4342
void ofxOfeliaIO::addControlIO()
@@ -71,11 +70,10 @@ void ofxOfeliaIO::freeControlIO()
7170

7271
void ofxOfeliaIO::freeSignalIO()
7372
{
74-
/* TODO: Implement this!
7573
int numInlets = this->numInlets;
7674
int numOutlets = this->numOutlets;
77-
freebytes(dataPtr->signal.w, sizeof(t_int *) * (numInlets + numOutlets + 2));
78-
freeIO(--numInlets, numOutlets); */
75+
//freebytes(dataPtr->signal.w, sizeof(t_int *) * (numInlets + numOutlets + 2));
76+
freeIO(--numInlets, numOutlets);
7977
}
8078

8179
void ofxOfeliaIO::doList(int argc, t_atom *argv)

Source/Objects/ofxOfeliaLua.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ class ofxOfeliaLua : public ofxOfeliaMessageListener
126126
}
127127
break;
128128
}
129+
case pd_audio_block:
130+
{
131+
auto [identifier, block] = messageManager->parseMessage<std::string, std::vector<std::vector<float>>>(message);
132+
133+
if(identifier != uid()) return;
134+
135+
dataPtr->signal.receiveAudioSamples(block);
136+
break;
137+
}
129138
case canvas_get_dollar_zero:
130139
{
131140
auto [identifier, parent] = messageManager->parseMessage<std::string, int>(message);
@@ -440,17 +449,13 @@ class ofxOfeliaLua : public ofxOfeliaMessageListener
440449
auto* name = dataPtr->sym->s_name;
441450
ss << "package.preload['" << name << "'] = nil package.loaded['" << name << "'] = nil\n"
442451
<< "package.preload['" << name << "'] = function(this) local ofelia = {} local M = ofelia\n";
443-
444-
std::cout << name << std::endl;
445-
446-
auto isSignalObject = false;
447-
452+
448453
if (!dataPtr->isFunctionMode)
449454
ss << s;
450-
else if (!isSignalObject)
451-
{
452-
ss << "function M.bang() return M.anything(nil) end function M.float(f) return M.anything(f) end function M.symbol(s) return M.anything(s) end function M.pointer(p) return M.anything(p) end function M.list(l) return M.anything(l) end function M.anything(a)\n" << s << "\nend";
453-
}
455+
else if (!dataPtr->isSignalObject)
456+
{
457+
ss << "function M.bang() return M.anything(nil) end function M.float(f) return M.anything(f) end function M.symbol(s) return M.anything(s) end function M.pointer(p) return M.anything(p) end function M.list(l) return M.anything(l) end function M.anything(a)\n" << s << "\nend";
458+
}
454459
else
455460
{
456461
ss << "function M.perform(";

Source/Objects/ofxOfeliaSet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ void ofxOfeliaSet::setup()
105105
reinterpret_cast<t_newmethod>(newWrapper),
106106
reinterpret_cast<t_method>(freeWrapper),
107107
sizeof(ofxOfeliaSet), 0, A_GIMME, 0);
108-
// TODO: Implement this!
109-
//CLASS_MAINSIGNALIN(pdClass, ofxOfeliaSet, client.data.signal.f);
108+
109+
CLASS_MAINSIGNALIN(pdClass, ofxOfeliaSet, client.data.signal.f);
110110
class_addbang(pdClass, bangWrapper);
111111
class_addfloat(pdClass, floatWrapper);
112112
class_addsymbol(pdClass, symbolWrapper);

Source/Objects/ofxOfeliaSignal.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "ofxOfeliaSignal.h"
2+
#include "ofxOfeliaData.h"
3+
#include "ofxOfeliaLua.h"
4+
5+
void ofxOfeliaSignal::addDsp(t_signal **sp)
6+
{
7+
dataPtr->lua->doFunction(gensym("dsp"));
8+
const ofxOfeliaIO &io = dataPtr->io;
9+
int sum = io.numInlets + io.numOutlets;
10+
t_int **w = this->w;
11+
w[0] = reinterpret_cast<t_int *>(dataPtr);
12+
w[1] = reinterpret_cast<t_int *>(sp[0]->s_n);
13+
for (int i = 0; i < sum; i++)
14+
w[i + 2] = reinterpret_cast<t_int *>(sp[i]->s_vec);
15+
dsp_addv(perform, sum + 2, reinterpret_cast<t_int *>(w));
16+
}
17+
18+
19+
void ofxOfeliaSignal::receiveAudioSamples(std::vector<std::vector<float>>& samples)
20+
{
21+
for(int i = 0; i < samples.size(); i++)
22+
{
23+
audioRingBuffer[i].push(samples[i]);
24+
}
25+
}
26+
27+
t_int *ofxOfeliaSignal::perform(t_int *w)
28+
{
29+
ofxOfeliaData *x = reinterpret_cast<ofxOfeliaData *>(w[1]);
30+
const int nSamples = static_cast<int>(w[2]);
31+
const int numInlets = x->io.numInlets;
32+
const int numOutlets = x->io.numOutlets;
33+
34+
std::vector<std::vector<float>> input = std::vector<std::vector<float>>(numInlets, std::vector<float>(nSamples));
35+
36+
for(int i = 0; i < numInlets; i++)
37+
{
38+
auto* in = reinterpret_cast<float*>(w[i + 3]);
39+
std::copy(in, in + nSamples, input[i].begin());
40+
}
41+
42+
// send message to runner
43+
x->lua->messageManager->sendMessage(ofx_lua_audio_block, std::string(x->getUniqueSym()->s_name), input, numOutlets);
44+
45+
for(int i = 0; i < numOutlets; i++)
46+
{
47+
auto outputSamples = x->signal.audioRingBuffer[i].pop(nSamples);
48+
std::copy(outputSamples.begin(), outputSamples.end(), reinterpret_cast<float*>(w[i + 3 + numInlets]));
49+
}
50+
51+
return w + numInlets + numOutlets + 3;
52+
}

Source/Objects/ofxOfeliaSignal.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma once
2+
3+
#include "m_pd.h"
4+
#include <vector>
5+
6+
template <typename T, size_t Size>
7+
class RingBuffer {
8+
public:
9+
RingBuffer() : buffer_(Size), head_(0), tail_(0), count_(0) {}
10+
11+
bool isEmpty() const {
12+
return count_ == 0;
13+
}
14+
15+
bool isFull() const {
16+
return count_ == Size;
17+
}
18+
19+
void push(const std::vector<T>& values) {
20+
if (values.size() > Size - count_) {
21+
return;
22+
}
23+
24+
for (const T& value : values) {
25+
buffer_[head_] = value;
26+
head_ = (head_ + 1) % Size;
27+
++count_;
28+
}
29+
}
30+
31+
std::vector<T> pop(size_t numItems) {
32+
if (numItems > count_) {
33+
numItems = count_;
34+
}
35+
36+
std::vector<T> poppedItems;
37+
poppedItems.reserve(numItems);
38+
39+
for (size_t i = 0; i < numItems; ++i) {
40+
poppedItems.push_back(buffer_[tail_]);
41+
tail_ = (tail_ + 1) % Size;
42+
--count_;
43+
}
44+
45+
return poppedItems;
46+
}
47+
48+
private:
49+
std::vector<T> buffer_;
50+
size_t head_;
51+
size_t tail_;
52+
size_t count_;
53+
};
54+
55+
class ofxOfeliaData;
56+
class ofxOfeliaSignal
57+
{
58+
public:
59+
ofxOfeliaSignal(ofxOfeliaData *dataPtr)
60+
:dataPtr(dataPtr){};
61+
void addDsp(t_signal **sp);
62+
static t_int *perform(t_int *w);
63+
64+
void receiveAudioSamples(std::vector<std::vector<float>>& samples);
65+
66+
t_int **w;
67+
t_float f; // variable for main signal inlet
68+
69+
private:
70+
ofxOfeliaData *dataPtr; // pointer to data
71+
RingBuffer<float, 4096> audioRingBuffer[8];
72+
};

Source/Runner/ofxOfeliaLua.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "ofxPdInterface.h"
4+
#include "ofxOfeliaSignal.h"
45
#include "../Shared/ofxOfeliaStream.h"
56
#include "lua.hpp"
67
#include <string>
@@ -18,7 +19,7 @@ class ofxOfeliaLua
1819
{
1920
public:
2021
ofxOfeliaLua(ofxOfeliaMessageManager* messageMan, const std::string& uid)
21-
: isChunkRun(false), uniqueId(uid), messageManager(messageMan), name(uid)
22+
: isChunkRun(false), uniqueId(uid), messageManager(messageMan), name(uid), signal(this)
2223
{};
2324

2425
static bool init();
@@ -81,6 +82,8 @@ class ofxOfeliaLua
8182
bool isChunkRun; /* whether the chunk is run or not */
8283
ofxOfeliaMessageManager* messageManager;
8384

85+
ofxOfeliaSignal signal;
86+
8487
private:
8588

8689
static std::map<std::string, std::unique_ptr<ofxOfeliaLua>> ofxLuaInstances;

Source/Runner/ofxOfeliaMain.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ struct ofApp : public ofBaseApp
106106
if(auto* ofxLua = ofxOfeliaLua::getPtr(std::get<0>(parsed))) ofxLua->doString(std::get<1>(parsed));
107107
break;
108108
}
109+
case ofx_lua_audio_block:
110+
{
111+
auto parsed = mm.parseMessage<std::string, std::vector<std::vector<float>>, int>(message);
112+
if(auto* ofxLua = ofxOfeliaLua::getPtr(std::get<0>(parsed)))
113+
ofxLua->signal.perform(std::get<1>(parsed), std::get<2>(parsed));
114+
break;
115+
}
109116

110117
default: break;
111118
}

Source/Runner/ofxOfeliaPdBindings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ class pdInlet
472472
}
473473
void setSignalInlet(t_floatarg f)
474474
{
475-
//ofxOfeliaMessageManager::sendMessage(pd_inlet_set_signal, x->getUniqueId(), f);
475+
ofxOfeliaMessageManager::sendMessage(pd_inlet_set_signal, x->getUniqueId(), f);
476476
}
477477

478478
ofxOfeliaLua *x;

0 commit comments

Comments
 (0)