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.

234 lines
4.7KB

  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 "ComplexEnvelopePlugin.h"
  19. #include "ComplexEnvelopePluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. extern "C" {
  23. SpiralPlugin* CreateInstance()
  24. {
  25. return new ComplexEnvelopePlugin;
  26. }
  27. char** GetIcon()
  28. {
  29. return SpiralIcon_xpm;
  30. }
  31. int GetID()
  32. {
  33. return 33;
  34. }
  35. }
  36. ///////////////////////////////////////////////////////
  37. ComplexEnvelopePlugin::ComplexEnvelopePlugin() :
  38. m_Triggered(0),
  39. m_Position(-1),
  40. m_SampleTime(2.0f)
  41. {
  42. m_PluginInfo.Name="ComplexEnv";
  43. m_PluginInfo.Width=320;
  44. m_PluginInfo.Height=200;
  45. m_PluginInfo.NumInputs=2;
  46. m_PluginInfo.NumOutputs=2;
  47. m_PluginInfo.PortTips.push_back("Trigger CV");
  48. m_PluginInfo.PortTips.push_back("Input");
  49. m_PluginInfo.PortTips.push_back("CV");
  50. m_PluginInfo.PortTips.push_back("Output");
  51. }
  52. ComplexEnvelopePlugin::~ComplexEnvelopePlugin()
  53. {
  54. }
  55. PluginInfo &ComplexEnvelopePlugin::Initialise(const HostInfo *Host)
  56. {
  57. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  58. CVListToEnvelope(m_CVVec);
  59. return Info;
  60. }
  61. SpiralGUIType *ComplexEnvelopePlugin::CreateGUI()
  62. {
  63. m_GUI = new ComplexEnvelopePluginGUI(m_PluginInfo.Width,
  64. m_PluginInfo.Height,
  65. this,m_HostInfo);
  66. m_GUI->hide();
  67. return m_GUI;
  68. }
  69. void ComplexEnvelopePlugin::CVListToEnvelope(const vector<Vec2> &CVVec)
  70. {
  71. m_EnvSample.Zero();
  72. m_CVVec=CVVec;
  73. // check length
  74. if (m_EnvSample.GetLength()/m_HostInfo->SAMPLERATE!=m_SampleTime)
  75. {
  76. m_EnvSample.Clear();
  77. m_EnvSample.Allocate((int)m_SampleTime*m_HostInfo->SAMPLERATE);
  78. }
  79. vector<Vec2> Lines;
  80. if (m_Bezier)
  81. {
  82. // calculate the bezier spline that forms the line segments
  83. for (vector<Vec2>::const_iterator i=CVVec.begin();
  84. i!=CVVec.end(); i++)
  85. {
  86. AddToSpline(*i);
  87. }
  88. CalculateBezierSpline(&Lines,10);
  89. }
  90. else
  91. {
  92. // use the CV's as line endpoints for straight line interpolation
  93. Lines=CVVec;
  94. }
  95. vector<Vec2>::const_iterator i=Lines.begin();
  96. if (i==Lines.end()) return; // no lines
  97. vector<Vec2>::const_iterator ni=i+1;
  98. if (ni==Lines.end()) return; // no lines
  99. for (int n=0; n<m_EnvSample.GetLength(); n++)
  100. {
  101. float gt=n/(float)m_EnvSample.GetLength();
  102. float t=(gt-i->x)/(ni->x-i->x);
  103. if (t<0) t=0;
  104. // see if we need to find the next line
  105. if (gt>=ni->x)
  106. {
  107. i++;
  108. ni++;
  109. // env has ended before the sample length
  110. if (ni==Lines.end())
  111. {
  112. // todo: fill the rest with last value
  113. return;
  114. }
  115. t=Linear(i->x,ni->x,gt,0.0f,1.0f);
  116. if (t<0) t=0;
  117. }
  118. float aa=Linear(0.0f,1.0f,t,ni->y,i->y);
  119. m_EnvSample.Set(n,aa);
  120. }
  121. }
  122. void ComplexEnvelopePlugin::Execute()
  123. {
  124. float temp=0;
  125. bool Freeze=false;
  126. if (m_EnvSample.GetLength()==0) return;
  127. if (GetInput(0,0)>0)
  128. {
  129. if(!m_Triggered)
  130. {
  131. m_Triggered=1.0f;
  132. m_Position=0;
  133. }
  134. }
  135. else
  136. {
  137. if(m_Triggered)
  138. {
  139. m_Triggered=0;
  140. }
  141. }
  142. if (m_Position==-1)
  143. {
  144. GetOutputBuf(0)->Zero();
  145. return;
  146. }
  147. if (!InputExists(1))
  148. {
  149. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  150. {
  151. // see if we've fallen off the end
  152. if (m_Position>m_EnvSample.GetLength())
  153. {
  154. m_Position=-1;
  155. break;
  156. }
  157. SetOutput(0,n,m_EnvSample[m_Position]);
  158. m_Position++;
  159. }
  160. }
  161. else
  162. {
  163. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  164. {
  165. // see if we've fallen off the end
  166. if (m_Position>m_EnvSample.GetLength())
  167. {
  168. m_Position=-1;
  169. break;
  170. }
  171. SetOutput(0,n,m_EnvSample[m_Position]);
  172. SetOutput(1,n,GetInput(1,n)*m_EnvSample[m_Position]);
  173. m_Position++;
  174. }
  175. }
  176. }
  177. void ComplexEnvelopePlugin::StreamOut(ostream &s)
  178. {
  179. s<<m_Version<<" ";
  180. s<<m_Position<<" "<<m_SampleTime<<" "<<m_Bezier<<" ";
  181. s<<m_CVVec.size()<<endl;
  182. for (vector<Vec2>::iterator i=m_CVVec.begin();
  183. i!=m_CVVec.end(); i++)
  184. {
  185. s<<i->x<<" "<<i->y<<endl;
  186. }
  187. }
  188. void ComplexEnvelopePlugin::StreamIn(istream &s)
  189. {
  190. int version;
  191. s>>version;
  192. s>>m_Position>>m_SampleTime>>m_Bezier;
  193. m_CVVec.clear();
  194. int size;
  195. s>>size;
  196. for(int n=0; n<size; n++)
  197. {
  198. Vec2 t;
  199. s>>t.x>>t.y;
  200. m_CVVec.push_back(t);
  201. }
  202. ((ComplexEnvelopePluginGUI*)m_GUI)->SetCVList(m_CVVec);
  203. CVListToEnvelope(m_CVVec);
  204. }