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