yoppa.org


メディア芸術演習 VI - メディア・アート II

openFramewoks – OSC (Open Sound Control) を利用したネットワーク連携


使openFrameworksopenFrameworksopenFrameworksOpen Sound Control (OSC) 

Open Sound Control (OSC)


Open Sound Control CNMATThe Center for New Music and Audio TechnologiesCNMATOSC


Open Sound Control (OSC) is a protocol for communication among computers, sound synthesizers, and other multimedia devices that is optimized for modern networking technology. Bringing the benefits of modern networking technology to the world of electronic musical instruments, OSCs advantages include interoperability, accuracy, flexibility, and enhanced organization and documentation.

() Open Sound Control (OSC) OSC


OSCMIDI(Musical Instrument Digital Interface)(TCP/IPUDP/IP)

OSC



Max/MSP

Pure Data (pd)

SuperCollider

ChucK

Csound

Reactor



Flash + flosc

Processing

QuartzComposer

vvvv

OpenFrameworks



Lemur

monome

iPhone / iPad + OSC(TouchOSC)

OSC


OSC2OSC(OSC Message)OSC(OSC Arguments)

OSCOSCOSCOSCWWWURL (Uniform Resorce Locator) /OSC2mousepositionbutton/mouse/position/mouse/buttonOSCOSC使

OSC (OSC Argument) (int32)(float)(string)

KeynoteScreenSnapz001.png
OSC

osc_address.png
OSC

openFrameworksOSC


openFrameworksOSCOSC

OSC2使




/mouse/position

XY ( 320 240)





/mouse/button

 (up, down)




openFrameworks(Sender)(Receiver)2OSC使ofxOsc


=(oscReceiveExample)=(oscSenderExample)

 (oscReceiveExample)


testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxOsc.h"

//
#define PORT 8000

//
class testApp : public ofBaseApp{

public:

 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);
 void dumpOSC(ofxOscMessage m); //OSC

private:
 //OSC
 ofxOscReceiver receiver;
 //
 int remoteMouseX, remoteMouseY;
 // ("up", "down")
 string mouseButtonState;
 //string oscString;
};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 //
 receiver.setup( PORT );

 //
 mouseX = 0;
 mouseY = 0;
 mouseButtonState = "";

 ofBackground(0, 0, 0);
}

void testApp::update(){
 //OSC
 while( receiver.hasWaitingMessages() )
 {
 //
 ofxOscMessage m;
 receiver.getNextMessage( &m);

 //
 if ( m.getAddress() == "/mouse/position" ){
 remoteMouseX = m.getArgAsInt32( 0 );
 remoteMouseY = m.getArgAsInt32(1);

 }
 //
 else if ( m.getAddress() == "/mouse/button" ) {
 mouseButtonState = m.getArgAsString( 0 ) ;
 }

 //OSC
 dumpOSC(m);
 }
}

//OSC
void testApp::dumpOSC(ofxOscMessage m) {
 string msg_string;
 msg_string = m.getAddress();
 for (int i=0; i<m.getNumArgs(); i++ ) {
 msg_string += " ";
 if(m.getArgType(i) == OFXOSC_TYPE_INT32)
 msg_string += ofToString( m.getArgAsInt32(i));
 else if(m.getArgType(i) == OFXOSC_TYPE_FLOAT)
 msg_string += ofToString( m.getArgAsFloat(i));
 else if(m.getArgType(i) == OFXOSC_TYPE_STRING)
 msg_string += m.getArgAsString(i);
 }
 cout << msg_string << endl;
}

void testApp::draw(){
 int radius;
 if (mouseButtonState == "down") {
 //
 radius = 20;
 ofSetColor(255, 127, 0);
 } else {
 //
 radius = 10;
 ofSetColor(0, 127, 255);
 }
 ofCircle(remoteMouseX, remoteMouseY, radius);
}

void testApp::keyPressed (int key){}

void testApp::mouseMoved(int x, int y ){}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){}

void testApp::mouseReleased(int x, int y, int button){}

void testApp::windowResized(int w, int h){}
[/sourcecode]

 (oscSenderExample)


testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxOsc.h"

#define HOST "localhost" //IP
#define PORT 8000 //

//
class testApp : public ofBaseApp{

public:

 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);

