//-------------------------------------------------------------- void AppCore::setup(const int numOutChannels, const int numInChannels, const int sampleRate, const int ticksPerBuffer) { ofSetFrameRate(60); ofSetVerticalSync(true); //ofSetLogLevel(OF_LOG_VERBOSE); // double check where we are ... cout << ofFilePath::getCurrentWorkingDirectory() << endl; if(!pd.init(numOutChannels, numInChannels, sampleRate, ticksPerBuffer)) { ofLog(OF_LOG_ERROR, "Could not init pd"); OF_EXIT_APP(1); } midiChan = 1; // midi channels are 1-16 // subscribe to receive source names pd.subscribe("toOF"); pd.subscribe("env"); // add message receiver, disables polling (see processEvents) pd.addReceiver(*this); // automatically receives from all subscribed sources pd.ignore(*this, "env"); // don't receive from "env" //pd.ignore(*this); // ignore all sources //pd.receive(*this, "toOF"); // receive only from "toOF" // add midi receiver pd.addMidiReceiver(*this); // automatically receives from all channels //pd.ignoreMidi(*this, 1); // ignore midi channel 1 //pd.ignoreMidi(*this); // ignore all channels //pd.receiveMidi(*this, 1); // receive only from channel 1 // add the data/pd folder to the search path pd.addToSearchPath("pd"); // audio processing on pd.start(); cout << endl << "BEGIN Patch Test" << endl; // open patch Patch patch = pd.openPatch("test.pd"); cout << patch << endl; // close patch pd.closePatch(patch); cout << patch << endl; // open patch patch = pd.openPatch("test.pd"); cout << patch << endl; cout << "FINISH Patch Test" << endl; cout << endl << "BEGIN Message Test" << endl; // test basic atoms pd.sendBang("fromOF"); pd.sendFloat("fromOF", 100); pd.sendSymbol("fromOF", "test string"); // stream interface pd << Bang("fromOF") << Float("fromOF", 100) << Symbol("fromOF", "test string"); // send a list pd.startMessage(); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finishList("fromOF"); // send a message to the $0 receiver ie $0-toOF pd.startMessage(); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finishList(patch.dollarZeroStr()+"-fromOF"); // send a list using the List object List testList; testList.addFloat(1.23); testList.addSymbol("sent from a List object"); pd.sendList("fromOF", testList); pd.sendMessage("fromOF", "msg", testList); // stream interface for list pd << StartMessage() << 1.23 << "sent from a streamed list" << FinishList("fromOF"); cout << "FINISH Message Test" << endl; cout << endl << "BEGIN MIDI Test" << endl; // send functions pd.sendNoteOn(midiChan, 60); pd.sendControlChange(midiChan, 0, 64); pd.sendProgramChange(midiChan, 100); // note: pgm num range is 1 - 128 pd.sendPitchBend(midiChan, 2000); // note: ofxPd uses -8192 - 8192 while [bendin] returns 0 - 16383, // so sending a val of 2000 gives 10192 in pd pd.sendAftertouch(midiChan, 100); pd.sendPolyAftertouch(midiChan, 64, 100); pd.sendMidiByte(0, 239); // note: pd adds +2 to the port number from [midiin], [sysexin], & [realtimein] pd.sendSysex(0, 239); // so sending to port 0 gives port 2 in pd pd.sendSysRealTime(0, 239); // stream pd << NoteOn(midiChan, 60) << ControlChange(midiChan, 100, 64) << ProgramChange(midiChan, 100) << PitchBend(midiChan, 2000) << Aftertouch(midiChan, 100) << PolyAftertouch(midiChan, 64, 100) << StartMidi(0) << 239 << Finish() << StartSysex(0) << 239 << Finish() << StartSysRealTime(0) << 239 << Finish(); cout << "FINISH MIDI Test" << endl; cout << endl << "BEGIN Array Test" << endl; // array check length cout << "array1 len: " << pd.arraySize("array1") << endl; // read array std::vector<float> array1; pd.readArray("array1", array1); // sets array to correct size cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // write array for(int i = 0; i < array1.size(); ++i) array1[i] = i; pd.writeArray("array1", array1); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // clear array pd.clearArray("array1", 10); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; cout << "FINISH Array Test" << endl; cout << endl << "BEGIN PD Test" << endl; pd.sendSymbol("fromOF", "test"); cout << "FINISH PD Test" << endl << endl; cout << endl << "BEGIN Event Polling Test" << endl; // clear receivers, enable polling pd.clearReceivers(); pd.clearMidiReceivers(); pd.sendSymbol("fromOF", "test"); processEvents(); // <-- manually poll for events // re-add receivers, disable polling pd.addReceiver(*this); pd.addMidiReceiver(*this); pd.ignore(*this, "env"); cout << "FINISH Event Polling Test" << endl << endl; // play a tone by sending a list // [list tone pitch 72 ( pd.startMessage(); pd.addSymbol("pitch"); pd.addFloat(72); pd.finishList("tone"); pd.sendBang("tone"); }
void AudioGenerativeApp::setup() { mAudioBuff = (float*) malloc(1000000 * sizeof(float)); mPd = new ofxPd(); mPdListener = new PdListener(); // Audio capture set up // //iterate input devices and print their names to the console audio::InputDeviceRef builtInMic = audio::Input::findDeviceByName("Built-in Microphone"); audio::InputDeviceRef builtInInput = audio::Input::findDeviceByName("Built-in Input"); const std::vector<audio::InputDeviceRef>& devices = audio::Input::getDevices(); for( std::vector<audio::InputDeviceRef>::const_iterator iter = devices.begin(); iter != devices.end(); ++iter ) { console() << (*iter)->getName() << std::endl; } if (builtInMic){// && false) { console() << "Using Built-in Microphone\n"; mInput = new audio::Input(builtInMic); } else if (builtInInput) { console() << "Using Built-in Input\n"; mInput = new audio::Input(builtInInput); } else { console() << "Using default input\n"; mInput = new audio::Input(); } ofxPd& pd = *mPd; // libpd ticks per buffer, 8 ticks * 64 bytes = buffer len 512 int ticksPerBuffer = 8; int outChannels = 2; int inChannels = inputChannelCount; int sampleRate = 44100; if (!pd.init(outChannels, inChannels, sampleRate, ticksPerBuffer)) { console() << "ERROR: libpd didn't initialize." << std::endl; } // audio processing on pd.dspOn(); // Everything beyond this point is test code to demonstrate usage // add receive source names pd.addSource("toOF"); pd.addSource("env"); // add listener pd.addListener(*mPdListener); pd.subscribe(*mPdListener); // listen to everything pd.unsubscribe(*mPdListener, "env"); // don't listen to "env" //pd.subscribe(*this, "toOF"); // listen to "toOF" //pd.unsubscribe(*this); // don't listen to anything // add the data/pd folder to the search path pd.addToSearchPath("pd"); cout << endl << "BEGIN Patch Test" << endl; //DataSourceRef data = app::App::loadResource(RES_TEST_PD); //Buffer& buffer = data->getBuffer(); //const char* text = (const char*) buffer.getData(); Patch patch = pd.openPatch("resources", "test.pd"); //pd.openPatchFromText(text); cout << patch << endl; // close patch pd.closePatch(patch); cout << patch << endl; // open patch patch = pd.openPatch("resources", "test.pd"); //pd.openPatchFromText(text); cout << patch << endl; cout << "FINISH Patch Test" << endl; cout << endl << "BEGIN Message Test" << endl; // test basic atoms pd.sendBang("fromOF"); pd.sendFloat("fromOF", 100); pd.sendSymbol("fromOF", "test string"); // send a list pd.startList("fromOF"); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finish(); // send a message to the $0 reciever ie $0-toOF pd.startList(patch.dollarZeroStr()+"-fromOF"); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finish(); // send a list using the List object List testList; testList.addFloat(1.23); testList.addSymbol("sent from a List object"); pd.sendList("fromOF", testList); pd.sendMsg("fromOF", "msg", testList); cout << "FINISH Message Test" << endl; cout << endl << "BEGIN MIDI Test" << endl; // send functions pd.sendNote(60); pd.sendCtl(100, 64); pd.sendPgm(100); pd.sendBend(2000); pd.sendTouch(100); pd.sendPolyTouch(64, 100); pd.sendMidiByte(239, 1); pd.sendSysExByte(239, 1); pd.sendSysRTByte(239, 1); // stream pd << Note(60) << Ctl(100, 64) << Bend(2000) << Touch(100) << PolyTouch(64, 100) << StartMidi(1) << 239 << Finish() << StartSysEx(1) << 239 << Finish() << StartSysRT(1) << 239 << Finish(); cout << "FINISH MIDI Test" << endl; cout << endl << "BEGIN Array Test" << endl; // array check length cout << "array1 len: " << pd.getArrayLen("array1") << endl; // read array std::vector<float> array1; pd.readArray("array1", array1); // sets array to correct size cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // write array for(int i = 0; i < array1.size(); ++i) array1[i] = i; pd.writeArray("array1", array1); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // clear array pd.clearArray("array1", 10); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; cout << "FINISH Array Test" << endl; cout << endl << "BEGIN PD Test" << endl; pd.sendSymbol("fromOF", "test"); cout << "FINISH PD Test" << endl << endl; // play a tone by sending a list // [list tone pitch 72 ( pd.startList("tone"); pd.addSymbol("pitch"); pd.addFloat(72); pd.finish(); pd.sendBang("tone"); cout << "PLAYED A TONE" << endl; mPdTrack = audio::Output::addTrack(audio::createCallback( this, &AudioGenerativeApp::audioSynthCallback)); mPdTrack->play(); }
// Howdy gentle libpd user, // // This is just a simple test to make sure message passing in the libpd c++ layer // is working. Like the C test, this simulates 10 seconds of logical audio time // by calling processFloat in a for loop. Running this app will not produce any // sound as it is not using an audio api. You need to add that yourself using // something like PortAudio, Jack, etc as C++ does not have a default audio library. // int main(int argc, char **argv) { // our pd engine PdBase pd; // one input channel, two output channels // block size 64, one tick per buffer float inbuf[64], outbuf[128]; // custom receiver object for messages and midi PdObject pdObject; // init pd int srate = 44100; if(!pd.init(1, 2, srate)) { cerr << "Could not init pd" << endl; exit(1); } int midiChan = 1; // midi channels are 0-15 // subscribe to receive source names pd.subscribe("toCPP"); pd.subscribe("env"); // set receivers pd.setReceiver(&pdObject); pd.setMidiReceiver(&pdObject); // add the data/pd folder to the search path pd.addToSearchPath("pd"); // audio processing on pd.computeAudio(true); cout << endl << "BEGIN Patch Test" << endl; // open patch Patch patch = pd.openPatch("test.pd", "."); cout << patch << endl; // close patch pd.closePatch(patch); cout << patch << endl; // open patch again patch = pd.openPatch(patch); cout << patch << endl; cout << "FINISH Patch Test" << endl; cout << endl << "BEGIN Message Test" << endl; // test basic atoms pd.sendBang("fromCPP"); pd.sendFloat("fromCPP", 100); pd.sendSymbol("fromCPP", "test string"); // stream interface pd << Bang("fromCPP") << Float("fromCPP", 100) << Symbol("fromCPP", "test string"); // send a list pd.startMessage(); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finishList("fromCPP"); // send a message to the $0 receiver ie $0-toOF pd.startMessage(); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finishList(patch.dollarZeroStr()+"-fromCPP"); // send a list using the List object List testList; testList.addFloat(1.23); testList.addSymbol("sent from a List object"); pd.sendList("fromCPP", testList); pd.sendMessage("fromCPP", "msg", testList); // stream interface for list pd << StartMessage() << 1.23 << "sent from a streamed list" << FinishList("fromCPP"); cout << "FINISH Message Test" << endl; cout << endl << "BEGIN MIDI Test" << endl; // send functions pd.sendNoteOn(midiChan, 60); pd.sendControlChange(midiChan, 0, 64); pd.sendProgramChange(midiChan, 100); // note: pgm num range is 1 - 128 pd.sendPitchBend(midiChan, 2000); // note: libpd uses -8192 - 8192 while [bendin] returns 0 - 16383, // so sending a val of 2000 gives 10192 in pd pd.sendAftertouch(midiChan, 100); pd.sendPolyAftertouch(midiChan, 64, 100); pd.sendMidiByte(0, 239); // note: pd adds +2 to the port number from [midiin], [sysexin], & [realtimein] pd.sendSysex(0, 239); // so sending to port 0 gives port 2 in pd pd.sendSysRealTime(0, 239); // stream pd << NoteOn(midiChan, 60) << ControlChange(midiChan, 100, 64) << ProgramChange(midiChan, 100) << PitchBend(midiChan, 2000) << Aftertouch(midiChan, 100) << PolyAftertouch(midiChan, 64, 100) << StartMidi(0) << 239 << Finish() << StartSysex(0) << 239 << Finish() << StartSysRealTime(0) << 239 << Finish(); cout << "FINISH MIDI Test" << endl; cout << endl << "BEGIN Array Test" << endl; // array check length cout << "array1 len: " << pd.arraySize("array1") << endl; // read array std::vector<float> array1; pd.readArray("array1", array1); // sets array to correct size cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // write array for(int i = 0; i < array1.size(); ++i) array1[i] = i; pd.writeArray("array1", array1); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // clear array pd.clearArray("array1", 10); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; cout << "FINISH Array Test" << endl; cout << endl << "BEGIN PD Test" << endl; pd.sendSymbol("fromCPP", "test"); cout << "FINISH PD Test" << endl << endl; cout << endl << "BEGIN Event Polling Test" << endl; // disable receivers, enable polling pd.setReceiver(NULL); pd.setMidiReceiver(NULL); pd.sendSymbol("fromCPP", "test"); testEventPolling(pd); // reenable receivers, disable polling pd.setReceiver(&pdObject); pd.setMidiReceiver(&pdObject); cout << "FINISH Event Polling Test" << endl << endl; // play a tone by sending a list // [list tone pitch 72 ( pd.startMessage(); pd.addSymbol("pitch"); pd.addFloat(72); pd.finishList("tone"); pd.sendBang("tone"); // now run pd for ten seconds (logical time) for(int i = 0; i < 10 * srate / 64; i++) { // fill inbuf here pd.processFloat(1, inbuf, outbuf); // use outbuf here } // be nice and clean up on exit pd.closePatch(patch); pd.computeAudio(false); return 0; }
//-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetVerticalSync(true); //ofSetLogLevel("Pd", OF_LOG_VERBOSE); // see verbose info inside // double check where we are ... cout << ofFilePath::getCurrentWorkingDirectory() << endl; // the number of libpd ticks per buffer, // used to compute the audio buffer len: tpb * blocksize (always 64) #ifdef TARGET_LINUX_ARM // longer latency for Raspberry PI int ticksPerBuffer = 32; // 32 * 64 = buffer len of 2048 int numInputs = 0; // no built in mic #else int ticksPerBuffer = 8; // 8 * 64 = buffer len of 512 int numInputs = 1; #endif // setup OF sound stream ofSoundStreamSetup(2, numInputs, this, 44100, ofxPd::blockSize()*ticksPerBuffer, 3); // setup Pd // // set 4th arg to true for queued message passing using an internal ringbuffer, // this is useful if you need to control where and when the message callbacks // happen (ie. within a GUI thread) // // note: you won't see any message prints until update() is called since // the queued messages are processed there, this is normal // if(!pd.init(2, numInputs, 44100, ticksPerBuffer, false)) { OF_EXIT_APP(1); } midiChan = 1; // midi channels are 1-16 // subscribe to receive source names pd.subscribe("toOF"); pd.subscribe("env"); // add message receiver, required if you want to recieve messages pd.addReceiver(*this); // automatically receives from all subscribed sources pd.ignoreSource(*this, "env"); // don't receive from "env" //pd.ignoreSource(*this); // ignore all sources //pd.receiveSource(*this, "toOF"); // receive only from "toOF" // add midi receiver, required if you want to recieve midi messages pd.addMidiReceiver(*this); // automatically receives from all channels //pd.ignoreMidiChannel(*this, 1); // ignore midi channel 1 //pd.ignoreMidiChannel(*this); // ignore all channels //pd.receiveMidiChannel(*this, 1); // receive only from channel 1 // add the data/pd folder to the search path pd.addToSearchPath("pd/abs"); // audio processing on pd.start(); // ----------------------------------------------------- cout << endl << "BEGIN Patch Test" << endl; // open patch Patch patch = pd.openPatch("pd/test.pd"); cout << patch << endl; // close patch pd.closePatch(patch); cout << patch << endl; // open patch again patch = pd.openPatch(patch); cout << patch << endl; cout << "FINISH Patch Test" << endl; // ----------------------------------------------------- cout << endl << "BEGIN Message Test" << endl; // test basic atoms pd.sendBang("fromOF"); pd.sendFloat("fromOF", 100); pd.sendSymbol("fromOF", "test string"); // stream interface pd << Bang("fromOF") << Float("fromOF", 100) << Symbol("fromOF", "test string"); // send a list pd.startMessage(); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finishList("fromOF"); // send a message to the $0 receiver ie $0-fromOF pd.startMessage(); pd.addFloat(1.23); pd.addSymbol("a symbol"); pd.finishList(patch.dollarZeroStr()+"-fromOF"); // send a list using the List object List testList; testList.addFloat(1.23); testList.addSymbol("sent from a List object"); pd.sendList("fromOF", testList); pd.sendMessage("fromOF", "msg", testList); // stream interface for list pd << StartMessage() << 1.23 << "sent from a streamed list" << FinishList("fromOF"); cout << "FINISH Message Test" << endl; // ----------------------------------------------------- cout << endl << "BEGIN MIDI Test" << endl; // send functions pd.sendNoteOn(midiChan, 60); pd.sendControlChange(midiChan, 0, 64); pd.sendProgramChange(midiChan, 100); // note: pgm num range is 1 - 128 pd.sendPitchBend(midiChan, 2000); // note: ofxPd uses -8192 - 8192 while [bendin] returns 0 - 16383, // so sending a val of 2000 gives 10192 in pd pd.sendAftertouch(midiChan, 100); pd.sendPolyAftertouch(midiChan, 64, 100); pd.sendMidiByte(0, 239); // note: pd adds +2 to the port number from [midiin], [sysexin], & [realtimein] pd.sendSysex(0, 239); // so sending to port 0 gives port 2 in pd pd.sendSysRealTime(0, 239); // stream pd << NoteOn(midiChan, 60) << ControlChange(midiChan, 100, 64) << ProgramChange(midiChan, 100) << PitchBend(midiChan, 2000) << Aftertouch(midiChan, 100) << PolyAftertouch(midiChan, 64, 100) << StartMidi(0) << 239 << Finish() << StartSysex(0) << 239 << Finish() << StartSysRealTime(0) << 239 << Finish(); cout << "FINISH MIDI Test" << endl; // ----------------------------------------------------- cout << endl << "BEGIN Array Test" << endl; // array check length cout << "array1 len: " << pd.arraySize("array1") << endl; // read array std::vector<float> array1; pd.readArray("array1", array1); // sets array to correct size cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // write array for(int i = 0; i < array1.size(); ++i) array1[i] = i; pd.writeArray("array1", array1); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; // clear array pd.clearArray("array1", 10); // ready array pd.readArray("array1", array1); cout << "array1 "; for(int i = 0; i < array1.size(); ++i) cout << array1[i] << " "; cout << endl; cout << "FINISH Array Test" << endl; // ----------------------------------------------------- cout << endl << "BEGIN PD Test" << endl; pd.sendSymbol("fromOF", "test"); cout << "FINISH PD Test" << endl << endl; // ----------------------------------------------------- cout << endl << "BEGIN Instance Test" << endl; // open 10 instances for(int i = 0; i < 10; ++i) { Patch p = pd.openPatch("pd/instance.pd"); instances.push_back(p); } // send a hello bang to each instance individually using the dollarZero // to [r $0-instance] which should print the instance dollarZero unique id // and a unique random number for(int i = 0; i < instances.size(); ++i) { pd.sendBang(instances[i].dollarZeroStr()+"-instance"); } // send a random float between 0 and 100 for(int i = 0; i < instances.size(); ++i) { pd.sendFloat(instances[i].dollarZeroStr()+"-instance", int(ofRandom(0, 100))); } // send a symbol for(int i = 0; i < instances.size(); ++i) { pd.sendSymbol(instances[i].dollarZeroStr()+"-instance", "howdy dude"); } // close all instances for(int i = 0; i < instances.size(); ++i) { pd.closePatch(instances[i]); } instances.clear(); cout << "FINISH Instance Test" << endl; // ----------------------------------------------------- // play a tone by sending a list // [list tone pitch 72 ( pd.startMessage(); pd.addSymbol("pitch"); pd.addFloat(72); pd.finishList("tone"); pd.sendBang("tone"); }