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.

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