LFOPlugin.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * LFOPlugin.h
  3. * -----------
  4. * Purpose: Plugin for automating other plugins' parameters
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #ifndef NO_PLUGINS
  12. #include "PlugInterface.h"
  13. #include "../../common/mptRandom.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. class LFOPlugin final : public IMixPlugin
  16. {
  17. friend class LFOPluginEditor;
  18. protected:
  19. enum Parameters
  20. {
  21. kAmplitude = 0,
  22. kOffset,
  23. kFrequency,
  24. kTempoSync,
  25. kWaveform,
  26. kPolarity,
  27. kBypassed,
  28. kLoopMode,
  29. kCurrentPhase,
  30. kLFONumParameters
  31. };
  32. enum LFOWaveform
  33. {
  34. kSine = 0,
  35. kTriangle,
  36. kSaw,
  37. kSquare,
  38. kSHNoise,
  39. kSmoothNoise,
  40. kNumWaveforms
  41. };
  42. std::vector<std::byte> m_chunkData;
  43. static constexpr PlugParamIndex INVALID_OUTPUT_PARAM = uint32_max;
  44. // LFO parameters
  45. float m_amplitude = 0.5f, m_offset = 0.5f, m_frequency = 0.290241f;
  46. LFOWaveform m_waveForm = kSine;
  47. PlugParamIndex m_outputParam = INVALID_OUTPUT_PARAM;
  48. bool m_tempoSync = false, m_polarity = false, m_bypassed = false, m_outputToCC = false, m_oneshot = false;
  49. // LFO state
  50. double m_computedFrequency = 0.0;
  51. double m_phase = 0.0, m_increment = 0.0;
  52. double m_random = 0.0, m_nextRandom = 0.0;
  53. double m_tempo = 0.0;
  54. mpt::fast_prng m_PRNG;
  55. #ifdef MODPLUG_TRACKER
  56. static constexpr int WM_PARAM_UDPATE = WM_USER + 500;
  57. #endif
  58. public:
  59. static IMixPlugin* Create(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct);
  60. LFOPlugin(VSTPluginLib &factory, CSoundFile &sndFile, SNDMIXPLUGIN *mixStruct);
  61. void Release() override { delete this; }
  62. int32 GetUID() const override { int32 id; memcpy(&id, "LFO ", 4); return id; }
  63. int32 GetVersion() const override { return 0; }
  64. void Idle() override { }
  65. uint32 GetLatency() const override { return 0; }
  66. void Process(float *pOutL, float *pOutR, uint32 numFrames) override;
  67. float RenderSilence(uint32) override { return 0.0f; }
  68. // MIDI event handling (mostly passing it through to the follow-up plugin)
  69. bool MidiSend(uint32 midiCode) override;
  70. bool MidiSysexSend(mpt::const_byte_span sysex) override;
  71. void MidiCC(MIDIEvents::MidiCC nController, uint8 nParam, CHANNELINDEX trackChannel) override;
  72. void MidiPitchBend(int32 increment, int8 pwd, CHANNELINDEX trackChannel) override;
  73. void MidiVibrato(int32 depth, int8 pwd, CHANNELINDEX trackChannel) override;
  74. void MidiCommand(const ModInstrument &instr, uint16 note, uint16 vol, CHANNELINDEX trackChannel) override;
  75. void HardAllNotesOff() override;
  76. bool IsNotePlaying(uint8 note, CHANNELINDEX trackerChn) override;
  77. int32 GetNumPrograms() const override { return 0; }
  78. int32 GetCurrentProgram() override { return 0; }
  79. void SetCurrentProgram(int32) override { }
  80. PlugParamIndex GetNumParameters() const override { return kLFONumParameters; }
  81. PlugParamValue GetParameter(PlugParamIndex index) override;
  82. void SetParameter(PlugParamIndex index, PlugParamValue value) override;
  83. void Resume() override;
  84. void Suspend() override { m_isResumed = false; }
  85. void PositionChanged() override;
  86. bool IsInstrument() const override { return false; }
  87. bool CanRecieveMidiEvents() override { return false; }
  88. bool ShouldProcessSilence() override { return true; }
  89. #ifdef MODPLUG_TRACKER
  90. CString GetDefaultEffectName() override { return _T("LFO"); }
  91. std::pair<PlugParamValue, PlugParamValue> GetParamUIRange(PlugParamIndex param) override;
  92. CString GetParamName(PlugParamIndex param) override;
  93. CString GetParamLabel(PlugParamIndex) override;
  94. CString GetParamDisplay(PlugParamIndex param) override;
  95. CString GetCurrentProgramName() override { return CString(); }
  96. void SetCurrentProgramName(const CString &) override { }
  97. CString GetProgramName(int32) override { return CString(); }
  98. bool HasEditor() const override { return true; }
  99. protected:
  100. CAbstractVstEditor *OpenEditor() override;
  101. #endif
  102. public:
  103. int GetNumInputChannels() const override { return 2; }
  104. int GetNumOutputChannels() const override { return 2; }
  105. bool ProgramsAreChunks() const override { return true; }
  106. // Save parameters for storing them in a module file
  107. void SaveAllParameters() override;
  108. // Restore parameters from module file
  109. void RestoreAllParameters(int32 program) override;
  110. ChunkData GetChunk(bool) override;
  111. void SetChunk(const ChunkData &chunk, bool) override;
  112. protected:
  113. void NextRandom();
  114. void RecalculateFrequency();
  115. void RecalculateIncrement();
  116. IMixPlugin *GetOutputPlugin() const;
  117. public:
  118. static LFOWaveform ParamToWaveform(float param) { return static_cast<LFOWaveform>(std::clamp(mpt::saturate_round<int>(param * 32.0f), 0, kNumWaveforms - 1)); }
  119. static float WaveformToParam(LFOWaveform waveform) { return static_cast<int>(waveform) / 32.0f; }
  120. };
  121. OPENMPT_NAMESPACE_END
  122. #endif // NO_PLUGINS