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.

185 lines
4.9KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013 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 GPL.txt file
  16. */
  17. #include "CarlaNative.hpp"
  18. #ifndef CARLA_OS_WIN
  19. # include <dlfcn.h>
  20. #endif
  21. #define SUNVOX_MAIN
  22. #include "sunvox/sunvox.h"
  23. class SunVoxFilePlugin : public PluginDescriptorClass
  24. {
  25. public:
  26. SunVoxFilePlugin(const HostDescriptor* const host)
  27. : PluginDescriptorClass(host),
  28. fSlot(sLastSlot++)
  29. {
  30. sv_open_slot(fSlot);
  31. sv_set_autostop(fSlot, 0);
  32. }
  33. ~SunVoxFilePlugin() override
  34. {
  35. sv_close_slot(fSlot);
  36. }
  37. protected:
  38. // -------------------------------------------------------------------
  39. // Plugin state calls
  40. void setCustomData(const char* const key, const char* const value) override
  41. {
  42. CARLA_ASSERT(key != nullptr);
  43. CARLA_ASSERT(value != nullptr);
  44. if (std::strcmp(key, "file") != 0)
  45. return;
  46. sv_load(fSlot, value);
  47. sv_play_from_beginning(fSlot);
  48. }
  49. // -------------------------------------------------------------------
  50. // Plugin process calls
  51. void process(float**, float** outBuf, const uint32_t frames, const uint32_t, const MidiEvent* const) override
  52. {
  53. const TimeInfo* const timePos = getTimeInfo();
  54. if (timePos->playing)
  55. {
  56. float svBuffer[frames*2];
  57. for (uint32_t i=0, j=0; i < frames; ++i)
  58. {
  59. svBuffer[j++] = outBuf[0][i];
  60. svBuffer[j++] = outBuf[1][i];
  61. }
  62. unsigned int ticks = sv_get_ticks();
  63. ticks += timePos->frame * sTicksPerFrame;
  64. sv_audio_callback(svBuffer, frames, 0, ticks+10000);
  65. }
  66. else
  67. {
  68. carla_zeroFloat(outBuf[0], frames);
  69. carla_zeroFloat(outBuf[1], frames);
  70. }
  71. }
  72. // -------------------------------------------------------------------
  73. // Plugin UI calls
  74. void uiShow(const bool show) override
  75. {
  76. if (! show)
  77. return;
  78. if (const char* const filename = uiOpenFile(false, "Open Audio File", "MIDI Files *.mid;*.midi;;"))
  79. {
  80. uiCustomDataChanged("file", filename);
  81. }
  82. uiClosed();
  83. }
  84. private:
  85. int fSlot;
  86. static int sInstanceCount;
  87. static int sLastSlot;
  88. static double sTicksPerFrame;
  89. public:
  90. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host)
  91. {
  92. if (sInstanceCount == 0)
  93. {
  94. CARLA_ASSERT(sLastSlot == 0);
  95. CARLA_ASSERT(sTicksPerFrame == 0);
  96. if (sv_load_dll() != 0)
  97. return nullptr;
  98. const double sampleRate(host->get_sample_rate(host->handle));
  99. if (sv_init(nullptr, (int)sampleRate, 2, SV_INIT_FLAG_USER_AUDIO_CALLBACK|SV_INIT_FLAG_AUDIO_FLOAT32|SV_INIT_FLAG_ONE_THREAD) == 0)
  100. return nullptr;
  101. sTicksPerFrame = double(sv_get_ticks_per_second())/sampleRate;
  102. }
  103. sInstanceCount++;
  104. return new SunVoxFilePlugin(host);
  105. }
  106. static void _cleanup(PluginHandle handle)
  107. {
  108. delete (SunVoxFilePlugin*)handle;
  109. if (--sInstanceCount == 0)
  110. {
  111. CARLA_ASSERT(sLastSlot > 0);
  112. CARLA_ASSERT(sTicksPerFrame > 0.0);
  113. sLastSlot = 0;
  114. sTicksPerFrame = 0.0;
  115. sv_deinit();
  116. sv_unload_dll();
  117. }
  118. }
  119. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SunVoxFilePlugin)
  120. };
  121. int SunVoxFilePlugin::sInstanceCount = 0;
  122. int SunVoxFilePlugin::sLastSlot = 0;
  123. double SunVoxFilePlugin::sTicksPerFrame = 0.0;
  124. // -----------------------------------------------------------------------
  125. static const PluginDescriptor sunvoxfileDesc = {
  126. /* category */ PLUGIN_CATEGORY_UTILITY,
  127. /* hints */ static_cast<PluginHints>(PLUGIN_HAS_GUI|PLUGIN_USES_GUI_AS_FILE),
  128. /* audioIns */ 0,
  129. /* audioOuts */ 2,
  130. /* midiIns */ 0,
  131. /* midiOuts */ 0,
  132. /* paramIns */ 0,
  133. /* paramOuts */ 0,
  134. /* name */ "SunVox File",
  135. /* label */ "sunvoxfile",
  136. /* maker */ "falkTX",
  137. /* copyright */ "GNU GPL v2+",
  138. PluginDescriptorFILL(SunVoxFilePlugin)
  139. };
  140. // -----------------------------------------------------------------------
  141. void carla_register_native_plugin_sunvoxfile()
  142. {
  143. carla_register_native_plugin(&sunvoxfileDesc);
  144. }
  145. // -----------------------------------------------------------------------