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.

176 lines
5.8KB

  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 <FL/Fl_Pixmap.H>
  20. #include <jack/jack.h>
  21. using namespace std;
  22. typedef jack_default_audio_sample_t sample_t;
  23. #ifndef JackPLUGIN
  24. #define JackPLUGIN
  25. const int MAX_PORTS = 64;
  26. const int MIN_PORTS = 2;
  27. class JackClient
  28. {
  29. public:
  30. JackClient();
  31. virtual ~JackClient();
  32. void AddInputPort(int NewPortNumber);
  33. void AddOutputPort(int NewPortNumber);
  34. void RemoveInputPort(int PortNumber);
  35. void RemoveOutputPort(int PortNumber);
  36. bool Attach();
  37. void Detach();
  38. bool IsAttached() { return m_Attached; }
  39. void SetAttached(bool Attached) { m_Attached = Attached; }
  40. void SetCallback(void(*Run)(void*, bool m),void *Context) { RunCallback=Run; RunContext=Context; }
  41. void GetPortNames(std::vector<std::string> &InputNames,std::vector<std::string> &OutputNames);
  42. void ConnectInput(int n, const std::string &JackPort);
  43. void ConnectOutput(int n, const std::string &JackPort);
  44. void DisconnectInput(int n);
  45. void DisconnectOutput(int n);
  46. std::string GetInputName(int ID) { return m_InputPortMap[ID]->Name; }
  47. std::string GetOutputName(int ID) { return m_OutputPortMap[ID]->Name; }
  48. void SetInputBuf(int ID, float* s);
  49. void SetOutputBuf(int ID, float* s);
  50. int GetJackInstanceID() { return m_JackInstanceID; }
  51. void SetJackInstanceID(int JackInstanceID) { m_JackInstanceID=JackInstanceID; }
  52. int GetBufferSize() { return m_BufferSize; }
  53. void SetBufferSize(jack_nframes_t BufferSize) { m_BufferSize=BufferSize; }
  54. int GetSampleRate() { return m_BufferSize; }
  55. void SetSampleRate(jack_nframes_t SampleRate) { m_SampleRate=SampleRate; }
  56. int GetJackInputCount() { return m_JackInputCount; }
  57. void SetJackInputCount(int JackInputCount) { m_JackInputCount=JackInputCount; }
  58. int GetJackOutputCount() { return m_JackOutputCount; }
  59. void SetJackOutputCount(int JackOutputCount) { m_JackOutputCount=JackOutputCount; }
  60. class JackPort
  61. {
  62. public:
  63. JackPort::JackPort() :
  64. Connected(false),Buf(NULL),Port(NULL) {}
  65. int PortNo;
  66. std::string Name;
  67. bool Connected;
  68. float* Buf;
  69. jack_port_t* Port;
  70. std::string ConnectedTo;
  71. };
  72. jack_client_t* m_Client;
  73. std::map<int,JackPort*> m_InputPortMap;
  74. std::map<int,JackPort*> m_OutputPortMap;
  75. //// Kludge for GUI ////
  76. bool CheckingPortChanges;
  77. std::vector<JackPort*> m_OutputPortsChanged;
  78. std::vector<JackPort*> m_InputPortsChanged;
  79. //// inline Callbacks ////
  80. inline void JackProcess_i(jack_nframes_t nframes);
  81. inline void SampleRateChange_i(jack_nframes_t nframes);
  82. inline void JackShutdown_i();
  83. //// static Callbacks ////
  84. static int JackProcess(jack_nframes_t nframes, void *jack_client) { ((JackClient *)jack_client)->JackProcess_i(nframes); return 0;}
  85. static int SampleRateChange(jack_nframes_t nframes, void *jack_client) { ((JackClient *)jack_client)->SampleRateChange_i(nframes); return 0;}
  86. static void JackShutdown(void *jack_client) { ((JackClient *)jack_client)->JackShutdown_i();}
  87. private:
  88. jack_nframes_t m_BufferSize;
  89. jack_nframes_t m_SampleRate;
  90. bool m_Attached;
  91. int m_JackInputCount;
  92. int m_JackOutputCount;
  93. int m_JackInstanceID;
  94. static int JackProcessInstanceID;
  95. void(*RunCallback)(void*, bool m);
  96. void *RunContext;
  97. };
  98. ///////////////////////////////////////////////////
  99. class JackPlugin : public SpiralPlugin
  100. {
  101. public:
  102. JackPlugin();
  103. virtual ~JackPlugin();
  104. virtual PluginInfo& Initialise(const HostInfo *Host);
  105. virtual SpiralGUIType* CreateGUI();
  106. virtual void Execute();
  107. virtual void ExecuteCommands();
  108. virtual void StreamOut(std::ostream &s);
  109. virtual void StreamIn(std::istream &s);
  110. JackClient *GetJackClient() { return m_JackClient; }
  111. void SetNumberPorts (int nInputs, int nOutputs);
  112. enum GUICommands{NONE,UPDATE_NAMES,SET_PORT_COUNT,CHECK_PORT_CHANGES};
  113. struct GUIArgs
  114. {
  115. int NumInputs;
  116. int NumOutputs;
  117. char Port[256];
  118. };
  119. void Attach() { m_JackClient->Attach(); }
  120. void Detach() { m_JackClient->Detach(); }
  121. private:
  122. const HostInfo* host;
  123. GUIArgs m_GUIArgs;
  124. int m_Version;
  125. // slightly clumsy, but we have to share this data with the gui
  126. int m_NumInputPortNames;
  127. char m_InputPortNames[MAX_PORTS][256];
  128. int m_NumOutputPortNames;
  129. char m_OutputPortNames[MAX_PORTS][256];
  130. void GetPortNames(std::vector<std::string> &InputNames,std::vector<std::string> &OutputNames) { m_JackClient->GetPortNames(InputNames,OutputNames); }
  131. void ConnectInput(int n, const std::string &JackPort) { m_JackClient->ConnectInput(n,JackPort); }
  132. void ConnectOutput(int n, const std::string &JackPort) { m_JackClient->ConnectOutput(n,JackPort); }
  133. void CreatePorts (int nInputs, int nOutputs, bool );
  134. bool m_UpdateNames;
  135. bool m_Connected;
  136. JackClient *m_JackClient;
  137. int m_JackInstanceID;
  138. //clunky work-around for unique ID
  139. static int JackInstanceCount;
  140. };
  141. #endif