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.

191 lines
4.4KB

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