/// init the pd instance bool PdBase::PdContext::init(const int numInChannels, const int numOutChannels, const int sampleRate) { // attach callbacks libpd_printhook = (t_libpd_printhook) _print; libpd_banghook = (t_libpd_banghook) _bang; libpd_floathook = (t_libpd_floathook) _float; libpd_symbolhook = (t_libpd_symbolhook) _symbol; libpd_listhook = (t_libpd_listhook) _list; libpd_messagehook = (t_libpd_messagehook) _message; libpd_noteonhook = (t_libpd_noteonhook) _noteon; libpd_controlchangehook = (t_libpd_controlchangehook) _controlchange; libpd_programchangehook = (t_libpd_programchangehook) _programchange; libpd_pitchbendhook = (t_libpd_pitchbendhook) _pitchbend; libpd_aftertouchhook = (t_libpd_aftertouchhook) _aftertouch; libpd_polyaftertouchhook = (t_libpd_polyaftertouchhook) _polyaftertouch; libpd_midibytehook = (t_libpd_midibytehook) _midibyte; // init pd libpd_init(); if(libpd_init_audio(numInChannels, numOutChannels, sampleRate) != 0) { return false; } bInited = true; return bInited; }
JNIEXPORT void JNICALL Java_org_puredata_core_PdBase_initialize (JNIEnv *env, jclass cls) { CACHE_ENV objClass = LIBPD_CLASS_REF("java/lang/Object"); floatClass = LIBPD_CLASS_REF("java/lang/Float"); floatInit = (*env)->GetMethodID(env, floatClass, "<init>", "(F)V"); libpd_printhook = (t_libpd_printhook) java_printhook; libpd_banghook = (t_libpd_banghook) java_sendBang; libpd_floathook = (t_libpd_floathook) java_sendFloat; libpd_symbolhook = (t_libpd_symbolhook) java_sendSymbol; libpd_listhook = (t_libpd_listhook) java_sendList; libpd_messagehook = (t_libpd_messagehook) java_sendMessage; libpd_noteonhook = (t_libpd_noteonhook) java_sendNoteOn; libpd_controlchangehook = (t_libpd_controlchangehook) java_sendControlChange; libpd_programchangehook = (t_libpd_programchangehook) java_sendProgramChange; libpd_pitchbendhook = (t_libpd_pitchbendhook) java_sendPitchBend; libpd_aftertouchhook = (t_libpd_aftertouchhook) java_sendAftertouch; libpd_polyaftertouchhook = (t_libpd_polyaftertouchhook) java_sendPolyAftertouch; libpd_midibytehook = (t_libpd_midibytehook) java_sendMidiByte; libpd_init(); }
int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: %s file folder\n", argv[0]); return -1; } // init pd int srate = 44100; libpd_printhook = (t_libpd_printhook) pdprint; libpd_init(); libpd_init_audio(1, 2, srate, 1); float inbuf[64], outbuf[128]; // one input channel, two output channels // block size 64, one tick per buffer // compute audio [; pd dsp 1( libpd_start_message(); libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); // open patch [; pd open file folder( libpd_start_message(); libpd_add_symbol(argv[1]); libpd_add_symbol(argv[2]); libpd_finish_message("pd", "open"); // now run pd for ten seconds (logical time) int i; for (i = 0; i < 10 * srate / 64; i++) { // fill inbuf here libpd_process_float(inbuf, outbuf); // use outbuf here } return 0; }
Window::Window() { ap = new audioProcessor(); ap->setFrameLength(256); // Default vector size for Pd is 64. // We are calling libpd_process_float with 4 * 64 samples in "audiooutput.c". // creating our GUI controls createPureDataControls("Pure Data Controls"); createPortaudioControls(tr("Portaudio Controls")); QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(portaudioControlsGroup); layout->addWidget(pureDataControlsGroup); setLayout(layout); libpd_banghook = (t_libpd_banghook) Window::bangpd; // binding the function bangpd to receive bangs from our pd patchers libpd_printhook = (t_libpd_printhook) Window::printpd; // binding printpd libpd_init(); libpd_init_audio(0, 2, 44100); // 0 inputs, 2 outputs, we should adjust this (also for portaudio) every time we change the input or output device // and load a corresponding output patch, in this example it is statically set to 2 outputs and 0 inputs QString path = QCoreApplication::applicationDirPath(); libpd_openfile("puredata/stereoout.pd", path.toLatin1()); // loading an output patcher with 2 outputs libpd_openfile("puredata/firstpatcher.pd", path.toLatin1()); libpd_openfile("puredata/secondpatcher.pd", path.toLatin1()); // preloads the playback patchers, dynamically loading patchers seemed a little unstable on os x libpd_bind("playfinishedfirst"); // binding the pd patcher send "playfinishedfirst" to our libpd_banghook, libpd_printhook etc.. libpd_bind("playfinishedsecond"); // same for the send object "playfinishedsecond" setWindowTitle(tr("Libpd, Portaudio and Qt Test Project")); Window::globalWindow = this; // binding a static window to our window object in order to access our instance from bangpd and printpd }
int libpd_queued_init() { pd_receive_buffer = rb_create(BUFFER_SIZE); if (!pd_receive_buffer) return -1; midi_receive_buffer = rb_create(BUFFER_SIZE); if (!midi_receive_buffer) return -1; libpd_printhook = (t_libpd_printhook) internal_printhook; libpd_banghook = (t_libpd_banghook) internal_banghook; libpd_floathook = (t_libpd_floathook) internal_floathook; libpd_symbolhook = (t_libpd_symbolhook) internal_symbolhook; libpd_listhook = (t_libpd_listhook) internal_listhook; libpd_messagehook = (t_libpd_messagehook) internal_messagehook; libpd_noteonhook = (t_libpd_noteonhook) internal_noteonhook; libpd_controlchangehook = (t_libpd_controlchangehook) internal_controlchangehook; libpd_programchangehook = (t_libpd_programchangehook) internal_programchangehook; libpd_pitchbendhook = (t_libpd_pitchbendhook) internal_pitchbendhook; libpd_aftertouchhook = (t_libpd_aftertouchhook) internal_aftertouchhook; libpd_polyaftertouchhook = (t_libpd_polyaftertouchhook) internal_polyaftertouchhook; libpd_midibytehook = (t_libpd_midibytehook) internal_midibytehook; libpd_init(); return 0; }
/* require "pd" */ int luaopen_pd(lua_State * L) { /* make sure that both LuaAV and CppSound are initialized */ if (!initialized) { libpd_init(); initialized = 1; } // // luaL_newmetatable(L, "CppSoundSynth_metatable"); // lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); // lua_pushstring(L, "Csound"); lua_setfield(L, -2, "name"); // // luaL_Reg metalib[] = { // //{ "output", luaav3_Synth_setoutput }, //// { "setoutput", luaav3_Synth_setoutput }, //// { "amp", luaav3_Synth_setamp }, //// { "setamp", luaav3_Synth_setamp }, //// { "fadeout", luaav3_Synth_fadeout }, //// //// { "verbose", CppSoundSynth_verbose }, //// { "nchnls", CppSoundSynth_nchnls }, //// { "ksmps", CppSoundSynth_ksmps }, //// { "kr", CppSoundSynth_kr }, //// { "sr", CppSoundSynth_sr }, //// { "0dbfs", CppSoundSynth_0dbfs }, //// { "scoretime", CppSoundSynth_scoretime }, //// { "scorepending", CppSoundSynth_scorepending }, //// { "scorerewind", CppSoundSynth_scorerewind }, //// { "scoreevent", CppSoundSynth_scoreevent }, //// { "messagelevel", CppSoundSynth_messagelevel }, //// { "message", CppSoundSynth_message }, //// { "listopcodes", CppSoundSynth_listopcodes }, //// //// { "getinstrumentcount", CppSoundSynth_getInstrumentCount }, //// { "ins", CppSoundSynth_getSpin }, // // { NULL, NULL } // }; // luaL_register(L, NULL, metalib); // sentinel on unload //gc_sentinel(L, -1, unload); // define the module table luaL_Reg lib[] = { { "clear_search_path", lua_clear_search_path }, { "add_to_search_path", lua_add_to_search_path }, { NULL, NULL } }; luaL_register(L, "pd", lib); // lua_pushinteger(L, csoundGetVersion()); // lua_setfield(L, -2, "version"); return 1; }
int portAudio::initPortAudio() { //the *hook functions deal with receiving messages from Pd, see https://github.com/libpd/libpd/wiki/libpd libpd_set_printhook(pdprint); //libpd_set_noteonhook(pdnoteon); //libpd_set_floathook(pdfloat); libpd_init(); libpd_init_audio(2, 2, this->getSampleRate()); //one channel in, one channel out // compute audio [; pd dsp 1( libpd_start_message(1); // one entry in list libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); // open patch [; pd open file folder(, the handle is a void* handle = libpd_openfile("latido.pd","../../Source"); //pass a number to pd, myMessage is a pd receive libpd_float("myMessage", 10011); for (int i = 0; i < this->getBlockSize(); i++) { //intialize buffer, this is probably unnecessary theBuffer.outbuf[i] = 0; } /* Initialize portaudio library before making any other calls. */ err = Pa_Initialize(); if( err != paNoError ) this->sendError(err); /* Open an portaudio I/O stream. */ err = Pa_OpenDefaultStream( &stream, 1, /* input channels */ 2, /* output channels */ paFloat32, /* 32 bit floating point output */ portAudio::getSampleRate(), portAudio::getBlockSize(), /* frames per buffer */ portAudio::paCallback, &theBuffer ); if( err != paNoError ) this->sendError(err); err = Pa_StartStream( stream ); if( err != paNoError ) this->sendError(err); return err; }
// ===================== // = INITIALIZE LIB PD = // ===================== void initLibPd() { // init the pd engine libpd_init(); libpd_init_audio(2, 2, sampleRate); //nInputs, nOutputs, sampleRate // send compute audio 1 message to pd libpd_start_message(1); libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); // load the patch patchFile = libpd_openfile( (char*)filename.c_str(), (char*)directory.c_str() ); if (patchFile == NULL) { std::cout << "Could not open patch"; exit(1); } }
int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "usage: %s file folder\n", argv[0]); return -1; } // init pd int srate = 44100, foo; libpd_set_printhook((t_libpd_printhook)pdprint); libpd_set_noteonhook((t_libpd_noteonhook)pdnoteon); libpd_init(); libpd_init_audio(1, 2, srate); // compute audio [; pd dsp 1( libpd_start_message(1); // one entry in list libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); // open patch [; pd open file folder( void *file = libpd_openfile(argv[1], argv[2]); // now run pd for (foo = 0; foo < 2; foo++) /* note: doesn't yet work the second time */ { printf("running nogui for 1000 ticks...\n"); runawhile(1); printf("starting gui..\n"); if (libpd_start_gui("../../../pure-data/")) printf("gui startup failed\n"); printf("running for 2000 more ticks...\n"); runawhile(2); libpd_stop_gui(); } printf("Closing and exiting\n"); libpd_closefile(file); return 0; }
// ===================== // = INITIALIZE LIB PD = // ===================== void initLibPd(){ // The Jack server only seems to run fine at 256 samples per frame and libpd only processess samples // in chunks (ticks) of n*64 samples at a time. We need to set the ticksPerBuffer to 4. // init the pd engine libpd_init(); libpd_init_audio(2, 2, sampleRate, 4); //nInputs, nOutputs, sampleRate, ticksPerBuffer // send compute audio 1 message to pd libpd_start_message(1); libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); // load the patch patchFile = libpd_openfile( (char*)filename.c_str(), (char*)directory.c_str() ); if (patchFile == NULL) { std::cout << "Could not open patch"; exit(1); } }
/// init the pd instance bool PdBase::PdContext::init(const int numInChannels, const int numOutChannels, const int sampleRate) { // attach callbacks libpd_printhook = (t_libpd_printhook) libpd_print_concatenator; libpd_concatenated_printhook = (t_libpd_printhook) _print; libpd_banghook = (t_libpd_banghook) _bang; libpd_floathook = (t_libpd_floathook) _float; libpd_symbolhook = (t_libpd_symbolhook) _symbol; libpd_listhook = (t_libpd_listhook) _list; libpd_messagehook = (t_libpd_messagehook) _message; libpd_noteonhook = (t_libpd_noteonhook) _noteon; libpd_controlchangehook = (t_libpd_controlchangehook) _controlchange; libpd_programchangehook = (t_libpd_programchangehook) _programchange; libpd_pitchbendhook = (t_libpd_pitchbendhook) _pitchbend; libpd_aftertouchhook = (t_libpd_aftertouchhook) _aftertouch; libpd_polyaftertouchhook = (t_libpd_polyaftertouchhook) _polyaftertouch; libpd_midibytehook = (t_libpd_midibytehook) _midibyte; // init libpd, should only be called once! if(!bLibPDInited) { libpd_init(); bLibPDInited = true; } // init audio if(libpd_init_audio(numInChannels, numOutChannels, sampleRate) != 0) { return false; } bInited = true; messages.clear(); return bInited; }
/// init the pd instance bool PdBase::PdContext::init(const int numInChannels, const int numOutChannels, const int sampleRate) { // attach callbacks libpd_set_printhook(libpd_print_concatenator); libpd_set_concatenated_printhook(_print); libpd_set_banghook(_bang); libpd_set_floathook(_float); libpd_set_symbolhook(_symbol); libpd_set_listhook(_list); libpd_set_messagehook(_message); libpd_set_noteonhook(_noteon); libpd_set_controlchangehook(_controlchange); libpd_set_programchangehook(_programchange); libpd_set_pitchbendhook(_pitchbend); libpd_set_aftertouchhook(_aftertouch); libpd_set_polyaftertouchhook(_polyaftertouch); libpd_set_midibytehook(_midibyte); // init libpd, should only be called once! if(!bLibPDInited) { libpd_init(); bLibPDInited = true; } // init audio if(libpd_init_audio(numInChannels, numOutChannels, sampleRate) != 0) { return false; } bInited = true; messages.clear(); return bInited; }
PdDataflowTest() { libpd_init(); libpd_set_printhook([](const char* s) { qDebug() << "string: " << s; }); }
int main(int argc, char **argv) { t_pdinstance *pd1 = pdinstance_new(), *pd2 = pdinstance_new(); if (argc < 3) { fprintf(stderr, "usage: %s file folder\n", argv[0]); return -1; } int srate = 44100; // maybe these two calls should be available per-instnace somehow: libpd_set_printhook(pdprint); libpd_set_noteonhook(pdnoteon); /* set a "current" instance before libpd_init() or else Pd will make an unnecessary third "default" instance. */ pd_setinstance(pd1); libpd_init(); /* ... here we'd sure like to be able to have number of channels be per-nstance. The sample rate is still global within Pd but we might also consider relaxing that restrction. */ libpd_init_audio(1, 2, srate); float inbuf[64], outbuf[128]; // one input channel, two output channels // block size 64, one tick per buffer pd_setinstance(pd1); // talk to first pd instance // compute audio [; pd dsp 1( libpd_start_message(1); // one entry in list libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); // open patch [; pd open file folder( libpd_openfile(argv[1], argv[2]); pd_setinstance(pd2); // compute audio [; pd dsp 1( libpd_start_message(1); // one entry in list libpd_add_float(1.0f); libpd_finish_message("pd", "dsp"); // open patch [; pd open file folder( libpd_openfile(argv[1], argv[2]); /* the following two messages can be sent without setting the pd nstance and anyhow the symbols are global so they may affect multiple instances. However, if the messages change anyhing in the pd instacne structure (DSP state; current time; list of all canvases n our instance) those changes will apply to the current Pd nstance, so the earlier messages, for instance, were sensitive to which was the current one. Note also that I'm using the fact that $0 is set to 1003, 1004, ... as patches are opened -it would be better to opent the patches with settable $1, etc parameters to libpd_openfile(). */ // [; pd frequency 1 ( libpd_start_message(1); // one entry in list libpd_add_float(1.0f); libpd_finish_message("1003-frequency", "float"); // [; pd frequency 1 ( libpd_start_message(1); // one entry in list libpd_add_float(2.0f); libpd_finish_message("1004-frequency", "float"); // now run pd for ten seconds (logical time) int i, j; for (i = 0; i < 3; i++) { // fill inbuf here pd_setinstance(pd1); libpd_process_float(1, inbuf, outbuf); if (i < 2) { for (j = 0; j < 8; j++) printf("%f ", outbuf[j]); printf("\n"); } pd_setinstance(pd2); libpd_process_float(1, inbuf, outbuf); if (i < 2) { for (j = 0; j < 8; j++) printf("%f ", outbuf[j]); printf("\n"); } } return 0; }