The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

161 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceLibraryCode/JuceHeader.h"
  18. #include "../../GenericEditor.h"
  19. //==============================================================================
  20. /**
  21. */
  22. class Arpeggiator : public AudioProcessor
  23. {
  24. public:
  25. //==============================================================================
  26. Arpeggiator()
  27. {
  28. addParameter (speed = new AudioParameterFloat ("speed", "Arpeggiator Speed", 0.0, 1.0, 0.5));
  29. }
  30. ~Arpeggiator() {}
  31. //==============================================================================
  32. bool setPreferredBusArrangement (bool isInputBus, int busIndex,
  33. const AudioChannelSet& preferred) override
  34. {
  35. ignoreUnused (isInputBus, busIndex, preferred);
  36. // we don't support any audio buses
  37. return false;
  38. }
  39. //==============================================================================
  40. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  41. {
  42. ignoreUnused (samplesPerBlock);
  43. notes.clear();
  44. currentNote = 0;
  45. lastNoteValue = -1;
  46. time = 0.0;
  47. rate = static_cast<float> (sampleRate);
  48. }
  49. void releaseResources() override {}
  50. void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midi) override
  51. {
  52. // the audio buffer in a midi effect will have zero channels!
  53. jassert (buffer.getNumChannels() == 0);
  54. // however we use the buffer to get timing information
  55. const int numSamples = buffer.getNumSamples();
  56. // get note duration
  57. const int noteDuration = static_cast<int> (std::ceil (rate * 0.25f * (0.1f + (1.0f - (*speed)))));
  58. MidiMessage msg;
  59. int ignore;
  60. for (MidiBuffer::Iterator it (midi); it.getNextEvent (msg, ignore);)
  61. {
  62. if (msg.isNoteOn()) notes.add (msg.getNoteNumber());
  63. else if (msg.isNoteOff()) notes.removeValue (msg.getNoteNumber());
  64. }
  65. midi.clear();
  66. if ((time + numSamples) >= noteDuration)
  67. {
  68. const int offset = jmax (0, jmin ((int) (noteDuration - time), numSamples - 1));
  69. if (lastNoteValue > 0)
  70. {
  71. midi.addEvent (MidiMessage::noteOff (1, lastNoteValue), offset);
  72. lastNoteValue = -1;
  73. }
  74. if (notes.size() > 0)
  75. {
  76. currentNote = (currentNote + 1) % notes.size();
  77. lastNoteValue = notes[currentNote];
  78. midi.addEvent (MidiMessage::noteOn (1, lastNoteValue, (uint8) 127), offset);
  79. }
  80. }
  81. time = (time + numSamples) % noteDuration;
  82. }
  83. //==============================================================================
  84. AudioProcessorEditor* createEditor() override { return new GenericEditor (*this); }
  85. bool hasEditor() const override { return true; }
  86. //==============================================================================
  87. const String getName() const override { return "Arpeggiator"; }
  88. bool acceptsMidi() const override { return false; }
  89. bool producesMidi() const override { return false; }
  90. double getTailLengthSeconds() const override { return 0; }
  91. //==============================================================================
  92. int getNumPrograms() override { return 1; }
  93. int getCurrentProgram() override { return 0; }
  94. void setCurrentProgram (int) override {}
  95. const String getProgramName (int) override { return String(); }
  96. void changeProgramName (int , const String& ) override { }
  97. //==============================================================================
  98. void getStateInformation (MemoryBlock& destData) override
  99. {
  100. MemoryOutputStream (destData, true).writeFloat (*speed);
  101. }
  102. void setStateInformation (const void* data, int sizeInBytes) override
  103. {
  104. speed->setValueNotifyingHost (MemoryInputStream (data, static_cast<size_t> (sizeInBytes), false).readFloat());
  105. }
  106. //==============================================================================
  107. private:
  108. //==============================================================================
  109. AudioParameterFloat* speed;
  110. int currentNote, lastNoteValue;
  111. int time;
  112. float rate;
  113. SortedSet<int> notes;
  114. //==============================================================================
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Arpeggiator)
  116. };
  117. //==============================================================================
  118. // This creates new instances of the plugin..
  119. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  120. {
  121. return new Arpeggiator();
  122. }