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.

190 lines
6.3KB

  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. long int JackSampleRate() { return m_JackSampleRate; }
  61. long int JackBufferSize() { return m_JackBufferSize; }
  62. class JackPort
  63. {
  64. public:
  65. JackPort() :
  66. Connected(false),Buf(NULL),Port(NULL) {}
  67. int PortNo;
  68. std::string Name;
  69. bool Connected;
  70. float* Buf;
  71. jack_port_t* Port;
  72. std::string ConnectedTo;
  73. };
  74. jack_client_t* m_Client;
  75. std::map<int,JackPort*> m_InputPortMap;
  76. std::map<int,JackPort*> m_OutputPortMap;
  77. //// Kludge for GUI ////
  78. bool CheckingPortChanges;
  79. std::vector<JackPort*> m_OutputPortsChanged;
  80. std::vector<JackPort*> m_InputPortsChanged;
  81. //// inline Callbacks ////
  82. inline void JackProcess_i(jack_nframes_t nframes);
  83. inline void SampleRateChange_i(jack_nframes_t nframes);
  84. inline void JackShutdown_i();
  85. //// static Callbacks ////
  86. static int JackProcess(jack_nframes_t nframes, void *jack_client) { ((JackClient *)jack_client)->JackProcess_i(nframes); return 0;}
  87. static int SampleRateChange(jack_nframes_t nframes, void *jack_client) { ((JackClient *)jack_client)->SampleRateChange_i(nframes); return 0;}
  88. static void JackShutdown(void *jack_client) { ((JackClient *)jack_client)->JackShutdown_i();}
  89. private:
  90. jack_nframes_t m_BufferSize;
  91. jack_nframes_t m_SampleRate;
  92. bool m_Attached;
  93. int m_JackInputCount;
  94. int m_JackOutputCount;
  95. int m_JackInstanceID;
  96. long int m_JackSampleRate;
  97. long int m_JackBufferSize;
  98. static int JackProcessInstanceID;
  99. void(*RunCallback)(void*, bool m);
  100. void *RunContext;
  101. };
  102. ///////////////////////////////////////////////////
  103. class JackPlugin : public AudioDriver
  104. {
  105. public:
  106. JackPlugin();
  107. virtual ~JackPlugin();
  108. virtual PluginInfo& Initialise(const HostInfo *Host);
  109. virtual SpiralGUIType* CreateGUI();
  110. /* General Plugin Function */
  111. virtual void Execute();
  112. virtual void ExecuteCommands();
  113. virtual bool Kill();
  114. virtual void Reset();
  115. /* Audio Driver Specific Functions */
  116. virtual bool IsAudioDriver() { return true; }
  117. virtual AudioProcessType ProcessType() { return AudioDriver::ALWAYS; }
  118. virtual void ProcessAudio();
  119. /* Jack Plugin Specific Functions */
  120. JackClient *GetJackClient() { return m_JackClient; }
  121. void SetNumberPorts (int nInputs, int nOutputs);
  122. enum GUICommands{NONE,UPDATE_NAMES,SET_PORT_COUNT,CHECK_PORT_CHANGES};
  123. struct GUIArgs
  124. {
  125. int NumInputs;
  126. int NumOutputs;
  127. char Port[256];
  128. };
  129. void Attach() { m_JackClient->Attach(); }
  130. void Detach() { m_JackClient->Detach(); }
  131. /* Jack Plugin Streaming - soon to be obsolete and for backward compatibility only*/
  132. virtual void StreamOut(std::ostream &s);
  133. virtual void StreamIn(std::istream &s);
  134. private:
  135. GUIArgs m_GUIArgs;
  136. int m_Version;
  137. // slightly clumsy, but we have to share this data with the gui
  138. int m_NumInputPortNames;
  139. char m_InputPortNames[MAX_PORTS][256];
  140. int m_NumOutputPortNames;
  141. char m_OutputPortNames[MAX_PORTS][256];
  142. void GetPortNames(std::vector<std::string> &InputNames,std::vector<std::string> &OutputNames) { m_JackClient->GetPortNames(InputNames,OutputNames); }
  143. void ConnectInput(int n, const std::string &JackPort) { m_JackClient->ConnectInput(n,JackPort); }
  144. void ConnectOutput(int n, const std::string &JackPort) { m_JackClient->ConnectOutput(n,JackPort); }
  145. void CreatePorts (int nInputs, int nOutputs, bool );
  146. bool m_UpdateNames;
  147. bool m_Connected;
  148. JackClient *m_JackClient;
  149. int m_JackInstanceID;
  150. //clunky work-around for unique ID
  151. static int JackInstanceCount;
  152. };
  153. #endif