DPF Plugin examples
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.

220 lines
6.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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. #include "DistrhoPluginInfo.h"
  17. #include "DistrhoUI.hpp"
  18. START_NAMESPACE_DISTRHO
  19. // -----------------------------------------------------------------------------------------------------------
  20. class ExampleUIInfo : public UI
  21. {
  22. public:
  23. ExampleUIInfo()
  24. : UI(405, 256)
  25. {
  26. std::memset(fParameters, 0, sizeof(float)*kParameterCount);
  27. std::memset(fStrBuf, 0, sizeof(char)*(0xff+1));
  28. fSampleRate = getSampleRate();
  29. fFont = createFont("sans", "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf");
  30. }
  31. protected:
  32. /* --------------------------------------------------------------------------------------------------------
  33. * DSP/Plugin Callbacks */
  34. /**
  35. A parameter has changed on the plugin side.
  36. This is called by the host to inform the UI about parameter changes.
  37. */
  38. void parameterChanged(uint32_t index, float value) override
  39. {
  40. fParameters[index] = value;
  41. repaint();
  42. }
  43. /* --------------------------------------------------------------------------------------------------------
  44. * DSP/Plugin Callbacks (optional) */
  45. /**
  46. Optional callback to inform the UI about a sample rate change on the plugin side.
  47. */
  48. void sampleRateChanged(double newSampleRate)
  49. {
  50. fSampleRate = newSampleRate;
  51. repaint();
  52. }
  53. /* --------------------------------------------------------------------------------------------------------
  54. * Widget Callbacks */
  55. /**
  56. The NanoVG drawing function.
  57. */
  58. void onNanoDisplay() override
  59. {
  60. static const float lineHeight = 20;
  61. fontSize(15.0f);
  62. textLineHeight(lineHeight);
  63. float x = 0;
  64. float y = 15;
  65. // buffer size
  66. drawLeft(x, y, "Buffer Size:");
  67. drawRight(x, y, getTextBufInt(fParameters[kParameterBufferSize]));
  68. y+=lineHeight;
  69. // sample rate
  70. drawLeft(x, y, "Sample Rate:");
  71. drawRight(x, y, getTextBufFloat(fSampleRate));
  72. y+=lineHeight;
  73. // nothing
  74. y+=lineHeight;
  75. // time stuff
  76. drawLeft(x, y, "Playing:");
  77. drawRight(x, y, (fParameters[kParameterTimePlaying] > 0.5f) ? "Yes" : "No");
  78. y+=lineHeight;
  79. drawLeft(x, y, "Frame:");
  80. drawRight(x, y, getTextBufInt(fParameters[kParameterTimeFrame]));
  81. y+=lineHeight;
  82. drawLeft(x, y, "Time:");
  83. drawRight(x, y, getTextBufTime(fParameters[kParameterTimeFrame]));
  84. y+=lineHeight;
  85. // BBT
  86. x = 200;
  87. y = 15;
  88. const bool validBBT(fParameters[kParameterTimeValidBBT] > 0.5f);
  89. drawLeft(x, y, "BBT Valid:");
  90. drawRight(x, y, validBBT ? "Yes" : "No");
  91. y+=lineHeight;
  92. if (! validBBT)
  93. return;
  94. drawLeft(x, y, "Bar:");
  95. drawRight(x, y, getTextBufInt(fParameters[kParameterTimeBar]));
  96. y+=lineHeight;
  97. drawLeft(x, y, "Beat:");
  98. drawRight(x, y, getTextBufInt(fParameters[kParameterTimeBeat]));
  99. y+=lineHeight;
  100. drawLeft(x, y, "Tick:");
  101. drawRight(x, y, getTextBufInt(fParameters[kParameterTimeTick]));
  102. y+=lineHeight;
  103. drawLeft(x, y, "Bar Start Tick:");
  104. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBarStartTick]));
  105. y+=lineHeight;
  106. drawLeft(x, y, "Beats Per Bar:");
  107. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBeatsPerBar]));
  108. y+=lineHeight;
  109. drawLeft(x, y, "Beat Type:");
  110. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBeatType]));
  111. y+=lineHeight;
  112. drawLeft(x, y, "Ticks Per Beat:");
  113. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeTicksPerBeat]));
  114. y+=lineHeight;
  115. drawLeft(x, y, "BPM:");
  116. drawRight(x, y, getTextBufFloat(fParameters[kParameterTimeBeatsPerMinute]));
  117. y+=lineHeight;
  118. }
  119. // -------------------------------------------------------------------------------------------------------
  120. private:
  121. // Parameters
  122. float fParameters[kParameterCount];
  123. double fSampleRate;
  124. FontId fFont;
  125. // temp buf for text
  126. char fStrBuf[0xff+1];
  127. const char* getTextBufInt(const int value)
  128. {
  129. std::snprintf(fStrBuf, 0xff, "%i", value);
  130. return fStrBuf;
  131. }
  132. const char* getTextBufFloat(const float value)
  133. {
  134. std::snprintf(fStrBuf, 0xff, "%.1f", value);
  135. return fStrBuf;
  136. }
  137. const char* getTextBufTime(const uint64_t frame)
  138. {
  139. const uint32_t time = frame / uint64_t(fSampleRate);
  140. const uint32_t secs = time % 60;
  141. const uint32_t mins = (time / 60) % 60;
  142. const uint32_t hrs = (time / 3600) % 60;
  143. std::snprintf(fStrBuf, 0xff, "%02i:%02i:%02i", hrs, mins, secs);
  144. return fStrBuf;
  145. }
  146. void drawLeft(const float x, const float y, const char* const text)
  147. {
  148. beginPath();
  149. fillColor(200,200,200);
  150. textAlign(Align(ALIGN_RIGHT|ALIGN_TOP));
  151. textBox(x, y, 100, text, nullptr);
  152. closePath();
  153. }
  154. void drawRight(const float x, const float y, const char* const text)
  155. {
  156. beginPath();
  157. fillColor(255,255,255);
  158. textAlign(Align(ALIGN_LEFT|ALIGN_TOP));
  159. textBox(x+105, y, 100, text, nullptr);
  160. closePath();
  161. }
  162. /**
  163. Set our UI class as non-copyable and add a leak detector just in case.
  164. */
  165. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExampleUIInfo)
  166. };
  167. /* ------------------------------------------------------------------------------------------------------------
  168. * UI entry point, called by DPF to create a new UI instance. */
  169. UI* createUI()
  170. {
  171. return new ExampleUIInfo();
  172. }
  173. // -----------------------------------------------------------------------------------------------------------
  174. END_NAMESPACE_DISTRHO