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.

217 lines
4.6KB

  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 "EnvelopePlugin.h"
  19. #include "EnvelopePluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. using namespace std;
  23. // a bit of a crap filter to smooth clicks
  24. static float SMOOTH = 0.99;
  25. static float ONEMINUS_SMOOTH = 1-SMOOTH;
  26. extern "C" {
  27. SpiralPlugin* SpiralPlugin_CreateInstance()
  28. {
  29. return new EnvelopePlugin;
  30. }
  31. char** SpiralPlugin_GetIcon()
  32. {
  33. return SpiralIcon_xpm;
  34. }
  35. int SpiralPlugin_GetID()
  36. {
  37. return 0x0005;
  38. }
  39. string SpiralPlugin_GetGroupName()
  40. {
  41. return "Control";
  42. }
  43. }
  44. ///////////////////////////////////////////////////////
  45. EnvelopePlugin::EnvelopePlugin() :
  46. m_Trigger(false),
  47. m_t(-1.0f),
  48. m_Attack(0.0f),
  49. m_Decay(0.5f),
  50. m_Sustain(1.0f),
  51. m_Release(1.0f),
  52. m_Volume(0.5f),
  53. m_TrigThresh(0.01),
  54. m_Current(0)
  55. {
  56. m_PluginInfo.Name="Envelope";
  57. m_PluginInfo.Width=142;
  58. m_PluginInfo.Height=132;
  59. m_PluginInfo.NumInputs=2;
  60. m_PluginInfo.NumOutputs=2;
  61. m_PluginInfo.PortTips.push_back("Trigger CV");
  62. m_PluginInfo.PortTips.push_back("Input");
  63. m_PluginInfo.PortTips.push_back("CV");
  64. m_PluginInfo.PortTips.push_back("Output");
  65. m_AudioCH->Register("Attack",&m_Attack);
  66. m_AudioCH->Register("Decay",&m_Decay);
  67. m_AudioCH->Register("Sustain",&m_Sustain);
  68. m_AudioCH->Register("Release",&m_Release);
  69. m_AudioCH->Register("Volume",&m_Volume);
  70. m_AudioCH->Register("Trig",&m_TrigThresh);
  71. }
  72. EnvelopePlugin::~EnvelopePlugin()
  73. {
  74. }
  75. PluginInfo &EnvelopePlugin::Initialise(const HostInfo *Host)
  76. {
  77. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  78. m_SampleTime=1.0/(float)(m_HostInfo->SAMPLERATE);
  79. return Info;
  80. }
  81. void EnvelopePlugin::Reset()
  82. {
  83. ResetPorts();
  84. m_SampleTime=1.0/(float)(m_HostInfo->SAMPLERATE);
  85. m_Trigger = false;
  86. m_t = -1.0f;
  87. }
  88. SpiralGUIType *EnvelopePlugin::CreateGUI()
  89. {
  90. return new EnvelopePluginGUI(m_PluginInfo.Width,
  91. m_PluginInfo.Height,
  92. this,m_AudioCH,m_HostInfo);
  93. }
  94. void EnvelopePlugin::Execute()
  95. {
  96. float temp=0;
  97. bool Freeze=false;
  98. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  99. {
  100. // Check the trigger CV values
  101. if (GetInput(0,n)>m_TrigThresh)
  102. {
  103. if (m_Trigger==false)
  104. {
  105. m_t=0.0f;
  106. m_Trigger=true;
  107. }
  108. }
  109. else
  110. {
  111. m_Trigger=false;
  112. }
  113. // if we are in the envelope...
  114. if (m_t>=0 && m_t<m_Attack+m_Decay+m_Release)
  115. {
  116. // find out what part of the envelope we are in
  117. // in the attack
  118. if (m_t<m_Attack)
  119. {
  120. // get normalised position to
  121. // get the volume between 0 and 1
  122. temp=m_t/m_Attack;
  123. }
  124. else
  125. // in the decay
  126. if (m_t<m_Attack+m_Decay)
  127. {
  128. //if (n==0) cerr<<"in decay"<<endl;
  129. // normalised position in m_Attack->m_Decay range
  130. float nt=(m_t-m_Attack)/m_Decay;
  131. // volume between 1 and m_Sustain
  132. temp=(1-nt)+(m_Sustain*nt);
  133. }
  134. else
  135. // in the release
  136. {
  137. //if (n==0) cerr<<"in release"<<endl;
  138. // normalised position in m_Decay->m_Release range
  139. float nt=(m_t-(m_Attack+m_Decay))/m_Release;
  140. // volume between m_Sustain and 0
  141. temp=m_Sustain*(1-nt);
  142. if (m_Release<0.2f)
  143. {
  144. temp=m_Sustain;
  145. }
  146. if (m_Trigger) Freeze=true;
  147. }
  148. temp*=m_Volume;
  149. if (!feq(temp,m_Current,0.01))
  150. {
  151. // only filter if necc
  152. temp=(temp*ONEMINUS_SMOOTH+m_Current*SMOOTH);
  153. }
  154. SetOutput(0,n,temp);
  155. SetOutput(1,n,GetInput(1,n)*temp);
  156. m_Current=temp;
  157. if (!Freeze) m_t+=m_SampleTime;
  158. }
  159. else
  160. {
  161. if (!feq(temp,m_Current,0.01))
  162. {
  163. temp=m_Current*SMOOTH;
  164. }
  165. SetOutput(0,n,temp);
  166. SetOutput(1,n,GetInput(1,n)*temp);
  167. m_Current=temp;
  168. // if we've run off the end
  169. if (m_t>m_Attack+m_Decay+m_Release)
  170. {
  171. m_t=-1;
  172. return;
  173. }
  174. }
  175. }
  176. }
  177. void EnvelopePlugin::StreamOut(ostream &s)
  178. {
  179. s<<m_Version<<" "<<m_Attack<<" "<<m_Decay<<" "<<m_Sustain<<" "<<
  180. m_Release<<" "<<m_Volume<<" "<<m_TrigThresh;
  181. }
  182. void EnvelopePlugin::StreamIn(istream &s)
  183. {
  184. int version;
  185. s>>version;
  186. s>>m_Attack>>m_Decay>>m_Sustain>>m_Release>>m_Volume>>m_TrigThresh;
  187. }