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.

197 lines
5.5KB

  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 <stdio.h>
  19. #include "LogicPlugin.h"
  20. #include "LogicPluginGUI.h"
  21. #include <FL/Fl_Button.h>
  22. #include "SpiralIcon.xpm"
  23. #include "../../NoteTable.h"
  24. extern "C" {
  25. SpiralPlugin* CreateInstance()
  26. {
  27. return new LogicPlugin;
  28. }
  29. char** GetIcon()
  30. {
  31. return SpiralIcon_xpm;
  32. }
  33. int GetID()
  34. {
  35. return 52;
  36. }
  37. string GetGroupName()
  38. {
  39. return "Maths/Logic";
  40. }
  41. }
  42. ///////////////////////////////////////////////////////
  43. LogicPlugin::LogicPlugin() :
  44. m_Operator(AND)
  45. {
  46. m_Version = 2;
  47. m_PluginInfo.Name="Logic";
  48. m_PluginInfo.Width=75;
  49. // Andy Preston - multiple inputs - was 100
  50. m_PluginInfo.Height=130;
  51. // Andy Preston - Multiple Inputs
  52. CreatePorts ();
  53. // m_PluginInfo.NumInputs=2;
  54. // m_PluginInfo.NumOutputs=1;
  55. // m_PluginInfo.PortTips.push_back("Input 1");
  56. // m_PluginInfo.PortTips.push_back("Input 2");
  57. // m_PluginInfo.PortTips.push_back("Output");
  58. m_AudioCH->Register("Operator",(int*)&m_Operator);
  59. // Andy Preston - Multiple Inputs
  60. m_AudioCH->Register ("Inputs", &m_Inputs);
  61. }
  62. LogicPlugin::~LogicPlugin()
  63. {
  64. }
  65. PluginInfo &LogicPlugin::Initialise(const HostInfo *Host)
  66. {
  67. return SpiralPlugin::Initialise(Host);
  68. }
  69. SpiralGUIType *LogicPlugin::CreateGUI()
  70. {
  71. return new LogicPluginGUI(m_PluginInfo.Width,
  72. m_PluginInfo.Height,
  73. this,m_AudioCH,m_HostInfo);
  74. }
  75. // Andy Preston - Multiple Inputs
  76. void LogicPlugin::Execute (void) {
  77. float Freq=0, OldFreq=0;
  78. for (int n=0; n<m_HostInfo->BUFSIZE; n++) {
  79. switch (m_Operator) {
  80. case NOT: // Only Uses Input 1
  81. if (GetInput(0,n)>0) SetOutput(0,n,-1.0f);
  82. else SetOutput(0,n,1.0f);
  83. break;
  84. case XOR: // Only uses inputs 1 and 2
  85. if ((GetInput(0,n)>0 || GetInput(1,n)>0) && !(GetInput(0,n)>0 && GetInput(1,n)>0))
  86. SetOutput(0,n,1.0f);
  87. else SetOutput(0,n,-1.0f);
  88. break;
  89. case XNOR: // Only uses inputs 1 and 2
  90. if ((GetInput(0,n)>0 && GetInput(1,n)>0) || (!(GetInput(0,n)>0) && !(GetInput(1,n)>0)))
  91. SetOutput(0,n,1.0f);
  92. else SetOutput(0,n,-1.0f);
  93. break;
  94. default: // Uses all available inputs
  95. int true_val, give_up;
  96. switch (m_Operator) {
  97. case AND: true_val = 1; give_up = -1; break;
  98. case OR: true_val = 1; give_up = 1; break;
  99. case NAND: true_val = -1; give_up = 1; break;
  100. case NOR : true_val = -1; give_up = -1; break;
  101. default: true_val = 0; give_up = 0; break;
  102. }
  103. int result = 0;
  104. for (int i=0; i<m_PluginInfo.NumInputs; i++) {
  105. if (GetInput (i, n) > 0) result = true_val; else result = -true_val;
  106. if (result == give_up) break;
  107. }
  108. SetOutput (0, n, (float)result);
  109. break;
  110. }
  111. }
  112. }
  113. void LogicPlugin::ExecuteCommands() {
  114. // Andy Preston - Multiple Inputs
  115. if (m_AudioCH->IsCommandWaiting ()) {
  116. switch (m_AudioCH->GetCommand()) {
  117. case SETINPUTS:
  118. SetInputs (m_Inputs);
  119. break;
  120. }
  121. }
  122. }
  123. // Andy Preston - Multiple Inputs
  124. void LogicPlugin::SetInputs (int n) {
  125. // once to clear the connections with the current info
  126. // do we need this????
  127. UpdatePluginInfoWithHost();
  128. // Things can get a bit confused deleting and adding inputs
  129. // so we just chuck away all the ports...
  130. RemoveAllInputs ();
  131. RemoveAllOutputs ();
  132. m_PluginInfo.NumInputs = 0;
  133. m_PluginInfo.NumOutputs = 0;
  134. m_PluginInfo.PortTips.clear ();
  135. // ... and then create some new ones
  136. CreatePorts (n, true);
  137. // do the actual update
  138. UpdatePluginInfoWithHost ();
  139. }
  140. // Andy Preston - Multiple Inputs
  141. void LogicPlugin::CreatePorts (int n, bool AddPorts) {
  142. int c;
  143. m_PluginInfo.NumInputs = n;
  144. m_Inputs = n;
  145. char t[256];
  146. for (c=1; c<=n; c++) {
  147. sprintf (t, "Input %d", c);
  148. m_PluginInfo.PortTips.push_back (t);
  149. }
  150. m_PluginInfo.NumOutputs = 1;
  151. m_PluginInfo.PortTips.push_back ("Output");
  152. if (AddPorts) {
  153. for (c=0; c<m_PluginInfo.NumInputs; c++) AddInput();
  154. for (c=0; c<m_PluginInfo.NumOutputs; c++) AddOutput();
  155. }
  156. }
  157. void LogicPlugin::StreamOut(ostream &s)
  158. {
  159. s << 1 /*m_Version*/ << endl;
  160. s << m_PluginInfo.NumInputs << " " << m_Operator;
  161. }
  162. void LogicPlugin::StreamIn(istream &s) {
  163. int version, datum;
  164. s >> version;
  165. switch (version) {
  166. case 1: // Original Version
  167. s >> datum; // Version 1 saved a constant that is not used now
  168. SetInputs (2);
  169. break;
  170. case 2: // Andy Preston
  171. s >> datum;
  172. SetInputs (datum);
  173. s >> datum;
  174. m_Operator = (OperatorType)datum;
  175. break;
  176. }
  177. }