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.

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