private:
 //OSC
 ofxOscSender sender;
};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 ofBackground(0, 0, 0);
 //IP
 sender.setup( HOST, PORT );
}

void testApp::update(){
}

void testApp::draw(){
 //
 ofSetColor(255, 255, 255);
 ofCircle(mouseX, mouseY, 10);
}

void testApp::keyPressed (int key){}

void testApp::mouseMoved(int x, int y ){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/position" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 //
 sender.sendMessage(m);
}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/button" );
 //OSC"down"
 m.addStringArg( "down" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 sender.sendMessage(m);
}

void testApp::mouseReleased(int x, int y, int button){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/button" );
 //OSC"up"
 m.addStringArg( "up" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 sender.sendMessage(m);
}

void testApp::windowResized(int w, int h){}
[/sourcecode]

oscSenderExampleDebugScreenSnapz001.png


1OSCIP調(oscSenderExample)testApp.h7IPlocalhost192.168.1.10IPOSC

XCodeOSC
/mouse/position 550 430
/mouse/position 547 430
/mouse/position 544 431
/mouse/position 543 431
/mouse/position 541 431
/mouse/position 540 432
/mouse/button down 540 432
/mouse/button up 540 432
/mouse/position 541 432
/mouse/position 543 431
/mouse/position 545 430
/mouse/position 547 429
...




RingIPOSC

 


testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxOsc.h"
#include "Ring.h"

//
#define PORT 8000

class testApp : public ofBaseApp{

public:

 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);
 void dumpOSC(ofxOscMessage m);
 vector <Ring *> rings; //"Ring"

private:
 ofxOscReceiver receiver;
 int remoteMouseX, remoteMouseY;
 string mouseButtonState;
 string oscString;
};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 //
 receiver.setup( PORT );
 ofSetFrameRate(60);

 //
 mouseX = 0;
 mouseY = 0;
 mouseButtonState = "";

 ofBackground(0, 0, 0);
}

void testApp::update(){
 //OSC
 while( receiver.hasWaitingMessages() )
 {
 //
 ofxOscMessage m;
 oscString = m.getAddress();
 receiver.getNextMessage( &m);

 //
 if ( m.getAddress() == "/mouse/position" ){
 remoteMouseX = m.getArgAsInt32( 0 );
 remoteMouseY = m.getArgAsInt32(1);

 }
 //
 else if ( m.getAddress() == "/mouse/button" ) {
 mouseButtonState = m.getArgAsString( 0 ) ;
 remoteMouseX = m.getArgAsInt32(1);
 remoteMouseY = m.getArgAsInt32(2);
 }

 //OSC
 dumpOSC(m);
 }

 //Ring
 if(mouseButtonState == "up"){
 rings.push_back(new Ring(ofPoint(remoteMouseX, remoteMouseY)));
 mouseButtonState = "";
 }

 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end();){
 (*it)->update();
 if ((*it)->dead) {
 delete (*it);
 it = rings.erase(it);
 } else {
 ++it;
 }
 }
}

//OSC
void testApp::dumpOSC(ofxOscMessage m) {
 string msg_string;
 msg_string = m.getAddress();
 for (int i=0; i<m.getNumArgs(); i++ ) {
 msg_string += " ";
 if(m.getArgType(i) == OFXOSC_TYPE_INT32)
 msg_string += ofToString( m.getArgAsInt32(i));
 else if(m.getArgType(i) == OFXOSC_TYPE_FLOAT)
 msg_string += ofToString( m.getArgAsFloat(i));
 else if(m.getArgType(i) == OFXOSC_TYPE_STRING)
 msg_string += m.getArgAsString(i);
 }
 cout << msg_string << endl;
}

void testApp::draw(){
 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end(); ++it){
 (*it)->draw();
 }
}

void testApp::keyPressed (int key){}

void testApp::mouseMoved(int x, int y ){}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){}

void testApp::mouseReleased(int x, int y, int button){
 rings.push_back(new Ring(ofPoint(x, y)));
}

void testApp::windowResized(int w, int h){}
[/sourcecode]

Ring.h

[sourcecode language=cpp]
#ifndef _RING
#define _RING

#include "ofMain.h"

class Ring {

public:
 Ring(ofPoint pos); //
 void update();
 void draw();

