Skip to content

Commit

Permalink
added loading of settings from lua script
Browse files Browse the repository at this point in the history
  • Loading branch information
danomatika committed Aug 11, 2012
1 parent 9ba3ab7 commit 55ddbfb
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bin/data/boot.lua
@@ -1,5 +1,5 @@
--[[
Copyright (c) 2011 Dan Wilcox <danomatika@gmail.com>
Copyright (c) 2012 Dan Wilcox <danomatika@gmail.com>
BSD Simplified License.
For information on usage and redistribution, and for a DISCLAIMER OF ALL
Expand Down
32 changes: 32 additions & 0 deletions bin/data/defaults.lua
@@ -0,0 +1,32 @@
--[[
Copyright (c) 2012 Dan Wilcox <danomatika@gmail.com>
BSD Simplified License.
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "LICENSE.txt," in this distribution.
See https://github.com/danomatika/robotcowboy for documentation
--]]
-- create tables
rc = {}
rc.settings = {}
rc.settings.osc = {}
rc.settings.visual = {}
rc.settings.audio = {}
rc.settings.midi = {}
rc.settings.midi.inputs = {}
rc.settings.midi.outputs = {}
-- settings
rc.settings.scenePath = "scenes"
rc.settings.osc.sendAddress = "127.0.0.1"
rc.settings.osc.sendPort = 8880
rc.settings.osc.receivePort = 9009
rc.settings.visual.sendsOut = false
rc.settings.audio.sendsOut = false
rc.settings.midi.inputs = {"IAC Driver Pure Data Out"}
rc.settings.midi.outputs = {"IAC Driver Pure Data In"}
45 changes: 45 additions & 0 deletions src/Global.cpp
Expand Up @@ -20,6 +20,8 @@ Global& Global::instance() {
void Global::setup(const int numOutChannels, const int numInChannels,
const int sampleRate, const int ticksPerBuffer) {

loadSettings("ka");

// setup osc
osc.sender.setup(oscSendAddress, oscSendPort);
osc.receiver.setup(oscReceivePort);
Expand All @@ -45,6 +47,49 @@ void Global::clear() {
gui.clear();
}

//--------------------------------------------------------------
void Global::loadSettings(string path) {

ofxLua lua;
lua.init();

// load defaults
if(!lua.doScript("defaults.lua")) {
return;
}

// load user settings
// if(!lua.doScript(path)) {
// return;
// }

lua.pushTable("rc");
lua.pushTable("settings");

scenePath = lua.readString("scenePath", scenePath);

lua.pushTable("osc");
oscSendAddress = lua.readString("sendAddress", oscSendAddress);
oscSendPort = lua.readUInt("sendPort", oscSendPort);
oscReceivePort = lua.readUInt("receivePort", oscReceivePort);
lua.popTable();

lua.pushTable("visual");
audioSendsOut = lua.readBool("sendsOut", visualSendsOut);
lua.popTable();

lua.pushTable("audio");
audioSendsOut = lua.readBool("sendsOut", audioSendsOut);
lua.popTable();

lua.pushTable("midi");
lua.readStringVector("inputs", midi.inputNames);
lua.readStringVector("outputs", midi.outputNames);
lua.popTable();

lua.popAllTables();
}

// PRIVATE

//--------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions src/Global.h
Expand Up @@ -54,6 +54,9 @@ class Global {

/// \section Functions

/// laod global settings from lua script
void loadSettings(string path);

/// setup objects
void setup(const int numOutChannels, const int numInChannels,
const int sampleRate, const int ticksPerBuffer);
Expand Down
78 changes: 68 additions & 10 deletions src/Midi.cpp
Expand Up @@ -23,22 +23,80 @@ bool Midi::setup() {
ofxMidiIn::listPorts();
ofxMidiOut::listPorts();

for(int i = 0; i < ofxMidiIn::getNumPorts(); ++i) {
inputs.push_back(new ofxMidiIn);
inputs[i]->addListener(this);
inputs[i]->openPort(i);
}

for(int i = 0; i < ofxMidiOut::getNumPorts(); ++i) {
outputs.push_back(new ofxMidiOut);
outputs[i]->openPort(i);
}
connect();

ofxMidi::setConnectionListener(this);

return true;
}

void Midi::clear() {
disconnect();
}

//--------------------------------------------------------------
void Midi::connect() {

// connect to everything if the whitelist is empty
if(inputNames.empty()) {
for(int i = 0; i < ofxMidiIn::getNumPorts(); ++i) {
ofxMidiIn *in = new ofxMidiIn;
in->addListener(this);
in->openPort(i);
inputs.push_back(in);
}
}
// connect to white list
else {
for(int i = 0; i < ofxMidiIn::getNumPorts(); ++i) {
// look in whitelist
for(int j = 0; j < inputNames.size(); ++j) {
if(inputNames[j] == ofxMidiIn::getPortName(i)) {
ofxMidiIn *in = new ofxMidiIn;
in->addListener(this);
in->openPort(i);
inputs.push_back(in);
}
}
}
}

// outputs
if(outputNames.empty()) {
for(int i = 0; i < ofxMidiOut::getNumPorts(); ++i) {
ofxMidiOut *out = new ofxMidiOut;
out->openPort(i);
outputs.push_back(out);
}
}
else {
for(int i = 0; i < ofxMidiOut::getNumPorts(); ++i) {
// look in whitelist
for(int j = 0; j < outputNames.size(); ++j) {
if(outputNames[j] == ofxMidiOut::getPortName(i)) {
ofxMidiOut *out = new ofxMidiOut;
out->openPort(i);
outputs.push_back(out);
}
}
}
}
}

//--------------------------------------------------------------
void Midi::disconnect() {
for(int i = 0; i < inputs.size(); ++i) {
inputs[i]->closePort();
delete inputs[i];
}
for(int i = 0; i < outputs.size(); ++i) {
outputs[i]->closePort();
delete outputs[i];
}
inputs.clear();
outputs.clear();
}

//--------------------------------------------------------------
void Midi::sendNoteOn(const int channel, const int pitch, const int velocity) {
vector<ofxMidiOut*>::iterator iter;
Expand Down
11 changes: 10 additions & 1 deletion src/Midi.h
Expand Up @@ -18,8 +18,13 @@ class Midi : public ofxMidiListener, public ofxMidiConnectionListener {

Midi();

// set the whitelist before calling this
bool setup();
void clear() {}
void clear();

// connect, disconnect from whitelisted devices
void connect();
void disconnect();

// sending
void sendNoteOn(const int channel, const int pitch, const int velocity);
Expand All @@ -39,6 +44,10 @@ class Midi : public ofxMidiListener, public ofxMidiConnectionListener {
void midiOutputAdded(string nam, bool isNetwork);
void midiOutputRemoved(string name, bool isNetwork);

// whitelist
vector<string> inputNames;
vector<string> outputNames;

vector<ofxMidiIn*> inputs;
vector<ofxMidiOut*> outputs;
};

0 comments on commit 55ddbfb

Please sign in to comment.