sound.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/unix/sound.h
  3. // Purpose: wxSound class
  4. // Author: Julian Smart, Vaclav Slavik
  5. // Modified by:
  6. // Created: 25/10/98
  7. // Copyright: (c) Julian Smart, Vaclav Slavik
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_SOUND_H_
  11. #define _WX_SOUND_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_SOUND
  14. #include "wx/object.h"
  15. // ----------------------------------------------------------------------------
  16. // wxSound: simple audio playback class
  17. // ----------------------------------------------------------------------------
  18. class WXDLLIMPEXP_FWD_ADV wxSoundBackend;
  19. class WXDLLIMPEXP_FWD_ADV wxSound;
  20. class WXDLLIMPEXP_FWD_BASE wxDynamicLibrary;
  21. /// Sound data, as loaded from .wav file:
  22. class WXDLLIMPEXP_ADV wxSoundData
  23. {
  24. public:
  25. wxSoundData() : m_refCnt(1) {}
  26. void IncRef();
  27. void DecRef();
  28. // .wav header information:
  29. unsigned m_channels; // num of channels (mono:1, stereo:2)
  30. unsigned m_samplingRate;
  31. unsigned m_bitsPerSample; // if 8, then m_data contains unsigned 8bit
  32. // samples (wxUint8), if 16 then signed 16bit
  33. // (wxInt16)
  34. unsigned m_samples; // length in samples:
  35. // wave data:
  36. size_t m_dataBytes;
  37. wxUint8 *m_data; // m_dataBytes bytes of data
  38. private:
  39. ~wxSoundData();
  40. unsigned m_refCnt;
  41. wxUint8 *m_dataWithHeader; // ditto, but prefixed with .wav header
  42. friend class wxSound;
  43. };
  44. /// Simple sound class:
  45. class WXDLLIMPEXP_ADV wxSound : public wxSoundBase
  46. {
  47. public:
  48. wxSound();
  49. wxSound(const wxString& fileName, bool isResource = false);
  50. wxSound(size_t size, const void* data);
  51. virtual ~wxSound();
  52. // Create from resource or file
  53. bool Create(const wxString& fileName, bool isResource = false);
  54. // Create from data
  55. bool Create(size_t size, const void* data);
  56. bool IsOk() const { return m_data != NULL; }
  57. // Stop playing any sound
  58. static void Stop();
  59. // Returns true if a sound is being played
  60. static bool IsPlaying();
  61. // for internal use
  62. static void UnloadBackend();
  63. protected:
  64. bool DoPlay(unsigned flags) const;
  65. static void EnsureBackend();
  66. void Free();
  67. bool LoadWAV(const void* data, size_t length, bool copyData);
  68. static wxSoundBackend *ms_backend;
  69. #if wxUSE_LIBSDL && wxUSE_PLUGINS
  70. // FIXME - temporary, until we have plugins architecture
  71. static wxDynamicLibrary *ms_backendSDL;
  72. #endif
  73. private:
  74. wxSoundData *m_data;
  75. };
  76. // ----------------------------------------------------------------------------
  77. // wxSoundBackend:
  78. // ----------------------------------------------------------------------------
  79. // This is interface to sound playing implementation. There are multiple
  80. // sound architectures in use on Unix platforms and wxWidgets can use several
  81. // of them for playback, depending on their availability at runtime; hence
  82. // the need for backends. This class is for use by wxWidgets and people writing
  83. // additional backends only, it is _not_ for use by applications!
  84. // Structure that holds playback status information
  85. struct wxSoundPlaybackStatus
  86. {
  87. // playback is in progress
  88. bool m_playing;
  89. // main thread called wxSound::Stop()
  90. bool m_stopRequested;
  91. };
  92. // Audio backend interface
  93. class WXDLLIMPEXP_ADV wxSoundBackend
  94. {
  95. public:
  96. virtual ~wxSoundBackend() {}
  97. // Returns the name of the backend (e.g. "Open Sound System")
  98. virtual wxString GetName() const = 0;
  99. // Returns priority (higher priority backends are tried first)
  100. virtual int GetPriority() const = 0;
  101. // Checks if the backend's audio system is available and the backend can
  102. // be used for playback
  103. virtual bool IsAvailable() const = 0;
  104. // Returns true if the backend is capable of playing sound asynchronously.
  105. // If false, then wxWidgets creates a playback thread and handles async
  106. // playback, otherwise it is left up to the backend (will usually be more
  107. // effective).
  108. virtual bool HasNativeAsyncPlayback() const = 0;
  109. // Plays the sound. flags are same flags as those passed to wxSound::Play.
  110. // The function should periodically check the value of
  111. // status->m_stopRequested and terminate if it is set to true (it may
  112. // be modified by another thread)
  113. virtual bool Play(wxSoundData *data, unsigned flags,
  114. volatile wxSoundPlaybackStatus *status) = 0;
  115. // Stops playback (if something is played).
  116. virtual void Stop() = 0;
  117. // Returns true if the backend is playing anything at the moment.
  118. // (This method is never called for backends that don't support async
  119. // playback.)
  120. virtual bool IsPlaying() const = 0;
  121. };
  122. #endif // wxUSE_SOUND
  123. #endif // _WX_SOUND_H_