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.

238 lines
4.9KB

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