 ofPoint pos; //
 float radius; //
 float radiusSpeed; //
 bool dead; //

private:

};

#endif
[/sourcecode]

Ring.cpp

[sourcecode language=cpp]
#include "Ring.h"

Ring::Ring(ofPoint _pos)
{
 pos = _pos;
 radius = 0;
 radiusSpeed = 0.5;
 dead = false;
}

void Ring::update()
{
 radius += radiusSpeed;
 if (radius > ofGetWidth()) {
 dead = true;
 }
}

void Ring::draw()
{
 ofSetCircleResolution(64);
 glEnable(GL_BLEND);
 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
 ofEnableSmoothing();
 ofSetLineWidth(1);
 ofPushMatrix();
 ofTranslate(pos.x, pos.y);
 ofNoFill();
 ofSetColor(63, 127, 255, 127  radius * 127 / ofGetHeight());
 ofCircle(0, 0, radius);
 ofFill();
 ofSetColor(63, 127, 255, 31  radius * 31 / ofGetHeight());
 ofCircle(0, 0, radius);
 ofPopMatrix();
}
[/sourcecode]


testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxOsc.h"
#include "Ring.h"

#define HOST "192.168.1.10" //IP
#define PORT 8000

//
class testApp : public ofBaseApp{

public:

 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);
 vector <Ring *> rings; //

private:
 //OSC
 ofxOscSender sender;
};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 ofBackground(0, 0, 0);
 ofSetFrameRate(60);
 //IP
 sender.setup( HOST, PORT );
}

void testApp::update(){
 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end();){
 (*it)->update();
 if ((*it)->dead) {
 delete (*it);
 it = rings.erase(it);
 } else {
 ++it;
 }
 }
}

void testApp::draw(){
 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end(); ++it){
 (*it)->draw();
 }
}

void testApp::keyPressed (int key){}

void testApp::mouseMoved(int x, int y ){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/position" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 //
 sender.sendMessage(m);
}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/button" );
 //OSC"down"
 m.addStringArg( "down" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 sender.sendMessage(m);
}

void testApp::mouseReleased(int x, int y, int button){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/button" );
 //OSC"up"
 m.addStringArg( "up" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 sender.sendMessage(m);

 //Ring
 rings.push_back(new Ring(ofPoint(x, y)));
}

void testApp::windowResized(int w, int h){}
[/sourcecode]

 Ring.hRing.cpp(oscRingReceiver)

oscRingReceiverDebugScreenSnapz001.png

1Pure Data (pd) 


Open Sound Control OSCopenFrameworksopenFrameworksDSPopenFrameworks

OSCMax/MSPPure DataCsoundSuperColliderChucKPure Data(pd)SuperColliderOpenFrameworks

pdMillerPacket(pd-vanilla)(pd-extended)
pdOSCpd-extended使pd-extended


Pure Data Downloads


PdOSCdumpOSCOSC1OSCOSCroute/mouse/positionOSCOSCroute /mouse/mouseOSCroute /position/positionunpackOSC

pdFMXYOSC/mouse/position

screen(2010-12-12 16.37.45).png
pdOSCopenFrameworksopenFrameworksOSC

testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxOsc.h"
#define HOST "localhost" //IP
#define PORT 8000

//
class testApp : public ofBaseApp{

public:

 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);

private:
 //OSC
 ofxOscSender sender;
};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 ofBackground(0, 0, 0);
 //IP
 sender.setup( HOST, PORT );
}

void testApp::update(){}

void testApp::draw(){
 //
 ofSetColor(255, 255, 255);
 ofCircle(mouseX, mouseY, 10);
}

void testApp::keyPressed (int key){}

void testApp::mouseMoved(int x, int y ){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/position" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 //
 sender.sendMessage(m);
}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/button" );
 //OSC"down"
 m.addStringArg( "down" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 sender.sendMessage(m);
}

void testApp::mouseReleased(int x, int y, int button){
 //OSC
 ofxOscMessage m;
 //OSC
 m.setAddress( "/mouse/button" );
 //OSC"up"
 m.addStringArg( "up" );
 //OSC(x, y)
 m.addIntArg(x);
 m.addIntArg(y);
 sender.sendMessage(m);

}

void testApp::windowResized(int w, int h){}
[/sourcecode]

