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.

154 lines
3.1KB

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