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.

149 lines
4.0KB

  1. /* SpiralSound
  2. * Copyleft (C) 2003 Andy Preston <andy@clublinux.co.uk>
  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 "TranslatePlugin.h"
  19. #include "TranslatePluginGUI.h"
  20. #include "SpiralIcon.xpm"
  21. #include "../../NoteTable.h"
  22. using namespace std;
  23. extern "C" {
  24. SpiralPlugin* SpiralPlugin_CreateInstance () { return new TranslatePlugin; }
  25. char** SpiralPlugin_GetIcon () { return SpiralIcon_xpm; }
  26. int SpiralPlugin_GetID () { return 121; }
  27. string SpiralPlugin_GetGroupName() { return "Control"; }
  28. }
  29. ///////////////////////////////////////////////////////
  30. float TranslatePass::Translate (float i) {
  31. return i;
  32. }
  33. float TranslateNoteToFreq::Translate (float i) {
  34. return (i + 1.0f) * (MAX_FREQ / 2);
  35. }
  36. float TranslateFreqToNote::Translate (float i) {
  37. return (i / MAX_FREQ * 2) - 1.0f;
  38. }
  39. float TranslateNoteToVolt::Translate (float i) {
  40. float Freq = (i + 1.0f) * (MAX_FREQ / 2);
  41. float Dif, MinDif = 30000;
  42. int Match=0;
  43. for (int c=0; c<131; c++) {
  44. Dif = fabs (NoteTable[c] - Freq);
  45. if (Dif > MinDif) break;
  46. MinDif = Dif;
  47. Match = c;
  48. }
  49. return (float)Match;
  50. }
  51. float TranslateVoltToNote::Translate (float i) {
  52. return (NoteTable[int (i)] / MAX_FREQ * 2) - 1.0f;
  53. }
  54. ///////////////////////////////////////////////////////
  55. TranslatePlugin::TranslatePlugin () :
  56. m_Method (tr_Pass),
  57. m_Translator (NULL)
  58. {
  59. m_PluginInfo.Name = "Translate";
  60. m_PluginInfo.Width = 150;
  61. m_PluginInfo.Height = 60;
  62. m_PluginInfo.NumInputs = 1;
  63. m_PluginInfo.NumOutputs = 1;
  64. m_PluginInfo.PortTips.push_back ("Input");
  65. m_PluginInfo.PortTips.push_back ("Output");
  66. m_AudioCH->Register ("Method", &m_Method);
  67. SetUpTranslatorClass ();
  68. }
  69. TranslatePlugin::~TranslatePlugin () {
  70. if (m_Translator) delete m_Translator;
  71. }
  72. PluginInfo &TranslatePlugin::Initialise (const HostInfo *Host) {
  73. return SpiralPlugin::Initialise (Host);
  74. }
  75. SpiralGUIType *TranslatePlugin::CreateGUI() {
  76. return new TranslatePluginGUI (m_PluginInfo.Width, m_PluginInfo.Height, this, m_AudioCH, m_HostInfo);
  77. }
  78. void TranslatePlugin::SetUpTranslatorClass (void) {
  79. if (m_Translator) {
  80. delete m_Translator;
  81. m_Translator = NULL;
  82. }
  83. switch (m_Method) {
  84. case tr_Pass:
  85. m_Translator = new TranslatePass;
  86. break;
  87. case tr_NoteToFreq:
  88. m_Translator = new TranslateNoteToFreq;
  89. break;
  90. case tr_FreqToNote:
  91. m_Translator = new TranslateFreqToNote;
  92. break;
  93. case tr_NoteToVolt:
  94. m_Translator = new TranslateNoteToVolt;
  95. break;
  96. case tr_VoltToNote:
  97. m_Translator = new TranslateVoltToNote;
  98. break;
  99. }
  100. }
  101. void TranslatePlugin::ExecuteCommands () {
  102. if (m_AudioCH->IsCommandWaiting ()) {
  103. switch (m_AudioCH->GetCommand()) {
  104. case SETMETHOD:
  105. SetUpTranslatorClass ();
  106. break;
  107. }
  108. }
  109. }
  110. void TranslatePlugin::Execute () {
  111. for (int n=0; n<m_HostInfo->BUFSIZE; n++) {
  112. SetOutput (0, n, m_Translator->Translate (GetInput (0, n)));
  113. }
  114. }
  115. void TranslatePlugin::StreamOut (ostream &s) {
  116. s << m_Version << endl;
  117. s << m_Method;
  118. }
  119. void TranslatePlugin::StreamIn (istream &s) {
  120. int version;
  121. s >> version;
  122. s >> m_Method;
  123. SetUpTranslatorClass ();
  124. }