utils_jack.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. #include "utils.h"
  4. #include <cstdio>
  5. #include <cstring>
  6. #ifdef HAVE_ALSA
  7. #include <alsa/asoundlib.h>
  8. #endif
  9. #include <jack/jack.h>
  10. #include <algorithm>
  11. #include <mutex>
  12. #include <string>
  13. #include <vector>
  14. #define ALSA_SOUNDCARD_DEFAULT_ID "MODDUO"
  15. #define ALSA_CONTROL_BYPASS_LEFT "Left True-Bypass"
  16. #define ALSA_CONTROL_BYPASS_RIGHT "Right True-Bypass"
  17. #define ALSA_CONTROL_LOOPBACK1 "LOOPBACK"
  18. #define ALSA_CONTROL_LOOPBACK2 "Loopback Switch"
  19. #define ALSA_CONTROL_SPDIF_ENABLE "SPDIF Enable"
  20. #define ALSA_CONTROL_MASTER_VOLUME "DAC"
  21. #define JACK_EXTERNAL_PREFIX "mod-external"
  22. #define JACK_EXTERNAL_PREFIX_LEN 12
  23. // --------------------------------------------------------------------------------------------------------
  24. static jack_client_t* gClient = nullptr;
  25. static volatile unsigned gNewBufSize = 0;
  26. static volatile unsigned gXrunCount = 0;
  27. static const char** gPortListRet = nullptr;
  28. static std::mutex gPortRegisterMutex;
  29. static std::vector<std::string> gRegisteredPorts;
  30. static std::mutex gPortUnregisterMutex;
  31. static std::vector<std::string> gUnregisteredPorts;
  32. #ifdef HAVE_ALSA
  33. static snd_mixer_t* gAlsaMixer = nullptr;
  34. static snd_mixer_elem_t* gAlsaControlLeft = nullptr;
  35. static snd_mixer_elem_t* gAlsaControlRight = nullptr;
  36. static snd_mixer_elem_t* gAlsaControlCvExp = nullptr;
  37. static bool gLastAlsaValueLeft = true;
  38. static bool gLastAlsaValueRight = true;
  39. static bool gLastAlsaValueCvExp = false;
  40. #endif
  41. static JackBufSizeChanged jack_bufsize_changed_cb = nullptr;
  42. static JackPortAppeared jack_port_appeared_cb = nullptr;
  43. static JackPortDeleted jack_port_deleted_cb = nullptr;
  44. static TrueBypassStateChanged true_bypass_changed_cb = nullptr;
  45. #ifdef HAVE_ALSA
  46. static CvExpInputModeChanged cv_exp_mode_changed_cb = nullptr;
  47. #endif
  48. // --------------------------------------------------------------------------------------------------------
  49. #ifdef HAVE_ALSA
  50. static bool _get_alsa_switch_value(snd_mixer_elem_t* const elem)
  51. {
  52. int val = 0;
  53. snd_mixer_selem_get_playback_switch(elem, SND_MIXER_SCHN_MONO, &val);
  54. return (val != 0);
  55. }
  56. #endif
  57. // --------------------------------------------------------------------------------------------------------
  58. static int JackBufSize(jack_nframes_t frames, void*)
  59. {
  60. gNewBufSize = frames;
  61. return 0;
  62. }
  63. static void JackPortRegistration(jack_port_id_t port_id, int reg, void*)
  64. {
  65. if (gClient == nullptr)
  66. return;
  67. if (reg)
  68. {
  69. if (jack_port_appeared_cb == nullptr)
  70. return;
  71. }
  72. else
  73. {
  74. if (jack_port_deleted_cb == nullptr)
  75. return;
  76. }
  77. if (const jack_port_t* const port = jack_port_by_id(gClient, port_id))
  78. {
  79. if ((jack_port_flags(port) & JackPortIsPhysical) == 0x0 && reg != 0)
  80. return;
  81. if (const char* const port_name = jack_port_name(port))
  82. {
  83. if (strncmp(port_name, "system:midi_", 12) != 0 &&
  84. strncmp(port_name, JACK_EXTERNAL_PREFIX ":", JACK_EXTERNAL_PREFIX_LEN + 1) != 0 &&
  85. strncmp(port_name, "nooice", 5) != 0)
  86. return;
  87. const std::string portName(port_name);
  88. if (reg)
  89. {
  90. const std::lock_guard<std::mutex> clg(gPortRegisterMutex);
  91. if (std::find(gRegisteredPorts.begin(), gRegisteredPorts.end(), portName) == gRegisteredPorts.end())
  92. gRegisteredPorts.push_back(portName);
  93. }
  94. else
  95. {
  96. const std::lock_guard<std::mutex> clgr(gPortRegisterMutex);
  97. const std::lock_guard<std::mutex> clgu(gPortUnregisterMutex);
  98. if (std::find(gUnregisteredPorts.begin(), gUnregisteredPorts.end(), portName) == gUnregisteredPorts.end())
  99. gUnregisteredPorts.push_back(portName);
  100. const std::vector<std::string>::iterator portNameItr =
  101. std::find(gRegisteredPorts.begin(), gRegisteredPorts.end(), portName);
  102. if (portNameItr != gRegisteredPorts.end())
  103. gRegisteredPorts.erase(portNameItr);
  104. }
  105. }
  106. }
  107. }
  108. static int JackXRun(void*)
  109. {
  110. gXrunCount += 1;
  111. return 0;
  112. }
  113. static void JackShutdown(void*)
  114. {
  115. gClient = nullptr;
  116. }
  117. // --------------------------------------------------------------------------------------------------------
  118. bool init_jack(void)
  119. {
  120. #ifdef HAVE_ALSA
  121. if (gAlsaMixer == nullptr)
  122. {
  123. if (snd_mixer_open(&gAlsaMixer, SND_MIXER_ELEM_SIMPLE) == 0)
  124. {
  125. snd_mixer_selem_id_t* sid;
  126. char soundcard[32] = "hw:";
  127. if (const char* const cardname = getenv("MOD_SOUNDCARD"))
  128. strncat(soundcard, cardname, 28);
  129. else
  130. strncat(soundcard, ALSA_SOUNDCARD_DEFAULT_ID, 28);
  131. soundcard[31] = '\0';
  132. if (snd_mixer_attach(gAlsaMixer, soundcard) == 0 &&
  133. snd_mixer_selem_register(gAlsaMixer, nullptr, nullptr) == 0 &&
  134. snd_mixer_load(gAlsaMixer) == 0 &&
  135. snd_mixer_selem_id_malloc(&sid) == 0)
  136. {
  137. snd_mixer_selem_id_set_index(sid, 0);
  138. snd_mixer_selem_id_set_name(sid, ALSA_CONTROL_BYPASS_LEFT);
  139. gAlsaControlLeft = snd_mixer_find_selem(gAlsaMixer, sid);
  140. snd_mixer_selem_id_set_index(sid, 0);
  141. snd_mixer_selem_id_set_name(sid, ALSA_CONTROL_BYPASS_RIGHT);
  142. gAlsaControlRight = snd_mixer_find_selem(gAlsaMixer, sid);
  143. #ifdef _MOD_DEVICE_DUOX
  144. // special case until HMI<->system comm is not in place yet
  145. snd_mixer_selem_id_set_index(sid, 0);
  146. snd_mixer_selem_id_set_name(sid, "CV/Exp.Pedal Mode");
  147. gAlsaControlCvExp = snd_mixer_find_selem(gAlsaMixer, sid);
  148. #endif
  149. snd_mixer_selem_id_free(sid);
  150. }
  151. else
  152. {
  153. snd_mixer_close(gAlsaMixer);
  154. gAlsaMixer = nullptr;
  155. }
  156. }
  157. }
  158. #endif
  159. if (gClient != nullptr)
  160. {
  161. printf("jack client activated before, nothing to do\n");
  162. return true;
  163. }
  164. #ifdef _MOD_DESKTOP
  165. const jack_options_t options = static_cast<jack_options_t>(JackNoStartServer|JackUseExactName|JackServerName);
  166. const char* servername = std::getenv("MOD_DESKTOP_SERVER_NAME");
  167. if (servername == nullptr)
  168. servername = "mod-desktop";
  169. jack_client_t* const client = jack_client_open("mod-ui", options, nullptr, servername);
  170. #else
  171. const jack_options_t options = static_cast<jack_options_t>(JackNoStartServer|JackUseExactName);
  172. jack_client_t* const client = jack_client_open("mod-ui", options, nullptr);
  173. #endif
  174. if (client == nullptr)
  175. return false;
  176. jack_set_buffer_size_callback(client, JackBufSize, nullptr);
  177. jack_set_port_registration_callback(client, JackPortRegistration, nullptr);
  178. jack_set_xrun_callback(client, JackXRun, nullptr);
  179. jack_on_shutdown(client, JackShutdown, nullptr);
  180. gClient = client;
  181. gNewBufSize = 0;
  182. gXrunCount = 0;
  183. jack_activate(client);
  184. printf("jack client activated\n");
  185. return true;
  186. }
  187. void close_jack(void)
  188. {
  189. if (gPortListRet != nullptr)
  190. {
  191. jack_free(gPortListRet);
  192. gPortListRet = nullptr;
  193. }
  194. #ifdef HAVE_ALSA
  195. if (gAlsaMixer != nullptr)
  196. {
  197. gAlsaControlLeft = nullptr;
  198. gAlsaControlRight = nullptr;
  199. snd_mixer_close(gAlsaMixer);
  200. gAlsaMixer = nullptr;
  201. }
  202. #endif
  203. if (gClient == nullptr)
  204. {
  205. printf("jack client deactivated NOT\n");
  206. return;
  207. }
  208. jack_client_t* const client = gClient;
  209. gClient = nullptr;
  210. jack_deactivate(client);
  211. jack_client_close(client);
  212. printf("jack client deactivated\n");
  213. }
  214. // --------------------------------------------------------------------------------------------------------
  215. JackData* get_jack_data(bool withTransport)
  216. {
  217. static JackData data = { 0.0f, 0, false, 4.0, 120.0 };
  218. static std::vector<std::string> localPorts;
  219. if (gClient != nullptr)
  220. {
  221. data.cpuLoad = jack_cpu_load(gClient);
  222. data.xruns = gXrunCount;
  223. if (withTransport)
  224. {
  225. jack_position_t pos;
  226. data.rolling = jack_transport_query(gClient, &pos) == JackTransportRolling;
  227. if (pos.valid & JackTransportBBT)
  228. {
  229. data.bpb = pos.beats_per_bar;
  230. data.bpm = pos.beats_per_minute;
  231. }
  232. else
  233. {
  234. data.bpb = 4.0;
  235. data.bpm = 120.0;
  236. }
  237. }
  238. if (jack_port_deleted_cb != nullptr)
  239. {
  240. // See if any new ports have been unregistered
  241. {
  242. const std::lock_guard<std::mutex> clg(gPortUnregisterMutex);
  243. if (gUnregisteredPorts.size() > 0)
  244. gUnregisteredPorts.swap(localPorts);
  245. }
  246. for (const std::string& portName : localPorts)
  247. jack_port_deleted_cb(portName.c_str());
  248. localPorts.clear();
  249. }
  250. if (jack_port_appeared_cb != nullptr)
  251. {
  252. // See if any new ports have been registered
  253. {
  254. const std::lock_guard<std::mutex> clg(gPortRegisterMutex);
  255. if (gRegisteredPorts.size() > 0)
  256. gRegisteredPorts.swap(localPorts);
  257. }
  258. for (const std::string& portName : localPorts)
  259. {
  260. if (jack_port_t* const port = jack_port_by_name(gClient, portName.c_str()))
  261. {
  262. const bool isOutput = jack_port_flags(port) & JackPortIsInput; // inverted on purpose
  263. jack_port_appeared_cb(portName.c_str(), isOutput);
  264. }
  265. }
  266. localPorts.clear();
  267. }
  268. }
  269. else
  270. {
  271. data.cpuLoad = 0.0f;
  272. data.xruns = 0;
  273. data.rolling = false;
  274. data.bpb = 4.0;
  275. data.bpm = 120.0;
  276. }
  277. if (gNewBufSize > 0 && jack_bufsize_changed_cb != nullptr)
  278. {
  279. const unsigned int bufsize = gNewBufSize;
  280. gNewBufSize = 0;
  281. jack_bufsize_changed_cb(bufsize);
  282. }
  283. #ifdef HAVE_ALSA
  284. if (gAlsaMixer != nullptr && (true_bypass_changed_cb != nullptr || cv_exp_mode_changed_cb != nullptr))
  285. {
  286. bool changedBypass = false;
  287. snd_mixer_handle_events(gAlsaMixer);
  288. if (gAlsaControlLeft != nullptr)
  289. {
  290. const bool newValue = _get_alsa_switch_value(gAlsaControlLeft);
  291. if (gLastAlsaValueLeft != newValue)
  292. {
  293. changedBypass = true;
  294. gLastAlsaValueLeft = newValue;
  295. }
  296. }
  297. if (gAlsaControlRight != nullptr)
  298. {
  299. const bool newValue = _get_alsa_switch_value(gAlsaControlRight);
  300. if (gLastAlsaValueRight != newValue)
  301. {
  302. changedBypass = true;
  303. gLastAlsaValueRight = newValue;
  304. }
  305. }
  306. if (changedBypass && true_bypass_changed_cb != nullptr)
  307. true_bypass_changed_cb(gLastAlsaValueLeft, gLastAlsaValueRight);
  308. #ifdef _MOD_DEVICE_DUOX
  309. // special case until HMI<->system comm is not in place yet
  310. bool changedCvExpMode = false;
  311. if (gAlsaControlCvExp != nullptr)
  312. {
  313. bool newValue = gLastAlsaValueCvExp;
  314. // open new mixer to force read of cv/exp value
  315. snd_mixer_t* mixer;
  316. if (snd_mixer_open(&mixer, SND_MIXER_ELEM_SIMPLE) == 0)
  317. {
  318. snd_mixer_selem_id_t* sid;
  319. if (snd_mixer_attach(mixer, "hw:DUOX") == 0 &&
  320. snd_mixer_selem_register(mixer, nullptr, nullptr) == 0 &&
  321. snd_mixer_load(mixer) == 0 &&
  322. snd_mixer_selem_id_malloc(&sid) == 0)
  323. {
  324. snd_mixer_selem_id_set_index(sid, 0);
  325. snd_mixer_selem_id_set_name(sid, "CV/Exp.Pedal Mode");
  326. if (snd_mixer_elem_t* const elem = snd_mixer_find_selem(mixer, sid))
  327. newValue = _get_alsa_switch_value(elem);
  328. snd_mixer_selem_id_free(sid);
  329. }
  330. snd_mixer_close(mixer);
  331. }
  332. if (gLastAlsaValueCvExp != newValue)
  333. {
  334. changedCvExpMode = true;
  335. gLastAlsaValueCvExp = newValue;
  336. }
  337. }
  338. if (changedCvExpMode && cv_exp_mode_changed_cb != nullptr)
  339. cv_exp_mode_changed_cb(gLastAlsaValueCvExp);
  340. #endif
  341. }
  342. #endif
  343. return &data;
  344. }
  345. unsigned get_jack_buffer_size(void)
  346. {
  347. if (gClient == nullptr)
  348. return 128;
  349. return jack_get_buffer_size(gClient);
  350. }
  351. unsigned set_jack_buffer_size(unsigned size)
  352. {
  353. if (gClient == nullptr)
  354. return 0;
  355. if (jack_set_buffer_size(gClient, size) == 0)
  356. return size;
  357. return jack_get_buffer_size(gClient);
  358. }
  359. float get_jack_sample_rate(void)
  360. {
  361. if (gClient == nullptr)
  362. return 48000.0f;
  363. return jack_get_sample_rate(gClient);
  364. }
  365. const char* get_jack_port_alias(const char* portname)
  366. {
  367. static char aliases[2][320];
  368. static char* aliasesptr[2] = {
  369. aliases[0],
  370. aliases[1]
  371. };
  372. if (gClient != nullptr)
  373. if (jack_port_t* const port = jack_port_by_name(gClient, portname))
  374. if (jack_port_get_aliases(port, aliasesptr) > 0)
  375. return aliases[0];
  376. return nullptr;
  377. }
  378. const char* const* get_jack_hardware_ports(const bool isAudio, bool isOutput)
  379. {
  380. if (gPortListRet != nullptr)
  381. {
  382. jack_free(gPortListRet);
  383. gPortListRet = nullptr;
  384. }
  385. if (gClient == nullptr)
  386. return nullptr;
  387. const unsigned long flags = JackPortIsPhysical | (isOutput ? JackPortIsInput : JackPortIsOutput);
  388. const char* const type = isAudio ? JACK_DEFAULT_AUDIO_TYPE : JACK_DEFAULT_MIDI_TYPE;
  389. const char** const ports = jack_get_ports(gClient, "", type, flags);
  390. if (ports == nullptr)
  391. return nullptr;
  392. // hide midi-through capture ports
  393. if (!isAudio && !isOutput)
  394. {
  395. static char aliases[2][320];
  396. static char* aliasesptr[2] = {
  397. aliases[0],
  398. aliases[1]
  399. };
  400. for (int i=0; ports[i] != nullptr; ++i)
  401. {
  402. if (strncmp(ports[i], "system:midi_capture_", 20))
  403. continue;
  404. jack_port_t* const port = jack_port_by_name(gClient, ports[i]);
  405. if (port == nullptr)
  406. continue;
  407. if (jack_port_get_aliases(port, aliasesptr) <= 0)
  408. continue;
  409. if (strncmp(aliases[0], "alsa_pcm:Midi-Through/", 22))
  410. continue;
  411. for (int j=i; ports[j] != nullptr; ++j)
  412. ports[j] = ports[j+1];
  413. --i;
  414. }
  415. }
  416. #ifdef _MOD_DEVICE_DUOX
  417. // Duo X special case for SPDIF mirrored mode
  418. if (isAudio && isOutput && ! has_duox_split_spdif())
  419. {
  420. for (int i=0; ports[i] != nullptr; ++i)
  421. {
  422. if (ports[i+1] == nullptr)
  423. break;
  424. if (std::strcmp(ports[i], "system:playback_3") != 0)
  425. continue;
  426. if (std::strcmp(ports[i+1], "system:playback_4") != 0)
  427. continue;
  428. for (int j=i+2; ports[j] != nullptr; ++i, ++j)
  429. ports[i] = ports[j];
  430. ports[i] = nullptr;
  431. break;
  432. }
  433. }
  434. #endif
  435. gPortListRet = ports;
  436. return ports;
  437. }
  438. // --------------------------------------------------------------------------------------------------------
  439. bool has_midi_beat_clock_sender_port(void)
  440. {
  441. if (gClient == nullptr)
  442. return false;
  443. // TODO: This must be the same as in `settings.py`!
  444. return (jack_port_by_name(gClient, "effect_9993:mclk") != nullptr);
  445. }
  446. bool has_serial_midi_input_port(void)
  447. {
  448. if (gClient == nullptr)
  449. return false;
  450. return (jack_port_by_name(gClient, "ttymidi:MIDI_in") != nullptr);
  451. }
  452. bool has_serial_midi_output_port(void)
  453. {
  454. if (gClient == nullptr)
  455. return false;
  456. return (jack_port_by_name(gClient, "ttymidi:MIDI_out") != nullptr);
  457. }
  458. /**
  459. * Ask the JACK server if there is the midi-merger client available.
  460. */
  461. bool has_midi_merger_output_port(void)
  462. {
  463. if (gClient == nullptr)
  464. return false;
  465. return (jack_port_by_name(gClient, "mod-midi-merger:out") != nullptr);
  466. }
  467. /**
  468. * Ask the JACK server if there is the midi-broadcaster client available.
  469. */
  470. bool has_midi_broadcaster_input_port(void)
  471. {
  472. if (gClient == nullptr)
  473. return false;
  474. return (jack_port_by_name(gClient, "mod-midi-broadcaster:in") != nullptr);
  475. }
  476. bool has_duox_split_spdif(void)
  477. {
  478. #ifdef _MOD_DEVICE_DUOX
  479. if (gClient == nullptr)
  480. return false;
  481. return (jack_port_by_name(gClient, "mod-monitor:in_3") != nullptr);
  482. #else
  483. return false;
  484. #endif
  485. }
  486. // --------------------------------------------------------------------------------------------------------
  487. bool connect_jack_ports(const char* port1, const char* port2)
  488. {
  489. if (gClient == nullptr)
  490. return false;
  491. int ret;
  492. ret = jack_connect(gClient, port1, port2);
  493. if (ret == 0 || ret == EEXIST)
  494. return true;
  495. ret = jack_connect(gClient, port2, port1);
  496. if (ret == 0 || ret == EEXIST)
  497. return true;
  498. return false;
  499. }
  500. bool connect_jack_midi_output_ports(const char* port)
  501. {
  502. if (gClient == nullptr)
  503. return false;
  504. int ret;
  505. if (jack_port_by_name(gClient, "mod-midi-broadcaster:in") != nullptr)
  506. {
  507. ret = jack_connect(gClient, port, "mod-midi-broadcaster:in");
  508. return (ret == 0 || ret == EEXIST);
  509. }
  510. if (const char** const ports = jack_get_ports(gClient, "",
  511. JACK_DEFAULT_MIDI_TYPE,
  512. JackPortIsPhysical | JackPortIsInput))
  513. {
  514. for (int i=0; ports[i] != nullptr; ++i)
  515. jack_connect(gClient, port, ports[i]);
  516. jack_free(ports);
  517. return true;
  518. }
  519. return false;
  520. }
  521. bool disconnect_jack_ports(const char* port1, const char* port2)
  522. {
  523. if (gClient == nullptr)
  524. return false;
  525. if (jack_disconnect(gClient, port1, port2) == 0)
  526. return true;
  527. if (jack_disconnect(gClient, port2, port1) == 0)
  528. return true;
  529. return false;
  530. }
  531. bool disconnect_all_jack_ports(const char* portname)
  532. {
  533. if (gClient == nullptr)
  534. return false;
  535. jack_port_t* const port = jack_port_by_name(gClient, portname);
  536. if (port == nullptr)
  537. return false;
  538. const bool isOutput = jack_port_flags(port) & JackPortIsOutput;
  539. if (const char** const ports = jack_port_get_all_connections(gClient, port))
  540. {
  541. for (int i=0; ports[i] != nullptr; ++i)
  542. {
  543. if (isOutput)
  544. jack_disconnect(gClient, portname, ports[i]);
  545. else
  546. jack_disconnect(gClient, ports[i], portname);
  547. }
  548. jack_free(ports);
  549. }
  550. return true;
  551. }
  552. void reset_xruns(void)
  553. {
  554. gXrunCount = 0;
  555. }
  556. // --------------------------------------------------------------------------------------------------------
  557. void init_bypass(void)
  558. {
  559. #ifdef HAVE_ALSA
  560. if (gAlsaMixer == nullptr)
  561. return;
  562. if (gAlsaControlLeft != nullptr)
  563. snd_mixer_selem_set_playback_switch_all(gAlsaControlLeft, 0);
  564. if (gAlsaControlRight != nullptr)
  565. snd_mixer_selem_set_playback_switch_all(gAlsaControlRight, 0);
  566. #ifdef _MOD_DEVICE_DUOX
  567. // special case until HMI<->system comm is not in place yet
  568. if (gAlsaControlCvExp != nullptr)
  569. gLastAlsaValueCvExp = _get_alsa_switch_value(gAlsaControlCvExp);
  570. #endif
  571. snd_mixer_selem_id_t* sid;
  572. if (snd_mixer_selem_id_malloc(&sid) == 0)
  573. {
  574. snd_mixer_selem_id_set_index(sid, 0);
  575. snd_mixer_selem_id_set_name(sid, ALSA_CONTROL_LOOPBACK1);
  576. if (snd_mixer_elem_t* const elem = snd_mixer_find_selem(gAlsaMixer, sid))
  577. snd_mixer_selem_set_playback_switch_all(elem, 0);
  578. snd_mixer_selem_id_set_index(sid, 0);
  579. snd_mixer_selem_id_set_name(sid, ALSA_CONTROL_LOOPBACK2);
  580. if (snd_mixer_elem_t* const elem = snd_mixer_find_selem(gAlsaMixer, sid))
  581. snd_mixer_selem_set_playback_switch_all(elem, 0);
  582. snd_mixer_selem_id_set_index(sid, 0);
  583. snd_mixer_selem_id_set_name(sid, ALSA_CONTROL_SPDIF_ENABLE);
  584. if (snd_mixer_elem_t* const elem = snd_mixer_find_selem(gAlsaMixer, sid))
  585. snd_mixer_selem_set_playback_switch_all(elem, 1);
  586. snd_mixer_selem_id_free(sid);
  587. }
  588. #endif
  589. }
  590. bool get_truebypass_value(bool right)
  591. {
  592. #ifdef HAVE_ALSA
  593. return right ? gLastAlsaValueRight : gLastAlsaValueLeft;
  594. #else
  595. return false;
  596. // unused
  597. (void)right;
  598. #endif
  599. }
  600. bool set_truebypass_value(bool right, bool bypassed)
  601. {
  602. #ifdef HAVE_ALSA
  603. if (gAlsaMixer == nullptr)
  604. return false;
  605. if (right)
  606. {
  607. if (gAlsaControlRight != nullptr)
  608. return (snd_mixer_selem_set_playback_switch_all(gAlsaControlRight, bypassed) == 0);
  609. }
  610. else
  611. {
  612. if (gAlsaControlLeft != nullptr)
  613. return (snd_mixer_selem_set_playback_switch_all(gAlsaControlLeft, bypassed) == 0);
  614. }
  615. #endif
  616. return false;
  617. #ifndef HAVE_ALSA
  618. // unused
  619. (void)right;
  620. (void)bypassed;
  621. #endif
  622. }
  623. float get_master_volume(bool right)
  624. {
  625. #ifdef HAVE_ALSA
  626. if (gAlsaMixer == nullptr)
  627. return -127.5f;
  628. snd_mixer_selem_id_t* sid;
  629. if (snd_mixer_selem_id_malloc(&sid) != 0)
  630. return -127.5f;
  631. snd_mixer_selem_id_set_index(sid, 0);
  632. snd_mixer_selem_id_set_name(sid, ALSA_CONTROL_MASTER_VOLUME);
  633. float val = -127.5f;
  634. if (snd_mixer_elem_t* const elem = snd_mixer_find_selem(gAlsaMixer, sid))
  635. {
  636. long aval = 0;
  637. snd_mixer_selem_get_playback_volume(elem,
  638. right ? SND_MIXER_SCHN_FRONT_RIGHT : SND_MIXER_SCHN_FRONT_LEFT,
  639. &aval);
  640. const float a = 127.5f / 255.f;
  641. const float b = - a * 255.f;
  642. val = a * aval + b;
  643. }
  644. snd_mixer_selem_id_free(sid);
  645. return val;
  646. #else
  647. return -127.5f;
  648. // unused
  649. (void)right;
  650. #endif
  651. }
  652. // --------------------------------------------------------------------------------------------------------
  653. void set_util_callbacks(JackBufSizeChanged bufSizeChanged,
  654. JackPortAppeared portAppeared,
  655. JackPortDeleted portDeleted,
  656. TrueBypassStateChanged trueBypassChanged)
  657. {
  658. jack_bufsize_changed_cb = bufSizeChanged;
  659. jack_port_appeared_cb = portAppeared;
  660. jack_port_deleted_cb = portDeleted;
  661. true_bypass_changed_cb = trueBypassChanged;
  662. }
  663. void set_extra_util_callbacks(CvExpInputModeChanged cvExpInputModeChanged)
  664. {
  665. #ifdef _MOD_DEVICE_DUOX
  666. cv_exp_mode_changed_cb = cvExpInputModeChanged;
  667. #else
  668. // unused
  669. (void)cvExpInputModeChanged;
  670. #ifdef HAVE_ALSA
  671. (void)gAlsaControlCvExp;
  672. (void)gLastAlsaValueCvExp;
  673. #endif
  674. #endif
  675. }
  676. // --------------------------------------------------------------------------------------------------------