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.

148 lines
4.0KB

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