]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | #ifndef _WX_SOUND_H_ | |
12 | #define _WX_SOUND_H_ | |
13 | ||
14 | #include "wx/defs.h" | |
15 | ||
16 | #if wxUSE_SOUND | |
17 | ||
18 | #include "wx/object.h" | |
19 | ||
20 | // ---------------------------------------------------------------------------- | |
21 | // wxSound: simple audio playback class | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
24 | class WXDLLIMPEXP_FWD_ADV wxSoundBackend; | |
25 | class WXDLLIMPEXP_FWD_ADV wxSound; | |
26 | class WXDLLIMPEXP_FWD_BASE wxDynamicLibrary; | |
27 | ||
28 | /// Sound data, as loaded from .wav file: | |
29 | class WXDLLIMPEXP_ADV wxSoundData | |
30 | { | |
31 | public: | |
32 | wxSoundData() : m_refCnt(1) {} | |
33 | void IncRef(); | |
34 | void DecRef(); | |
35 | ||
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 | |
41 | // (wxInt16) | |
42 | unsigned m_samples; // length in samples: | |
43 | ||
44 | // wave data: | |
45 | size_t m_dataBytes; | |
46 | wxUint8 *m_data; // m_dataBytes bytes of data | |
47 | ||
48 | private: | |
49 | ~wxSoundData(); | |
50 | unsigned m_refCnt; | |
51 | wxUint8 *m_dataWithHeader; // ditto, but prefixed with .wav header | |
52 | friend class wxSound; | |
53 | }; | |
54 | ||
55 | ||
56 | /// Simple sound class: | |
57 | class WXDLLIMPEXP_ADV wxSound : public wxSoundBase | |
58 | { | |
59 | public: | |
60 | wxSound(); | |
61 | wxSound(const wxString& fileName, bool isResource = false); | |
62 | wxSound(size_t size, const void* data); | |
63 | virtual ~wxSound(); | |
64 | ||
65 | // Create from resource or file | |
66 | bool Create(const wxString& fileName, bool isResource = false); | |
67 | // Create from data | |
68 | bool Create(size_t size, const void* data); | |
69 | ||
70 | bool IsOk() const { return m_data != NULL; } | |
71 | ||
72 | // Stop playing any sound | |
73 | static void Stop(); | |
74 | ||
75 | // Returns true if a sound is being played | |
76 | static bool IsPlaying(); | |
77 | ||
78 | // for internal use | |
79 | static void UnloadBackend(); | |
80 | ||
81 | protected: | |
82 | bool DoPlay(unsigned flags) const; | |
83 | ||
84 | static void EnsureBackend(); | |
85 | void Free(); | |
86 | bool LoadWAV(const void* data, size_t length, bool copyData); | |
87 | ||
88 | static wxSoundBackend *ms_backend; | |
89 | #if wxUSE_LIBSDL && wxUSE_PLUGINS | |
90 | // FIXME - temporary, until we have plugins architecture | |
91 | static wxDynamicLibrary *ms_backendSDL; | |
92 | #endif | |
93 | ||
94 | private: | |
95 | wxSoundData *m_data; | |
96 | }; | |
97 | ||
98 | ||
99 | // ---------------------------------------------------------------------------- | |
100 | // wxSoundBackend: | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
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! | |
108 | ||
109 | // Structure that holds playback status information | |
110 | struct wxSoundPlaybackStatus | |
111 | { | |
112 | // playback is in progress | |
113 | bool m_playing; | |
114 | // main thread called wxSound::Stop() | |
115 | bool m_stopRequested; | |
116 | }; | |
117 | ||
118 | // Audio backend interface | |
119 | class WXDLLIMPEXP_ADV wxSoundBackend | |
120 | { | |
121 | public: | |
122 | virtual ~wxSoundBackend() {} | |
123 | ||
124 | // Returns the name of the backend (e.g. "Open Sound System") | |
125 | virtual wxString GetName() const = 0; | |
126 | ||
127 | // Returns priority (higher priority backends are tried first) | |
128 | virtual int GetPriority() const = 0; | |
129 | ||
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; | |
133 | ||
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 | |
137 | // effective). | |
138 | virtual bool HasNativeAsyncPlayback() const = 0; | |
139 | ||
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; | |
146 | ||
147 | // Stops playback (if something is played). | |
148 | virtual void Stop() = 0; | |
149 | ||
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 | |
152 | // playback.) | |
153 | virtual bool IsPlaying() const = 0; | |
154 | }; | |
155 | ||
156 | ||
157 | #endif // wxUSE_SOUND | |
158 | ||
159 | #endif // _WX_SOUND_H_ | |
160 |