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.

147 lines
3.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 "DelayPlugin.h"
  19. #include "DelayPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. static const float MAX_DELAY=1.0f;
  23. extern "C" {
  24. SpiralPlugin* SpiralPlugin_CreateInstance()
  25. {
  26. return new DelayPlugin;
  27. }
  28. char** SpiralPlugin_GetIcon()
  29. {
  30. return SpiralIcon_xpm;
  31. }
  32. int SpiralPlugin_GetID()
  33. {
  34. return 0x000f;
  35. }
  36. string SpiralPlugin_GetGroupName()
  37. {
  38. return "Delay/Sampling";
  39. }
  40. }
  41. ///////////////////////////////////////////////////////
  42. DelayPlugin::DelayPlugin() :
  43. m_Delay(0.75),
  44. m_Mix(0.4),
  45. m_ReadHeadPos(0),
  46. m_WriteHeadPos(0)
  47. {
  48. m_PluginInfo.Name="Delay";
  49. m_PluginInfo.Width=120;
  50. m_PluginInfo.Height=112;
  51. m_PluginInfo.NumInputs=3;
  52. m_PluginInfo.NumOutputs=1;
  53. m_PluginInfo.PortTips.push_back("Input");
  54. m_PluginInfo.PortTips.push_back("Delay CV");
  55. m_PluginInfo.PortTips.push_back("ReadHead CV");
  56. m_PluginInfo.PortTips.push_back("Output");
  57. m_AudioCH->Register("Delay",&m_Delay);
  58. m_AudioCH->Register("Mix",&m_Mix);
  59. }
  60. DelayPlugin::~DelayPlugin()
  61. {
  62. }
  63. PluginInfo &DelayPlugin::Initialise(const HostInfo *Host)
  64. {
  65. PluginInfo& Info = SpiralPlugin::Initialise(Host);
  66. m_Buffer.Allocate((int)(m_HostInfo->SAMPLERATE*MAX_DELAY));
  67. return Info;
  68. }
  69. SpiralGUIType *DelayPlugin::CreateGUI()
  70. {
  71. return new DelayPluginGUI(m_PluginInfo.Width,
  72. m_PluginInfo.Height,
  73. this,m_AudioCH,m_HostInfo);
  74. }
  75. void DelayPlugin::Execute()
  76. {
  77. int Delay;
  78. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  79. {
  80. Delay=(int)((m_Delay+GetInput(1,n)*0.5f)*(float)m_HostInfo->SAMPLERATE);
  81. if (Delay>=MAX_DELAY*m_HostInfo->SAMPLERATE)
  82. {
  83. Delay=(int)(MAX_DELAY*m_HostInfo->SAMPLERATE)-1;
  84. }
  85. if (Delay<0) Delay=0;
  86. if (m_ReadHeadPos<0 || m_ReadHeadPos>=MAX_DELAY*m_HostInfo->SAMPLERATE)
  87. cerr<<"read agh! "<<m_ReadHeadPos<<endl;
  88. if (m_WriteHeadPos<0 || m_WriteHeadPos>=MAX_DELAY*m_HostInfo->SAMPLERATE)
  89. cerr<<"write agh! "<<m_WriteHeadPos<<endl;
  90. // Read from the buffer
  91. SetOutput(0,n,m_Buffer[m_ReadHeadPos]+m_Mix*GetInput(0,n));
  92. // Write to the buffer
  93. m_Buffer.Set((int)m_WriteHeadPos,GetInput(0,n));
  94. m_WriteHeadPos++;
  95. m_ReadHeadPos=m_WriteHeadPos+GetInput(2,n)*Delay;
  96. if (m_ReadHeadPos<0) m_ReadHeadPos=Delay+m_ReadHeadPos;
  97. if (m_WriteHeadPos<0) m_WriteHeadPos=Delay+m_WriteHeadPos;
  98. if (Delay>0)
  99. {
  100. if (m_ReadHeadPos>=Delay) m_ReadHeadPos=m_ReadHeadPos-Delay;
  101. if (m_WriteHeadPos>=Delay) m_WriteHeadPos=m_WriteHeadPos-Delay;
  102. }
  103. else
  104. {
  105. m_ReadHeadPos=0;
  106. m_WriteHeadPos=0;
  107. }
  108. }
  109. }
  110. void DelayPlugin::Randomise()
  111. {
  112. }
  113. void DelayPlugin::StreamOut(ostream &s)
  114. {
  115. s<<m_Version<<" "<<m_Delay<<" "<<m_Mix<<" ";
  116. }
  117. void DelayPlugin::StreamIn(istream &s)
  118. {
  119. int version;
  120. s>>version;
  121. s>>m_Delay>>m_Mix;
  122. }