DISTRHO Plugin Framework
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.

434 lines
12KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation.
  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 Lesser General Public License for more details.
  13. *
  14. * For a full copy of the license see the LGPL.txt file
  15. */
  16. #include "DistrhoPluginInternal.hpp"
  17. #if ! DISTRHO_PLUGIN_HAS_UI
  18. # error JACK export requires an UI
  19. #endif
  20. #include "DistrhoUIInternal.hpp"
  21. #include "jack/jack.h"
  22. #include "jack/midiport.h"
  23. #include "jack/transport.h"
  24. // -----------------------------------------------------------------------
  25. START_NAMESPACE_DISTRHO
  26. #if ! DISTRHO_PLUGIN_WANT_STATE
  27. static const setStateFunc setStateCallback = nullptr;
  28. #endif
  29. // -----------------------------------------------------------------------
  30. class PluginJack : public IdleCallback
  31. {
  32. public:
  33. PluginJack(jack_client_t* const client)
  34. : fPlugin(),
  35. fUI(this, 0, nullptr, setParameterValueCallback, setStateCallback, nullptr, setSizeCallback, fPlugin.getInstancePointer()),
  36. fClient(client)
  37. {
  38. char strBuf[0xff+1];
  39. strBuf[0xff] = '\0';
  40. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  41. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  42. {
  43. std::snprintf(strBuf, 0xff, "in%i", i+1);
  44. fPortAudioIns[i] = jack_port_register(fClient, strBuf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  45. }
  46. #endif
  47. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  48. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  49. {
  50. std::snprintf(strBuf, 0xff, "out%i", i+1);
  51. fPortAudioOuts[i] = jack_port_register(fClient, strBuf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  52. }
  53. #endif
  54. #if DISTRHO_PLUGIN_IS_SYNTH
  55. fPortMidiIn = jack_port_register(fClient, "midi-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  56. #endif
  57. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  58. if (fPlugin.getProgramCount() > 0)
  59. {
  60. fPlugin.setProgram(0);
  61. fUI.programChanged(0);
  62. }
  63. #endif
  64. if (const uint32_t count = fPlugin.getParameterCount())
  65. {
  66. fLastOutputValues = new float[count];
  67. for (uint32_t i=0; i < count; ++i)
  68. {
  69. if (fPlugin.isParameterOutput(i))
  70. {
  71. fLastOutputValues[i] = fPlugin.getParameterValue(i);
  72. }
  73. else
  74. {
  75. fLastOutputValues[i] = 0.0f;
  76. fUI.parameterChanged(i, fPlugin.getParameterValue(i));
  77. }
  78. }
  79. }
  80. else
  81. {
  82. fLastOutputValues = nullptr;
  83. }
  84. jack_set_buffer_size_callback(fClient, jackBufferSizeCallback, this);
  85. jack_set_sample_rate_callback(fClient, jackSampleRateCallback, this);
  86. jack_set_process_callback(fClient, jackProcessCallback, this);
  87. jack_on_shutdown(fClient, jackShutdownCallback, this);
  88. jack_activate(fClient);
  89. if (const char* const name = jack_get_client_name(fClient))
  90. fUI.setTitle(name);
  91. else
  92. fUI.setTitle(DISTRHO_PLUGIN_NAME);
  93. fUI.exec(this);
  94. }
  95. ~PluginJack()
  96. {
  97. if (fClient == nullptr)
  98. return;
  99. jack_deactivate(fClient);
  100. #if DISTRHO_PLUGIN_IS_SYNTH
  101. jack_port_unregister(fClient, fPortMidiIn);
  102. fPortMidiIn = nullptr;
  103. #endif
  104. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  105. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  106. {
  107. jack_port_unregister(fClient, fPortAudioIns[i]);
  108. fPortAudioIns[i] = nullptr;
  109. }
  110. #endif
  111. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  112. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  113. {
  114. jack_port_unregister(fClient, fPortAudioOuts[i]);
  115. fPortAudioOuts[i] = nullptr;
  116. }
  117. #endif
  118. jack_client_close(fClient);
  119. }
  120. // -------------------------------------------------------------------
  121. protected:
  122. void idleCallback() override
  123. {
  124. float value;
  125. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  126. {
  127. if (! fPlugin.isParameterOutput(i))
  128. continue;
  129. value = fPlugin.getParameterValue(i);
  130. if (fLastOutputValues[i] == value)
  131. continue;
  132. fLastOutputValues[i] = value;
  133. fUI.parameterChanged(i, value);
  134. }
  135. fUI.exec_idle();
  136. }
  137. void jackBufferSize(const jack_nframes_t nframes)
  138. {
  139. fPlugin.setBufferSize(nframes, true);
  140. }
  141. void jackSampleRate(const jack_nframes_t nframes)
  142. {
  143. fPlugin.setSampleRate(nframes, true);
  144. }
  145. void jackProcess(const jack_nframes_t nframes)
  146. {
  147. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  148. const float* audioIns[DISTRHO_PLUGIN_NUM_INPUTS];
  149. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  150. audioIns[i] = (const float*)jack_port_get_buffer(fPortAudioIns[i], nframes);
  151. #else
  152. static const float** audioIns = nullptr;
  153. #endif
  154. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  155. float* audioOuts[DISTRHO_PLUGIN_NUM_OUTPUTS];
  156. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  157. audioOuts[i] = (float*)jack_port_get_buffer(fPortAudioOuts[i], nframes);
  158. #else
  159. static float** audioOuts = nullptr;
  160. #endif
  161. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  162. jack_position_t pos;
  163. fTimePos.playing = (jack_transport_query(fClient, &pos) == JackTransportRolling);
  164. if (pos.unique_1 == pos.unique_2)
  165. {
  166. if (pos.valid & JackTransportPosition)
  167. fTimePos.frame = pos.frame;
  168. else
  169. fTimePos.frame = 0;
  170. if (pos.valid & JackTransportBBT)
  171. {
  172. fTimePos.bbt.valid = true;
  173. fTimePos.bbt.bar = pos.bar;
  174. fTimePos.bbt.beat = pos.beat;
  175. fTimePos.bbt.tick = pos.tick;
  176. fTimePos.bbt.barStartTick = pos.bar_start_tick;
  177. fTimePos.bbt.beatsPerBar = pos.beats_per_bar;
  178. fTimePos.bbt.beatType = pos.beat_type;
  179. fTimePos.bbt.ticksPerBeat = pos.ticks_per_beat;
  180. fTimePos.bbt.beatsPerMinute = pos.beats_per_minute;
  181. }
  182. else
  183. fTimePos.bbt.valid = false;
  184. }
  185. else
  186. {
  187. fTimePos.bbt.valid = false;
  188. fTimePos.frame = 0;
  189. }
  190. fPlugin.setTimePos(fTimePos);
  191. #endif
  192. #if DISTRHO_PLUGIN_IS_SYNTH
  193. void* const midiBuf = jack_port_get_buffer(fPortMidiIn, nframes);
  194. if (const uint32_t eventCount = jack_midi_get_event_count(midiBuf))
  195. {
  196. uint32_t midiEventCount = 0;
  197. MidiEvent midiEvents[eventCount];
  198. jack_midi_event_t jevent;
  199. for (uint32_t i=0; i < eventCount; ++i)
  200. {
  201. if (jack_midi_event_get(&jevent, midiBuf, i) != 0)
  202. break;
  203. if (jevent.size > 4)
  204. continue;
  205. MidiEvent& midiEvent(midiEvents[midiEventCount++]);
  206. midiEvent.frame = jevent.time;
  207. midiEvent.size = jevent.size;
  208. std::memcpy(midiEvent.buf, jevent.buffer, jevent.size);
  209. }
  210. fPlugin.run(audioIns, audioOuts, nframes, midiEvents, midiEventCount);
  211. }
  212. else
  213. {
  214. fPlugin.run(audioIns, audioOuts, nframes, nullptr, 0);
  215. }
  216. #else
  217. fPlugin.run(audioIns, audioOuts, nframes);
  218. #endif
  219. }
  220. void jackShutdown()
  221. {
  222. d_stderr("jack has shutdown, quitting now...");
  223. fClient = nullptr;
  224. fUI.quit();
  225. }
  226. // -------------------------------------------------------------------
  227. void setParameterValue(const uint32_t index, const float value)
  228. {
  229. fPlugin.setParameterValue(index, value);
  230. }
  231. #if DISTRHO_PLUGIN_WANT_STATE
  232. void setState(const char* const key, const char* const value)
  233. {
  234. fPlugin.setState(key, value);
  235. }
  236. #endif
  237. void setSize(const uint width, const uint height)
  238. {
  239. fUI.setSize(width, height);
  240. }
  241. // -------------------------------------------------------------------
  242. private:
  243. PluginExporter fPlugin;
  244. UIExporter fUI;
  245. jack_client_t* fClient;
  246. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  247. jack_port_t* fPortAudioIns[DISTRHO_PLUGIN_NUM_INPUTS];
  248. #endif
  249. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  250. jack_port_t* fPortAudioOuts[DISTRHO_PLUGIN_NUM_OUTPUTS];
  251. #endif
  252. #if DISTRHO_PLUGIN_IS_SYNTH
  253. jack_port_t* fPortMidiIn;
  254. #endif
  255. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  256. TimePos fTimePos;
  257. #endif
  258. // Temporary data
  259. float* fLastOutputValues;
  260. // -------------------------------------------------------------------
  261. // Callbacks
  262. #define uiPtr ((PluginJack*)ptr)
  263. static int jackBufferSizeCallback(jack_nframes_t nframes, void* ptr)
  264. {
  265. uiPtr->jackBufferSize(nframes);
  266. return 0;
  267. }
  268. static int jackSampleRateCallback(jack_nframes_t nframes, void* ptr)
  269. {
  270. uiPtr->jackSampleRate(nframes);
  271. return 0;
  272. }
  273. static int jackProcessCallback(jack_nframes_t nframes, void* ptr)
  274. {
  275. uiPtr->jackProcess(nframes);
  276. return 0;
  277. }
  278. static void jackShutdownCallback(void* ptr)
  279. {
  280. uiPtr->jackShutdown();
  281. }
  282. static void setParameterValueCallback(void* ptr, uint32_t index, float value)
  283. {
  284. uiPtr->setParameterValue(index, value);
  285. }
  286. #if DISTRHO_PLUGIN_WANT_STATE
  287. static void setStateCallback(void* ptr, const char* key, const char* value)
  288. {
  289. uiPtr->setState(key, value);
  290. }
  291. #endif
  292. static void setSizeCallback(void* ptr, uint width, uint height)
  293. {
  294. uiPtr->setSize(width, height);
  295. }
  296. #undef uiPtr
  297. };
  298. END_NAMESPACE_DISTRHO
  299. // -----------------------------------------------------------------------
  300. int main()
  301. {
  302. jack_status_t status = jack_status_t(0x0);
  303. jack_client_t* client = jack_client_open(DISTRHO_PLUGIN_NAME, JackNoStartServer, &status);
  304. if (client == nullptr)
  305. {
  306. d_string errorString;
  307. if (status & JackFailure)
  308. errorString += "Overall operation failed;\n";
  309. if (status & JackInvalidOption)
  310. errorString += "The operation contained an invalid or unsupported option;\n";
  311. if (status & JackNameNotUnique)
  312. errorString += "The desired client name was not unique;\n";
  313. if (status & JackServerStarted)
  314. errorString += "The JACK server was started as a result of this operation;\n";
  315. if (status & JackServerFailed)
  316. errorString += "Unable to connect to the JACK server;\n";
  317. if (status & JackServerError)
  318. errorString += "Communication error with the JACK server;\n";
  319. if (status & JackNoSuchClient)
  320. errorString += "Requested client does not exist;\n";
  321. if (status & JackLoadFailure)
  322. errorString += "Unable to load internal client;\n";
  323. if (status & JackInitFailure)
  324. errorString += "Unable to initialize client;\n";
  325. if (status & JackShmFailure)
  326. errorString += "Unable to access shared memory;\n";
  327. if (status & JackVersionError)
  328. errorString += "Client's protocol version does not match;\n";
  329. if (status & JackBackendError)
  330. errorString += "Backend Error;\n";
  331. if (status & JackClientZombie)
  332. errorString += "Client is being shutdown against its will;\n";
  333. if (errorString.isNotEmpty())
  334. {
  335. errorString[errorString.length()-2] = '.';
  336. d_stderr("Failed to create jack client, reason was:\n%s", errorString.buffer());
  337. }
  338. else
  339. d_stderr("Failed to create jack client, cannot continue!");
  340. return 1;
  341. }
  342. USE_NAMESPACE_DISTRHO;
  343. d_lastBufferSize = jack_get_buffer_size(client);
  344. d_lastSampleRate = jack_get_sample_rate(client);
  345. d_lastUiSampleRate = d_lastSampleRate;
  346. const PluginJack p(client);
  347. return 0;
  348. }
  349. // -----------------------------------------------------------------------