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.

212 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. 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::Execute()
  92. {
  93. short noisev=0;
  94. float Freq=0;
  95. float CycleLen=0;
  96. int samplelen, PW;
  97. switch (m_Type)
  98. {
  99. case SQUARE:
  100. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  101. {
  102. if (InputExists(0)) Freq=GetInputPitch(0,n);
  103. else Freq=110;
  104. Freq*=m_FineFreq;
  105. if (m_Octave>0) Freq*=1<<(m_Octave);
  106. if (m_Octave<0) Freq/=1<<(-m_Octave);
  107. CycleLen = m_HostInfo->SAMPLERATE/Freq;
  108. PW = (int)((m_PulseWidth+GetInput(IN_PW,n)*m_ModAmount) * CycleLen);
  109. // calculate square wave pattern
  110. m_CyclePos++;
  111. if (m_CyclePos>CycleLen) m_CyclePos=0;
  112. if (m_CyclePos<PW) SetOutput(OUT_MAIN,n,1);
  113. else SetOutput(OUT_MAIN,n,-1);
  114. }
  115. break;
  116. case SAW:
  117. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  118. {
  119. if (InputExists(0)) Freq=GetInputPitch(0,n);
  120. else Freq=110;
  121. Freq*=m_FineFreq;
  122. if (m_Octave>0) Freq*=1<<(m_Octave);
  123. if (m_Octave<0) Freq/=1<<(-m_Octave);
  124. CycleLen = m_HostInfo->SAMPLERATE/Freq;
  125. PW = (int)((m_PulseWidth+GetInput(IN_PW,n)*m_ModAmount) * CycleLen);
  126. // get normailise position between cycle
  127. m_CyclePos++;
  128. if (m_CyclePos>CycleLen) m_CyclePos=0;
  129. if (m_CyclePos<PW)
  130. {
  131. // before pw -1->1
  132. SetOutput(OUT_MAIN,n,Linear(0,PW,m_CyclePos,-1,1));
  133. }
  134. else
  135. {
  136. // after pw 1->-1
  137. SetOutput(OUT_MAIN,n,Linear(PW,CycleLen,m_CyclePos,1,-1));
  138. }
  139. }
  140. break;
  141. case NOISE:
  142. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  143. {
  144. m_CyclePos++;
  145. //modulate the sample & hold length
  146. samplelen = (int)((m_SHLen+GetInput(IN_SHLEN,n)*m_ModAmount)*m_HostInfo->SAMPLERATE);
  147. // do sample & hold on the noise
  148. if (m_CyclePos>samplelen)
  149. {
  150. m_Noisev=(short)((rand()%SHRT_MAX*2)-SHRT_MAX);
  151. m_CyclePos=0;
  152. }
  153. SetOutput(OUT_MAIN,n,m_Noisev/(float)SHRT_MAX);
  154. }
  155. break;
  156. case NONE: break;
  157. }
  158. }
  159. void OscillatorPlugin::StreamOut(ostream &s)
  160. {
  161. s<<m_Version<<" "<<*this;
  162. }
  163. void OscillatorPlugin::StreamIn(istream &s)
  164. {
  165. int version;
  166. s>>version>>*this;
  167. }
  168. istream &operator>>(istream &s, OscillatorPlugin &o)
  169. {
  170. float dummy=0;
  171. s>>(int&)o.m_Type>>o.m_Octave>>o.m_FineFreq>>o.m_PulseWidth>>dummy>>
  172. o.m_SHLen>>o.m_ModAmount;
  173. return s;
  174. }
  175. ostream &operator<<(ostream &s, OscillatorPlugin &o)
  176. {
  177. float dummy=0;
  178. s<<(int)o.m_Type<<" "<<o.m_Octave<<" "<<o.m_FineFreq<<" "<<o.m_PulseWidth<<" "<<
  179. dummy<<" "<<o.m_SHLen<<" "<<o.m_ModAmount<<" ";
  180. return s;
  181. }