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.

183 lines
5.0KB

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