1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/sound.h
3 // Purpose: wxSound class
4 // Author: Julian Smart, Vaclav Slavik
7 // Copyright: (c) Julian Smart, Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
18 #include "wx/object.h"
20 // ----------------------------------------------------------------------------
21 // wxSound: simple audio playback class
22 // ----------------------------------------------------------------------------
24 class WXDLLIMPEXP_FWD_ADV wxSoundBackend
;
25 class WXDLLIMPEXP_FWD_ADV wxSound
;
26 class WXDLLIMPEXP_FWD_BASE wxDynamicLibrary
;
28 /// Sound data, as loaded from .wav file:
29 class WXDLLIMPEXP_ADV wxSoundData
32 wxSoundData() : m_refCnt(1) {}
36 // .wav header information:
37 unsigned m_channels
; // num of channels (mono:1, stereo:2)
38 unsigned m_samplingRate
;
39 unsigned m_bitsPerSample
; // if 8, then m_data contains unsigned 8bit
40 // samples (wxUint8), if 16 then signed 16bit
42 unsigned m_samples
; // length in samples:
46 wxUint8
*m_data
; // m_dataBytes bytes of data
51 wxUint8
*m_dataWithHeader
; // ditto, but prefixed with .wav header
56 /// Simple sound class:
57 class WXDLLIMPEXP_ADV wxSound
: public wxSoundBase
61 wxSound(const wxString
& fileName
, bool isResource
= false);
62 wxSound(size_t size
, const void* data
);
65 // Create from resource or file
66 bool Create(const wxString
& fileName
, bool isResource
= false);
68 bool Create(size_t size
, const void* data
);
70 bool IsOk() const { return m_data
!= NULL
; }
72 // Stop playing any sound
75 // Returns true if a sound is being played
76 static bool IsPlaying();
79 static void UnloadBackend();
82 bool DoPlay(unsigned flags
) const;
84 static void EnsureBackend();
86 bool LoadWAV(const void* data
, size_t length
, bool copyData
);
88 static wxSoundBackend
*ms_backend
;
89 #if wxUSE_LIBSDL && wxUSE_PLUGINS
90 // FIXME - temporary, until we have plugins architecture
91 static wxDynamicLibrary
*ms_backendSDL
;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // This is interface to sound playing implementation. There are multiple
104 // sound architectures in use on Unix platforms and wxWidgets can use several
105 // of them for playback, depending on their availability at runtime; hence
106 // the need for backends. This class is for use by wxWidgets and people writing
107 // additional backends only, it is _not_ for use by applications!
109 // Structure that holds playback status information
110 struct wxSoundPlaybackStatus
112 // playback is in progress
114 // main thread called wxSound::Stop()
115 bool m_stopRequested
;
118 // Audio backend interface
119 class WXDLLIMPEXP_ADV wxSoundBackend
122 virtual ~wxSoundBackend() {}
124 // Returns the name of the backend (e.g. "Open Sound System")
125 virtual wxString
GetName() const = 0;
127 // Returns priority (higher priority backends are tried first)
128 virtual int GetPriority() const = 0;
130 // Checks if the backend's audio system is available and the backend can
131 // be used for playback
132 virtual bool IsAvailable() const = 0;
134 // Returns true if the backend is capable of playing sound asynchronously.
135 // If false, then wxWidgets creates a playback thread and handles async
136 // playback, otherwise it is left up to the backend (will usually be more
138 virtual bool HasNativeAsyncPlayback() const = 0;
140 // Plays the sound. flags are same flags as those passed to wxSound::Play.
141 // The function should periodically check the value of
142 // status->m_stopRequested and terminate if it is set to true (it may
143 // be modified by another thread)
144 virtual bool Play(wxSoundData
*data
, unsigned flags
,
145 volatile wxSoundPlaybackStatus
*status
) = 0;
147 // Stops playback (if something is played).
148 virtual void Stop() = 0;
150 // Returns true if the backend is playing anything at the moment.
151 // (This method is never called for backends that don't support async
153 virtual bool IsPlaying() const = 0;
157 #endif // wxUSE_SOUND
159 #endif // _WX_SOUND_H_