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.

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