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.

222 lines
6.6KB

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