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.

138 lines
5.5KB

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