PdDSPONopenFrameworksFMopenFramworksIP

PdopenFrameworks  ofxPd


openFrameworksPdOSC便ofxPd使PddumpOSCOSCroute使r

ofxPd使PdopenFrameworksPdofxPdpd使openFrameworksbin/data/

ofxPd使FM200dumpOSCr

screen(2010-12-12 17.31.02).png
openFrameworksofxOSCofxPdofxThred




testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxPd.h"
#include "ofxOsc.h"

class testApp : public ofBaseApp{

public:

 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void keyReleased(int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);

 void audioRequested(float * output, int bufferSize, int nChannels);
 void audioReceived(float * input, int bufferSize, int nChannels );

 float * audioInputData;

 // Pd
 ofxPd pd;

 // PdOSC
 ofxOscSender osc_sender;

};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 ofSetFrameRate(60);
 ofBackground(0, 0, 0);

 //OSC
 static const string HOST = "localhost";
 static const int PORT = 8000;
 osc_sender.setup( "localhost", PORT);

 // pd
 // 
 static const int NUM_OUT_CHANNELS = 2;
 static const int NUM_IN_CHANNELS = 2;
 // 
 static const int BITRATE = 44100;
 // 
 static const int BUFFER_SIZE = 256;
 // 使
 static const int NUM_BUFFERS = 4;
 // Pd
 static const int PD_BLOCK_SIZE = 64;

 // Pd
 pd.setup( "", NUM_OUT_CHANNELS, NUM_IN_CHANNELS, BITRATE, PD_BLOCK_SIZE );
 // Pd
 pd.addOpenFile( "simple_fm.pd" );
 // Pd
 pd.start();
 // 
 audioInputData = new float[BUFFER_SIZE*NUM_IN_CHANNELS];
 // 
 ofSoundStreamSetup( NUM_OUT_CHANNELS, NUM_IN_CHANNELS, this, BITRATE, BUFFER_SIZE, NUM_BUFFERS );
 // Pd
 pd.startDSP();
}

void testApp::update(){}

void testApp::draw(){
 ofSetColor(0, 127, 255);
 ofCircle(mouseX, mouseY, 20);
}

void testApp::keyPressed (int key){
}

void testApp::keyReleased (int key){
}

void testApp::mouseMoved(int x, int y ){
 // Pd
 pd.sendFloat( "modulator_freq", x );
 pd.sendFloat( "modulator_index", y );
}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){}

void testApp::mouseReleased(int x, int y, int button){}

void testApp::windowResized(int w, int h){}

void testApp::audioRequested(float * output, int bufferSize, int nChannels){
 //Pd
 pd.renderAudio( audioInputData, output, bufferSize, nChannels );
}

void testApp::audioReceived(float * input, int bufferSize, int nChannels){
 memcpy(audioInputData, input, bufferSize*nChannels*sizeof(float));
}
[/sourcecode]

openFramwroksPdopenFrameworksPdopenFrameworks使

openFrameworksSuperCollider  ofxSuperCollider


openFrameworksSuperCollider

SuperColliderOSCOSCSuperColliderSynthDef使SuperCollideropenFrameworksSuperColliderOSC

openFrameworksSuperColliderofxOSCOSCSuperColliderofxSuperColliderofxSuperCollider使ofxSuperColliderofxOsc

SuperCollidernewRingreverb

SuperCollider (sc_inst.scd)

[sourcecode]
// SynthDef
(
SynthDef("reverb", {
 arg wet=1.0;
 var in, fx;
 in = In.ar(0, 2);
 fx = in;
 fx = GVerb.ar(fx, 80);
 ReplaceOut.ar(0, fx);
}).store;

SynthDef("baseSound", {
 arg note=40, amp=0.1, fadein=12.0;
 var env, out;
 env = EnvGen.kr(Env.new([0, amp], [fadein]));
 out = RLPF.ar(LFPulse.ar([note, note+7].midicps, 0.15), SinOsc.kr(0.1, 0, 10, 72).midicps, 0.1, 0.1);
 Out.ar(0, out*env);
}).store;

SynthDef("newRing", {
 arg note=40, amp=0.5, pan = 0.0, decay=4.0;
 var env1, out1, env2, out2, mix;
 out1 = RLPF.ar(LFPulse.ar([note, note+7].midicps, 0.15), SinOsc.kr(0.1, 0, 10, 72).midicps, 0.1, 0.1);
 out2 = SinOsc.ar([(note+48).midicps, (note+55).midicps]);
 env1 = EnvGen.kr(Env.perc(decay/4.0, decay/4.0*3.0, amp, -4), doneAction: 2);
 env2 = EnvGen.kr(Env.adsr(0.001, 0.4, 0.0, decay, amp*0.1, -4));
 mix = (out1 * env1) + (out2 * env2);
 mix = CombN.ar(mix, 0.31, 0.31, 2, 0.5, mix);
 Out.ar(0, mix);
}).store;
)

