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.

125 lines
4.8KB

  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. An AudioIODeviceCallback object which streams audio through an AudioProcessor.
  24. To use one of these, just make it the callback used by your AudioIODevice, and
  25. give it a processor to use by calling setProcessor().
  26. It's also a MidiInputCallback, so you can connect it to both an audio and midi
  27. input to send both streams through the processor. To set a MidiOutput for the processor,
  28. use the setMidiOutput() method.
  29. @see AudioProcessor, AudioProcessorGraph
  30. @tags{Audio}
  31. */
  32. class JUCE_API AudioProcessorPlayer : public AudioIODeviceCallback,
  33. public MidiInputCallback
  34. {
  35. public:
  36. //==============================================================================
  37. AudioProcessorPlayer (bool doDoublePrecisionProcessing = false);
  38. /** Destructor. */
  39. virtual ~AudioProcessorPlayer();
  40. //==============================================================================
  41. /** Sets the processor that should be played.
  42. The processor that is passed in will not be deleted or owned by this object.
  43. To stop anything playing, pass a nullptr to this method.
  44. */
  45. void setProcessor (AudioProcessor* processorToPlay);
  46. /** Returns the current audio processor that is being played. */
  47. AudioProcessor* getCurrentProcessor() const noexcept { return processor; }
  48. /** Returns a midi message collector that you can pass midi messages to if you
  49. want them to be injected into the midi stream that is being sent to the
  50. processor.
  51. */
  52. MidiMessageCollector& getMidiMessageCollector() noexcept { return messageCollector; }
  53. /** Sets the MIDI output that should be used, if required.
  54. The MIDI output will not be deleted or owned by this object. If the MIDI output is
  55. deleted, pass a nullptr to this method.
  56. */
  57. void setMidiOutput (MidiOutput* midiOutputToUse);
  58. /** Switch between double and single floating point precisions processing.
  59. The audio IO callbacks will still operate in single floating point precision,
  60. however, all internal processing including the AudioProcessor will be processed in
  61. double floating point precision if the AudioProcessor supports it (see
  62. AudioProcessor::supportsDoublePrecisionProcessing()). Otherwise, the processing will
  63. remain single precision irrespective of the parameter doublePrecision.
  64. */
  65. void setDoublePrecisionProcessing (bool doublePrecision);
  66. /** Returns true if this player processes internally processes the samples with
  67. double floating point precision.
  68. */
  69. inline bool getDoublePrecisionProcessing() { return isDoublePrecision; }
  70. //==============================================================================
  71. /** @internal */
  72. void audioDeviceIOCallback (const float**, int, float**, int, int) override;
  73. /** @internal */
  74. void audioDeviceAboutToStart (AudioIODevice*) override;
  75. /** @internal */
  76. void audioDeviceStopped() override;
  77. /** @internal */
  78. void handleIncomingMidiMessage (MidiInput*, const MidiMessage&) override;
  79. private:
  80. //==============================================================================
  81. AudioProcessor* processor = nullptr;
  82. CriticalSection lock;
  83. double sampleRate = 0;
  84. int blockSize = 0;
  85. bool isPrepared = false, isDoublePrecision = false;
  86. int numInputChans = 0, numOutputChans = 0;
  87. HeapBlock<float*> channels;
  88. AudioBuffer<float> tempBuffer;
  89. AudioBuffer<double> conversionBuffer;
  90. MidiBuffer incomingMidi;
  91. MidiMessageCollector messageCollector;
  92. MidiOutput* midiOutput = nullptr;
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioProcessorPlayer)
  94. };
  95. } // namespace juce