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

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