// test
s.sendMsg("/s_new", "reverb", x = s.nextNodeID, 1, 1);
s.sendMsg("/s_new", "baseSound", x = s.nextNodeID, 1, 1);
s.sendMsg("/s_new", "newRing", x = s.nextNodeID, 1, 1, "note", 42);
[/sourcecode]

testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxSuperCollider.h"
#include "Ring.h"

class testApp : public ofBaseApp{

public:
 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void keyReleased(int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);

 ofxSCSynth *reverb; // SC "reverb"
 ofxSCSynth *newRing; // SC "newRing"
 ofxSCSynth *baseSound; // SC "baseSound"
 vector <Ring *> rings; //"Ring"
};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 ofSetFrameRate(60);
 ofBackground(0, 0, 0);

 reverb = new ofxSCSynth("reverb");
 reverb->create();
 baseSound = new ofxSCSynth("baseSound");
 baseSound->create();
}

void testApp::update(){
 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end();){
 (*it)->update();
 if ((*it)->dead) {
 delete (*it);
 it = rings.erase(it);
 } else {
 ++it;
 }
 }
}

void testApp::draw(){
 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end(); ++it){
 (*it)->draw();
 }
}

void testApp::keyPressed(int key){}

void testApp::keyReleased(int key){}

void testApp::mouseMoved(int x, int y ){}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){}

void testApp::mouseReleased(int x, int y, int button){
 //newRing
 int note[8] = {28, 35, 40, 47, 52, 59, 64, 71};
 newRing = new ofxSCSynth("newRing");
 newRing->set("note", note[int(ofRandom(0, 8))]);
 newRing->set("pan", (x  ofGetWidth() / 2.0) / ofGetWidth() * 2.0);
 newRing->create();

 //Ring
 rings.push_back(new Ring(ofPoint(x, y)));
}

void testApp::windowResized(int w, int h){}
[/sourcecode]

Ring.h

[sourcecode language=cpp]
#ifndef _RING
#define _RING

#include "ofMain.h"

class Ring {

public:
 Ring(ofPoint pos);
 void update();
 void draw();

 ofPoint pos;
 float radius;
 float radiusSpeed;
 bool dead;

private:

};
#endif
[/sourcecode]

Ring.cpp

[sourcecode language=cpp]

#include "Ring.h"

Ring::Ring(ofPoint _pos)
{
 //
 pos = _pos;
 radius = 0;
 radiusSpeed = 0.5;
 dead = false;
}

void Ring::update()
{
 //
 radius += radiusSpeed;
 //
 if (radius > ofGetWidth()) {
 dead = true;
 }
}

void Ring::draw()
{
 //
 ofSetCircleResolution(64);
 glEnable(GL_BLEND);
 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
 ofEnableSmoothing();
 ofSetLineWidth(2);
 ofPushMatrix();
 ofTranslate(pos.x, pos.y);
 ofNoFill();
 ofSetColor(63, 127, 255, 200);
 ofCircle(0, 0, radius);
 ofPopMatrix();
}
[/sourcecode]

oscRingReceiverDebugScreenSnapz001.png
SuperColliderEnteropenFrameworksSC

SuperCollideropenFrameworks  ofxSuperColliderServer


SuperCollideropenFrameworksSuperCollideropenFrameworksofxSuperColliderServer使openFrameworksSuperCollider便使ofxSuperColliderServerofxSuperCllider

openFramework

testApp.h

[sourcecode language=cpp]
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxOsc.h"
#include "Ring.h"
#include "ofxSuperCollider.h"
#include "ofxSuperColliderServer.h"

//
#define PORT 8000

