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.

137 lines
5.5KB

  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. A simple sound player that you can add to the AudioDeviceManager to play
  23. simple sounds.
  24. @see AudioProcessor, AudioProcessorGraph
  25. @tags{Audio}
  26. */
  27. class JUCE_API SoundPlayer : public AudioIODeviceCallback
  28. {
  29. public:
  30. //==============================================================================
  31. SoundPlayer();
  32. /** Destructor. */
  33. ~SoundPlayer() override;
  34. //==============================================================================
  35. /** Plays a sound from a file. */
  36. void play (const File& file);
  37. /** Convenient method to play sound from a JUCE resource. */
  38. void play (const void* resourceData, size_t resourceSize);
  39. /** Plays the sound from an audio format reader.
  40. If deleteWhenFinished is true then the format reader will be
  41. automatically deleted once the sound has finished playing.
  42. */
  43. void play (AudioFormatReader* buffer, bool deleteWhenFinished = false);
  44. /** Plays the sound from a positionable audio source.
  45. This will output the sound coming from a positionable audio source.
  46. This gives you slightly more control over the sound playback compared
  47. to the other playSound methods. For example, if you would like to
  48. stop the sound prematurely you can call this method with a
  49. TransportAudioSource and then call audioSource->stop. Note that,
  50. you must call audioSource->start to start the playback, if your
  51. audioSource is a TransportAudioSource.
  52. The audio device manager will not hold any references to this audio
  53. source once the audio source has stopped playing for any reason,
  54. for example when the sound has finished playing or when you have
  55. called audioSource->stop. Therefore, calling audioSource->start() on
  56. a finished audioSource will not restart the sound again. If this is
  57. desired simply call playSound with the same audioSource again.
  58. @param audioSource the audio source to play
  59. @param deleteWhenFinished If this is true then the audio source will
  60. be deleted once the device manager has finished
  61. playing.
  62. @param sampleRateOfSource The sample rate of the source. If this is zero, JUCE
  63. will assume that the sample rate is the same as the
  64. audio output device.
  65. */
  66. void play (PositionableAudioSource* audioSource, bool deleteWhenFinished = false,
  67. double sampleRateOfSource = 0.0);
  68. /** Plays the sound from an audio sample buffer.
  69. This will output the sound contained in an audio sample buffer. If
  70. deleteWhenFinished is true then the audio sample buffer will be
  71. automatically deleted once the sound has finished playing.
  72. If playOnAllOutputChannels is true, then if there are more output channels
  73. than buffer channels, then the ones that are available will be re-used on
  74. multiple outputs so that something is sent to all output channels. If it
  75. is false, then the buffer will just be played on the first output channels.
  76. */
  77. void play (AudioBuffer<float>* buffer,
  78. bool deleteWhenFinished = false,
  79. bool playOnAllOutputChannels = false);
  80. /** Plays a beep through the current audio device.
  81. This is here to allow the audio setup UI panels to easily include a "test"
  82. button so that the user can check where the audio is coming from.
  83. */
  84. void playTestSound();
  85. //==============================================================================
  86. /** @internal */
  87. void audioDeviceIOCallback (const float**, int, float**, int, int) override;
  88. /** @internal */
  89. void audioDeviceAboutToStart (AudioIODevice*) override;
  90. /** @internal */
  91. void audioDeviceStopped() override;
  92. /** @internal */
  93. void audioDeviceError (const String& errorMessage) override;
  94. private:
  95. //==============================================================================
  96. AudioFormatManager formatManager;
  97. AudioSourcePlayer player;
  98. MixerAudioSource mixer;
  99. OwnedArray<AudioSource> sources;
  100. //==============================================================================
  101. double sampleRate;
  102. int bufferSize;
  103. //==============================================================================
  104. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SoundPlayer)
  105. };
  106. } // namespace juce