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.

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