Extra "ports" of juce-based plugins using the distrho build system
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.

408 lines
10KB

  1. #include "Plugin.h"
  2. #include "PluginEditor.h"
  3. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  4. {
  5. return new Plugin();
  6. }
  7. Plugin::Plugin()
  8. {
  9. parameters = new Parameters(this, kInternalBlocksize);
  10. granulator = new Granulator(parameters, kInternalBlocksize);
  11. program_bank = new ProgramBank(kNumPrograms, parameters);
  12. editor_parameter_update_pending = false;
  13. editor_program_update_pending = false;
  14. current_program = 0;
  15. saved_state = true;
  16. block_sample_pos = 0;
  17. progchange_param_enabled = true;
  18. }
  19. Plugin::~Plugin()
  20. {
  21. delete parameters;
  22. delete granulator;
  23. delete program_bank;
  24. }
  25. /*
  26. void Plugin::createAppSettingsDir()
  27. {
  28. File appDataDir = File::getSpecialLocation(File::userApplicationDataDirectory);
  29. File* argotlunarDir = new File(appDataDir.getFullPathName() + "/.argotlunar");
  30. argotlunarDir->createDirectory();
  31. }
  32. File* Plugin::getMidiMapFile()
  33. {
  34. File appDataDir = File::getSpecialLocation(File::userApplicationDataDirectory);
  35. return new File(appDataDir.getFullPathName() + "/.argotlunar/midimap.xml");
  36. }
  37. */
  38. bool Plugin::acceptsMidi() const
  39. {
  40. return false;
  41. }
  42. bool Plugin::isInputChannelStereoPair(int index) const
  43. {
  44. return true;
  45. }
  46. bool Plugin::isOutputChannelStereoPair(int index) const
  47. {
  48. return true;
  49. }
  50. bool Plugin::producesMidi() const
  51. {
  52. return false;
  53. }
  54. bool Plugin::hasEditor() const
  55. {
  56. return true;
  57. }
  58. const String Plugin::getName() const
  59. {
  60. return "Argotlunar2";
  61. }
  62. const String Plugin::getInputChannelName(const int channelIndex) const
  63. {
  64. return String (channelIndex + 1);
  65. }
  66. const String Plugin::getOutputChannelName(const int channelIndex) const
  67. {
  68. return (channelIndex == 0) ? "L" : "R";
  69. }
  70. double Plugin::getTailLengthSeconds() const
  71. {
  72. return 0.0;
  73. }
  74. AudioProcessorEditor* Plugin::createEditor()
  75. {
  76. return new PluginEditor(this);
  77. }
  78. void Plugin::prepareToPlay (double samplerate, int samples_per_block)
  79. {
  80. granulator->prepareToPlay(static_cast<float>(samplerate));
  81. }
  82. void Plugin::releaseResources()
  83. {
  84. granulator->releaseResources();
  85. }
  86. void Plugin::processBlock(AudioSampleBuffer& buffer, MidiBuffer& midiMessages)
  87. {
  88. if (getTotalNumInputChannels() != 2 && getTotalNumOutputChannels() != 2) {
  89. return;
  90. }
  91. float* chan1 = buffer.getWritePointer(0);
  92. float* chan2 = buffer.getWritePointer(1);
  93. int sampleframes = buffer.getNumSamples();
  94. int blocks = sampleframes / kInternalBlocksize;
  95. if (getPlayHead() != 0 && getPlayHead()->getCurrentPosition(pos)) {
  96. if ((&pos)->bpm == 0.0f) {
  97. parameters->setQuantizationDisabled();
  98. parameters->setParameter(kDelayQuant, 0.0f, false);
  99. parameters->setParameter(kIotQuant, 0.0f, false);
  100. parameters->setParameter(kDurQuant, 0.0f, false);
  101. }
  102. else
  103. parameters->time_quantizer->setPositionInfo(&pos);
  104. } else {
  105. parameters->setQuantizationDisabled();
  106. }
  107. block_sample_pos = 0;
  108. for (int i = 0; i < blocks; i++) {
  109. granulator->processInternalBlock(chan1, chan2, kInternalBlocksize);
  110. chan1 += kInternalBlocksize;
  111. chan2 += kInternalBlocksize;
  112. parameters->time_quantizer->incrementPositionInfo();
  113. }
  114. int samples_remaining = sampleframes % kInternalBlocksize;
  115. if (samples_remaining) {
  116. granulator->processInternalBlock(chan1, chan2, samples_remaining);
  117. }
  118. }
  119. int Plugin::getNumParameters()
  120. {
  121. return static_cast<int>(NUM_PARAMS);
  122. }
  123. float Plugin::getParameter (int index)
  124. {
  125. if (index == kProgram) {
  126. return static_cast<float>(current_program / (kNumPrograms - 1));
  127. } else {
  128. return parameters->param[index];
  129. }
  130. }
  131. const String Plugin::getParameterName (int index)
  132. {
  133. return parameters->getParameterName(index);
  134. }
  135. const String Plugin::getParameterText (int index)
  136. {
  137. if (index == kProgram) {
  138. return String(current_program + 1);
  139. } else return parameters->getParameterText(index);
  140. }
  141. std::vector<String> Plugin::getScaleNames()
  142. {
  143. return granulator->grain_param_generator->pitch_quantizer->getNames();
  144. }
  145. bool Plugin::isMetaParameter(int parameterIndex) const
  146. {
  147. if (parameterIndex == kProgram) {
  148. return true;
  149. }
  150. else
  151. return false;
  152. }
  153. bool Plugin::silenceInProducesSilenceOut(void) const
  154. {
  155. return false;
  156. }
  157. void Plugin::setParameter(int index, float new_value)
  158. {
  159. if (index != kNone) {
  160. if (index == kProgram) {
  161. if (progchange_param_enabled) {
  162. int selected_program = static_cast<int>(new_value * (kNumPrograms - 1));
  163. if (current_program != selected_program) {
  164. setCurrentProgram(selected_program);
  165. }
  166. }
  167. } else {
  168. parameters->setParameter(index, new_value, false);
  169. }
  170. setParametersChangedState();
  171. }
  172. }
  173. void Plugin::toggleProgchangeEnabled()
  174. {
  175. progchange_param_enabled = !progchange_param_enabled;
  176. }
  177. bool Plugin:: isProgchangeEnabled()
  178. {
  179. return progchange_param_enabled;
  180. }
  181. void Plugin::setMatrixSource(int index, int param)
  182. {
  183. parameters->setMatrixSource(index, param);
  184. }
  185. void Plugin::setMatrixDest(int index, int param)
  186. {
  187. parameters->setMatrixDest(index, param);
  188. }
  189. void Plugin::setMatrixMode(int index, int param)
  190. {
  191. parameters->setMatrixMode(index, param);
  192. }
  193. const String Plugin::getProgramName(int index)
  194. {
  195. return program_bank->getProgramName(index);
  196. }
  197. void Plugin::changeProgramName(int index, const String& newName)
  198. {
  199. program_bank->setProgramName(index, newName);
  200. editor_program_update_pending = true;
  201. }
  202. int Plugin::getCurrentProgram()
  203. {
  204. return current_program;
  205. }
  206. bool Plugin::getSavedState()
  207. {
  208. return saved_state;
  209. }
  210. void Plugin::setSavedState(bool state)
  211. {
  212. if (saved_state == true && state == false) {
  213. editor_program_update_pending = true;
  214. }
  215. saved_state = state;
  216. }
  217. bool Plugin::getParametersChangedState()
  218. {
  219. if (editor_parameter_update_pending) {
  220. editor_parameter_update_pending = false;
  221. return true;
  222. }
  223. else
  224. return false;
  225. }
  226. void Plugin::setParametersChangedState()
  227. {
  228. editor_parameter_update_pending = true;
  229. }
  230. bool Plugin::getProgramChangedState()
  231. {
  232. if (editor_program_update_pending) {
  233. editor_program_update_pending = false;
  234. return true;
  235. }
  236. else
  237. return false;
  238. }
  239. int Plugin::getNumPrograms()
  240. {
  241. return kNumPrograms;
  242. }
  243. void Plugin::initCurrentProgram()
  244. {
  245. program_bank->initProgram(current_program);
  246. program_bank->loadProgramState(current_program);
  247. setSavedState(true);
  248. editor_program_update_pending = true;
  249. }
  250. void Plugin::saveProgramTo(int index)
  251. {
  252. String current_program_name = program_bank->getProgramName(current_program);
  253. program_bank->saveProgramState(index);
  254. program_bank->setProgramName(index, current_program_name);
  255. current_program = index;
  256. setSavedState(true);
  257. editor_program_update_pending = true;
  258. }
  259. void Plugin::setCurrentProgram(int index)
  260. {
  261. program_bank->loadProgramState(index);
  262. current_program = index;
  263. setSavedState(true);
  264. editor_program_update_pending = true;
  265. }
  266. void Plugin::getCurrentProgramStateInformation(MemoryBlock& destData)
  267. {
  268. //save current settings
  269. program_bank->saveProgramState(current_program);
  270. saveProgramTo(current_program);
  271. //output program to host
  272. XmlElement* program = program_bank->createProgramXml(current_program);
  273. copyXmlToBinary (*program, destData);
  274. delete program;
  275. }
  276. void Plugin::setCurrentProgramStateInformation(const void* data, int sizeInBytes)
  277. {
  278. //load program from host
  279. XmlElement* const xml_state = getXmlFromBinary(data, sizeInBytes);
  280. if (xml_state != 0) {
  281. program_bank->loadProgramFromXml(current_program, xml_state);
  282. setCurrentProgram(current_program);
  283. delete xml_state;
  284. editor_program_update_pending = true;
  285. }
  286. }
  287. void Plugin::getStateInformation (MemoryBlock& destData)
  288. {
  289. // save current program
  290. program_bank->saveProgramState(current_program);
  291. // output program_bank to host
  292. XmlElement* bankXml = program_bank->createBankXml();
  293. copyXmlToBinary(*bankXml, destData);
  294. editor_program_update_pending = true;
  295. }
  296. void Plugin::setStateInformation (const void* data, int sizeInBytes)
  297. {
  298. //load bank from host
  299. XmlElement* const xml_state = getXmlFromBinary(data, sizeInBytes);
  300. if (xml_state != 0) {
  301. program_bank->loadBankFromXml(xml_state);
  302. setCurrentProgram(current_program);
  303. delete xml_state;
  304. editor_program_update_pending = true;
  305. }
  306. }
  307. void Plugin::loadBankXml(File* file)
  308. {
  309. //load bank from file
  310. XmlDocument xml_document(*file);
  311. XmlElement* xml_state = xml_document.getDocumentElement();
  312. if (xml_state != 0) {
  313. program_bank->loadBankFromXml(xml_state);
  314. setCurrentProgram(current_program);
  315. delete xml_state;
  316. editor_program_update_pending = true;
  317. }
  318. }
  319. void Plugin::saveBankXml(File* file)
  320. {
  321. // save current program
  322. program_bank->saveProgramState(current_program);
  323. // output bank to file
  324. XmlElement* bankXml = program_bank->createBankXml();
  325. file->replaceWithText(bankXml->createDocument(String()));
  326. delete bankXml;
  327. editor_program_update_pending = true;
  328. setSavedState(true);
  329. }
  330. void Plugin::loadCurrentProgramXml(File* file)
  331. {
  332. //load program from file
  333. XmlDocument xml_document(*file);
  334. XmlElement* xml_state = xml_document.getDocumentElement();
  335. if (xml_state != 0) {
  336. program_bank->loadProgramFromXml(current_program, xml_state);
  337. setCurrentProgram(current_program);
  338. delete xml_state;
  339. editor_program_update_pending = true;
  340. }
  341. }
  342. void Plugin::saveCurrentProgramXml(File* file)
  343. {
  344. //save current settings
  345. program_bank->saveProgramState(current_program);
  346. saveProgramTo(current_program);
  347. //output program to file
  348. XmlElement* program = program_bank->createProgramXml(current_program);
  349. file->replaceWithText(program->createDocument(String()));
  350. delete program;
  351. }