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.

139 lines
3.8KB

  1. /*
  2. * Carla Bridge utils
  3. * Copyright (C) 2013-2017 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 "CarlaBridgeUtils.hpp"
  18. #ifndef BUILD_BRIDGE
  19. # include "CarlaShmUtils.hpp"
  20. #endif
  21. // must be last
  22. #include "jackbridge/JackBridge.hpp"
  23. #if defined(CARLA_OS_WIN) && defined(BUILDING_CARLA_FOR_WINDOWS)
  24. # define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "Global\\carla-bridge_shm_ap_"
  25. # define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "Global\\carla-bridge_shm_rtC_"
  26. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "Global\\carla-bridge_shm_nonrtC_"
  27. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "Global\\carla-bridge_shm_nonrtS_"
  28. #else
  29. # define PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "/crlbrdg_shm_ap_"
  30. # define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "/crlbrdg_shm_rtC_"
  31. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "/crlbrdg_shm_nonrtC_"
  32. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "/crlbrdg_shm_nonrtS_"
  33. #endif
  34. // -------------------------------------------------------------------------------------------------------------------
  35. BridgeAudioPool::BridgeAudioPool() noexcept
  36. : data(nullptr),
  37. dataSize(0),
  38. filename()
  39. {
  40. carla_zeroChars(shm, 64);
  41. jackbridge_shm_init(shm);
  42. }
  43. BridgeAudioPool::~BridgeAudioPool() noexcept
  44. {
  45. // should be cleared by now
  46. CARLA_SAFE_ASSERT(data == nullptr);
  47. clear();
  48. }
  49. bool BridgeAudioPool::initializeServer() noexcept
  50. {
  51. #ifndef BUILD_BRIDGE
  52. char tmpFileBase[64];
  53. std::sprintf(tmpFileBase, PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL "XXXXXX");
  54. const carla_shm_t shm2 = carla_shm_create_temp(tmpFileBase);
  55. CARLA_SAFE_ASSERT_RETURN(carla_is_shm_valid(shm2), false);
  56. void* const shmptr = shm;
  57. carla_shm_t& shm1 = *(carla_shm_t*)shmptr;
  58. carla_copyStruct(shm1, shm2);
  59. filename = tmpFileBase;
  60. return true;
  61. #else
  62. return false;
  63. #endif
  64. }
  65. bool BridgeAudioPool::attachClient(const char* const basename) noexcept
  66. {
  67. CARLA_SAFE_ASSERT_RETURN(basename != nullptr && basename[0] != '\0', false);
  68. #ifdef BUILD_BRIDGE
  69. // must be invalid right now
  70. CARLA_SAFE_ASSERT_RETURN(! jackbridge_shm_is_valid(shm), false);
  71. filename = PLUGIN_BRIDGE_NAMEPREFIX_AUDIO_POOL;
  72. filename += basename;
  73. jackbridge_shm_attach(shm, filename);
  74. return jackbridge_shm_is_valid(shm);
  75. #else
  76. return false;
  77. #endif
  78. }
  79. void BridgeAudioPool::clear() noexcept
  80. {
  81. filename.clear();
  82. if (! jackbridge_shm_is_valid(shm))
  83. {
  84. CARLA_SAFE_ASSERT(data == nullptr);
  85. return;
  86. }
  87. if (data != nullptr)
  88. {
  89. #ifndef BUILD_BRIDGE
  90. jackbridge_shm_unmap(shm, data);
  91. #endif
  92. data = nullptr;
  93. }
  94. dataSize = 0;
  95. jackbridge_shm_close(shm);
  96. jackbridge_shm_init(shm);
  97. }
  98. void BridgeAudioPool::resize(const uint32_t bufferSize, const uint32_t audioPortCount, const uint32_t cvPortCount) noexcept
  99. {
  100. CARLA_SAFE_ASSERT_RETURN(jackbridge_shm_is_valid(shm),);
  101. if (data != nullptr)
  102. jackbridge_shm_unmap(shm, data);
  103. dataSize = (audioPortCount+cvPortCount)*bufferSize*sizeof(float);
  104. if (dataSize == 0)
  105. dataSize = sizeof(float);
  106. data = (float*)jackbridge_shm_map(shm, dataSize);
  107. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  108. std::memset(data, 0, dataSize);
  109. }
  110. // -------------------------------------------------------------------------------------------------------------------