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.

199 lines
4.5KB

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