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