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.

201 lines
4.3KB

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