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.

136 lines
4.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 "../SpiralPlugin.h"
  19. #include "../../RiffWav.h"
  20. #include <FL/Fl_Pixmap.H>
  21. #include <map>
  22. #include <glib.h>
  23. #include <jack/jack.h>
  24. typedef jack_default_audio_sample_t sample_t;
  25. #ifndef JackPLUGIN
  26. #define JackPLUGIN
  27. const int NUM_INPUTS = 8;
  28. const int NUM_OUTPUTS = 8;
  29. const int MAX_INPUTPORTS = 256;
  30. const int MAX_OUTPUTPORTS = 256;
  31. class JackClient
  32. {
  33. public:
  34. static JackClient *Get() { if(!m_Singleton) m_Singleton=new JackClient; return m_Singleton; }
  35. static void PackUpAndGoHome() { if(m_Singleton) { delete m_Singleton; m_Singleton=NULL; } }
  36. bool Attach();
  37. void Detach();
  38. bool IsAttached() { return m_Attached; }
  39. void SetCallback(void(*Run)(void*, bool m),void *Context) { RunCallback=Run; RunContext=Context; }
  40. void GetPortNames(vector<string> &InputNames,vector<string> &OutputNames);
  41. void ConnectInput(int n, const string &JackPort);
  42. void ConnectOutput(int n, const string &JackPort);
  43. void DisconnectInput(int n);
  44. void DisconnectOutput(int n);
  45. string GetInputName(int ID) { return m_InputPortMap[ID]->Name; }
  46. string GetOutputName(int ID) { return m_OutputPortMap[ID]->Name; }
  47. void SetInputBuf(int ID, float* s);
  48. void SetOutputBuf(int ID, float* s);
  49. protected:
  50. JackClient();
  51. ~JackClient();
  52. static int Process(jack_nframes_t nframes, void *o);
  53. static int OnBufSizeChange(long unsigned int n, void *o);
  54. static int OnSRateChange(long unsigned int n, void *o);
  55. static void OnJackShutdown(void *o);
  56. private:
  57. class JackPort
  58. {
  59. public:
  60. JackPort::JackPort() :
  61. Connected(false),Buf(NULL),Port(NULL) {}
  62. string Name;
  63. bool Connected;
  64. float* Buf;
  65. jack_port_t* Port;
  66. string ConnectedTo;
  67. };
  68. static JackClient* m_Singleton;
  69. static jack_client_t* m_Client;
  70. static map<int,JackPort*> m_InputPortMap;
  71. static map<int,JackPort*> m_OutputPortMap;
  72. static long unsigned int m_BufferSize;
  73. static long unsigned int m_SampleRate;
  74. static bool m_Attached;
  75. static void(*RunCallback)(void*, bool m);
  76. static void *RunContext;
  77. };
  78. ///////////////////////////////////////////////////
  79. class JackPlugin : public SpiralPlugin
  80. {
  81. public:
  82. JackPlugin();
  83. virtual ~JackPlugin();
  84. virtual PluginInfo& Initialise(const HostInfo *Host);
  85. virtual SpiralGUIType* CreateGUI();
  86. virtual void Execute();
  87. virtual void ExecuteCommands();
  88. virtual void StreamOut(ostream &s) {}
  89. virtual void StreamIn(istream &s) {}
  90. enum GUICommands{NONE,ATTACH,DETACH,CONNECTINPUT,CONNECTOUTPUT,UPDATE_NAMES};
  91. struct GUIArgs
  92. {
  93. int Num;
  94. char Port[256];
  95. };
  96. private:
  97. GUIArgs m_GUIArgs;
  98. // slightly clumsy, but we have to share this data with the gui
  99. int m_NumInputPortNames;
  100. char m_InputPortNames[MAX_INPUTPORTS][256];
  101. int m_NumOutputPortNames;
  102. char m_OutputPortNames[MAX_OUTPUTPORTS][256];
  103. void Attach() { JackClient::Get()->Attach(); }
  104. void Detach() { JackClient::Get()->Detach(); }
  105. void GetPortNames(vector<string> &InputNames,vector<string> &OutputNames) { JackClient::Get()->GetPortNames(InputNames,OutputNames); }
  106. void ConnectInput(int n, const string &JackPort) { JackClient::Get()->ConnectInput(n,JackPort); }
  107. void ConnectOutput(int n, const string &JackPort) { JackClient::Get()->ConnectOutput(n,JackPort); }
  108. static int m_RefCount;
  109. static int m_NoExecuted;
  110. bool m_UpdateNames;
  111. bool m_Connected;
  112. };
  113. #endif