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.

153 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceLibraryCode/JuceHeader.h"
  20. //==============================================================================
  21. /**
  22. */
  23. class Arpeggiator : public AudioProcessor
  24. {
  25. public:
  26. //==============================================================================
  27. Arpeggiator()
  28. : AudioProcessor (BusesProperties()) // add no audio buses at all
  29. {
  30. addParameter (speed = new AudioParameterFloat ("speed", "Arpeggiator Speed", 0.0, 1.0, 0.5));
  31. }
  32. ~Arpeggiator() {}
  33. //==============================================================================
  34. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  35. {
  36. ignoreUnused (samplesPerBlock);
  37. notes.clear();
  38. currentNote = 0;
  39. lastNoteValue = -1;
  40. time = 0.0;
  41. rate = static_cast<float> (sampleRate);
  42. }
  43. void releaseResources() override {}
  44. void processBlock (AudioBuffer<float>& buffer, MidiBuffer& midi) override
  45. {
  46. // the audio buffer in a midi effect will have zero channels!
  47. jassert (buffer.getNumChannels() == 0);
  48. // however we use the buffer to get timing information
  49. auto numSamples = buffer.getNumSamples();
  50. // get note duration
  51. auto noteDuration = static_cast<int> (std::ceil (rate * 0.25f * (0.1f + (1.0f - (*speed)))));
  52. MidiMessage msg;
  53. int ignore;
  54. for (MidiBuffer::Iterator it (midi); it.getNextEvent (msg, ignore);)
  55. {
  56. if (msg.isNoteOn()) notes.add (msg.getNoteNumber());
  57. else if (msg.isNoteOff()) notes.removeValue (msg.getNoteNumber());
  58. }
  59. midi.clear();
  60. if ((time + numSamples) >= noteDuration)
  61. {
  62. auto offset = jmax (0, jmin ((int) (noteDuration - time), numSamples - 1));
  63. if (lastNoteValue > 0)
  64. {
  65. midi.addEvent (MidiMessage::noteOff (1, lastNoteValue), offset);
  66. lastNoteValue = -1;
  67. }
  68. if (notes.size() > 0)
  69. {
  70. currentNote = (currentNote + 1) % notes.size();
  71. lastNoteValue = notes[currentNote];
  72. midi.addEvent (MidiMessage::noteOn (1, lastNoteValue, (uint8) 127), offset);
  73. }
  74. }
  75. time = (time + numSamples) % noteDuration;
  76. }
  77. //==============================================================================
  78. bool isMidiEffect() const override { return true; }
  79. //==============================================================================
  80. AudioProcessorEditor* createEditor() override { return new GenericAudioProcessorEditor (this); }
  81. bool hasEditor() const override { return true; }
  82. //==============================================================================
  83. const String getName() const override { return "Arpeggiator"; }
  84. bool acceptsMidi() const override { return true; }
  85. bool producesMidi() const override { return true; }
  86. double getTailLengthSeconds() const override { return 0; }
  87. //==============================================================================
  88. int getNumPrograms() override { return 1; }
  89. int getCurrentProgram() override { return 0; }
  90. void setCurrentProgram (int) override {}
  91. const String getProgramName (int) override { return String(); }
  92. void changeProgramName (int , const String& ) override { }
  93. //==============================================================================
  94. void getStateInformation (MemoryBlock& destData) override
  95. {
  96. MemoryOutputStream (destData, true).writeFloat (*speed);
  97. }
  98. void setStateInformation (const void* data, int sizeInBytes) override
  99. {
  100. speed->setValueNotifyingHost (MemoryInputStream (data, static_cast<size_t> (sizeInBytes), false).readFloat());
  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. }