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.

194 lines
4.2KB

  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. m_AudioCH->Register("Attack",&m_Attack);
  57. m_AudioCH->Register("Decay",&m_Decay);
  58. m_AudioCH->Register("Sustain",&m_Sustain);
  59. m_AudioCH->Register("Release",&m_Release);
  60. m_AudioCH->Register("Volume",&m_Volume);
  61. m_AudioCH->Register("Trig",&m_TrigThresh);
  62. }
  63. EnvelopePlugin::~EnvelopePlugin()
  64. {
  65. }
  66. PluginInfo &EnvelopePlugin::Initialise(const HostInfo *Host)
  67. {
  68. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  69. m_SampleTime=1.0/(float)(m_HostInfo->SAMPLERATE);
  70. return Info;
  71. }
  72. SpiralGUIType *EnvelopePlugin::CreateGUI()
  73. {
  74. return new EnvelopePluginGUI(m_PluginInfo.Width,
  75. m_PluginInfo.Height,
  76. this,m_AudioCH,m_HostInfo);
  77. }
  78. void EnvelopePlugin::Execute()
  79. {
  80. float temp=0;
  81. bool Freeze=false;
  82. // Early out?
  83. /*if (m_t<0 && (!m_Input[0] || m_Input[0]->IsEmpty()))
  84. {
  85. m_Output[0]->Zero();
  86. m_Output[1]->Zero();
  87. return;
  88. }*/
  89. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  90. {
  91. // Check the trigger CV values
  92. if (GetInput(0,n)>m_TrigThresh)
  93. {
  94. if (m_Trigger==false)
  95. {
  96. m_t=0.0f;
  97. m_Trigger=true;
  98. }
  99. }
  100. else
  101. {
  102. m_Trigger=false;
  103. }
  104. // if we are in the envelope...
  105. if (m_t>=0 && m_t<m_Attack+m_Decay+m_Release)
  106. {
  107. // find out what part of the envelope we are in
  108. // in the attack
  109. if (m_t<m_Attack)
  110. {
  111. // get normalised position to
  112. // get the volume between 0 and 1
  113. temp=m_t/m_Attack;
  114. }
  115. else
  116. // in the decay
  117. if (m_t<m_Attack+m_Decay)
  118. {
  119. //if (n==0) cerr<<"in decay"<<endl;
  120. // normalised position in m_Attack->m_Decay range
  121. float nt=(m_t-m_Attack)/m_Decay;
  122. // volume between 1 and m_Sustain
  123. temp=(1-nt)+(m_Sustain*nt);
  124. }
  125. else
  126. // in the release
  127. {
  128. //if (n==0) cerr<<"in release"<<endl;
  129. // normalised position in m_Decay->m_Release range
  130. float nt=(m_t-(m_Attack+m_Decay))/m_Release;
  131. // volume between m_Sustain and 0
  132. temp=m_Sustain*(1-nt);
  133. if (m_Release<0.2f)
  134. {
  135. temp=m_Sustain;
  136. }
  137. if (m_Trigger) Freeze=true;
  138. }
  139. temp*=m_Volume;
  140. SetOutput(0,n,temp);
  141. SetOutput(1,n,GetInput(1,n)*temp);
  142. if (!Freeze) m_t+=m_SampleTime;
  143. }
  144. else
  145. {
  146. SetOutput(0,n,0);
  147. SetOutput(1,n,0);
  148. // if we've run off the end
  149. if (m_t>m_Attack+m_Decay+m_Release)
  150. {
  151. m_t=-1;
  152. //m_Output[0]->Zero();
  153. //m_Output[1]->Zero();
  154. return;
  155. }
  156. }
  157. }
  158. }
  159. void EnvelopePlugin::StreamOut(ostream &s)
  160. {
  161. s<<m_Version<<" "<<m_Attack<<" "<<m_Decay<<" "<<m_Sustain<<" "<<
  162. m_Release<<" "<<m_Volume<<" "<<m_TrigThresh;
  163. }
  164. void EnvelopePlugin::StreamIn(istream &s)
  165. {
  166. int version;
  167. s>>version;
  168. s>>m_Attack>>m_Decay>>m_Sustain>>m_Release>>m_Volume>>m_TrigThresh;
  169. }