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.

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