You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.0KB

  1. #include "ScriptEngine.hpp"
  2. #include "z_libpd.h"
  3. #include <iostream>
  4. #include <fstream>
  5. using namespace std;
  6. #define BUFFERSIZE MAX_BUFFER_SIZE * NUM_ROWS
  7. void chair_write_patch_to_file(const string &patch, const string &path, const string &name )
  8. {
  9. ofstream myfile;
  10. cout << "file to write: " << path + "/" + name << endl;
  11. myfile.open (path + "/" + name);
  12. myfile << patch;
  13. myfile.close();
  14. }
  15. void chair_load_patch(const string &patch, const string &name)
  16. {
  17. string path = ".";
  18. chair_write_patch_to_file(patch, path, name);
  19. libpd_openfile(name.c_str(), path.c_str());
  20. remove( (path + "/" + name).c_str() );
  21. }
  22. struct LibPDEngine : ScriptEngine {
  23. ~LibPDEngine() {
  24. }
  25. // // variables for libpd
  26. t_pdinstance *_lpd;
  27. int _pd_block_size = 64;
  28. int _sampleRate = 0;
  29. int _ticks = 0;
  30. float _output[BUFFERSIZE];
  31. float _input[BUFFERSIZE];// = (float*)malloc(1024*2*sizeof(float));
  32. // end variables for libpd
  33. std::string getEngineName() override {
  34. return "Pure Data";
  35. }
  36. int run(const std::string& path, const std::string& script) override {
  37. ProcessBlock* block = getProcessBlock();
  38. _sampleRate = block->sampleRate;
  39. setBufferSize(_pd_block_size);
  40. setFrameDivider(1);
  41. libpd_init();
  42. _lpd = libpd_new_instance();
  43. libpd_set_instance(_lpd);
  44. libpd_init_audio(NUM_ROWS, NUM_ROWS, _sampleRate);
  45. //cout << "_lpd is initialized" << endl;
  46. //cout << "num od pd instances: " << libpd_num_instances() << endl;
  47. // compute audio [; pd dsp 1(
  48. libpd_start_message(1); // one enstry in list
  49. libpd_add_float(1.0f);
  50. libpd_finish_message("pd", "dsp");
  51. std::string name = "test.pd";
  52. std::string patch = "#N canvas 653 457 450 300 12;\n"
  53. "#X obj 151 125 adc~ 1 2 3 4 5 6;\n"
  54. "#X obj 152 184 dac~ 1 2 3 4 5 6;\n"
  55. "#X connect 0 0 1 0;\n"
  56. "#X connect 0 1 1 1;\n"
  57. "#X connect 0 2 1 2;\n"
  58. "#X connect 0 3 1 3;\n"
  59. "#X connect 0 4 1 4;\n"
  60. "#X connect 0 5 1 5;";
  61. /*"#N canvas 333 425 105 153 10;\n"
  62. "#X obj 32 79 dac~;\n"
  63. "#X obj 32 27 adc~;\n"
  64. "#X connect 1 0 0 0;\n"
  65. "#X connect 1 1 0 1;";*/
  66. //libpd_openfile(patch.c_str(), path.c_str());
  67. chair_load_patch(patch, name);
  68. return 0;
  69. }
  70. int process() override {
  71. // block
  72. ProcessBlock* block = getProcessBlock();
  73. display(std::to_string(block->bufferSize));
  74. // pass samples to/from libpd
  75. _ticks = 1;//curr_bufSize / 64;
  76. int rows = NUM_ROWS;
  77. for (int s = 0; s < _pd_block_size; s++) {
  78. for (int r = 0; r < rows; r++) {
  79. _input[s*rows+r] = block->inputs[r][s];
  80. }
  81. }
  82. libpd_set_instance(_lpd);
  83. libpd_process_float(_ticks, _input, _output);
  84. for (int s = 0; s < _pd_block_size; s++) {
  85. for (int r = 0; r < rows; r++) {
  86. block->outputs[r][s] = _output[s*rows+r];
  87. }
  88. }
  89. return 0;
  90. }
  91. };
  92. __attribute__((constructor(1000)))
  93. static void constructor() {
  94. addScriptEngine<LibPDEngine>("pd");
  95. }