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.

153 lines
4.3KB

  1. /*
  2. * DISTRHO Kars Plugin, based on karplong by Chris Cannam.
  3. * Copyright (C) 2015 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_PLUGIN_KARS_HPP_INCLUDED
  17. #define DISTRHO_PLUGIN_KARS_HPP_INCLUDED
  18. #include "DistrhoPlugin.hpp"
  19. START_NAMESPACE_DISTRHO
  20. // -----------------------------------------------------------------------
  21. class DistrhoPluginKars : public Plugin
  22. {
  23. public:
  24. static const int kMaxNotes = 128;
  25. static const uint32_t kNoteNull = (uint32_t)-1;
  26. enum Parameters
  27. {
  28. paramSustain = 0,
  29. paramCount
  30. };
  31. DistrhoPluginKars();
  32. protected:
  33. // -------------------------------------------------------------------
  34. // Information
  35. const char* getLabel() const noexcept override
  36. {
  37. return "Kars";
  38. }
  39. const char* getDescription() const override
  40. {
  41. return "Simple karplus-strong plucked string synth.";
  42. }
  43. const char* getMaker() const noexcept override
  44. {
  45. return "falkTX";
  46. }
  47. const char* getHomePage() const override
  48. {
  49. return "https://github.com/DISTRHO/Kars";
  50. }
  51. const char* getLicense() const noexcept override
  52. {
  53. return "ISC";
  54. }
  55. uint32_t getVersion() const noexcept override
  56. {
  57. return d_version(1, 0, 0);
  58. }
  59. int64_t getUniqueId() const noexcept override
  60. {
  61. return d_cconst('D', 'K', 'r', 's');
  62. }
  63. // -------------------------------------------------------------------
  64. // Init
  65. void initParameter(uint32_t index, Parameter& parameter) override;
  66. // -------------------------------------------------------------------
  67. // Internal data
  68. float getParameterValue(uint32_t index) const override;
  69. void setParameterValue(uint32_t index, float value) override;
  70. // -------------------------------------------------------------------
  71. // Process
  72. void activate() override;
  73. void run(const float**, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount) override;
  74. // -------------------------------------------------------------------
  75. private:
  76. bool fSustain;
  77. double fSampleRate;
  78. uint32_t fBlockStart;
  79. struct Note {
  80. uint32_t on;
  81. uint32_t off;
  82. uint8_t velocity;
  83. float index;
  84. float size;
  85. int sizei;
  86. float* wavetable;
  87. Note() noexcept
  88. : on(kNoteNull),
  89. off(kNoteNull),
  90. velocity(0),
  91. index(0.0f),
  92. size(0.0f),
  93. wavetable(nullptr) {}
  94. ~Note() noexcept
  95. {
  96. if (wavetable != nullptr)
  97. {
  98. delete[] wavetable;
  99. wavetable = nullptr;
  100. }
  101. }
  102. void setSampleRate(const double sampleRate)
  103. {
  104. if (wavetable != nullptr)
  105. delete[] wavetable;
  106. const float frequency = 440.0f * std::pow(2.0f, (index - 69.0f) / 12.0f);
  107. size = sampleRate / frequency;
  108. sizei = int(size)+1;
  109. wavetable = new float[sizei];
  110. std::memset(wavetable, 0, sizeof(float)*static_cast<size_t>(sizei));
  111. }
  112. } fNotes[kMaxNotes];
  113. void addSamples(float* out, int voice, uint32_t offset, uint32_t count);
  114. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DistrhoPluginKars)
  115. };
  116. // -----------------------------------------------------------------------
  117. END_NAMESPACE_DISTRHO
  118. #endif // DISTRHO_PLUGIN_KARS_HPP_INCLUDED