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.

189 lines
4.0KB

  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. extern "C" {
  23. SpiralPlugin* CreateInstance()
  24. {
  25. return new EnvelopePlugin;
  26. }
  27. char** GetIcon()
  28. {
  29. return SpiralIcon_xpm;
  30. }
  31. int GetID()
  32. {
  33. return 0x0005;
  34. }
  35. }
  36. ///////////////////////////////////////////////////////
  37. EnvelopePlugin::EnvelopePlugin() :
  38. m_Trigger(false),
  39. m_t(-1.0f),
  40. m_Attack(0.0f),
  41. m_Decay(0.5f),
  42. m_Sustain(1.0f),
  43. m_Release(1.0f),
  44. m_Volume(0.5f),
  45. m_TrigThresh(0.01)
  46. {
  47. m_PluginInfo.Name="Envelope";
  48. m_PluginInfo.Width=142;
  49. m_PluginInfo.Height=110;
  50. m_PluginInfo.NumInputs=2;
  51. m_PluginInfo.NumOutputs=2;
  52. m_PluginInfo.PortTips.push_back("Trigger CV");
  53. m_PluginInfo.PortTips.push_back("Input");
  54. m_PluginInfo.PortTips.push_back("CV");
  55. m_PluginInfo.PortTips.push_back("Output");
  56. }
  57. EnvelopePlugin::~EnvelopePlugin()
  58. {
  59. }
  60. PluginInfo &EnvelopePlugin::Initialise(const HostInfo *Host)
  61. {
  62. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  63. m_SampleTime=1.0/(float)(m_HostInfo->SAMPLERATE);
  64. return Info;
  65. }
  66. SpiralGUIType *EnvelopePlugin::CreateGUI()
  67. {
  68. m_GUI = new EnvelopePluginGUI(m_PluginInfo.Width,
  69. m_PluginInfo.Height,
  70. this,m_HostInfo);
  71. m_GUI->hide();
  72. return m_GUI;
  73. }
  74. void EnvelopePlugin::Execute()
  75. {
  76. float temp=0;
  77. bool Freeze=false;
  78. // Early out?
  79. /*if (m_t<0 && (!m_Input[0] || m_Input[0]->IsEmpty()))
  80. {
  81. m_Output[0]->Zero();
  82. m_Output[1]->Zero();
  83. return;
  84. }*/
  85. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  86. {
  87. // Check the trigger CV values
  88. if (GetInput(0,n)>m_TrigThresh)
  89. {
  90. if (m_Trigger==false)
  91. {
  92. m_t=0.0f;
  93. m_Trigger=true;
  94. }
  95. }
  96. else
  97. {
  98. m_Trigger=false;
  99. }
  100. // if we are in the envelope...
  101. if (m_t>=0 && m_t<m_Attack+m_Decay+m_Release)
  102. {
  103. // find out what part of the envelope we are in
  104. // in the attack
  105. if (m_t<m_Attack)
  106. {
  107. // get normalised position to
  108. // get the volume between 0 and 1
  109. temp=m_t/m_Attack;
  110. }
  111. else
  112. // in the decay
  113. if (m_t<m_Attack+m_Decay)
  114. {
  115. //if (n==0) cerr<<"in decay"<<endl;
  116. // normalised position in m_Attack->m_Decay range
  117. float nt=(m_t-m_Attack)/m_Decay;
  118. // volume between 1 and m_Sustain
  119. temp=(1-nt)+(m_Sustain*nt);
  120. }
  121. else
  122. // in the release
  123. {
  124. //if (n==0) cerr<<"in release"<<endl;
  125. // normalised position in m_Decay->m_Release range
  126. float nt=(m_t-(m_Attack+m_Decay))/m_Release;
  127. // volume between m_Sustain and 0
  128. temp=m_Sustain*(1-nt);
  129. if (m_Release<0.2f)
  130. {
  131. temp=m_Sustain;
  132. }
  133. if (m_Trigger) Freeze=true;
  134. }
  135. temp*=m_Volume;
  136. SetOutput(0,n,temp);
  137. SetOutput(1,n,GetInput(1,n)*temp);
  138. if (!Freeze) m_t+=m_SampleTime;
  139. }
  140. else
  141. {
  142. SetOutput(0,n,0);
  143. SetOutput(1,n,0);
  144. // if we've run off the end
  145. if (m_t>m_Attack+m_Decay+m_Release)
  146. {
  147. m_t=-1;
  148. //m_Output[0]->Zero();
  149. //m_Output[1]->Zero();
  150. return;
  151. }
  152. }
  153. }
  154. }
  155. void EnvelopePlugin::StreamOut(ostream &s)
  156. {
  157. s<<m_Version<<" "<<m_Attack<<" "<<m_Decay<<" "<<m_Sustain<<" "<<
  158. m_Release<<" "<<m_Volume<<" "<<m_TrigThresh;
  159. }
  160. void EnvelopePlugin::StreamIn(istream &s)
  161. {
  162. int version;
  163. s>>version;
  164. s>>m_Attack>>m_Decay>>m_Sustain>>m_Release>>m_Volume>>m_TrigThresh;
  165. }