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