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.

159 lines
3.2KB

  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 "BeatMatchPlugin.h"
  19. #include "BeatMatchPluginGUI.h"
  20. #include <FL/Fl_Button.h>
  21. #include "SpiralIcon.xpm"
  22. #include "../../NoteTable.h"
  23. using namespace std;
  24. extern "C" {
  25. SpiralPlugin* SpiralPlugin_CreateInstance()
  26. {
  27. return new BeatMatchPlugin;
  28. }
  29. char** SpiralPlugin_GetIcon()
  30. {
  31. return SpiralIcon_xpm;
  32. }
  33. int SpiralPlugin_GetID()
  34. {
  35. return 48;
  36. }
  37. string SpiralPlugin_GetGroupName()
  38. {
  39. return "Maths/Logic";
  40. }
  41. }
  42. ///////////////////////////////////////////////////////
  43. BeatMatchPlugin::BeatMatchPlugin() :
  44. m_Triggered(false),
  45. m_EstimatedDuration(100),
  46. m_BeatTime(100),
  47. m_NextBeat(100),
  48. m_OutputLevel(1.0f),
  49. m_Sensitivity(0.5f)
  50. {
  51. m_PluginInfo.Name="BeatMatch";
  52. m_PluginInfo.Width=80;
  53. m_PluginInfo.Height=80;
  54. m_PluginInfo.NumInputs=1;
  55. m_PluginInfo.NumOutputs=1;
  56. m_PluginInfo.PortTips.push_back("Input");
  57. m_PluginInfo.PortTips.push_back("Output");
  58. m_AudioCH->Register("Sensitivity",&m_Sensitivity);
  59. }
  60. BeatMatchPlugin::~BeatMatchPlugin()
  61. {
  62. }
  63. PluginInfo &BeatMatchPlugin::Initialise(const HostInfo *Host)
  64. {
  65. return SpiralPlugin::Initialise(Host);
  66. }
  67. SpiralGUIType *BeatMatchPlugin::CreateGUI()
  68. {
  69. return new BeatMatchPluginGUI(m_PluginInfo.Width,
  70. m_PluginInfo.Height,
  71. this,m_AudioCH,m_HostInfo);
  72. }
  73. void BeatMatchPlugin::Reset()
  74. {
  75. ResetPorts();
  76. m_Triggered = false;
  77. m_EstimatedDuration = 100;
  78. m_BeatTime = 100;
  79. m_NextBeat = 100;
  80. m_OutputLevel = 1.0f;
  81. }
  82. void BeatMatchPlugin::Execute()
  83. {
  84. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  85. {
  86. bool Triggered=false;
  87. if (GetInput(0,n)>0)
  88. {
  89. if(!m_Triggered)
  90. {
  91. m_Triggered=true;
  92. Triggered=true;
  93. }
  94. }
  95. else
  96. {
  97. if (m_Triggered)
  98. {
  99. m_Triggered=false;
  100. Triggered=true;
  101. }
  102. }
  103. if (Triggered)
  104. {
  105. // adjust estimated duration
  106. // error = m_BeatTime
  107. m_EstimatedDuration-=(int)(m_BeatTime*m_Sensitivity);
  108. m_BeatTime=m_EstimatedDuration;
  109. // push the sync closer
  110. int HalfBeat = m_EstimatedDuration/2;
  111. if (m_NextBeat<HalfBeat) m_NextBeat-=(int)(HalfBeat*m_Sensitivity);
  112. else m_NextBeat+=(int)(HalfBeat*m_Sensitivity);
  113. }
  114. if (m_NextBeat<=0)
  115. {
  116. m_NextBeat=m_EstimatedDuration;
  117. m_OutputLevel=-m_OutputLevel;
  118. }
  119. m_NextBeat--;
  120. m_BeatTime--;
  121. SetOutput(0,n,m_OutputLevel);
  122. }
  123. }
  124. void BeatMatchPlugin::ExecuteCommands()
  125. {
  126. }
  127. void BeatMatchPlugin::StreamOut(ostream &s)
  128. {
  129. s<<m_Version<<endl;
  130. s<<m_Sensitivity<<" ";
  131. }
  132. void BeatMatchPlugin::StreamIn(istream &s)
  133. {
  134. int version;
  135. s>>version;
  136. s>>m_Sensitivity;
  137. }