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.

157 lines
5.6KB

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