Audio plugin host https://kx.studio/carla
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.

JackEngine.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. JackEngine.cpp
  3. Copyright 2009, Alan Calvert
  4. This file is part of yoshimi, which is free software: you can
  5. redistribute it and/or modify it under the terms of the GNU General
  6. Public License as published by the Free Software Foundation, either
  7. version 3 of the License, or (at your option) any later version.
  8. yoshimi is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with yoshimi. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <iostream>
  16. #include <jack/midiport.h>
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19. #include <cassert>
  20. #include <cstring>
  21. #include "Nio.h"
  22. #include "InMgr.h"
  23. #include "JackEngine.h"
  24. using namespace std;
  25. extern char *instance_name;
  26. JackEngine::JackEngine()
  27. :AudioOut(), jackClient(NULL)
  28. {
  29. name = "JACK";
  30. audio.jackSamplerate = 0;
  31. audio.jackNframes = 0;
  32. for(int i = 0; i < 2; ++i) {
  33. audio.ports[i] = NULL;
  34. audio.portBuffs[i] = NULL;
  35. }
  36. midi.inport = NULL;
  37. }
  38. bool JackEngine::connectServer(string server)
  39. {
  40. bool autostart_jack = true;
  41. if(jackClient)
  42. return true;
  43. string clientname = "zynaddsubfx";
  44. string postfix = Nio::getPostfix();
  45. if(!postfix.empty())
  46. clientname += "_" + postfix;
  47. jack_status_t jackstatus;
  48. bool use_server_name = server.size() && server.compare("default") != 0;
  49. jack_options_t jopts = (jack_options_t)
  50. (((!instance_name
  51. && use_server_name) ? JackServerName :
  52. JackNullOption)
  53. | ((autostart_jack) ? JackNullOption :
  54. JackNoStartServer));
  55. if(instance_name)
  56. jackClient = jack_client_open(instance_name, jopts, &jackstatus);
  57. else {
  58. if(use_server_name)
  59. jackClient = jack_client_open(
  60. clientname.c_str(), jopts, &jackstatus,
  61. server.c_str());
  62. else
  63. jackClient = jack_client_open(
  64. clientname.c_str(), jopts, &jackstatus);
  65. }
  66. if(NULL != jackClient)
  67. return true;
  68. else
  69. cerr << "Error, failed to open jack client on server: " << server
  70. << " status " << jackstatus << endl;
  71. return false;
  72. }
  73. bool JackEngine::connectJack()
  74. {
  75. connectServer("");
  76. if(NULL != jackClient) {
  77. setBufferSize(jack_get_buffer_size(jackClient));
  78. int chk;
  79. jack_set_error_function(_errorCallback);
  80. jack_set_info_function(_infoCallback);
  81. if(jack_set_buffer_size_callback(jackClient, _bufferSizeCallback, this))
  82. cerr << "Error setting the bufferSize callback" << endl;
  83. if((chk = jack_set_xrun_callback(jackClient, _xrunCallback, this)))
  84. cerr << "Error setting jack xrun callback" << endl;
  85. if(jack_set_process_callback(jackClient, _processCallback, this)) {
  86. cerr << "Error, JackEngine failed to set process callback" << endl;
  87. return false;
  88. }
  89. if(jack_activate(jackClient)) {
  90. cerr << "Error, failed to activate jack client" << endl;
  91. return false;
  92. }
  93. return true;
  94. }
  95. else
  96. cerr << "Error, NULL jackClient through Start()" << endl;
  97. return false;
  98. }
  99. void JackEngine::disconnectJack()
  100. {
  101. if(jackClient) {
  102. cout << "Deactivating and closing JACK client" << endl;
  103. jack_deactivate(jackClient);
  104. jack_client_close(jackClient);
  105. jackClient = NULL;
  106. }
  107. }
  108. bool JackEngine::Start()
  109. {
  110. return openMidi() && openAudio();
  111. }
  112. void JackEngine::Stop()
  113. {
  114. stopMidi();
  115. stopAudio();
  116. }
  117. void JackEngine::setMidiEn(bool nval)
  118. {
  119. if(nval)
  120. openMidi();
  121. else
  122. stopMidi();
  123. }
  124. bool JackEngine::getMidiEn() const
  125. {
  126. return midi.inport;
  127. }
  128. void JackEngine::setAudioEn(bool nval)
  129. {
  130. if(nval)
  131. openAudio();
  132. else
  133. stopAudio();
  134. }
  135. bool JackEngine::getAudioEn() const
  136. {
  137. return audio.ports[0];
  138. }
  139. bool JackEngine::openAudio()
  140. {
  141. if(getAudioEn())
  142. return true;
  143. if(!getMidiEn())
  144. if(!connectJack())
  145. return false;
  146. const char *portnames[] = { "out_1", "out_2" };
  147. for(int port = 0; port < 2; ++port)
  148. audio.ports[port] = jack_port_register(
  149. jackClient,
  150. portnames[port],
  151. JACK_DEFAULT_AUDIO_TYPE,
  152. JackPortIsOutput
  153. | JackPortIsTerminal,
  154. 0);
  155. if((NULL != audio.ports[0]) && (NULL != audio.ports[1])) {
  156. audio.jackSamplerate = jack_get_sample_rate(jackClient);
  157. audio.jackNframes = jack_get_buffer_size(jackClient);
  158. samplerate = audio.jackSamplerate;
  159. bufferSize = audio.jackNframes;
  160. //Attempt to autoConnect when specified
  161. if(Nio::autoConnect) {
  162. const char **outPorts = jack_get_ports(
  163. jackClient,
  164. NULL,
  165. NULL,
  166. JackPortIsPhysical
  167. | JackPortIsInput);
  168. if(outPorts != NULL) {
  169. //Verify that stereo is available
  170. assert(outPorts[0]);
  171. assert(outPorts[1]);
  172. //Connect to physical outputs
  173. jack_connect(jackClient, jack_port_name(
  174. audio.ports[0]), outPorts[0]);
  175. jack_connect(jackClient, jack_port_name(
  176. audio.ports[1]), outPorts[1]);
  177. }
  178. else
  179. cerr << "Warning, No outputs to autoconnect to" << endl;
  180. }
  181. return true;
  182. }
  183. else
  184. cerr << "Error, failed to register jack audio ports" << endl;
  185. return false;
  186. }
  187. void JackEngine::stopAudio()
  188. {
  189. for(int i = 0; i < 2; ++i) {
  190. jack_port_t *port = audio.ports[i];
  191. audio.ports[i] = NULL;
  192. if(NULL != port)
  193. jack_port_unregister(jackClient, port);
  194. }
  195. if(!getMidiEn())
  196. disconnectJack();
  197. }
  198. bool JackEngine::openMidi()
  199. {
  200. if(getMidiEn())
  201. return true;
  202. if(!getAudioEn())
  203. if(!connectJack())
  204. return false;
  205. midi.inport = jack_port_register(jackClient, "midi_input",
  206. JACK_DEFAULT_MIDI_TYPE,
  207. JackPortIsInput | JackPortIsTerminal, 0);
  208. return midi.inport;
  209. }
  210. void JackEngine::stopMidi()
  211. {
  212. jack_port_t *port = midi.inport;
  213. midi.inport = NULL;
  214. if(port)
  215. jack_port_unregister(jackClient, port);
  216. if(!getAudioEn())
  217. disconnectJack();
  218. }
  219. int JackEngine::clientId()
  220. {
  221. if(NULL != jackClient)
  222. return (long)jack_client_thread_id(jackClient);
  223. else
  224. return -1;
  225. }
  226. string JackEngine::clientName()
  227. {
  228. if(NULL != jackClient)
  229. return string(jack_get_client_name(jackClient));
  230. else
  231. cerr << "Error, clientName() with null jackClient" << endl;
  232. return string("Oh, yoshimi :-(");
  233. }
  234. int JackEngine::_processCallback(jack_nframes_t nframes, void *arg)
  235. {
  236. return static_cast<JackEngine *>(arg)->processCallback(nframes);
  237. }
  238. int JackEngine::processCallback(jack_nframes_t nframes)
  239. {
  240. bool okaudio = true;
  241. if((NULL != audio.ports[0]) && (NULL != audio.ports[1]))
  242. okaudio = processAudio(nframes);
  243. if(okaudio)
  244. handleMidi(nframes);
  245. return okaudio ? 0 : -1;
  246. }
  247. bool JackEngine::processAudio(jack_nframes_t nframes)
  248. {
  249. for(int port = 0; port < 2; ++port) {
  250. audio.portBuffs[port] =
  251. (jsample_t *)jack_port_get_buffer(audio.ports[port], nframes);
  252. if(NULL == audio.portBuffs[port]) {
  253. cerr << "Error, failed to get jack audio port buffer: "
  254. << port << endl;
  255. return false;
  256. }
  257. }
  258. Stereo<float *> smp = getNext();
  259. //Assumes size of smp.l == nframes
  260. memcpy(audio.portBuffs[0], smp.l, bufferSize * sizeof(float));
  261. memcpy(audio.portBuffs[1], smp.r, bufferSize * sizeof(float));
  262. return true;
  263. }
  264. int JackEngine::_xrunCallback(void *)
  265. {
  266. cerr << "Jack reports xrun" << endl;
  267. return 0;
  268. }
  269. void JackEngine::_errorCallback(const char *msg)
  270. {
  271. cerr << "Jack reports error: " << msg << endl;
  272. }
  273. void JackEngine::_infoCallback(const char *msg)
  274. {
  275. cerr << "Jack info message: " << msg << endl;
  276. }
  277. int JackEngine::_bufferSizeCallback(jack_nframes_t nframes, void *arg)
  278. {
  279. return static_cast<JackEngine *>(arg)->bufferSizeCallback(nframes);
  280. }
  281. int JackEngine::bufferSizeCallback(jack_nframes_t nframes)
  282. {
  283. cerr << "Jack buffer resized" << endl;
  284. setBufferSize(nframes);
  285. return 0;
  286. }
  287. void JackEngine::handleMidi(unsigned long frames)
  288. {
  289. if(!midi.inport)
  290. return;
  291. void *midi_buf = jack_port_get_buffer(midi.inport, frames);
  292. jack_midi_event_t jack_midi_event;
  293. jack_nframes_t event_index = 0;
  294. unsigned char *midi_data;
  295. unsigned char type;
  296. while(jack_midi_event_get(&jack_midi_event, midi_buf,
  297. event_index++) == 0) {
  298. MidiEvent ev;
  299. midi_data = jack_midi_event.buffer;
  300. type = midi_data[0] & 0xF0;
  301. ev.channel = midi_data[0] & 0x0F;
  302. switch(type) {
  303. case 0x80: /* note-off */
  304. ev.type = M_NOTE;
  305. ev.num = midi_data[1];
  306. ev.value = 0;
  307. InMgr::getInstance().putEvent(ev);
  308. break;
  309. case 0x90: /* note-on */
  310. ev.type = M_NOTE;
  311. ev.num = midi_data[1];
  312. ev.value = midi_data[2];
  313. InMgr::getInstance().putEvent(ev);
  314. break;
  315. case 0xA0: /* pressure, aftertouch */
  316. ev.type = M_PRESSURE;
  317. ev.num = midi_data[1];
  318. ev.value = midi_data[2];
  319. InMgr::getInstance().putEvent(ev);
  320. break;
  321. case 0xB0: /* controller */
  322. ev.type = M_CONTROLLER;
  323. ev.num = midi_data[1];
  324. ev.value = midi_data[2];
  325. InMgr::getInstance().putEvent(ev);
  326. break;
  327. case 0xC0: /* program change */
  328. ev.type = M_PGMCHANGE;
  329. ev.num = midi_data[1];
  330. ev.value = 0;
  331. InMgr::getInstance().putEvent(ev);
  332. break;
  333. case 0xE0: /* pitch bend */
  334. ev.type = M_CONTROLLER;
  335. ev.num = C_pitchwheel;
  336. ev.value = ((midi_data[2] << 7) | midi_data[1]) - 8192;
  337. InMgr::getInstance().putEvent(ev);
  338. break;
  339. /* XXX TODO: handle MSB/LSB controllers and RPNs and NRPNs */
  340. }
  341. }
  342. }