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.

211 lines
4.4KB

  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 "WaveTablePlugin.h"
  19. #include "WaveTablePluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. static const int IN_FREQ = 0;
  23. static const int IN_PW = 1;
  24. static const int IN_SHLEN = 2;
  25. static const int OUT_MAIN = 0;
  26. extern "C" {
  27. SpiralPlugin* CreateInstance()
  28. {
  29. return new WaveTablePlugin;
  30. }
  31. char** GetIcon()
  32. {
  33. return SpiralIcon_xpm;
  34. }
  35. int GetID()
  36. {
  37. return 0x0017;
  38. }
  39. }
  40. ///////////////////////////////////////////////////////
  41. WaveTablePlugin::WaveTablePlugin() :
  42. m_Type(SINE),
  43. m_Octave(0),
  44. m_FineFreq(1.0f),
  45. m_ModAmount(1.0f),
  46. m_TableLength(DEFAULT_TABLE_LEN)
  47. {
  48. m_CyclePos=0;
  49. m_Note=0;
  50. m_PluginInfo.Name="WaveTable";
  51. m_PluginInfo.Width=245;
  52. m_PluginInfo.Height=110;
  53. m_PluginInfo.NumInputs=1;
  54. m_PluginInfo.NumOutputs=1;
  55. m_PluginInfo.PortTips.push_back("Frequency CV");
  56. m_PluginInfo.PortTips.push_back("Output");
  57. }
  58. WaveTablePlugin::~WaveTablePlugin()
  59. {
  60. }
  61. PluginInfo &WaveTablePlugin::Initialise(const HostInfo *Host)
  62. {
  63. PluginInfo& Info= SpiralPlugin::Initialise(Host);
  64. for (int n=0; n<NUM_TABLES; n++)
  65. {
  66. m_Table[n].Allocate(m_TableLength);
  67. }
  68. WriteWaves();
  69. return Info;
  70. }
  71. SpiralGUIType *WaveTablePlugin::CreateGUI()
  72. {
  73. m_GUI = new WaveTablePluginGUI(m_PluginInfo.Width,
  74. m_PluginInfo.Height,
  75. this,m_HostInfo);
  76. m_GUI->hide();
  77. return m_GUI;
  78. }
  79. void WaveTablePlugin::WriteWaves()
  80. {
  81. float RadCycle = (M_PI/180)*360;
  82. float Pos=0;
  83. for (int n=0; n<m_TableLength; n++)
  84. {
  85. if (n==0) Pos=0;
  86. else Pos=(n/(float)m_TableLength)*RadCycle;
  87. m_Table[SINE].Set(n,sin(Pos));
  88. }
  89. for (int n=0; n<m_TableLength; n++)
  90. {
  91. if (n<m_TableLength/2) m_Table[SQUARE].Set(n,1.0f);
  92. else m_Table[SQUARE].Set(n,-1);
  93. }
  94. for (int n=0; n<m_TableLength; n++)
  95. {
  96. m_Table[REVSAW].Set(n,((n/(float)m_TableLength)*2.0f)-1.0f);
  97. }
  98. for (int n=0; n<m_TableLength; n++)
  99. {
  100. m_Table[SAW].Set(n,1-(n/(float)m_TableLength)*2.0f);
  101. }
  102. float HalfTab=m_TableLength/2;
  103. float v=0;
  104. for (int n=0; n<m_TableLength; n++)
  105. {
  106. if (n<HalfTab) v=1-(n/HalfTab)*2.0f;
  107. else v=(((n-HalfTab)/HalfTab)*2.0f)-1.0f;
  108. v*=0.99;
  109. m_Table[TRIANGLE].Set(n,v);
  110. }
  111. for (int n=0; n<m_TableLength; n++)
  112. {
  113. if (n<m_TableLength/1.2) m_Table[PULSE1].Set(n,1);
  114. else m_Table[PULSE1].Set(n,-1);
  115. }
  116. for (int n=0; n<m_TableLength; n++)
  117. {
  118. if (n<m_TableLength/1.5) m_Table[PULSE2].Set(n,1);
  119. else m_Table[PULSE2].Set(n,-1);
  120. }
  121. Pos=0;
  122. for (int n=0; n<m_TableLength; n++)
  123. {
  124. if (n==0) Pos=0;
  125. else Pos=(n/(float)m_TableLength)*RadCycle;
  126. if (sin(Pos)==0) m_Table[INVSINE].Set(n,0);
  127. else m_Table[INVSINE].Set(n,(1.0f/sin(Pos))/10.0f);
  128. }
  129. }
  130. void WaveTablePlugin::Execute()
  131. {
  132. float Freq=0;
  133. float Incr;
  134. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  135. {
  136. SetOutput(OUT_MAIN,n,0);
  137. if (InputExists(0))
  138. {
  139. Freq=GetInputPitch(0,n);
  140. Freq*=m_ModAmount;
  141. }
  142. else
  143. {
  144. Freq=110;
  145. }
  146. Freq*=m_FineFreq;
  147. if (m_Octave>0) Freq*=1<<(m_Octave);
  148. if (m_Octave<0) Freq/=1<<(-m_Octave);
  149. Incr = Freq*(m_TableLength/(float)m_HostInfo->SAMPLERATE);
  150. m_CyclePos+=Incr;
  151. while (m_CyclePos>=m_TableLength) m_CyclePos-=m_TableLength;
  152. if (m_CyclePos<0 || m_CyclePos>=m_TableLength) m_CyclePos=0;
  153. SetOutput(OUT_MAIN,n,m_Table[m_Type][m_CyclePos]);
  154. }
  155. }
  156. void WaveTablePlugin::StreamOut(ostream &s)
  157. {
  158. s<<m_Version<<" "<<*this;
  159. }
  160. void WaveTablePlugin::StreamIn(istream &s)
  161. {
  162. int version;
  163. s>>version>>*this;
  164. }
  165. istream &operator>>(istream &s, WaveTablePlugin &o)
  166. {
  167. s>>(int&)o.m_Type>>o.m_Octave>>o.m_FineFreq>>o.m_ModAmount;
  168. return s;
  169. }
  170. ostream &operator<<(ostream &s, WaveTablePlugin &o)
  171. {
  172. s<<(int)o.m_Type<<" "<<o.m_Octave<<" "<<o.m_FineFreq<<" "<<o.m_ModAmount<<" ";
  173. return s;
  174. }