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.

239 lines
4.8KB

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