Skip to content

Instantly share code, notes, and snippets.

@prisonerjohn
Last active October 20, 2019 20:04
Show Gist options
  • Save prisonerjohn/a6ae4e71afb6ddebf2afb50f62fc52b5 to your computer and use it in GitHub Desktop.
Save prisonerjohn/a6ae4e71afb6ddebf2afb50f62fc52b5 to your computer and use it in GitHub Desktop.
Sensing Machines Syphon
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Set up Syphon clients.
clientGrabber.setup();
clientGrabber.set("Video Input", "syphon-sendDebug");
clientThreshold.setup();
clientThreshold.set("Threshold Image", "syphon-sendDebug");
// Set parameters and GUI.
recvGrabber.set("Recv Grabber", true);
recvThreshold.set("Recv Threshold", false);
guiPanel.setup("Syphon Recv", "settings.json");
guiPanel.add(recvGrabber);
guiPanel.add(recvThreshold);
}
void ofApp::update()
{
}
void ofApp::draw()
{
if (recvGrabber)
{
// Draw grabber.
clientGrabber.draw(0, 0);
}
else if (recvThreshold)
{
// Draw threshold.
clientThreshold.draw(0, 0);
}
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxSyphon.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxSyphonClient clientGrabber;
ofxSyphonClient clientThreshold;
ofParameter<bool> recvGrabber;
ofParameter<bool> recvThreshold;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Set up Syphon server directory.
serverDirectory.setup();
// Set up Syphon client.
client.setup();
// Set parameters and GUI.
serverIdx.set("Server Idx", 0, 0, 10);
serverIdx.addListener(this, &ofApp::serverIndexChanged);
guiPanel.setup("Syphon Recv", "settings.json");
guiPanel.add(serverIdx);
}
void ofApp::update()
{
}
void ofApp::draw()
{
if (serverDirectory.isValidIndex(serverIdx))
{
// Draw Syphon server.
client.draw(0, 0);
}
guiPanel.draw();
}
void ofApp::serverIndexChanged(int& val)
{
// Check that the server index is within bounds.
if (serverDirectory.isValidIndex(serverIdx))
{
ofLogNotice(__FUNCTION__) << "Bind server " << serverIdx;
// Bind the client to the selected server.
client.set(serverDirectory.getDescription(serverIdx));
}
else
{
ofLogWarning(__FUNCTION__) << "Server " << serverIdx << " out of bounds!";
}
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxSyphon.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
void serverIndexChanged(int& val);
ofxSyphonServerDirectory serverDirectory;
ofxSyphonClient client;
ofParameter<int> serverIdx;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Set up Syphon server directory.
serverDirectory.setup();
ofAddListener(serverDirectory.events.serverAnnounced, this, &ofApp::serverListChanged);
ofAddListener(serverDirectory.events.serverRetired, this, &ofApp::serverListChanged);
// Set up Syphon client.
client.setup();
// Set parameters and GUI.
serverIdx.set("Server Idx", 0, 0, 10);
serverIdx.addListener(this, &ofApp::serverIndexChanged);
guiPanel.setup("Syphon Recv", "settings.json");
guiPanel.add(serverIdx);
// Force call events once so that the app starts in a fully configured state.
ofxSyphonServerDirectoryEventArgs args;
serverListChanged(args);
int idx = serverIdx;
serverIndexChanged(idx);
}
void ofApp::update()
{
}
void ofApp::draw()
{
if (serverDirectory.isValidIndex(serverIdx))
{
// Draw Syphon server.
client.draw(0, 0);
}
guiPanel.draw();
}
void ofApp::serverListChanged(ofxSyphonServerDirectoryEventArgs& args)
{
// Adjust the range of the server index parameter.
serverIdx.setMax(serverDirectory.size() - 1);
// Make sure the server index is within bounds.
if (serverIdx >= serverDirectory.size())
{
serverIdx = 0;
}
}
void ofApp::serverIndexChanged(int& val)
{
// Check that the server index is within bounds.
if (serverDirectory.isValidIndex(serverIdx))
{
ofLogNotice(__FUNCTION__) << "Bind server " << serverIdx;
ofxSyphonServerDescription desc = serverDirectory.getDescription(serverIdx);
// Bind the client to the selected server.
client.set(desc);
// Update the window title.
ofSetWindowTitle(desc.appName + "::" + desc.serverName);
}
else
{
ofLogWarning(__FUNCTION__) << "Server " << serverIdx << " out of bounds!";
}
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxSyphon.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
void serverListChanged(ofxSyphonServerDirectoryEventArgs& args);
void serverIndexChanged(int& val);
ofxSyphonServerDirectory serverDirectory;
ofxSyphonClient client;
ofParameter<int> serverIdx;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Start video grabber.
grabber.setup(640, 480);
// Allocate threshold image (same size as video, single channel).
thresholdImg.allocate(640, 480, OF_IMAGE_GRAYSCALE);
// Set up Syphon servers.
serverGrabber.setName("Video Input");
serverThreshold.setName("Threshold Image");
// Set parameters and GUI.
thresholdVal.set("Threshold Val", 127, 0, 255);
sendGrabber.set("Send Grabber", true);
sendThreshold.set("Send Threshold", true);
guiPanel.setup("Syphon Send", "settings.json");
guiPanel.add(thresholdVal);
guiPanel.add(sendGrabber);
guiPanel.add(sendThreshold);
}
void ofApp::update()
{
grabber.update();
if (grabber.isFrameNew())
{
// Threshold video image.
// Use references (&) when getting the ofPixels objects to
// avoid unnecessary copies.
ofPixels& videoPix = grabber.getPixels();
ofPixels& thresholdPix = thresholdImg.getPixels();
for (int row = 0; row < videoPix.getHeight(); row++)
{
for (int col = 0; col < videoPix.getWidth(); col++)
{
int pixVal = videoPix.getColor(col, row).getBrightness();
if (pixVal < thresholdVal)
{
thresholdPix.setColor(col, row, ofColor(0));
}
else
{
thresholdPix.setColor(col, row, ofColor(255));
}
}
}
thresholdImg.update();
if (sendGrabber)
{
// Send grabber texture.
// This has to be a pointer, so we add & before the argument.
serverGrabber.publishTexture(&grabber.getTexture());
}
}
}
void ofApp::draw()
{
thresholdImg.draw(0, 0);
if (sendThreshold)
{
// Send current screen, which is just the threshold image.
serverThreshold.publishScreen();
}
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxSyphon.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofImage thresholdImg;
ofxSyphonServer serverGrabber;
ofxSyphonServer serverThreshold;
ofParameter<int> thresholdVal;
ofParameter<bool> sendGrabber;
ofParameter<bool> sendThreshold;
ofxPanel guiPanel;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment