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.

145 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. An AudioIODeviceCallback object which streams audio through an AudioProcessor.
  23. To use one of these, just make it the callback used by your AudioIODevice, and
  24. give it a processor to use by calling setProcessor().
  25. It's also a MidiInputCallback, so you can connect it to both an audio and midi
  26. input to send both streams through the processor. To set a MidiOutput for the processor,
  27. use the setMidiOutput() method.
  28. @see AudioProcessor, AudioProcessorGraph
  29. @tags{Audio}
  30. */
  31. class JUCE_API AudioProcessorPlayer : public AudioIODeviceCallback,
  32. public MidiInputCallback
  33. {
  34. public:
  35. //==============================================================================
  36. AudioProcessorPlayer (bool doDoublePrecisionProcessing = false);
  37. /** Destructor. */
  38. ~AudioProcessorPlayer() override;
  39. //==============================================================================
  40. /** Sets the processor that should be played.
  41. The processor that is passed in will not be deleted or owned by this object.
  42. To stop anything playing, pass a nullptr to this method.
  43. */
  44. void setProcessor (AudioProcessor* processorToPlay);
  45. /** Returns the current audio processor that is being played. */
  46. AudioProcessor* getCurrentProcessor() const noexcept { return processor; }
  47. /** Returns a midi message collector that you can pass midi messages to if you
  48. want them to be injected into the midi stream that is being sent to the
  49. processor.
  50. */
  51. MidiMessageCollector& getMidiMessageCollector() noexcept { return messageCollector; }
  52. /** Sets the MIDI output that should be used, if required.
  53. The MIDI output will not be deleted or owned by this object. If the MIDI output is
  54. deleted, pass a nullptr to this method.
  55. */
  56. void setMidiOutput (MidiOutput* midiOutputToUse);
  57. /** Switch between double and single floating point precisions processing.
  58. The audio IO callbacks will still operate in single floating point precision,
  59. however, all internal processing including the AudioProcessor will be processed in
  60. double floating point precision if the AudioProcessor supports it (see
  61. AudioProcessor::supportsDoublePrecisionProcessing()). Otherwise, the processing will
  62. remain single precision irrespective of the parameter doublePrecision.
  63. */
  64. void setDoublePrecisionProcessing (bool doublePrecision);
  65. /** Returns true if this player processes internally processes the samples with
  66. double floating point precision.
  67. */
  68. inline bool getDoublePrecisionProcessing() { return isDoublePrecision; }
  69. //==============================================================================
  70. /** @internal */
  71. void audioDeviceIOCallback (const float**, int, float**, int, int) override;
  72. /** @internal */
  73. void audioDeviceAboutToStart (AudioIODevice*) override;
  74. /** @internal */
  75. void audioDeviceStopped() override;
  76. /** @internal */
  77. void handleIncomingMidiMessage (MidiInput*, const MidiMessage&) override;
  78. private:
  79. struct NumChannels
  80. {
  81. NumChannels() = default;
  82. NumChannels (int numIns, int numOuts) : ins (numIns), outs (numOuts) {}
  83. explicit NumChannels (const AudioProcessor::BusesLayout& layout)
  84. : ins (layout.getNumChannels (true, 0)), outs (layout.getNumChannels (false, 0)) {}
  85. AudioProcessor::BusesLayout toLayout() const
  86. {
  87. return { { AudioChannelSet::canonicalChannelSet (ins) },
  88. { AudioChannelSet::canonicalChannelSet (outs) } };
  89. }
  90. int ins = 0, outs = 0;
  91. };
  92. //==============================================================================
  93. NumChannels findMostSuitableLayout (const AudioProcessor&) const;
  94. void resizeChannels();
  95. //==============================================================================
  96. AudioProcessor* processor = nullptr;
  97. CriticalSection lock;
  98. double sampleRate = 0;
  99. int blockSize = 0;
  100. bool isPrepared = false, isDoublePrecision = false;
  101. NumChannels deviceChannels, defaultProcessorChannels, actualProcessorChannels;
  102. std::vector<float*> channels;
  103. AudioBuffer<float> tempBuffer;
  104. AudioBuffer<double> conversionBuffer;
  105. MidiBuffer incomingMidi;
  106. MidiMessageCollector messageCollector;
  107. MidiOutput* midiOutput = nullptr;
  108. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorPlayer)
  109. };
  110. } // namespace juce