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.

193 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. 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::Execute()
  95. {
  96. GetOutputBuf(0)->Zero();
  97. if (!InputExists(0)) return;
  98. int n=0;
  99. float s=GetInput(0,0);
  100. int last=0;
  101. bool First=true;
  102. // paste any overlapping grains to the start of the buffer.
  103. for (vector<GrainDesc>::iterator i = m_OverlapVec.begin();
  104. i!=m_OverlapVec.end(); i++)
  105. {
  106. MixPitch(m_GrainStore[i->Grain],*GetOutputBuf(0),i->Pos-m_HostInfo->BUFSIZE,m_GrainPitch);
  107. }
  108. m_OverlapVec.clear();
  109. // chop up the buffer and put the grains in the grainstore
  110. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  111. {
  112. if ((s<0 && GetInput(0,n)>0) || (s>0 && GetInput(0,n)<0))
  113. {
  114. // chop the bits between zero crossings
  115. if (!First)
  116. {
  117. GetInput(0)->GetRegion(m_GrainStore[m_WriteGrain%m_GrainStoreSize],last,n);
  118. m_WriteGrain++;
  119. }
  120. else First=false;
  121. last=n;
  122. s=GetInput(0,n);
  123. }
  124. }
  125. int NextGrain=0;
  126. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  127. {
  128. int Density = m_Density;
  129. if (InputExists(2)) Density=(int)(Density*GetInput(2,n));
  130. if(n>=NextGrain || rand()%1000<Density)
  131. {
  132. int GrainNum = m_ReadGrain%m_GrainStoreSize;
  133. float Pitch=m_GrainPitch;
  134. if (InputExists(1)) Pitch*=fabs(GetInput(1,n));
  135. MixPitch(m_GrainStore[GrainNum],*GetOutputBuf(0),n,Pitch);
  136. int GrainLength = (int)(m_GrainStore[GrainNum].GetLength()*Pitch);
  137. NextGrain=n+m_GrainStore[GrainNum].GetLength();
  138. // if this grain overlaps the buffer
  139. if (n+GrainLength>m_HostInfo->BUFSIZE)
  140. {
  141. GrainDesc new_grain;
  142. new_grain.Pos=n;
  143. new_grain.Grain=GrainNum;
  144. m_OverlapVec.push_back(new_grain);
  145. }
  146. if (m_Randomness) m_ReadGrain+=1+rand()%m_Randomness;
  147. else m_ReadGrain++;
  148. }
  149. }
  150. }
  151. void MasherPlugin::Randomise()
  152. {
  153. }
  154. void MasherPlugin::StreamOut(ostream &s)
  155. {
  156. s<<m_Version<<" ";
  157. s<<m_GrainStoreSize<<" "<<m_Density<<" "<<m_Randomness<<" "<<m_GrainPitch<<" ";
  158. }
  159. void MasherPlugin::StreamIn(istream &s)
  160. {
  161. int version;
  162. s>>version;
  163. s>>m_GrainStoreSize>>m_Density>>m_Randomness>>m_GrainPitch;
  164. }