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.

162 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 "NoteSnapPlugin.h"
  19. #include "NoteSnapPluginGUI.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 NoteSnapPlugin;
  28. }
  29. char** SpiralPlugin_GetIcon()
  30. {
  31. return SpiralIcon_xpm;
  32. }
  33. int SpiralPlugin_GetID()
  34. {
  35. return 0x0018;
  36. }
  37. string SpiralPlugin_GetGroupName()
  38. {
  39. return "Control";
  40. }
  41. }
  42. ///////////////////////////////////////////////////////
  43. NoteSnapPlugin::NoteSnapPlugin() :
  44. m_Out(0)
  45. {
  46. m_PluginInfo.Name="Note Snap";
  47. m_PluginInfo.Width=80;
  48. m_PluginInfo.Height=80;
  49. m_PluginInfo.NumInputs=1;
  50. m_PluginInfo.NumOutputs=2;
  51. m_PluginInfo.PortTips.push_back("Input");
  52. m_PluginInfo.PortTips.push_back("Output");
  53. m_PluginInfo.PortTips.push_back("Changed Trigger");
  54. for (int n=0; n<12; n++)
  55. {
  56. m_Filter[n]=true;
  57. }
  58. m_AudioCH->Register("Note",&m_Note);
  59. }
  60. NoteSnapPlugin::~NoteSnapPlugin()
  61. {
  62. }
  63. PluginInfo &NoteSnapPlugin::Initialise(const HostInfo *Host)
  64. {
  65. return SpiralPlugin::Initialise(Host);
  66. }
  67. SpiralGUIType *NoteSnapPlugin::CreateGUI()
  68. {
  69. return new NoteSnapPluginGUI(m_PluginInfo.Width,
  70. m_PluginInfo.Height,
  71. this,m_AudioCH,m_HostInfo);
  72. }
  73. void NoteSnapPlugin::Reset()
  74. {
  75. ResetPorts();
  76. m_Out = 0;
  77. }
  78. void NoteSnapPlugin::Execute()
  79. {
  80. float Freq=0;
  81. for (int n=0; n<m_HostInfo->BUFSIZE; n++)
  82. {
  83. Freq=GetInputPitch(0,n);
  84. SetOutput(1,n,0);
  85. if (Freq!=m_LastFreq) // if it's changed
  86. {
  87. for (int i=0; i<131; i++) // for every note
  88. {
  89. if (m_Filter[(i+1)%12] && Freq>=NoteTable[i] && Freq<NoteTable[i+1])
  90. {
  91. m_Out=NoteTable[i];
  92. if (i!=m_LastNote)
  93. {
  94. SetOutput(1,n,1);
  95. m_LastNote=i;
  96. }
  97. }
  98. }
  99. }
  100. m_LastFreq=Freq;
  101. SetOutputPitch(0,n,m_Out);
  102. }
  103. }
  104. void NoteSnapPlugin::ExecuteCommands()
  105. {
  106. if (m_AudioCH->IsCommandWaiting())
  107. {
  108. switch (m_AudioCH->GetCommand())
  109. {
  110. case NOTE_ON : m_Filter[m_Note]=true; break;
  111. case NOTE_OFF : m_Filter[m_Note]=false; break;
  112. }
  113. }
  114. }
  115. void NoteSnapPlugin::StreamOut(ostream &s)
  116. {
  117. s<<m_Version<<endl;
  118. for (int n=0; n<12; n++)
  119. {
  120. s<<m_Filter[n]<<" ";
  121. }
  122. }
  123. void NoteSnapPlugin::StreamIn(istream &s)
  124. {
  125. // Pre-version 1 - check for blank line
  126. if (s.peek() == 10) {
  127. for (int n=0; n<12; n++)
  128. {
  129. // Use default (no notes filtered)
  130. m_Filter[n] = 1;
  131. }
  132. } else {
  133. int version;
  134. s>>version;
  135. for (int n=0; n<12; n++)
  136. {
  137. s>>m_Filter[n];
  138. }
  139. }
  140. }