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.

1166 lines
29KB

  1. /* LADSPAPlugin.C
  2. * Copyleft (C) 2001 David Griffiths <dave@pawfal.org>
  3. * LADSPA Plugin by Nicolas Noble <nicolas@nobis-crew.org>
  4. * Modified by Mike Rawes <myk@waxfrenzy.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <config.h>
  21. #include <cstdio>
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <cmath>
  25. #include "SpiralIcon.xpm"
  26. #include "LADSPAPlugin.h"
  27. #include "LADSPAPluginGUI.h"
  28. #include "LADSPAInfo.h"
  29. using namespace std;
  30. LADSPAInfo * LADSPAPlugin::m_LADSPAInfo= NULL;
  31. int LADSPAPlugin::InstanceCount=0;
  32. ////////////////////////////////////////////////////
  33. extern "C" {
  34. SpiralPlugin* SpiralPlugin_CreateInstance()
  35. {
  36. return new LADSPAPlugin;
  37. }
  38. char** SpiralPlugin_GetIcon()
  39. {
  40. return SpiralIcon_xpm;
  41. }
  42. int SpiralPlugin_GetID()
  43. {
  44. return 0x0016;
  45. }
  46. string SpiralPlugin_GetGroupName()
  47. {
  48. return "Filters/FX";
  49. }
  50. }
  51. ///////////////////////////////////////////////////////
  52. LADSPAPlugin::LADSPAPlugin()
  53. {
  54. InstanceCount++;
  55. if (!m_LADSPAInfo)
  56. {
  57. m_LADSPAInfo = new LADSPAInfo(false, "");
  58. }
  59. m_PlugDesc = NULL;
  60. ClearPlugin();
  61. m_Version=9;
  62. m_PluginInfo.Name="LADSPA";
  63. m_PluginInfo.Width=500;
  64. m_PluginInfo.Height=320;
  65. m_PluginInfo.NumInputs=0;
  66. m_PluginInfo.NumOutputs=1;
  67. m_PluginInfo.PortTips.push_back("Nuffink yet");
  68. m_MaxInputPortCount = m_LADSPAInfo->GetMaxInputPortCount();
  69. // For receiving from GUI
  70. m_AudioCH->RegisterData("SetUniqueID", ChannelHandler::INPUT,&(m_InData.UniqueID), sizeof(m_InData.UniqueID));
  71. m_AudioCH->RegisterData("SetPage", ChannelHandler::INPUT,&(m_InData.Page), sizeof(m_InData.Page));
  72. m_AudioCH->RegisterData("SetUpdateInputs", ChannelHandler::INPUT,&(m_InData.UpdateInputs),sizeof(m_InData.UpdateInputs));
  73. m_AudioCH->RegisterData("SetInputPortIndex", ChannelHandler::INPUT, &(m_InData.InputPortIndex), sizeof(m_InData.InputPortIndex));
  74. m_AudioCH->RegisterData("SetInputPortDefault", ChannelHandler::INPUT, &(m_InData.InputPortDefault), sizeof(m_InData.InputPortDefault));
  75. m_AudioCH->RegisterData("SetInputPortMin", ChannelHandler::INPUT, &(m_InData.InputPortMin), sizeof(m_InData.InputPortMin));
  76. m_AudioCH->RegisterData("SetInputPortMax", ChannelHandler::INPUT, &(m_InData.InputPortMax), sizeof(m_InData.InputPortMax));
  77. m_AudioCH->RegisterData("SetInputPortClamp", ChannelHandler::INPUT, &(m_InData.InputPortClamp), sizeof(m_InData.InputPortClamp));
  78. // For sending to GUI
  79. m_AudioCH->RegisterData("GetName",ChannelHandler::OUTPUT,m_Name,256);
  80. m_AudioCH->RegisterData("GetMaker",ChannelHandler::OUTPUT,m_Maker,256);
  81. m_AudioCH->RegisterData("GetMaxInputPortCount",ChannelHandler::OUTPUT,&(m_MaxInputPortCount),sizeof(m_MaxInputPortCount));
  82. m_AudioCH->RegisterData("GetInputPortCount",ChannelHandler::OUTPUT,&(m_InputPortCount),sizeof(m_InputPortCount));
  83. m_OutData.InputPortNames = (char *)malloc(256 * m_MaxInputPortCount);
  84. m_OutData.InputPortSettings = (PortSetting *)malloc(sizeof(PortSetting) * m_MaxInputPortCount);
  85. m_OutData.InputPortValues = (PortValue *)calloc(m_MaxInputPortCount, sizeof(PortValue));
  86. m_OutData.InputPortDefaults = (float *)calloc(m_MaxInputPortCount, sizeof(float));
  87. if (m_OutData.InputPortNames &&
  88. m_OutData.InputPortSettings &&
  89. m_OutData.InputPortValues &&
  90. m_OutData.InputPortDefaults)
  91. {
  92. m_AudioCH->RegisterData("GetInputPortNames", ChannelHandler::OUTPUT, m_OutData.InputPortNames, 256 * m_MaxInputPortCount);
  93. m_AudioCH->RegisterData("GetInputPortSettings", ChannelHandler::OUTPUT, m_OutData.InputPortSettings, sizeof(PortSetting) * m_MaxInputPortCount);
  94. m_AudioCH->RegisterData("GetInputPortValues", ChannelHandler::OUTPUT, m_OutData.InputPortValues, sizeof(PortValue) * m_MaxInputPortCount);
  95. m_AudioCH->RegisterData("GetInputPortDefaults", ChannelHandler::OUTPUT, m_OutData.InputPortDefaults, sizeof(float) * m_MaxInputPortCount);
  96. } else {
  97. cerr<<"LADSPA Plugin: Memory allocation error"<<endl;
  98. }
  99. }
  100. LADSPAPlugin::~LADSPAPlugin()
  101. {
  102. // Clear plugin
  103. ClearPlugin();
  104. // Free allocated buffers
  105. if (m_OutData.InputPortNames) free(m_OutData.InputPortNames);
  106. if (m_OutData.InputPortSettings) free(m_OutData.InputPortSettings);
  107. if (m_OutData.InputPortValues) free(m_OutData.InputPortValues);
  108. if (m_OutData.InputPortDefaults) free(m_OutData.InputPortDefaults);
  109. InstanceCount--;
  110. if (m_LADSPAInfo && InstanceCount<=0)
  111. {
  112. delete m_LADSPAInfo;
  113. m_LADSPAInfo = NULL;
  114. }
  115. }
  116. PluginInfo &LADSPAPlugin::Initialise(const HostInfo *Host)
  117. {
  118. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  119. LADSPA_Data *NewPort = new LADSPA_Data[m_HostInfo->BUFSIZE];
  120. m_LADSPABufVec.push_back(NewPort);
  121. return Info;
  122. }
  123. SpiralGUIType *LADSPAPlugin::CreateGUI()
  124. {
  125. return new LADSPAPluginGUI(m_PluginInfo.Width, m_PluginInfo.Height,
  126. this, m_AudioCH, m_HostInfo, m_LADSPAInfo->GetMenuList());
  127. }
  128. void LADSPAPlugin::Execute()
  129. {
  130. if (m_PlugDesc)
  131. {
  132. // convert inputs if exist (use default if not)
  133. for (int n=0; n<m_PluginInfo.NumInputs; n++)
  134. {
  135. if (GetInput(n))
  136. {
  137. if (m_InputPortClamp[n]) {
  138. // scale input to match hinted range
  139. float Offset=m_InputPortMin[n];
  140. float Scale=m_InputPortMax[n]-m_InputPortMin[n];
  141. //cerr<<n<<" ["<<Scale<<"] ["<<Offset<<"]"<<endl;
  142. for (int i=0; i<m_HostInfo->BUFSIZE; i++)
  143. {
  144. m_LADSPABufVec[n][i]=Offset+(GetInput(n,i)*0.5f+0.5f)*Scale;
  145. //cerr<<Scale<<" "<<Offset<<" "<<m_LADSPABufVec[n][i]<<endl;
  146. }
  147. } else {
  148. // pass input as is
  149. for (int i=0; i<m_HostInfo->BUFSIZE; i++)
  150. {
  151. m_LADSPABufVec[n][i]=GetInput(n,i);
  152. }
  153. }
  154. m_OutData.InputPortValues[n].Connected = true;
  155. m_InputPortDefault[n] = m_LADSPABufVec[n][0];
  156. }
  157. else // Use default
  158. {
  159. for (int i=0; i<m_HostInfo->BUFSIZE; i++) {
  160. m_LADSPABufVec[n][i]=m_InputPortDefault[n];
  161. }
  162. if (m_OutData.InputPortValues[n].Connected) {
  163. m_OutData.InputPortValues[n].Connected = false;
  164. m_InputPortDefault[n] = m_OutData.InputPortValues[n].Value;
  165. }
  166. }
  167. // Copy values into OutData value buffer for display in GUI
  168. m_OutData.InputPortValues[n].Value = m_LADSPABufVec[n][0];
  169. // Ditto for default, which may have been set to value
  170. m_OutData.InputPortDefaults[n] = m_InputPortDefault[n];
  171. }
  172. // run plugin
  173. m_PlugDesc->run(m_PlugInstHandle,m_HostInfo->BUFSIZE);
  174. // convert outputs
  175. for (int n=0; n<m_PluginInfo.NumOutputs; n++)
  176. {
  177. for (int i=0; i<m_HostInfo->BUFSIZE; i++)
  178. {
  179. SetOutput(n,i,m_LADSPABufVec[n+m_PluginInfo.NumInputs][i]);
  180. }
  181. }
  182. }
  183. }
  184. void LADSPAPlugin::ExecuteCommands()
  185. {
  186. if (m_AudioCH->IsCommandWaiting())
  187. {
  188. switch(m_AudioCH->GetCommand())
  189. {
  190. case (SETPAGE):
  191. {
  192. m_Page = m_InData.Page;
  193. }
  194. break;
  195. case (SELECTPLUGIN):
  196. {
  197. UpdatePlugin(m_InData.UniqueID);
  198. }
  199. break;
  200. case (CLEARPLUGIN):
  201. {
  202. ClearPlugin();
  203. m_PluginInfo.NumOutputs=1;
  204. m_PluginInfo.PortTips.push_back("Nuffink yet");
  205. UpdatePluginInfoWithHost();
  206. }
  207. break;
  208. case (SETUPDATEINPUTS):
  209. {
  210. m_UpdateInputs = m_InData.UpdateInputs;
  211. }
  212. break;
  213. case (SETDEFAULT):
  214. {
  215. m_InputPortDefault[m_InData.InputPortIndex] = m_InData.InputPortDefault;
  216. m_OutData.InputPortDefaults[m_InData.InputPortIndex] = m_InData.InputPortDefault;
  217. }
  218. break;
  219. case (SETMIN):
  220. {
  221. m_InputPortMin[m_InData.InputPortIndex] = m_InData.InputPortMin;
  222. m_OutData.InputPortSettings[m_InData.InputPortIndex].Min = m_InData.InputPortMin;
  223. }
  224. break;
  225. case (SETMAX):
  226. {
  227. m_InputPortMax[m_InData.InputPortIndex] = m_InData.InputPortMax;
  228. m_OutData.InputPortSettings[m_InData.InputPortIndex].Max = m_InData.InputPortMax;
  229. }
  230. break;
  231. case (SETCLAMP):
  232. {
  233. m_InputPortClamp[m_InData.InputPortIndex] = m_InData.InputPortClamp;
  234. m_OutData.InputPortSettings[m_InData.InputPortIndex].Clamp = m_InData.InputPortClamp;
  235. }
  236. }
  237. }
  238. // If there are no connections, Execute() will not be called.
  239. // If the last connection is removed, it will not be reflected in
  240. // the GUI data (m_OutData.InputPortValues.Connected)
  241. bool has_connection = false;
  242. for (int p = 0; p < m_PluginInfo.NumInputs && !has_connection; p++)
  243. {
  244. if (GetInput(p)) has_connection = true;
  245. }
  246. if (!has_connection)
  247. {
  248. // Only change stuff if there are no connections
  249. // (i.e. if Execute has not already taken care of this)
  250. for (int p = 0; p < m_PluginInfo.NumInputs; p++)
  251. {
  252. m_OutData.InputPortValues[p].Connected = false;
  253. }
  254. }
  255. }
  256. void LADSPAPlugin::StreamOut(ostream &s)
  257. {
  258. s<<m_Version<<" ";
  259. switch (m_Version)
  260. {
  261. case 9:
  262. {
  263. // Get number of unconnected inputs
  264. m_UnconnectedInputs = m_PluginInfo.NumInputs;
  265. for (int p = 0; p < m_PluginInfo.NumInputs; p++) {
  266. if (m_OutData.InputPortValues[p].Connected) m_UnconnectedInputs--;
  267. }
  268. s<<m_Page<<" ";
  269. s<<m_UpdateInputs<<" ";
  270. s<<m_UniqueID<<" ";
  271. s<<m_InputPortMin.size()<<" ";
  272. s<<m_UnconnectedInputs<<" ";
  273. assert(m_InputPortMin.size()==m_InputPortMax.size());
  274. assert(m_InputPortMin.size()==m_InputPortClamp.size());
  275. assert(m_InputPortMin.size()==m_InputPortDefault.size());
  276. for (vector<float>::iterator i=m_InputPortMin.begin();
  277. i!=m_InputPortMin.end(); i++)
  278. {
  279. float f = finite(*i)?(*i):0.0f;
  280. s<< f <<" ";
  281. }
  282. for (vector<float>::iterator i=m_InputPortMax.begin();
  283. i!=m_InputPortMax.end(); i++)
  284. {
  285. float f = finite(*i)?(*i):0.0f;
  286. s<< f <<" ";
  287. }
  288. for (vector<bool>::iterator i=m_InputPortClamp.begin();
  289. i!=m_InputPortClamp.end(); i++)
  290. {
  291. float f = finite(*i)?(*i):0.0f;
  292. s<< f <<" ";
  293. }
  294. for (vector<float>::iterator i=m_InputPortDefault.begin();
  295. i!=m_InputPortDefault.end(); i++)
  296. {
  297. float f = finite(*i)?(*i):0.0f;
  298. s<< f <<" ";
  299. }
  300. }
  301. break;
  302. case 8:
  303. {
  304. // s<<m_Page<<" ";
  305. // s<<m_UpdateInputs<<" ";
  306. // s<<m_UniqueID<<" ";
  307. // s<<m_InputPortMin.size()<<" ";
  308. // assert(m_InputPortMin.size()==m_InputPortMax.size());
  309. // assert(m_InputPortMin.size()==m_InputPortClamp.size());
  310. // assert(m_InputPortMin.size()==m_InputPortDefault.size());
  311. // for (vector<float>::iterator i=m_InputPortMin.begin();
  312. // i!=m_InputPortMin.end(); i++)
  313. // {
  314. // float f = finite(*i)?(*i):0.0f;
  315. // s<< f <<" ";
  316. // }
  317. // for (vector<float>::iterator i=m_InputPortMax.begin();
  318. // i!=m_InputPortMax.end(); i++)
  319. // {
  320. // float f = finite(*i)?(*i):0.0f;
  321. // s<< f <<" ";
  322. // }
  323. // for (vector<bool>::iterator i=m_InputPortClamp.begin();
  324. // i!=m_InputPortClamp.end(); i++)
  325. // {
  326. // float f = finite(*i)?(*i):0.0f;
  327. // s<< f <<" ";
  328. // }
  329. // for (vector<float>::iterator i=m_InputPortDefault.begin();
  330. // i!=m_InputPortDefault.end(); i++)
  331. // {
  332. // float f = finite(*i)?(*i):0.0f;
  333. // s<< f <<" ";
  334. // }
  335. }
  336. break;
  337. case 7:
  338. {
  339. // s<<m_Page<<" ";
  340. // s<<m_UniqueID<<" ";
  341. // s<<m_InputPortMin.size()<<" ";
  342. // assert(m_InputPortMin.size()==m_InputPortMax.size());
  343. // assert(m_InputPortMin.size()==m_InputPortClamp.size());
  344. // assert(m_InputPortMin.size()==m_InputPortDefault.size());
  345. // for (vector<float>::iterator i=m_InputPortMin.begin();
  346. // i!=m_InputPortMin.end(); i++)
  347. // {
  348. // s<<*i<<" ";
  349. // }
  350. // for (vector<float>::iterator i=m_InputPortMax.begin();
  351. // i!=m_InputPortMax.end(); i++)
  352. // {
  353. // s<<*i<<" ";
  354. // }
  355. // for (vector<bool>::iterator i=m_InputPortClamp.begin();
  356. // i!=m_InputPortClamp.end(); i++)
  357. // {
  358. // s<<*i<<" ";
  359. // }
  360. // for (vector<float>::iterator i=m_InputPortDefault.begin();
  361. // i!=m_InputPortDefault.end(); i++)
  362. // {
  363. // s<<*i<<" ";
  364. // }
  365. }
  366. break;
  367. case 6:
  368. {
  369. // s<<m_UniqueID<<" ";
  370. // s<<m_InputPortMin.size()<<" ";
  371. // assert(m_InputPortMin.size()==m_InputPortMax.size());
  372. // assert(m_InputPortMin.size()==m_InputPortClamp.size());
  373. // assert(m_InputPortMin.size()==m_InputPortDefault.size());
  374. // for (vector<float>::iterator i=m_InputPortMin.begin();
  375. // i!=m_InputPortMin.end(); i++)
  376. // {
  377. // s<<*i<<" ";
  378. // }
  379. // for (vector<float>::iterator i=m_InputPortMax.begin();
  380. // i!=m_InputPortMax.end(); i++)
  381. // {
  382. // s<<*i<<" ";
  383. // }
  384. // for (vector<bool>::iterator i=m_InputPortClamp.begin();
  385. // i!=m_InputPortClamp.end(); i++)
  386. // {
  387. // s<<*i<<" ";
  388. // }
  389. // for (vector<float>::iterator i=m_InputPortDefault.begin();
  390. // i!=m_InputPortDefault.end(); i++)
  391. // {
  392. // s<<*i<<" ";
  393. // }
  394. }
  395. break;
  396. case 5:
  397. {
  398. // s<<m_Gain<<" ";
  399. // s<<m_UniqueID<<" ";
  400. // s<<m_InputPortMin.size()<<" ";
  401. // assert(m_InputPortMin.size()==m_InputPortMax.size());
  402. // assert(m_InputPortMin.size()==m_InputPortClamp.size());
  403. // assert(m_InputPortMin.size()==m_InputPortDefault.size());
  404. // for (vector<float>::iterator i=m_InputPortMin.begin();
  405. // i!=m_InputPortMin.end(); i++)
  406. // {
  407. // s<<*i<<" ";
  408. // }
  409. // for (vector<float>::iterator i=m_InputPortMax.begin();
  410. // i!=m_InputPortMax.end(); i++)
  411. // {
  412. // s<<*i<<" ";
  413. // }
  414. // for (vector<bool>::iterator i=m_InputPortClamp.begin();
  415. // i!=m_InputPortClamp.end(); i++)
  416. // {
  417. // s<<*i<<" ";
  418. // }
  419. // for (vector<float>::iterator i=m_InputPortDefault.begin();
  420. // i!=m_InputPortDefault.end(); i++)
  421. // {
  422. // s<<*i<<" ";
  423. // }
  424. }
  425. break;
  426. case 4:
  427. {
  428. // s<<m_Gain<<" ";
  429. // s<<m_UniqueID<<" ";
  430. // s<<m_InputPortMin.size()<<" ";
  431. // assert(m_InputPortMin.size()==m_InputPortMax.size());
  432. // assert(m_InputPortMin.size()==m_InputPortClamp.size());
  433. // assert(m_InputPortMin.size()==m_InputPortDefault.size());
  434. // for (vector<float>::iterator i=m_InputPortMin.begin();
  435. // i!=m_InputPortMin.end(); i++)
  436. // {
  437. // s<<*i<<" ";
  438. // }
  439. // for (vector<float>::iterator i=m_InputPortMax.begin();
  440. // i!=m_InputPortMax.end(); i++)
  441. // {
  442. // s<<*i<<" ";
  443. // }
  444. // for (vector<bool>::iterator i=m_InputPortClamp.begin();
  445. // i!=m_InputPortClamp.end(); i++)
  446. // {
  447. // s<<*i<<" ";
  448. // }
  449. }
  450. break;
  451. case 3:
  452. {
  453. // s<<m_Gain<<" ";
  454. // s<<m_Filename<<" ";
  455. // s<<m_Label<<" ";
  456. // s<<m_InputPortMin.size()<<" ";
  457. // assert(m_InputPortMin.size()==m_InputPortMax.size());
  458. // assert(m_InputPortMin.size()==m_InputPortClamp.size());
  459. // for (vector<float>::iterator i=m_InputPortMin.begin();
  460. // i!=m_InputPortMin.end(); i++)
  461. // {
  462. // s<<*i<<" ";
  463. // }
  464. // for (vector<float>::iterator i=m_InputPortMax.begin();
  465. // i!=m_InputPortMax.end(); i++)
  466. // {
  467. // s<<*i<<" ";
  468. // }
  469. // for (vector<bool>::iterator i=m_InputPortClamp.begin();
  470. // i!=m_InputPortClamp.end(); i++)
  471. // {
  472. // s<<*i<<" ";
  473. // }
  474. }
  475. break;
  476. case 2:
  477. {
  478. // s<<m_Gain<<" ";
  479. // s<<m_Filename<<" ";
  480. // s<<m_Label<<" ";
  481. // s<<m_InputPortMin.size()<<" ";
  482. // assert(m_InputPortMin.size()==m_InputPortMax.size());
  483. // for (vector<float>::iterator i=m_InputPortMin.begin();
  484. // i!=m_InputPortMin.end(); i++)
  485. // {
  486. // s<<*i<<" ";
  487. // }
  488. // for (vector<float>::iterator i=m_InputPortMax.begin();
  489. // i!=m_InputPortMax.end(); i++)
  490. // {
  491. // s<<*i<<" ";
  492. // }
  493. }
  494. break;
  495. case 1:
  496. {
  497. // s<<m_Gain<<" ";
  498. // s<<m_Filename<<" ";
  499. // s<<m_Label<<" ";
  500. }
  501. break;
  502. }
  503. }
  504. void LADSPAPlugin::StreamIn(istream &s)
  505. {
  506. int Version;
  507. float Gain;
  508. unsigned long UniqueID;
  509. int PortCount;
  510. float Min, Max;
  511. bool Clamp;
  512. float Default;
  513. ClearPlugin();
  514. s >> Version;
  515. switch (Version)
  516. {
  517. case 9:
  518. {
  519. s >> m_Page;
  520. s >> m_UpdateInputs;
  521. s >> UniqueID;
  522. s >> PortCount;
  523. s >> m_UnconnectedInputs;
  524. for (int n=0; n<PortCount; n++)
  525. {
  526. s >> Min;
  527. m_InputPortMin.push_back(Min);
  528. }
  529. for (int n=0; n<PortCount; n++)
  530. {
  531. s >> Max;
  532. m_InputPortMax.push_back(Max);
  533. }
  534. for (int n=0; n<PortCount; n++)
  535. {
  536. s >> Clamp;
  537. m_InputPortClamp.push_back(Clamp);
  538. }
  539. for (int n=0; n<PortCount; n++)
  540. {
  541. s >> Default;
  542. m_InputPortDefault.push_back(Default);
  543. }
  544. }
  545. break;
  546. case 8:
  547. {
  548. s >> m_Page;
  549. s >> m_UpdateInputs;
  550. s >> UniqueID;
  551. s >> PortCount;
  552. for (int n=0; n<PortCount; n++)
  553. {
  554. s >> Min;
  555. m_InputPortMin.push_back(Min);
  556. }
  557. for (int n=0; n<PortCount; n++)
  558. {
  559. s >> Max;
  560. m_InputPortMax.push_back(Max);
  561. }
  562. for (int n=0; n<PortCount; n++)
  563. {
  564. s >> Clamp;
  565. m_InputPortClamp.push_back(Clamp);
  566. }
  567. for (int n=0; n<PortCount; n++)
  568. {
  569. s >> Default;
  570. m_InputPortDefault.push_back(Default);
  571. }
  572. }
  573. break;
  574. case 7:
  575. {
  576. s >> m_Page;
  577. s >> UniqueID;
  578. s >> PortCount;
  579. for (int n=0; n<PortCount; n++)
  580. {
  581. s >> Min;
  582. m_InputPortMin.push_back(Min);
  583. }
  584. for (int n=0; n<PortCount; n++)
  585. {
  586. s >> Max;
  587. m_InputPortMax.push_back(Max);
  588. }
  589. for (int n=0; n<PortCount; n++)
  590. {
  591. s >> Clamp;
  592. m_InputPortClamp.push_back(Clamp);
  593. }
  594. for (int n=0; n<PortCount; n++)
  595. {
  596. s >> Default;
  597. m_InputPortDefault.push_back(Default);
  598. }
  599. }
  600. break;
  601. case 6:
  602. {
  603. s >> UniqueID;
  604. s >> PortCount;
  605. for (int n=0; n<PortCount; n++)
  606. {
  607. s >> Min;
  608. m_InputPortMin.push_back(Min);
  609. }
  610. for (int n=0; n<PortCount; n++)
  611. {
  612. s >> Max;
  613. m_InputPortMax.push_back(Max);
  614. }
  615. for (int n=0; n<PortCount; n++)
  616. {
  617. s >> Clamp;
  618. m_InputPortClamp.push_back(Clamp);
  619. }
  620. for (int n=0; n<PortCount; n++)
  621. {
  622. s >> Default;
  623. m_InputPortDefault.push_back(Default);
  624. }
  625. }
  626. break;
  627. case 5:
  628. {
  629. s >> Gain;
  630. s >> UniqueID;
  631. s >> PortCount;
  632. for (int n=0; n<PortCount; n++)
  633. {
  634. s >> Min;
  635. m_InputPortMin.push_back(Min);
  636. }
  637. for (int n=0; n<PortCount; n++)
  638. {
  639. s >> Max;
  640. m_InputPortMax.push_back(Max);
  641. }
  642. for (int n=0; n<PortCount; n++)
  643. {
  644. s >> Clamp;
  645. m_InputPortClamp.push_back(Clamp);
  646. }
  647. for (int n=0; n<PortCount; n++)
  648. {
  649. s >> Default;
  650. m_InputPortDefault.push_back(Default);
  651. }
  652. }
  653. break;
  654. case 4:
  655. {
  656. s >> Gain;
  657. s >> UniqueID;
  658. s >> PortCount;
  659. for (int n=0; n<PortCount; n++)
  660. {
  661. s >> Min;
  662. m_InputPortMin.push_back(Min);
  663. }
  664. for (int n=0; n<PortCount; n++)
  665. {
  666. s >> Max;
  667. m_InputPortMax.push_back(Max);
  668. }
  669. for (int n=0; n<PortCount; n++)
  670. {
  671. s >> Clamp;
  672. m_InputPortClamp.push_back(Clamp);
  673. }
  674. for (int n=0; n<PortCount; n++)
  675. {
  676. // Set defaults to zero
  677. m_InputPortDefault.push_back(0.0f);
  678. }
  679. }
  680. break;
  681. case 3:
  682. {
  683. string Filename,Label;
  684. s >> Gain;
  685. s >> Filename;
  686. s >> Label;
  687. s >> PortCount;
  688. for (int n=0; n<PortCount; n++)
  689. {
  690. s >> Min;
  691. m_InputPortMin.push_back(Min);
  692. }
  693. for (int n=0; n<PortCount; n++)
  694. {
  695. s >> Max;
  696. m_InputPortMax.push_back(Max);
  697. }
  698. for (int n=0; n<PortCount; n++)
  699. {
  700. s >> Clamp;
  701. m_InputPortClamp.push_back(Clamp);
  702. }
  703. for (int n=0; n<PortCount; n++)
  704. {
  705. // Set defaults to zero
  706. m_InputPortDefault.push_back(0.0f);
  707. }
  708. if (Filename!="None")
  709. {
  710. // Get Unique ID from filename and label
  711. UniqueID = m_LADSPAInfo->GetIDFromFilenameAndLabel(Filename, Label);
  712. }
  713. }
  714. break;
  715. case 2:
  716. {
  717. string Filename, Label;
  718. s >> Gain;
  719. s >> Filename;
  720. s >> Label;
  721. s >> PortCount;
  722. for (int n=0; n<PortCount; n++)
  723. {
  724. s >> Min;
  725. m_InputPortMin.push_back(Min);
  726. }
  727. for (int n=0; n<PortCount; n++)
  728. {
  729. s >> Max;
  730. m_InputPortMax.push_back(Max);
  731. }
  732. for (int n=0; n<PortCount; n++)
  733. {
  734. // Set InputPortClamp to true as default
  735. m_InputPortClamp.push_back(true);
  736. }
  737. for (int n=0; n<PortCount; n++)
  738. {
  739. // Set defaults to zero
  740. m_InputPortDefault.push_back(0.0f);
  741. }
  742. if (Filename!="None")
  743. {
  744. // Get Unique ID from filename and label
  745. UniqueID = m_LADSPAInfo->GetIDFromFilenameAndLabel(Filename, Label);
  746. }
  747. }
  748. break;
  749. case 1:
  750. {
  751. string Filename, Label;
  752. s >> Gain;
  753. s >> Filename;
  754. s >> Label;
  755. if (Filename!="None")
  756. {
  757. // Get Unique ID from filename and label
  758. UniqueID = m_LADSPAInfo->GetIDFromFilenameAndLabel(Filename, Label);
  759. }
  760. }
  761. break;
  762. }
  763. if (Version == 1) {
  764. // Need to reset ports - none will have been saved
  765. UpdatePlugin(UniqueID);
  766. } else {
  767. // Versions prior to 9 have 'Setup' page as page 1
  768. // This is now page 2, as the sliders are page 1
  769. if (Version < 9 && m_Page == 1) m_Page = 2;
  770. if (SelectPlugin(UniqueID)) {
  771. // Versions prior to 9 will not have the unconnected inupts saved
  772. // Default to the number of input ports.
  773. if (Version < 9) m_UnconnectedInputs = m_InputPortCount;
  774. SetGUIExports();
  775. } else {
  776. ClearPlugin();
  777. }
  778. }
  779. }
  780. bool LADSPAPlugin::UpdatePlugin(unsigned long UniqueID)
  781. {
  782. ClearPlugin();
  783. if (SelectPlugin(UniqueID)) {
  784. ResetPortSettings();
  785. SetGUIExports();
  786. return true;
  787. }
  788. // Oops. Clean up.
  789. ClearPlugin();
  790. cerr << "Error loading LADSPA Plugin.\n";
  791. return false;
  792. }
  793. bool LADSPAPlugin::SelectPlugin(unsigned long UniqueID)
  794. {
  795. // Reject trivial case
  796. if (UniqueID == 0) return false;
  797. m_PlugDesc = m_LADSPAInfo->GetDescriptorByID(UniqueID);
  798. if (m_PlugDesc) {
  799. // Create instance
  800. if (!(m_PlugInstHandle = m_PlugDesc->instantiate(m_PlugDesc, m_HostInfo->SAMPLERATE))) {
  801. cerr << "WARNING: Could not instantiate plugin " << UniqueID << endl;
  802. m_LADSPAInfo->DiscardDescriptorByID(UniqueID);
  803. m_PlugDesc = 0;
  804. return false;
  805. }
  806. // Find number of input and output ports
  807. for (unsigned long i = 0; i < m_PlugDesc->PortCount; i++) {
  808. if (LADSPA_IS_PORT_INPUT(m_PlugDesc->PortDescriptors[i])) {
  809. m_PluginInfo.NumInputs++;
  810. } else if (LADSPA_IS_PORT_OUTPUT(m_PlugDesc->PortDescriptors[i])) {
  811. m_PluginInfo.NumOutputs++;
  812. }
  813. }
  814. /////////////////////////////////
  815. // LADSPA Buffers
  816. unsigned long c=0;
  817. for (unsigned int n=0; n<m_PlugDesc->PortCount; n++)
  818. {
  819. if (LADSPA_IS_PORT_INPUT(m_PlugDesc->PortDescriptors[n]))
  820. {
  821. LADSPA_Data *NewPort = new LADSPA_Data[m_HostInfo->BUFSIZE];
  822. m_LADSPABufVec.push_back(NewPort);
  823. m_PlugDesc->connect_port(m_PlugInstHandle, n, m_LADSPABufVec[c]);
  824. m_PortID.push_back(n);
  825. c++;
  826. }
  827. }
  828. for (unsigned int n=0; n<m_PlugDesc->PortCount; n++)
  829. {
  830. if (LADSPA_IS_PORT_OUTPUT(m_PlugDesc->PortDescriptors[n]))
  831. {
  832. LADSPA_Data *NewPort = new LADSPA_Data[m_HostInfo->BUFSIZE];
  833. m_LADSPABufVec.push_back(NewPort);
  834. m_PlugDesc->connect_port(m_PlugInstHandle, n, m_LADSPABufVec[c]);
  835. m_PortID.push_back(n);
  836. c++;
  837. }
  838. }
  839. // activate the plugin now
  840. if (m_PlugDesc->activate)
  841. m_PlugDesc->activate(m_PlugInstHandle);
  842. /////////////////////////////////
  843. // SSM Buffers
  844. // Allocate the i/o buffers required
  845. for (int n=0; n<m_PluginInfo.NumInputs; n++) AddInput();
  846. for (int n=0; n<m_PluginInfo.NumOutputs; n++) AddOutput();
  847. //////////////////////////////
  848. // Update the GUI stuff
  849. string desc;
  850. c=0;
  851. for (unsigned int i = 0; i < m_PlugDesc->PortCount; i++)
  852. {
  853. if (LADSPA_IS_PORT_INPUT(m_PlugDesc->PortDescriptors[i]))
  854. {
  855. desc = string(m_PlugDesc->PortNames[i]) +
  856. (LADSPA_IS_PORT_CONTROL(m_PlugDesc->PortDescriptors[i]) ? " (CV)" : " (AU)");
  857. m_PluginInfo.PortTips.push_back(desc.c_str());
  858. c++;
  859. }
  860. }
  861. for (unsigned int i = 0; i < m_PlugDesc->PortCount; i++)
  862. {
  863. if (LADSPA_IS_PORT_OUTPUT(m_PlugDesc->PortDescriptors[i])) {
  864. desc = string(m_PlugDesc->PortNames[i]) +
  865. (LADSPA_IS_PORT_CONTROL(m_PlugDesc->PortDescriptors[i]) ? " (CV)" : " (AU)");
  866. m_PluginInfo.PortTips.push_back(desc.c_str());
  867. }
  868. }
  869. UpdatePluginInfoWithHost();
  870. m_UniqueID = m_PlugDesc->UniqueID;
  871. m_InputPortCount = m_PluginInfo.NumInputs;
  872. int lbl_length;
  873. lbl_length = strlen(m_PlugDesc->Name);
  874. lbl_length = lbl_length > 255 ? 255 : lbl_length;
  875. strncpy(m_Name, m_PlugDesc->Name, lbl_length);
  876. m_Name[lbl_length] = '\0';
  877. lbl_length = strlen(m_PlugDesc->Maker);
  878. lbl_length = lbl_length > 255 ? 255 : lbl_length;
  879. strncpy(m_Maker, m_PlugDesc->Maker, lbl_length);
  880. m_Maker[lbl_length] = '\0';
  881. return true;
  882. }
  883. return false;
  884. }
  885. void LADSPAPlugin::ClearPlugin(void)
  886. {
  887. // Clear selected plugin
  888. if (m_PlugDesc) {
  889. if (m_PlugDesc->deactivate) m_PlugDesc->deactivate(m_PlugInstHandle);
  890. m_PlugDesc->cleanup(m_PlugInstHandle);
  891. m_PlugDesc = NULL;
  892. m_LADSPAInfo->DiscardDescriptorByID(m_UniqueID);
  893. }
  894. m_Page = 1;
  895. m_UpdateInputs = true;
  896. m_UniqueID = 0;
  897. m_InputPortCount = 0;
  898. m_UnconnectedInputs = 0;
  899. strncpy(m_Name, "None\0", 5);
  900. strncpy(m_Maker, "None\0", 5);
  901. for(vector<LADSPA_Data*>::iterator i=m_LADSPABufVec.begin();
  902. i!=m_LADSPABufVec.end(); i++)
  903. {
  904. if (*i) delete[] (*i);
  905. }
  906. m_LADSPABufVec.clear();
  907. RemoveAllInputs();
  908. RemoveAllOutputs();
  909. m_PluginInfo.NumInputs = 0;
  910. m_PluginInfo.NumOutputs = 0;
  911. m_PluginInfo.PortTips.clear();
  912. m_PortID.clear();
  913. m_InputPortMin.clear();
  914. m_InputPortMax.clear();
  915. m_InputPortClamp.clear();
  916. m_InputPortDefault.clear();
  917. }
  918. void LADSPAPlugin::ResetPortSettings(void)
  919. {
  920. for (int n=0; n<m_PluginInfo.NumInputs; n++)
  921. {
  922. float Max=1.0f, Min=-1.0f, Default=0.0f;
  923. int Port=m_PortID[n];
  924. // Get the bounding hints for the port
  925. LADSPA_PortRangeHintDescriptor HintDesc=m_PlugDesc->PortRangeHints[Port].HintDescriptor;
  926. if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc))
  927. {
  928. Min=m_PlugDesc->PortRangeHints[Port].LowerBound;
  929. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  930. {
  931. Min*=m_HostInfo->SAMPLERATE;
  932. }
  933. }
  934. if (LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc))
  935. {
  936. Max=m_PlugDesc->PortRangeHints[Port].UpperBound;
  937. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc))
  938. {
  939. Max*=m_HostInfo->SAMPLERATE;
  940. }
  941. }
  942. #ifdef LADSPA_VERSION
  943. // We've got a version of the header that supports port defaults
  944. if (LADSPA_IS_HINT_HAS_DEFAULT(HintDesc)) {
  945. // LADSPA_HINT_DEFAULT_0 is assumed anyway, so we don't check for it
  946. if (LADSPA_IS_HINT_DEFAULT_1(HintDesc)) {
  947. Default = 1.0f;
  948. } else if (LADSPA_IS_HINT_DEFAULT_100(HintDesc)) {
  949. Default = 100.0f;
  950. } else if (LADSPA_IS_HINT_DEFAULT_440(HintDesc)) {
  951. Default = 440.0f;
  952. } else {
  953. // These hints may be affected by SAMPLERATE, LOGARITHMIC and INTEGER
  954. if (LADSPA_IS_HINT_DEFAULT_MINIMUM(HintDesc) &&
  955. LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc)) {
  956. Default=m_PlugDesc->PortRangeHints[Port].LowerBound;
  957. } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(HintDesc) &&
  958. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  959. Default=m_PlugDesc->PortRangeHints[Port].UpperBound;
  960. } else if (LADSPA_IS_HINT_BOUNDED_BELOW(HintDesc) &&
  961. LADSPA_IS_HINT_BOUNDED_ABOVE(HintDesc)) {
  962. // These hints require both upper and lower bounds
  963. float lp = 0.0f, up = 0.0f;
  964. float min = m_PlugDesc->PortRangeHints[Port].LowerBound;
  965. float max = m_PlugDesc->PortRangeHints[Port].UpperBound;
  966. if (LADSPA_IS_HINT_DEFAULT_LOW(HintDesc)) {
  967. lp = 0.75f;
  968. up = 0.25f;
  969. } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(HintDesc)) {
  970. lp = 0.5f;
  971. up = 0.5f;
  972. } else if (LADSPA_IS_HINT_DEFAULT_HIGH(HintDesc)) {
  973. lp = 0.25f;
  974. up = 0.75f;
  975. }
  976. if (LADSPA_IS_HINT_LOGARITHMIC(HintDesc)) {
  977. if (min==0.0f || max==0.0f) {
  978. // Zero at either end means zero no matter
  979. // where hint is at, since:
  980. // log(n->0) -> Infinity
  981. Default = 0.0f;
  982. } else {
  983. // Catch negatives
  984. bool neg_min = min < 0.0f ? true : false;
  985. bool neg_max = max < 0.0f ? true : false;
  986. if (!neg_min && !neg_max) {
  987. Default = exp(log(min) * lp + log(max) * up);
  988. } else if (neg_min && neg_max) {
  989. Default = -exp(log(-min) * lp + log(-max) * up);
  990. } else {
  991. // Logarithmic range has asymptote
  992. // so just use linear scale
  993. Default = min * lp + max * up;
  994. }
  995. }
  996. } else {
  997. Default = min * lp + max * up;
  998. }
  999. }
  1000. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc)) {
  1001. Default *= m_HostInfo->SAMPLERATE;
  1002. }
  1003. if (LADSPA_IS_HINT_INTEGER(HintDesc)) {
  1004. Default = floorf(Default);
  1005. }
  1006. }
  1007. }
  1008. #else
  1009. // No LADSPA_VERSION - implies no defaults
  1010. #warning ************************************
  1011. #warning Your LADSPA header is out of date!
  1012. #warning Please get the latest sdk from
  1013. #warning www.ladspa.org
  1014. #warning Defaults will not be used.
  1015. #warning ************************************
  1016. Default = 0.0f;
  1017. #endif
  1018. m_InputPortMin.push_back(Min);
  1019. m_InputPortMax.push_back(Max);
  1020. m_InputPortClamp.push_back(true);
  1021. m_InputPortDefault.push_back(Default);
  1022. }
  1023. }
  1024. void LADSPAPlugin::SetGUIExports(void)
  1025. {
  1026. int lbl_length;
  1027. char *lbl_start;
  1028. lbl_start = m_OutData.InputPortNames;
  1029. for (unsigned long p = 0; p < m_InputPortCount; p++) {
  1030. int Port = m_PortID[p];
  1031. LADSPA_PortRangeHintDescriptor HintDesc=m_PlugDesc->PortRangeHints[Port].HintDescriptor;
  1032. // Port Labels
  1033. lbl_length = m_PluginInfo.PortTips[p].size();
  1034. lbl_length = lbl_length > 255 ? 255 : lbl_length;
  1035. strncpy(lbl_start, m_PluginInfo.PortTips[p].c_str(), lbl_length);
  1036. lbl_start[lbl_length] = '\0';
  1037. lbl_start += 256;
  1038. m_OutData.InputPortSettings[p].Integer = LADSPA_IS_HINT_INTEGER(HintDesc);
  1039. if (LADSPA_IS_HINT_LOGARITHMIC(HintDesc)) {
  1040. if (LADSPA_IS_HINT_SAMPLE_RATE(HintDesc)) {
  1041. m_OutData.InputPortSettings[p].LogBase = 2.0f;
  1042. } else {
  1043. m_OutData.InputPortSettings[p].LogBase = 10.0f;
  1044. }
  1045. } else {
  1046. m_OutData.InputPortSettings[p].LogBase = 0.0f;
  1047. }
  1048. m_OutData.InputPortSettings[p].Min = m_InputPortMin[p];
  1049. m_OutData.InputPortSettings[p].Max = m_InputPortMax[p];
  1050. m_OutData.InputPortSettings[p].Clamp = m_InputPortClamp[p];
  1051. m_OutData.InputPortDefaults[p] = m_InputPortDefault[p];
  1052. }
  1053. }