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.

204 lines
4.8KB

  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 "OscillatorPlugin.h"
  19. #include "OscillatorPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include <limits.h>
  22. #include "SpiralIcon.xpm"
  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. SpiralPlugin* CreateInstance()
  29. {
  30. return new OscillatorPlugin;
  31. }
  32. char** GetIcon()
  33. {
  34. return SpiralIcon_xpm;
  35. }
  36. int GetID()
  37. {
  38. return 0x0004;
  39. }
  40. }
  41. ///////////////////////////////////////////////////////
  42. OscillatorPlugin::OscillatorPlugin() :
  43. m_Type(SQUARE),
  44. m_Octave(0),
  45. m_FineFreq(1.0f),
  46. m_PulseWidth(0.5f),
  47. m_ModAmount(1.0f),
  48. m_Noisev(0),
  49. m_FreqModBuf(NULL),
  50. m_PulseWidthModBuf(NULL),
  51. m_SHModBuf(NULL)
  52. {
  53. m_CyclePos=0;
  54. m_Note=0;
  55. m_LastFreq=0;
  56. m_PluginInfo.Name="Oscillator";
  57. m_PluginInfo.Width=245;
  58. m_PluginInfo.Height=110;
  59. m_PluginInfo.NumInputs=3;
  60. m_PluginInfo.NumOutputs=1;
  61. m_PluginInfo.PortTips.push_back("Frequency CV");
  62. m_PluginInfo.PortTips.push_back("PulseWidth CV");
  63. m_PluginInfo.PortTips.push_back("Sample & Hold length CV");
  64. m_PluginInfo.PortTips.push_back("Output");
  65. m_AudioCH->Register("Octave",&m_Octave);
  66. m_AudioCH->Register("FineFreq",&m_FineFreq);
  67. m_AudioCH->Register("PulseWidth",&m_PulseWidth);
  68. m_AudioCH->Register("Type",(char*)&m_Type);
  69. m_AudioCH->Register("SHLen",&m_SHLen);
  70. m_AudioCH->Register("ModAmount",&m_ModAmount);
  71. }
  72. OscillatorPlugin::~OscillatorPlugin()
  73. {
  74. }
  75. PluginInfo &OscillatorPlugin::Initialise(const HostInfo *Host)
  76. {
  77. return SpiralPlugin::Initialise(Host);
  78. }
  79. SpiralGUIType *OscillatorPlugin::CreateGUI()
  80. {
  81. return new OscillatorPluginGUI(m_PluginInfo.Width,
  82. m_PluginInfo.Height,
  83. this,m_AudioCH,m_HostInfo);
  84. }
  85. void OscillatorPlugin::Execute()
  86. {
  87. short noisev=0;
  88. float Freq=0;
  89. float CycleLen=0;
  90. int samplelen, PW;
  91. switch (m_Type)
  92. {
  93. case SQUARE:
  94. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  95. {
  96. if (InputExists(0)) Freq=GetInputPitch(0,n);
  97. else Freq=110;
  98. Freq*=m_FineFreq;
  99. if (m_Octave>0) Freq*=1<<(m_Octave);
  100. if (m_Octave<0) Freq/=1<<(-m_Octave);
  101. CycleLen = m_HostInfo->SAMPLERATE/Freq;
  102. PW = (int)((m_PulseWidth+GetInput(IN_PW,n)*m_ModAmount) * CycleLen);
  103. // calculate square wave pattern
  104. m_CyclePos++;
  105. if (m_CyclePos>CycleLen) m_CyclePos=0;
  106. if (m_CyclePos<PW) SetOutput(OUT_MAIN,n,1);
  107. else SetOutput(OUT_MAIN,n,-1);
  108. }
  109. break;
  110. case SAW:
  111. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  112. {
  113. if (InputExists(0)) Freq=GetInputPitch(0,n);
  114. else Freq=110;
  115. Freq*=m_FineFreq;
  116. if (m_Octave>0) Freq*=1<<(m_Octave);
  117. if (m_Octave<0) Freq/=1<<(-m_Octave);
  118. CycleLen = m_HostInfo->SAMPLERATE/Freq;
  119. PW = (int)((m_PulseWidth+GetInput(IN_PW,n)*m_ModAmount) * CycleLen);
  120. // get normailise position between cycle
  121. m_CyclePos++;
  122. if (m_CyclePos>CycleLen) m_CyclePos=0;
  123. if (m_CyclePos<PW)
  124. {
  125. // before pw -1->1
  126. SetOutput(OUT_MAIN,n,Linear(0,PW,m_CyclePos,-1,1));
  127. }
  128. else
  129. {
  130. // after pw 1->-1
  131. SetOutput(OUT_MAIN,n,Linear(PW,CycleLen,m_CyclePos,1,-1));
  132. }
  133. }
  134. break;
  135. case NOISE:
  136. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  137. {
  138. m_CyclePos++;
  139. //modulate the sample & hold length
  140. samplelen = (int)((m_SHLen+GetInput(IN_SHLEN,n)*m_ModAmount)*m_HostInfo->SAMPLERATE);
  141. // do sample & hold on the noise
  142. if (m_CyclePos>samplelen)
  143. {
  144. m_Noisev=(short)((rand()%SHRT_MAX*2)-SHRT_MAX);
  145. m_CyclePos=0;
  146. }
  147. SetOutput(OUT_MAIN,n,m_Noisev/(float)SHRT_MAX);
  148. }
  149. break;
  150. case NONE: break;
  151. }
  152. }
  153. void OscillatorPlugin::StreamOut(ostream &s)
  154. {
  155. s<<m_Version<<" "<<*this;
  156. }
  157. void OscillatorPlugin::StreamIn(istream &s)
  158. {
  159. int version;
  160. s>>version>>*this;
  161. }
  162. istream &operator>>(istream &s, OscillatorPlugin &o)
  163. {
  164. float dummy=0;
  165. s>>(int&)o.m_Type>>o.m_Octave>>o.m_FineFreq>>o.m_PulseWidth>>dummy>>
  166. o.m_SHLen>>o.m_ModAmount;
  167. return s;
  168. }
  169. ostream &operator<<(ostream &s, OscillatorPlugin &o)
  170. {
  171. float dummy=0;
  172. s<<(int)o.m_Type<<" "<<o.m_Octave<<" "<<o.m_FineFreq<<" "<<o.m_PulseWidth<<" "<<
  173. dummy<<" "<<o.m_SHLen<<" "<<o.m_ModAmount<<" ";
  174. return s;
  175. }