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.

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