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.5KB

  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 <math.h>
  19. #include "MasherPlugin.h"
  20. #include "MasherPluginGUI.h"
  21. #include <FL/Fl_Button.h>
  22. #include "SpiralIcon.xpm"
  23. #define PI 3.141592654
  24. using namespace std;
  25. float RandRange(float L, float H)
  26. {
  27. return ((rand()%10000/10000.0f)*(H-L))+L;
  28. }
  29. extern "C" {
  30. SpiralPlugin* SpiralPlugin_CreateInstance()
  31. {
  32. return new MasherPlugin;
  33. }
  34. char** SpiralPlugin_GetIcon()
  35. {
  36. return SpiralIcon_xpm;
  37. }
  38. int SpiralPlugin_GetID()
  39. {
  40. return 54;
  41. }
  42. string SpiralPlugin_GetGroupName()
  43. {
  44. return "Filters/FX";
  45. }
  46. }
  47. ///////////////////////////////////////////////////////
  48. MasherPlugin::MasherPlugin() :
  49. m_GrainStoreSize(MAX_GRAINSTORE_SIZE),
  50. m_Density(10),
  51. m_Randomness(0),
  52. m_GrainPitch(1),
  53. m_ReadGrain(0),
  54. m_WriteGrain(0)
  55. {
  56. m_PluginInfo.Name="Masher";
  57. m_PluginInfo.Width=120;
  58. m_PluginInfo.Height=140;
  59. m_PluginInfo.NumInputs=3;
  60. m_PluginInfo.NumOutputs=1;
  61. m_PluginInfo.PortTips.push_back("Input");
  62. m_PluginInfo.PortTips.push_back("GrainPitch");
  63. m_PluginInfo.PortTips.push_back("Density");
  64. m_PluginInfo.PortTips.push_back("Output");
  65. m_AudioCH->Register("GrainPitch",&m_GrainPitch);
  66. m_AudioCH->Register("GrainStoreSize",&m_GrainStoreSize);
  67. m_AudioCH->Register("Density",&m_Density);
  68. m_AudioCH->Register("Randomness",&m_Randomness);
  69. }
  70. MasherPlugin::~MasherPlugin()
  71. {
  72. }
  73. PluginInfo &MasherPlugin::Initialise(const HostInfo *Host)
  74. {
  75. return SpiralPlugin::Initialise(Host);
  76. }
  77. SpiralGUIType *MasherPlugin::CreateGUI()
  78. {
  79. return new MasherPluginGUI(m_PluginInfo.Width,
  80. m_PluginInfo.Height,
  81. this,m_AudioCH,m_HostInfo);
  82. }
  83. void MixPitch(Sample &src, Sample &dst, int Pos, float Pitch)
  84. {
  85. float n=0;
  86. int p=Pos;
  87. while (n<src.GetLength() && p<dst.GetLength())
  88. {
  89. if (p>=0) dst.Set(p,dst[p]+src[n]);
  90. n+=Pitch;
  91. p++;
  92. }
  93. }
  94. void MasherPlugin::Reset()
  95. {
  96. ResetPorts();
  97. m_ReadGrain = 0;
  98. m_WriteGrain = 0;
  99. }
  100. void MasherPlugin::Execute()
  101. {
  102. GetOutputBuf(0)->Zero();
  103. if (!InputExists(0)) return;
  104. int n=0;
  105. float s=GetInput(0,0);
  106. int last=0;
  107. bool First=true;
  108. // paste any overlapping grains to the start of the buffer.
  109. for (vector<GrainDesc>::iterator i = m_OverlapVec.begin();
  110. i!=m_OverlapVec.end(); i++)
  111. {
  112. MixPitch(m_GrainStore[i->Grain],*GetOutputBuf(0),i->Pos-m_HostInfo->BUFSIZE,m_GrainPitch);
  113. }
  114. m_OverlapVec.clear();
  115. // chop up the buffer and put the grains in the grainstore
  116. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  117. {
  118. if ((s<0 && GetInput(0,n)>0) || (s>0 && GetInput(0,n)<0))
  119. {
  120. // chop the bits between zero crossings
  121. if (!First)
  122. {
  123. GetInput(0)->GetRegion(m_GrainStore[m_WriteGrain%m_GrainStoreSize],last,n);
  124. m_WriteGrain++;
  125. }
  126. else First=false;
  127. last=n;
  128. s=GetInput(0,n);
  129. }
  130. }
  131. int NextGrain=0;
  132. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  133. {
  134. int Density = m_Density;
  135. if (InputExists(2)) Density=(int)(Density*GetInput(2,n));
  136. if(n>=NextGrain || rand()%1000<Density)
  137. {
  138. int GrainNum = m_ReadGrain%m_GrainStoreSize;
  139. float Pitch=m_GrainPitch;
  140. if (InputExists(1)) Pitch*=fabs(GetInput(1,n));
  141. MixPitch(m_GrainStore[GrainNum],*GetOutputBuf(0),n,Pitch);
  142. int GrainLength = (int)(m_GrainStore[GrainNum].GetLength()*Pitch);
  143. NextGrain=n+m_GrainStore[GrainNum].GetLength();
  144. // if this grain overlaps the buffer
  145. if (n+GrainLength>m_HostInfo->BUFSIZE)
  146. {
  147. GrainDesc new_grain;
  148. new_grain.Pos=n;
  149. new_grain.Grain=GrainNum;
  150. m_OverlapVec.push_back(new_grain);
  151. }
  152. if (m_Randomness) m_ReadGrain+=1+rand()%m_Randomness;
  153. else m_ReadGrain++;
  154. }
  155. }
  156. }
  157. void MasherPlugin::Randomise()
  158. {
  159. }
  160. void MasherPlugin::StreamOut(ostream &s)
  161. {
  162. s<<m_Version<<" ";
  163. s<<m_GrainStoreSize<<" "<<m_Density<<" "<<m_Randomness<<" "<<m_GrainPitch<<" ";
  164. }
  165. void MasherPlugin::StreamIn(istream &s)
  166. {
  167. int version;
  168. s>>version;
  169. s>>m_GrainStoreSize>>m_Density>>m_Randomness>>m_GrainPitch;
  170. }