1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/sound.h
3 // Purpose: wxSound class
4 // Author: Julian Smart, Vaclav Slavik
8 // Copyright: (c) Julian Smart, Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
19 #include "wx/object.h"
21 // ----------------------------------------------------------------------------
22 // wxSound: simple audio playback class
23 // ----------------------------------------------------------------------------
25 class WXDLLIMPEXP_FWD_ADV wxSoundBackend
;
26 class WXDLLIMPEXP_FWD_ADV wxSound
;
27 class WXDLLIMPEXP_FWD_BASE wxDynamicLibrary
;
29 /// Sound data, as loaded from .wav file:
30 class WXDLLIMPEXP_ADV wxSoundData
33 wxSoundData() : m_refCnt(1) {}
37 // .wav header information:
38 unsigned m_channels
; // num of channels (mono:1, stereo:2)
39 unsigned m_samplingRate
;
40 unsigned m_bitsPerSample
; // if 8, then m_data contains unsigned 8bit
41 // samples (wxUint8), if 16 then signed 16bit
43 unsigned m_samples
; // length in samples:
47 wxUint8
*m_data
; // m_dataBytes bytes of data
52 wxUint8
*m_dataWithHeader
; // ditto, but prefixed with .wav header
57 /// Simple sound class:
58 class WXDLLIMPEXP_ADV wxSound
: public wxSoundBase
62 wxSound(const wxString
& fileName
, bool isResource
= false);
63 wxSound(int size
, const wxByte
* data
);
66 // Create from resource or file
67 bool Create(const wxString
& fileName
, bool isResource
= false);
69 bool Create(int size
, const wxByte
* data
);
71 bool IsOk() const { return m_data
!= NULL
; }
73 // Stop playing any sound
76 // Returns true if a sound is being played
77 static bool IsPlaying();
80 static void UnloadBackend();
83 bool DoPlay(unsigned flags
) const;
85 static void EnsureBackend();
87 bool LoadWAV(const wxUint8
*data
, size_t length
, bool copyData
);
89 static wxSoundBackend
*ms_backend
;
90 #if wxUSE_LIBSDL && wxUSE_PLUGINS
91 // FIXME - temporary, until we have plugins architecture
92 static wxDynamicLibrary
*ms_backendSDL
;
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 // This is interface to sound playing implementation. There are multiple
105 // sound architectures in use on Unix platforms and wxWidgets can use several
106 // of them for playback, depending on their availability at runtime; hence
107 // the need for backends. This class is for use by wxWidgets and people writing
108 // additional backends only, it is _not_ for use by applications!
110 // Structure that holds playback status information
111 struct wxSoundPlaybackStatus
113 // playback is in progress
115 // main thread called wxSound::Stop()
116 bool m_stopRequested
;
119 // Audio backend interface
120 class WXDLLIMPEXP_ADV wxSoundBackend
123 virtual ~wxSoundBackend() {}
125 // Returns the name of the backend (e.g. "Open Sound System")
126 virtual wxString
GetName() const = 0;
128 // Returns priority (higher priority backends are tried first)
129 virtual int GetPriority() const = 0;
131 // Checks if the backend's audio system is available and the backend can
132 // be used for playback
133 virtual bool IsAvailable() const = 0;
135 // Returns true if the backend is capable of playing sound asynchronously.
136 // If false, then wxWidgets creates a playback thread and handles async
137 // playback, otherwise it is left up to the backend (will usually be more
139 virtual bool HasNativeAsyncPlayback() const = 0;
141 // Plays the sound. flags are same flags as those passed to wxSound::Play.
142 // The function should periodically check the value of
143 // status->m_stopRequested and terminate if it is set to true (it may
144 // be modified by another thread)
145 virtual bool Play(wxSoundData
*data
, unsigned flags
,
146 volatile wxSoundPlaybackStatus
*status
) = 0;
148 // Stops playback (if something is played).
149 virtual void Stop() = 0;
151 // Returns true if the backend is playing anything at the moment.
152 // (This method is never called for backends that don't support async
154 virtual bool IsPlaying() const = 0;
158 #endif // wxUSE_SOUND
160 #endif // _WX_SOUND_H_