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.

155 lines
5.6KB

  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. : AudioProcessor (BusesProperties()) // add no audio buses at all
  28. {
  29. addParameter (speed = new AudioParameterFloat ("speed", "Arpeggiator Speed", 0.0, 1.0, 0.5));
  30. }
  31. ~Arpeggiator() {}
  32. //==============================================================================
  33. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  34. {
  35. ignoreUnused (samplesPerBlock);
  36. notes.clear();
  37. currentNote = 0;
  38. lastNoteValue = -1;
  39. time = 0.0;
  40. rate = static_cast<float> (sampleRate);
  41. }
  42. void releaseResources() override {}
  43. void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midi) override
  44. {
  45. // the audio buffer in a midi effect will have zero channels!
  46. jassert (buffer.getNumChannels() == 0);
  47. // however we use the buffer to get timing information
  48. const int numSamples = buffer.getNumSamples();
  49. // get note duration
  50. const int noteDuration = static_cast<int> (std::ceil (rate * 0.25f * (0.1f + (1.0f - (*speed)))));
  51. MidiMessage msg;
  52. int ignore;
  53. for (MidiBuffer::Iterator it (midi); it.getNextEvent (msg, ignore);)
  54. {
  55. if (msg.isNoteOn()) notes.add (msg.getNoteNumber());
  56. else if (msg.isNoteOff()) notes.removeValue (msg.getNoteNumber());
  57. }
  58. midi.clear();
  59. if ((time + numSamples) >= noteDuration)
  60. {
  61. const int offset = jmax (0, jmin ((int) (noteDuration - time), numSamples - 1));
  62. if (lastNoteValue > 0)
  63. {
  64. midi.addEvent (MidiMessage::noteOff (1, lastNoteValue), offset);
  65. lastNoteValue = -1;
  66. }
  67. if (notes.size() > 0)
  68. {
  69. currentNote = (currentNote + 1) % notes.size();
  70. lastNoteValue = notes[currentNote];
  71. midi.addEvent (MidiMessage::noteOn (1, lastNoteValue, (uint8) 127), offset);
  72. }
  73. }
  74. time = (time + numSamples) % noteDuration;
  75. }
  76. //==============================================================================
  77. bool isMidiEffect() const override { return true; }
  78. //==============================================================================
  79. AudioProcessorEditor* createEditor() override { return new GenericEditor (*this); }
  80. bool hasEditor() const override { return true; }
  81. //==============================================================================
  82. const String getName() const override { return "Arpeggiator"; }
  83. bool acceptsMidi() const override { return true; }
  84. bool producesMidi() const override { return true; }
  85. double getTailLengthSeconds() const override { return 0; }
  86. //==============================================================================
  87. int getNumPrograms() override { return 1; }
  88. int getCurrentProgram() override { return 0; }
  89. void setCurrentProgram (int) override {}
  90. const String getProgramName (int) override { return String(); }
  91. void changeProgramName (int , const String& ) override { }
  92. //==============================================================================
  93. void getStateInformation (MemoryBlock& destData) override
  94. {
  95. MemoryOutputStream (destData, true).writeFloat (*speed);
  96. }
  97. void setStateInformation (const void* data, int sizeInBytes) override
  98. {
  99. speed->setValueNotifyingHost (MemoryInputStream (data, static_cast<size_t> (sizeInBytes), false).readFloat());
  100. }
  101. //==============================================================================
  102. private:
  103. //==============================================================================
  104. AudioParameterFloat* speed;
  105. int currentNote, lastNoteValue;
  106. int time;
  107. float rate;
  108. SortedSet<int> notes;
  109. //==============================================================================
  110. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Arpeggiator)
  111. };
  112. //==============================================================================
  113. // This creates new instances of the plugin..
  114. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  115. {
  116. return new Arpeggiator();
  117. }