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.

215 lines
4.6KB

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