//
class testApp : public ofBaseApp{

public:
 void setup();
 void update();
 void draw();

 void keyPressed (int key);
 void mouseMoved(int x, int y );
 void mouseDragged(int x, int y, int button);
 void mousePressed(int x, int y, int button);
 void mouseReleased(int x, int y, int button);
 void windowResized(int w, int h);
 void dumpOSC(ofxOscMessage m);
 vector <Ring *> rings; //"Ring"
 ofxSCSynth *reverb; // SC "reverb"
 ofxSCSynth *newRing; // SC "newRing"
 ofxSCSynth *baseSound; // SC "baseSound"

private:
 ofxOscReceiver receiver;
 int remoteMouseX, remoteMouseY;
 string mouseButtonState;
 string oscString;
};

#endif
[/sourcecode]

testApp.cpp

[sourcecode language=cpp]
#include "testApp.h"

void testApp::setup(){
 //
 receiver.setup( PORT );
 ofSetFrameRate(60);

 //
 mouseX = 0;
 mouseY = 0;
 mouseButtonState = "";

 ofBackground(0, 0, 0);

 ofxSuperColliderServer::init();
 reverb = new ofxSCSynth("reverb");
 reverb->create();
 baseSound = new ofxSCSynth("baseSound");
 baseSound->create();
}

void testApp::update(){
 //OSC
 while( receiver.hasWaitingMessages() )
 {
 //
 ofxOscMessage m;
 oscString = m.getAddress();
 receiver.getNextMessage( &m);

 //
 if ( m.getAddress() == "/mouse/position" ){
 remoteMouseX = m.getArgAsInt32( 0 );
 remoteMouseY = m.getArgAsInt32(1);

 }
 //
 else if ( m.getAddress() == "/mouse/button" ) {
 mouseButtonState = m.getArgAsString( 0 ) ;
 remoteMouseX = m.getArgAsInt32(1);
 remoteMouseY = m.getArgAsInt32(2);
 }

 //OSC
 dumpOSC(m);
 }

 //Ring
 if(mouseButtonState == "up"){
 rings.push_back(new Ring(ofPoint(remoteMouseX, remoteMouseY)));
 mouseButtonState = "";

 //SC
 int note[8] = {28, 35, 40, 47, 52, 59, 64, 71};
 newRing = new ofxSCSynth("newRing");
 newRing->set("note", note[int(ofRandom(0, 8))]);
 newRing->set("pan", (remoteMouseX  ofGetWidth() / 2.0) / ofGetWidth() * 2.0);
 newRing->create();
 }

 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end();){
 (*it)->update();
 if ((*it)->dead) {
 delete (*it);
 it = rings.erase(it);
 } else {
 ++it;
 }
 }
}

//OSC
void testApp::dumpOSC(ofxOscMessage m) {
 string msg_string;
 msg_string = m.getAddress();
 for (int i=0; i<m.getNumArgs(); i++ ) {
 msg_string += " ";
 if(m.getArgType(i) == OFXOSC_TYPE_INT32)
 msg_string += ofToString( m.getArgAsInt32(i));
 else if(m.getArgType(i) == OFXOSC_TYPE_FLOAT)
 msg_string += ofToString( m.getArgAsFloat(i));
 else if(m.getArgType(i) == OFXOSC_TYPE_STRING)
 msg_string += m.getArgAsString(i);
 }
 cout << msg_string << endl;
}

void testApp::draw(){
 //Ring
 for(vector <Ring *>::iterator it = rings.begin(); it != rings.end(); ++it){
 (*it)->draw();
 }
}

void testApp::keyPressed (int key){}

void testApp::mouseMoved(int x, int y ){}

void testApp::mouseDragged(int x, int y, int button){}

void testApp::mousePressed(int x, int y, int button){}

void testApp::mouseReleased(int x, int y, int button){
 rings.push_back(new Ring(ofPoint(x, y)));
 int note[8] = {28, 35, 40, 47, 52, 59, 64, 71};
 newRing = new ofxSCSynth("newRing");
 newRing->set("note", note[int(ofRandom(0, 8))]);
 newRing->set("pan", (x  ofGetWidth() / 2.0) / ofGetWidth() * 2.0);
 newRing->create();
}

void testApp::windowResized(int w, int h){}
[/sourcecode]