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.

202 lines
5.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 "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::Reset()
  88. {
  89. ResetPorts();
  90. calc();
  91. }
  92. void WaveShaperPlugin::Execute () {
  93. float k1, k2;
  94. if (!InputExists (0)) return;
  95. for (int i=0; i<m_HostInfo->BUFSIZE; i++) {
  96. float v = GetInput (0, i);
  97. int index = (short)(256.0*v) + 256;
  98. // short rm = v - ((index-256)*(long)SpiralInfo::MAXSAMPLE)/256;
  99. if (index < 0) index = 0;
  100. if (index > 511) index = 511;
  101. // k1 = rm / (SpiralInfo::MAXSAMPLE/256.0);
  102. // v = (short)(k1*(wt[index]-wt[index+1])+wt[index+1]);
  103. SetOutput (0, i, m_wt[index]);
  104. }
  105. }
  106. // Functions used by GUI.UpdateValues
  107. float WaveShaperPlugin::GetCoef (int index) {
  108. if ((index < 0) || (index > 5)) return 0;
  109. return m_Coefs[index];
  110. }
  111. int WaveShaperPlugin::GetWaveType(){
  112. return m_Wave;
  113. }
  114. // Internal private functions
  115. void WaveShaperPlugin::set (int index, float v) {
  116. if ((index < 0) || (index > 511)) return;
  117. if (v > 1.0f) v = 1.0f;
  118. if (v < -1.0f) v = -1.0f;
  119. m_wt[index] = v;
  120. }
  121. void WaveShaperPlugin::calc (void) {
  122. int i;
  123. float xx,x,y,max;
  124. if (m_Wave) {
  125. max = 1.0;
  126. for (i=0; i<512; i++) {
  127. x = i / 256.0 - 1.0;
  128. y = 0;
  129. xx = x;
  130. for (int j=1; j<7; j++) {
  131. y += m_Coefs[j-1] * xx;
  132. xx *= x;
  133. }
  134. y = fabs(y);
  135. max = max > y? max:y;
  136. }
  137. for (i=0; i<512; i++) {
  138. x = i / 256.0 - 1.0;
  139. y = 0;
  140. xx = x;
  141. for (int j=1; j<7; j++) {
  142. y += m_Coefs[j-1] * xx;
  143. xx *= x;
  144. }
  145. y /= max;
  146. set (i, y);
  147. }
  148. }
  149. else {
  150. max = 1.0;
  151. for (i=0; i<512; i++) {
  152. x = (i / 256.0 - 1.0)*M_PI;
  153. y = 0;
  154. for (int j=1; j<7; j++) y += m_Coefs[j-1] * sin(x*(1+(j-1)*3));
  155. y = fabs(y);
  156. max = max > y? max:y;
  157. }
  158. for (i=0; i<512; i++) {
  159. x = (i / 256.0 - 1.0)*M_PI;
  160. y = 0;
  161. for (int j=1; j<7; j++) y += m_Coefs[j-1] * sin(x*(1+(j-1)*3));
  162. y /= max;
  163. set(i, y);
  164. }
  165. }
  166. for (i=0; i<256; i++) m_GUIArgs.FuncPlot[i] = m_wt[i*2];
  167. }
  168. // Streaming
  169. void WaveShaperPlugin::StreamOut (ostream &s) {
  170. s << m_Version << " " << m_Wave;
  171. for (int i=0; i<6; i++) s << " " << m_Coefs[i];
  172. }
  173. void WaveShaperPlugin::StreamIn (istream &s) {
  174. int version;
  175. s >> version >> m_Wave;
  176. for (int i=0; i<6; i++) s >> m_Coefs[i];
  177. calc ();
  178. }