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.

219 lines
7.3KB

  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2020 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 General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "libjack.hpp"
  18. #include "CarlaStringList.hpp"
  19. CARLA_BACKEND_USE_NAMESPACE
  20. // --------------------------------------------------------------------------------------------------------------------
  21. static const char* allocate_port_name(const char* const prefix, const uint num)
  22. {
  23. static CarlaStringList portList;
  24. char portName[STR_MAX];
  25. carla_zeroChars(portName, STR_MAX);
  26. std::snprintf(portName, STR_MAX-1, "%s%u", prefix, num+1);
  27. if (const char* const storedPortName = portList.containsAndReturnString(portName))
  28. return storedPortName;
  29. CARLA_SAFE_ASSERT_RETURN(portList.append(portName), nullptr);
  30. return portList.getLast();
  31. }
  32. // --------------------------------------------------------------------------------------------------------------------
  33. CARLA_EXPORT
  34. const char** jack_get_ports(jack_client_t* client, const char* a, const char* b, unsigned long flags)
  35. {
  36. carla_stdout("%s(%p, %s, %s, 0x%lx) WIP", __FUNCTION__, client, a, b, flags);
  37. JackClientState* const jclient = (JackClientState*)client;
  38. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, nullptr);
  39. const JackServerState& jserver(jclient->server);
  40. const uint numIns = static_cast<uint>(jserver.numAudioIns + jserver.numMidiIns);
  41. const uint numOuts = static_cast<uint>(jserver.numAudioOuts + jserver.numMidiOuts);
  42. if (flags == 0 || (flags & (JackPortIsInput|JackPortIsOutput)) == (JackPortIsInput|JackPortIsOutput))
  43. {
  44. if (const char** const ret = (const char**)calloc(numIns+numOuts+1, sizeof(const char*)))
  45. {
  46. uint i=0;
  47. for (uint j=0; j<jserver.numAudioIns; ++i, ++j)
  48. ret[i] = allocate_port_name("system:capture_", j);
  49. for (uint j=0; j<jserver.numAudioOuts; ++i, ++j)
  50. ret[i] = allocate_port_name("system:playback_", j);
  51. for (uint j=0; j<jserver.numMidiIns; ++i, ++j)
  52. ret[i] = allocate_port_name("system:midi_capture_", j);
  53. for (uint j=0; j<jserver.numMidiOuts; ++i, ++j)
  54. ret[i] = allocate_port_name("system:midi_playback_", j);
  55. ret[i] = nullptr;
  56. return ret;
  57. }
  58. }
  59. if (flags & JackPortIsInput)
  60. {
  61. if (const char** const ret = (const char**)calloc(numIns+1, sizeof(const char*)))
  62. {
  63. uint i=0;
  64. for (uint j=0; j<jserver.numAudioOuts; ++i, ++j)
  65. ret[i] = allocate_port_name("system:playback_", j);
  66. for (uint j=0; j<jserver.numMidiOuts; ++i, ++j)
  67. ret[i] = allocate_port_name("system:midi_playback_", j);
  68. ret[i] = nullptr;
  69. return ret;
  70. }
  71. }
  72. if (flags & JackPortIsOutput)
  73. {
  74. if (const char** const ret = (const char**)calloc(numOuts+1, sizeof(const char*)))
  75. {
  76. uint i=0;
  77. for (uint j=0; j<jserver.numAudioIns; ++i, ++j)
  78. ret[i] = allocate_port_name("system:capture_", j);
  79. for (uint j=0; j<jserver.numMidiIns; ++i, ++j)
  80. ret[i] = allocate_port_name("system:midi_capture_", j);
  81. ret[i] = nullptr;
  82. return ret;
  83. }
  84. }
  85. return nullptr;
  86. }
  87. CARLA_EXPORT
  88. jack_port_t* jack_port_by_name(jack_client_t* client, const char* name)
  89. {
  90. carla_debug("%s(%p, %s)", __FUNCTION__, client, name);
  91. JackClientState* const jclient = (JackClientState*)client;
  92. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, nullptr);
  93. if (std::strncmp(name, "system:", 7) == 0)
  94. {
  95. static JackPortState retPort(
  96. /* name */ nullptr,
  97. /* fullname */ nullptr,
  98. /* index */ 0,
  99. /* flags */ 0x0,
  100. /* isMidi */ false,
  101. /* isSystem */ true,
  102. /* isConnected */ false
  103. );
  104. static CarlaString rname, rfullname;
  105. const JackServerState& jserver(jclient->server);
  106. const int commonFlags = JackPortIsPhysical|JackPortIsTerminal;
  107. rfullname = name;
  108. name += 7;
  109. rname = name;
  110. retPort.name = rname.buffer();
  111. retPort.fullname = rfullname.buffer();
  112. /**/ if (std::strncmp(name, "capture_", 8) == 0)
  113. {
  114. name += 8;
  115. const int index = std::atoi(name)-1;
  116. CARLA_SAFE_ASSERT_RETURN(index >= 0 && index < jserver.numAudioIns, nullptr);
  117. retPort.index = static_cast<uint>(index);
  118. retPort.flags = commonFlags|JackPortIsOutput;
  119. retPort.isMidi = false;
  120. retPort.isConnected = jserver.numAudioIns > index;
  121. }
  122. else if (std::strncmp(name, "playback_", 9) == 0)
  123. {
  124. name += 9;
  125. const int index = std::atoi(name)-1;
  126. CARLA_SAFE_ASSERT_RETURN(index >= 0 && index < jserver.numAudioOuts, nullptr);
  127. retPort.index = static_cast<uint>(jserver.numAudioIns + index);
  128. retPort.flags = commonFlags|JackPortIsInput;
  129. retPort.isMidi = false;
  130. retPort.isConnected = jserver.numAudioOuts > index;
  131. }
  132. else if (std::strncmp(name, "midi_capture_", 13) == 0)
  133. {
  134. name += 13;
  135. const int index = std::atoi(name)-1;
  136. CARLA_SAFE_ASSERT_RETURN(index >= 0 && index < jserver.numMidiIns, nullptr);
  137. retPort.index = static_cast<uint>(index);
  138. retPort.flags = commonFlags|JackPortIsOutput;
  139. retPort.isMidi = true;
  140. retPort.isConnected = jserver.numMidiIns > index;
  141. }
  142. else if (std::strncmp(name, "midi_playback_", 14) == 0)
  143. {
  144. name += 14;
  145. const int index = std::atoi(name)-1;
  146. CARLA_SAFE_ASSERT_RETURN(index >= 0 && index < jserver.numMidiOuts, nullptr);
  147. retPort.index = static_cast<uint>(jserver.numMidiIns + index);
  148. retPort.flags = commonFlags|JackPortIsInput;
  149. retPort.isMidi = true;
  150. retPort.isConnected = jserver.numMidiOuts > index;
  151. }
  152. else
  153. {
  154. carla_stderr2("jack_port_by_name: invalid port short name '%s'", name);
  155. return nullptr;
  156. }
  157. return (jack_port_t*)&retPort;
  158. }
  159. else
  160. {
  161. if (JackPortState* const port = jclient->portNameMapping[name])
  162. return (jack_port_t*)port;
  163. }
  164. carla_stderr2("jack_port_by_name: invalid port name '%s'", name);
  165. return nullptr;
  166. }
  167. CARLA_EXPORT
  168. jack_port_t* jack_port_by_id(jack_client_t* client, jack_port_id_t port_id)
  169. {
  170. carla_stderr2("%s(%p, %u)", __FUNCTION__, client, port_id);
  171. return nullptr;
  172. }
  173. // --------------------------------------------------------------------------------------------------------------------