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.

220 lines
4.7KB

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