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.

193 lines
5.2KB

  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. // TESTING
  33. carla_stdout("sv_load");
  34. sv_load(fSlot, "/home/falktx/bin/sunvox/examples/8bit_tales.sunvox");
  35. carla_stdout("sv_load - FINISHED");
  36. sv_play_from_beginning(fSlot);
  37. }
  38. ~SunVoxFilePlugin() override
  39. {
  40. sv_close_slot(fSlot);
  41. }
  42. protected:
  43. // -------------------------------------------------------------------
  44. // Plugin state calls
  45. void setCustomData(const char* const key, const char* const value) override
  46. {
  47. CARLA_ASSERT(key != nullptr);
  48. CARLA_ASSERT(value != nullptr);
  49. if (std::strcmp(key, "file") != 0)
  50. return;
  51. //sv_load(fSlot, value);
  52. }
  53. // -------------------------------------------------------------------
  54. // Plugin process calls
  55. void process(float** inBuf, float** outBuf, const uint32_t frames, const uint32_t, const MidiEvent* const) override
  56. {
  57. const TimeInfo* const timePos = getTimeInfo();
  58. if (timePos->playing)
  59. {
  60. float svBuffer[frames*2];
  61. for (uint32_t i=0, j=0; i < frames; ++i)
  62. {
  63. svBuffer[j++] = outBuf[0][i];
  64. svBuffer[j++] = outBuf[1][i];
  65. }
  66. //double tickFrame = double(timePos->frame)*sTicksPerFrame;
  67. //int outTime = (timePos->usecs - ) & 0xFFFFFFFF;
  68. unsigned int tick = 0; sv_get_ticks()& 0xFFFFFFFF;
  69. sv_audio_callback(svBuffer, frames, 0, tick);
  70. printf("Line counter: %d, ticks: %u\n", sv_get_current_line(fSlot), tick);
  71. }
  72. else
  73. {
  74. carla_zeroFloat(outBuf[0], frames);
  75. carla_zeroFloat(outBuf[1], frames);
  76. }
  77. }
  78. // -------------------------------------------------------------------
  79. // Plugin UI calls
  80. void uiShow(const bool show) override
  81. {
  82. if (! show)
  83. return;
  84. if (const char* const filename = uiOpenFile(false, "Open Audio File", "MIDI Files *.mid;*.midi;;"))
  85. {
  86. uiCustomDataChanged("file", filename);
  87. }
  88. uiClosed();
  89. }
  90. private:
  91. int fSlot;
  92. static int sInstanceCount;
  93. static int sLastSlot;
  94. static double sTicksPerFrame;
  95. public:
  96. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host)
  97. {
  98. if (sInstanceCount == 0)
  99. {
  100. CARLA_ASSERT(sLastSlot == 0);
  101. CARLA_ASSERT(sTicksPerFrame == 0);
  102. if (sv_load_dll() != 0)
  103. return nullptr;
  104. const double sampleRate(host->get_sample_rate(host->handle));
  105. if (sv_init(nullptr, (int)sampleRate, 2, SV_INIT_FLAG_USER_AUDIO_CALLBACK|SV_INIT_FLAG_AUDIO_FLOAT32|SV_INIT_FLAG_ONE_THREAD) == 0)
  106. return nullptr;
  107. sTicksPerFrame = double(sv_get_ticks_per_second())/sampleRate;
  108. }
  109. sInstanceCount++;
  110. return new SunVoxFilePlugin(host);
  111. }
  112. static void _cleanup(PluginHandle handle)
  113. {
  114. delete (SunVoxFilePlugin*)handle;
  115. if (--sInstanceCount == 0)
  116. {
  117. CARLA_ASSERT(sLastSlot > 0);
  118. CARLA_ASSERT(sTicksPerFrame > 0.0);
  119. sLastSlot = 0;
  120. sTicksPerFrame = 0.0;
  121. sv_deinit();
  122. sv_unload_dll();
  123. }
  124. }
  125. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SunVoxFilePlugin)
  126. };
  127. int SunVoxFilePlugin::sInstanceCount = 0;
  128. int SunVoxFilePlugin::sLastSlot = 0;
  129. double SunVoxFilePlugin::sTicksPerFrame = 0.0;
  130. // -----------------------------------------------------------------------
  131. static const PluginDescriptor sunvoxfileDesc = {
  132. /* category */ PLUGIN_CATEGORY_UTILITY,
  133. /* hints */ static_cast<PluginHints>(PLUGIN_HAS_GUI),
  134. /* audioIns */ 0,
  135. /* audioOuts */ 2,
  136. /* midiIns */ 0,
  137. /* midiOuts */ 0,
  138. /* paramIns */ 0,
  139. /* paramOuts */ 0,
  140. /* name */ "SunVox File",
  141. /* label */ "sunvoxfile",
  142. /* maker */ "falkTX",
  143. /* copyright */ "GNU GPL v2+",
  144. PluginDescriptorFILL(SunVoxFilePlugin)
  145. };
  146. // -----------------------------------------------------------------------
  147. void carla_register_native_plugin_sunvoxfile()
  148. {
  149. carla_register_native_plugin(&sunvoxfileDesc);
  150. }
  151. // -----------------------------------------------------------------------