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.

194 lines
5.0KB

  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 "SpiralIcon.xpm"
  22. extern "C" {
  23. SpiralPlugin* SpiralPlugin_CreateInstance() {
  24. return new WaveShaperPlugin;
  25. }
  26. char** SpiralPlugin_GetIcon() {
  27. return SpiralIcon_xpm;
  28. }
  29. int SpiralPlugin_GetID() {
  30. return 0x0032;
  31. }
  32. string SpiralPlugin_GetGroupName()
  33. {
  34. return "Filters/FX";
  35. }
  36. }
  37. ///////////////////////////////////////////////////////
  38. WaveShaperPlugin::WaveShaperPlugin () {
  39. int i;
  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. m_wt = new float[512];
  48. for (i=0; i<512; i++) m_wt[i] = (i / 256.0 - 1.0);
  49. m_GUIArgs.FuncPlot = new float[256];
  50. for (i=0; i<256; i++) m_GUIArgs.FuncPlot[i] = m_wt[i*2];
  51. m_Wave = 1;
  52. for (i=0; i<6; i++) m_Coefs[i] = 0.0;
  53. m_Coefs[0] = 1.0;
  54. m_AudioCH->Register ("WaveType", &m_GUIArgs.WaveType);
  55. m_AudioCH->Register ("CoefNum", &m_GUIArgs.CoefNum);
  56. m_AudioCH->Register ("CoefVal", &m_GUIArgs.CoefVal);
  57. m_AudioCH->RegisterData ("WT", ChannelHandler::OUTPUT, m_GUIArgs.FuncPlot, 256 * sizeof (float));
  58. }
  59. WaveShaperPlugin::~WaveShaperPlugin() {
  60. delete [] m_GUIArgs.FuncPlot;
  61. delete [] m_wt;
  62. }
  63. PluginInfo &WaveShaperPlugin::Initialise (const HostInfo *Host) {
  64. PluginInfo& Info = SpiralPlugin::Initialise (Host);
  65. calc();
  66. return Info;
  67. }
  68. SpiralGUIType *WaveShaperPlugin::CreateGUI () {
  69. return new WaveShaperPluginGUI (m_PluginInfo.Width, m_PluginInfo.Height, this, m_AudioCH, m_HostInfo);
  70. }
  71. void WaveShaperPlugin::ExecuteCommands () {
  72. if (m_AudioCH->IsCommandWaiting ()) {
  73. switch (m_AudioCH->GetCommand ()) {
  74. case (SETWAVETYPE) :
  75. m_Wave = m_GUIArgs.WaveType;
  76. calc ();
  77. break;
  78. case (SETCOEF) :
  79. if ((m_GUIArgs.CoefNum < 0) || (m_GUIArgs.CoefNum > 5)) break;
  80. m_Coefs[m_GUIArgs.CoefNum] = m_GUIArgs.CoefVal;
  81. calc ();
  82. break;
  83. }
  84. }
  85. }
  86. void WaveShaperPlugin::Execute () {
  87. float k1, k2;
  88. if (!InputExists (0)) return;
  89. for (int i=0; i<m_HostInfo->BUFSIZE; i++) {
  90. float v = GetInput (0, i);
  91. int index = (short)(256.0*v) + 256;
  92. // short rm = v - ((index-256)*(long)SpiralInfo::MAXSAMPLE)/256;
  93. if (index < 0) index = 0;
  94. if (index > 511) index = 511;
  95. // k1 = rm / (SpiralInfo::MAXSAMPLE/256.0);
  96. // v = (short)(k1*(wt[index]-wt[index+1])+wt[index+1]);
  97. SetOutput (0, i, m_wt[index]);
  98. }
  99. }
  100. // Functions used by GUI.UpdateValues
  101. float WaveShaperPlugin::GetCoef (int index) {
  102. if ((index < 0) || (index > 5)) return 0;
  103. return m_Coefs[index];
  104. }
  105. int WaveShaperPlugin::GetWaveType(){
  106. return m_Wave;
  107. }
  108. // Internal private functions
  109. void WaveShaperPlugin::set (int index, float v) {
  110. if ((index < 0) || (index > 511)) return;
  111. if (v > 1.0f) v = 1.0f;
  112. if (v < -1.0f) v = -1.0f;
  113. m_wt[index] = v;
  114. }
  115. void WaveShaperPlugin::calc (void) {
  116. int i;
  117. float xx,x,y,max;
  118. if (m_Wave) {
  119. max = 1.0;
  120. for (i=0; i<512; i++) {
  121. x = i / 256.0 - 1.0;
  122. y = 0;
  123. xx = x;
  124. for (int j=1; j<7; j++) {
  125. y += m_Coefs[j-1] * xx;
  126. xx *= x;
  127. }
  128. y = fabs(y);
  129. max = max > y? max:y;
  130. }
  131. for (i=0; i<512; i++) {
  132. x = i / 256.0 - 1.0;
  133. y = 0;
  134. xx = x;
  135. for (int j=1; j<7; j++) {
  136. y += m_Coefs[j-1] * xx;
  137. xx *= x;
  138. }
  139. y /= max;
  140. set (i, y);
  141. }
  142. }
  143. else {
  144. max = 1.0;
  145. for (i=0; i<512; i++) {
  146. x = (i / 256.0 - 1.0)*M_PI;
  147. y = 0;
  148. for (int j=1; j<7; j++) y += m_Coefs[j-1] * sin(x*(1+(j-1)*3));
  149. y = fabs(y);
  150. max = max > y? max:y;
  151. }
  152. for (i=0; i<512; i++) {
  153. x = (i / 256.0 - 1.0)*M_PI;
  154. y = 0;
  155. for (int j=1; j<7; j++) y += m_Coefs[j-1] * sin(x*(1+(j-1)*3));
  156. y /= max;
  157. set(i, y);
  158. }
  159. }
  160. for (i=0; i<256; i++) m_GUIArgs.FuncPlot[i] = m_wt[i*2];
  161. }
  162. // Streaming
  163. void WaveShaperPlugin::StreamOut (ostream &s) {
  164. s << m_Version << " " << m_Wave;
  165. for (int i=0; i<6; i++) s << " " << m_Coefs[i];
  166. }
  167. void WaveShaperPlugin::StreamIn (istream &s) {
  168. int version;
  169. s >> version >> m_Wave;
  170. for (int i=0; i<6; i++) s >> m_Coefs[i];
  171. calc ();
  172. }