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.

1050 lines
26KB

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