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.

223 lines
6.6KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-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. #include "DistrhoPluginInfo.h"
  17. #include "DistrhoUI.hpp"
  18. START_NAMESPACE_DISTRHO
  19. // -----------------------------------------------------------------------------------------------------------
  20. class InfoExampleUI : public UI
  21. {
  22. public:
  23. InfoExampleUI()
  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 = createFontFromFile("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) override
  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. // font
  125. FontId fFont;
  126. // temp buf for text
  127. char fStrBuf[0xff+1];
  128. // helpers for putting text into fStrBuf and returning it
  129. const char* getTextBufInt(const int value)
  130. {
  131. std::snprintf(fStrBuf, 0xff, "%i", value);
  132. return fStrBuf;
  133. }
  134. const char* getTextBufFloat(const float value)
  135. {
  136. std::snprintf(fStrBuf, 0xff, "%.1f", value);
  137. return fStrBuf;
  138. }
  139. const char* getTextBufTime(const uint64_t frame)
  140. {
  141. const uint32_t time = frame / uint64_t(fSampleRate);
  142. const uint32_t secs = time % 60;
  143. const uint32_t mins = (time / 60) % 60;
  144. const uint32_t hrs = (time / 3600) % 60;
  145. std::snprintf(fStrBuf, 0xff, "%02i:%02i:%02i", hrs, mins, secs);
  146. return fStrBuf;
  147. }
  148. // helpers for drawing text
  149. void drawLeft(const float x, const float y, const char* const text)
  150. {
  151. beginPath();
  152. fillColor(200, 200, 200);
  153. textAlign(ALIGN_RIGHT|ALIGN_TOP);
  154. textBox(x, y, 100, text);
  155. closePath();
  156. }
  157. void drawRight(const float x, const float y, const char* const text)
  158. {
  159. beginPath();
  160. fillColor(255, 255, 255);
  161. textAlign(ALIGN_LEFT|ALIGN_TOP);
  162. textBox(x+105, y, 100, text);
  163. closePath();
  164. }
  165. /**
  166. Set our UI class as non-copyable and add a leak detector just in case.
  167. */
  168. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(InfoExampleUI)
  169. };
  170. /* ------------------------------------------------------------------------------------------------------------
  171. * UI entry point, called by DPF to create a new UI instance. */
  172. UI* createUI()
  173. {
  174. return new InfoExampleUI();
  175. }
  176. // -----------------------------------------------------------------------------------------------------------
  177. END_NAMESPACE_DISTRHO