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.

167 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: ArpeggiatorPlugin
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Arpeggiator audio plugin.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_plugin_client, juce_audio_processors,
  26. juce_audio_utils, juce_core, juce_data_structures,
  27. juce_events, juce_graphics, juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2022
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: AudioProcessor
  31. mainClass: Arpeggiator
  32. useLocalCopy: 1
  33. pluginCharacteristics: pluginWantsMidiIn, pluginProducesMidiOut, pluginIsMidiEffectPlugin
  34. END_JUCE_PIP_METADATA
  35. *******************************************************************************/
  36. #pragma once
  37. //==============================================================================
  38. class Arpeggiator : public AudioProcessor
  39. {
  40. public:
  41. //==============================================================================
  42. Arpeggiator()
  43. : AudioProcessor (BusesProperties()) // add no audio buses at all
  44. {
  45. addParameter (speed = new AudioParameterFloat ({ "speed", 1 }, "Arpeggiator Speed", 0.0, 1.0, 0.5));
  46. }
  47. //==============================================================================
  48. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  49. {
  50. ignoreUnused (samplesPerBlock);
  51. notes.clear();
  52. currentNote = 0;
  53. lastNoteValue = -1;
  54. time = 0;
  55. rate = static_cast<float> (sampleRate);
  56. }
  57. void releaseResources() override {}
  58. void processBlock (AudioBuffer<float>& buffer, MidiBuffer& midi) override
  59. {
  60. // A pure MIDI plugin shouldn't be provided any audio data
  61. jassert (buffer.getNumChannels() == 0);
  62. // however we use the buffer to get timing information
  63. auto numSamples = buffer.getNumSamples();
  64. // get note duration
  65. auto noteDuration = static_cast<int> (std::ceil (rate * 0.25f * (0.1f + (1.0f - (*speed)))));
  66. for (const auto metadata : midi)
  67. {
  68. const auto msg = metadata.getMessage();
  69. if (msg.isNoteOn()) notes.add (msg.getNoteNumber());
  70. else if (msg.isNoteOff()) notes.removeValue (msg.getNoteNumber());
  71. }
  72. midi.clear();
  73. if ((time + numSamples) >= noteDuration)
  74. {
  75. auto offset = jmax (0, jmin ((int) (noteDuration - time), numSamples - 1));
  76. if (lastNoteValue > 0)
  77. {
  78. midi.addEvent (MidiMessage::noteOff (1, lastNoteValue), offset);
  79. lastNoteValue = -1;
  80. }
  81. if (notes.size() > 0)
  82. {
  83. currentNote = (currentNote + 1) % notes.size();
  84. lastNoteValue = notes[currentNote];
  85. midi.addEvent (MidiMessage::noteOn (1, lastNoteValue, (uint8) 127), offset);
  86. }
  87. }
  88. time = (time + numSamples) % noteDuration;
  89. }
  90. using AudioProcessor::processBlock;
  91. //==============================================================================
  92. bool isMidiEffect() const override { return true; }
  93. //==============================================================================
  94. AudioProcessorEditor* createEditor() override { return new GenericAudioProcessorEditor (*this); }
  95. bool hasEditor() const override { return true; }
  96. //==============================================================================
  97. const String getName() const override { return "Arpeggiator"; }
  98. bool acceptsMidi() const override { return true; }
  99. bool producesMidi() const override { return true; }
  100. double getTailLengthSeconds() const override { return 0; }
  101. //==============================================================================
  102. int getNumPrograms() override { return 1; }
  103. int getCurrentProgram() override { return 0; }
  104. void setCurrentProgram (int) override {}
  105. const String getProgramName (int) override { return "None"; }
  106. void changeProgramName (int, const String&) override {}
  107. //==============================================================================
  108. void getStateInformation (MemoryBlock& destData) override
  109. {
  110. MemoryOutputStream (destData, true).writeFloat (*speed);
  111. }
  112. void setStateInformation (const void* data, int sizeInBytes) override
  113. {
  114. speed->setValueNotifyingHost (MemoryInputStream (data, static_cast<size_t> (sizeInBytes), false).readFloat());
  115. }
  116. private:
  117. //==============================================================================
  118. AudioParameterFloat* speed;
  119. int currentNote, lastNoteValue;
  120. int time;
  121. float rate;
  122. SortedSet<int> notes;
  123. //==============================================================================
  124. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Arpeggiator)
  125. };