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