]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia/sndsnd.cpp
1. wxLoad/SaveFileSelector return "wxString" instead of "char *"
[wxWidgets.git] / utils / wxMMedia / sndsnd.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 // Name: sndsnd.cpp
3 // Purpose: wxMMedia
4 // Author: Guilhem Lavaux
5 // Created: 1997
6 // Updated: 1998
7 // Copyright: (C) 1997, 1998, Guilhem Lavaux
8 // License: wxWindows license
9 ////////////////////////////////////////////////////////////////////////////////
10 #ifdef __GNUG__
11 #pragma implementation "sndsnd.h"
12 #endif
13 #ifdef WX_PRECOMP
14 #include "wx_prec.h"
15 #else
16 #include "wx/wx.h"
17 #endif
18 #include "sndsnd.h"
19 #include "sndfrmt.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #define PROCESS_EVENT() wxYield()
26 // #define PROCESS_EVENT()
27
28 // ----------------------------------------------------------------------------
29 // wxSndBuffer: base sound buffer class
30 // ----------------------------------------------------------------------------
31
32
33 wxSndBuffer::wxSndBuffer()
34 : m_sndmode(wxSND_OUTPUT), m_sndflags(0), m_sndoutput(NULL), m_sndcodec(NULL)
35 {
36 }
37
38 wxSndBuffer::~wxSndBuffer()
39 {
40 }
41
42 void wxSndBuffer::Set(wxSndFlags flags)
43 {
44 m_sndflags |= flags;
45
46 if ((m_sndflags & wxSND_BUFAUTO) && (flags & wxSND_BUFREADY))
47 m_sndoutput->QueueBuffer(*this);
48 }
49
50 void wxSndBuffer::SetError(wxSndError error)
51 {
52 if (error == wxSND_NOERROR)
53 Clear(wxSND_BUFERR);
54 else
55 Set(wxSND_BUFERR);
56
57 m_snderror = error;
58 }
59
60 wxSndError wxSndBuffer::GetError()
61 {
62 if (IsNotSet(wxSND_BUFERR))
63 return wxSND_NOERROR;
64 Clear(wxSND_BUFERR);
65 return m_snderror;
66 }
67
68 void wxSndBuffer::OnPlayFinished()
69 {
70 }
71
72 void wxSndBuffer::OnBufferOutFinished()
73 {
74 }
75
76 void wxSndBuffer::OnBufferInFinished(char *WXUNUSED(iobuf),
77 wxUint32& WXUNUSED(size))
78 {
79 }
80
81 bool wxSndBuffer::Wait()
82 {
83 if (IsNotSet(wxSND_BUFLOCKED))
84 return FALSE;
85
86 while (IsSet(wxSND_BUFLOCKED))
87 PROCESS_EVENT();
88
89 return IsNotSet(wxSND_BUFERR);
90 }
91
92 void wxSndBuffer::HardLock()
93 {
94 m_mutex.Lock();
95 }
96
97 void wxSndBuffer::HardUnlock()
98 {
99 m_mutex.Unlock();
100 }
101
102 void wxSndBuffer::ChangeCodec(int no)
103 {
104 m_sndformat.SetCodecNo(no);
105 m_sndcodec = m_sndformat.GetCodec();
106 m_sndcodec->SetIOBuffer(this);
107 }
108
109 // ----------------------------------------------------------------------------
110 // wxSndSimpleBuffer: the simplest sound buffer
111 // ----------------------------------------------------------------------------
112
113
114 wxSndSimpleBuffer::wxSndSimpleBuffer(char *buffer, wxUint32 bufsize,
115 wxSndMode mode)
116 : wxSndBuffer()
117 {
118 m_sndbuf = buffer;
119 m_sndsize = bufsize;
120 m_sndmode = mode;
121
122 m_sndptr = 0;
123 }
124
125 wxSndSimpleBuffer::~wxSndSimpleBuffer()
126 {
127 }
128
129 void wxSndSimpleBuffer::OnNeedOutputData(char *iobuf, wxUint32& size)
130 {
131 char *buf = m_sndbuf + m_sndptr;
132 wxUint32 nbdata_left = m_sndsize - m_sndptr;
133
134 if (m_sndptr >= m_sndsize) {
135 size = 0;
136 return;
137 }
138
139 if (size > nbdata_left)
140 size = nbdata_left;
141
142 m_sndptr += size;
143
144 memcpy(iobuf, buf, size);
145 }
146
147 void wxSndSimpleBuffer::OnBufferOutFinished()
148 {
149 if (m_sndptr >= m_sndsize)
150 OnPlayFinished();
151 }
152
153 void wxSndSimpleBuffer::OnBufferInFinished(char *iobuf, wxUint32& size)
154 {
155 char *raw_buf = m_sndbuf + m_sndptr;
156 wxUint32 data_left = m_sndsize - m_sndptr;
157
158 if (!data_left) {
159 size = 0;
160 return;
161 }
162
163 if (size > data_left)
164 size = data_left;
165
166 memcpy(raw_buf, iobuf, size);
167 m_sndptr += size;
168 }
169
170 void wxSndSimpleBuffer::SetData(char *buffer, wxUint32 bufsize,
171 wxSndMode mode)
172 {
173 m_sndbuf = buffer;
174 m_sndsize = bufsize;
175 m_sndmode = mode;
176 }
177
178 bool wxSndSimpleBuffer::RestartBuffer(wxSndMode mode)
179 {
180 m_sndptr = 0;
181 return TRUE;
182 }
183
184 wxUint32 wxSndSimpleBuffer::GetSize() const
185 {
186 return m_sndsize;
187 }
188
189 wxUint32 wxSndSimpleBuffer::Available() const
190 {
191 return m_sndsize - m_sndptr;
192 }
193
194 // ----------------------------------------------------------------------------
195 // wxSound: base sound driver implementation
196 // ----------------------------------------------------------------------------
197
198 wxSound::wxSound()
199 : wxObject(),
200 m_lastbuf(NULL), m_sndcbk(NULL), m_snderror(wxSND_NOERROR)
201 {
202 m_buffers.Clear();
203 }
204
205 wxSound::~wxSound()
206 {
207 wxNode *node = m_buffers.First();
208
209 while (node) {
210 wxSndBuffer *buf = (wxSndBuffer *)node->Data();
211
212 buf->Clear(wxSND_BUFLOCKED);
213 }
214 }
215
216 bool wxSound::QueueBuffer(wxSndBuffer& buf)
217 {
218 if (buf.IsSet(wxSND_BUFLOCKED) || buf.IsNotSet(wxSND_BUFREADY))
219 return FALSE;
220
221 buf.Set(wxSND_BUFLOCKED);
222 buf.SetOutput(*this);
223
224 m_buffers.Append(&buf);
225 return Wakeup(buf);
226 }
227
228 bool wxSound::UnqueueBuffer(wxSndBuffer& buf)
229 {
230 wxNode *node;
231
232 if (buf.IsNotSet(wxSND_BUFLOCKED))
233 return FALSE;
234
235 node = m_buffers.Member(&buf);
236 if (!node)
237 return FALSE;
238
239 StopBuffer(buf);
240 node = m_buffers.Member(&buf);
241 if (node)
242 delete node;
243
244 return TRUE;
245 }
246
247 void wxSound::Callback(wxSndCallback cbk)
248 {
249 m_sndcbk = cbk;
250 }
251
252 void wxSound::SetClientData(char *cdata)
253 {
254 m_cdata = cdata;
255 }
256
257 void wxSound::OnPlayBuffer(wxSndBuffer& buf)
258 {
259 m_lastbuf = &buf;
260 if (m_sndcbk)
261 m_sndcbk(*this, buf, m_cdata);
262 }