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.

205 lines
4.1KB

  1. /* WaveShaper Plugin Copyleft (C) 2001 Yves Usson
  2. * for SpiralSynthModular
  3. * Copyleft (C) 2001 David Griffiths <dave@pawfal.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include "WaveShaperPlugin.h"
  20. #include "WaveShaperPluginGUI.h"
  21. #include <FL/Fl_Button.h>
  22. #include "WaveShaperIcon.xpm"
  23. extern "C" {
  24. SpiralPlugin* CreateInstance()
  25. {
  26. return new WaveShaperPlugin;
  27. }
  28. char** GetIcon()
  29. {
  30. return WaveShaperIcon_xpm;
  31. }
  32. int GetID()
  33. {
  34. return 0x0032;
  35. }
  36. }
  37. ///////////////////////////////////////////////////////
  38. WaveShaperPlugin::WaveShaperPlugin()
  39. {
  40. m_PluginInfo.Name="WaveShaper";
  41. m_PluginInfo.Width=278;
  42. m_PluginInfo.Height=260;
  43. m_PluginInfo.NumInputs=1;
  44. m_PluginInfo.NumOutputs=1;
  45. m_PluginInfo.PortTips.push_back("Input");
  46. m_PluginInfo.PortTips.push_back("Out");
  47. wt = new float[512];
  48. for (int i=0;i<512;i++) wt[i] = (i / 256.0 - 1.0);
  49. m_Wave = 1;
  50. for (int i=0;i<6;i++) m_Coefs[i] = 0.0;
  51. m_Coefs[0] = 1.0;
  52. }
  53. WaveShaperPlugin::~WaveShaperPlugin()
  54. {
  55. delete [] wt;
  56. }
  57. PluginInfo &WaveShaperPlugin::Initialise(const HostInfo *Host)
  58. {
  59. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  60. Calc();
  61. return Info;
  62. }
  63. SpiralGUIType *WaveShaperPlugin::CreateGUI()
  64. {
  65. m_GUI = new WaveShaperPluginGUI(m_PluginInfo.Width,
  66. m_PluginInfo.Height,
  67. this,m_HostInfo);
  68. m_GUI->hide();
  69. return m_GUI;
  70. }
  71. void WaveShaperPlugin::Execute()
  72. {
  73. float k1,k2;
  74. if (!InputExists(0)) return;
  75. for (int i=0;i<m_HostInfo->BUFSIZE;i++)
  76. {
  77. float v = GetInput(0,i);
  78. int index = (short)(256.0*v)+256;
  79. // short rm = v - ((index-256)*(long)SpiralInfo::MAXSAMPLE)/256;
  80. if (index<0) index=0;
  81. if (index>511) index=511;
  82. // k1 = rm / (SpiralInfo::MAXSAMPLE/256.0);
  83. // v = (short)(k1*(wt[index]-wt[index+1])+wt[index+1]);
  84. SetOutput(0,i,wt[index]);
  85. }
  86. }
  87. void WaveShaperPlugin::Calc(void)
  88. {
  89. int i;
  90. float xx,x,y,max;
  91. if (m_Wave)
  92. {
  93. max = 1.0;
  94. for (i=0;i<512;i++)
  95. {
  96. x = i / 256.0 - 1.0;
  97. y = 0;
  98. xx = x;
  99. for (int j=1; j<7; j++)
  100. {
  101. y += m_Coefs[j-1] * xx;
  102. xx *= x;
  103. }
  104. y = fabs(y);
  105. max = max > y? max:y;
  106. }
  107. for (i=0;i<512;i++)
  108. {
  109. x = i / 256.0 - 1.0;
  110. y = 0;
  111. xx = x;
  112. for (int j=1; j<7; j++)
  113. {
  114. y += m_Coefs[j-1] * xx;
  115. xx *= x;
  116. }
  117. y /= max;
  118. set(i,y);
  119. }
  120. }
  121. else
  122. {
  123. max = 1.0;
  124. for (i=0;i<512;i++)
  125. {
  126. x = (i / 256.0 - 1.0)*M_PI;
  127. y = 0;
  128. for (int j=1; j<7; j++)
  129. y += m_Coefs[j-1] * sin(x*(1+(j-1)*3));
  130. y = fabs(y);
  131. max = max > y? max:y;
  132. }
  133. for (i=0;i<512;i++)
  134. {
  135. x = (i / 256.0 - 1.0)*M_PI;
  136. y = 0;
  137. for (int j=1; j<7; j++)
  138. y += m_Coefs[j-1] * sin(x*(1+(j-1)*3));
  139. y /= max;
  140. set(i,y);
  141. }
  142. }
  143. }
  144. float WaveShaperPlugin::Get(int index)
  145. {
  146. if ((index < 0) || (index > 511)) return 0;
  147. return wt[index];
  148. }
  149. void WaveShaperPlugin::set(int index,float v)
  150. {
  151. if ((index < 0) || (index > 511)) return;
  152. if (v > 1.0f) v = 1.0f;
  153. if (v < -1.0f) v = -1.0f;
  154. wt[index] = v;
  155. }
  156. void WaveShaperPlugin::SetCoef(int index,float v){
  157. if ((index < 0) || (index > 5)) return;
  158. m_Coefs[index] = v;
  159. }
  160. void WaveShaperPlugin::SetWaveType(int t){
  161. m_Wave = t;
  162. }
  163. float WaveShaperPlugin::GetCoef(int index){
  164. if ((index < 0) || (index > 5)) return 0;
  165. return m_Coefs[index];
  166. }
  167. int WaveShaperPlugin::GetWaveType(){
  168. return m_Wave;
  169. }
  170. void WaveShaperPlugin::StreamOut(ostream &s)
  171. {
  172. s<<m_Version<<" "<<m_Wave;
  173. for (int i=0;i<6;i++) s<<" "<<m_Coefs[i];
  174. }
  175. void WaveShaperPlugin::StreamIn(istream &s)
  176. {
  177. int version;
  178. s>>version>>m_Wave;
  179. for (int i=0;i<6;i++) s>>m_Coefs[i];
  180. Calc();
  181. }