]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/mmedia/sndesd.cpp
Typos fixed and further clarification of the Python situation on Panther
[wxWidgets.git] / contrib / src / mmedia / sndesd.cpp
1 // --------------------------------------------------------------------------
2 // Name: sndesd.cpp
3 // Purpose:
4 // Date: 08/11/1999
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
6 // CVSID: $Id$
7 // --------------------------------------------------------------------------
8 #ifdef __GNUG__
9 #pragma implementation "sndesd.cpp"
10 #endif
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/defs.h"
16 #include "wx/string.h"
17 #endif
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 // --------------------------------------------------------------------------
24 // MMedia headers
25 // --------------------------------------------------------------------------
26
27 #include "wx/mmedia/sndbase.h"
28 #include "wx/mmedia/sndesd.h"
29 #include "wx/mmedia/sndpcm.h"
30
31 // --------------------------------------------------------------------------
32 // System headers
33 // --------------------------------------------------------------------------
34
35 #ifdef HAVE_ESD_H
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <esd.h>
40 #ifdef __WXGTK__
41 #include <gdk/gdk.h>
42 #endif
43 #endif
44
45 // --------------------------------------------------------------------------
46
47 #define MY_ESD_NAME "wxWindows/wxSoundStreamESD"
48
49 // --------------------------------------------------------------------------
50 // wxSoundStreamESD: ESD sound driver
51
52 // --------------------------------------------------------------------------
53 // Constructors/Destructors
54 // --------------------------------------------------------------------------
55
56 wxSoundStreamESD::wxSoundStreamESD(const wxString& hostname)
57 {
58 #ifndef HAVE_ESD_H
59 m_snderror = wxSOUND_INVDEV;
60 return;
61 #else
62 wxSoundFormatPcm pcm_default;
63
64 // First, we make some basic test: is there ESD on this computer ?
65 m_esd_ok = FALSE;
66
67 if (hostname.IsNull())
68 m_fd_output = esd_play_stream(ESD_PLAY | ESD_STREAM, 22050,
69 hostname.mb_str(), MY_ESD_NAME);
70 else
71 m_fd_output = esd_play_stream(ESD_PLAY | ESD_STREAM, 22050,
72 NULL, MY_ESD_NAME);
73 if (m_fd_output == -1) {
74 // Answer: no. We return with an error.
75 m_snderror = wxSOUND_INVDEV;
76 return;
77 }
78
79 // Close this unuseful stream.
80 esd_close(m_fd_output);
81
82 m_hostname = hostname;
83
84 // Set the default audio format
85 SetSoundFormat(pcm_default);
86
87 // Initialize some variable
88 m_snderror = wxSOUND_NOERROR;
89 m_esd_stop = TRUE;
90 m_q_filled = TRUE;
91 m_esd_ok = TRUE;
92 m_fd_output= -1;
93 m_fd_input = -1;
94 #endif // defined HAVE_ESD_H
95 }
96
97 wxSoundStreamESD::~wxSoundStreamESD()
98 {
99 #ifdef HAVE_ESD_H
100 if (!m_esd_stop)
101 StopProduction();
102 #endif // defined HAVE_ESD_H
103 }
104
105 // --------------------------------------------------------------------------
106 // Read several samples
107 // --------------------------------------------------------------------------
108
109 wxSoundStream& wxSoundStreamESD::Read(void *buffer, wxUint32 len)
110 {
111 #ifndef HAVE_ESD_H
112 m_snderror = wxSOUND_INVDEV;
113 return *this;
114 #else
115 int ret;
116
117 if (m_esd_stop) {
118 m_snderror = wxSOUND_NOTSTARTED;
119 return *this;
120 }
121
122 m_lastcount = (wxUint32)ret = read(m_fd_input, buffer, len);
123
124 if (ret < 0)
125 m_snderror = wxSOUND_IOERROR;
126 else
127 m_snderror = wxSOUND_NOERROR;
128
129 return *this;
130 #endif // defined HAVE_ESD_H
131 }
132
133 // --------------------------------------------------------------------------
134 // Write several samples
135 // --------------------------------------------------------------------------
136 wxSoundStream& wxSoundStreamESD::Write(const void *buffer, wxUint32 len)
137 {
138 #ifndef HAVE_ESD_H
139 m_snderror = wxSOUND_INVDEV;
140 return *this;
141 #else
142 int ret;
143
144 if (m_esd_stop) {
145 m_lastcount = 0;
146 m_snderror = wxSOUND_NOTSTARTED;
147 return *this;
148 }
149
150 m_lastcount = (wxUint32)ret = write(m_fd_output, buffer, len);
151
152 if (ret < 0)
153 m_snderror = wxSOUND_IOERROR;
154 else
155 m_snderror = wxSOUND_NOERROR;
156
157 m_q_filled = TRUE;
158
159 return *this;
160 #endif // defined HAVE_ESD_H
161 }
162
163 // --------------------------------------------------------------------------
164 // SetSoundFormat(): this function specifies which format we want and which
165 // format is available
166 // --------------------------------------------------------------------------
167 bool wxSoundStreamESD::SetSoundFormat(const wxSoundFormatBase& format)
168 {
169 #ifndef HAVE_ESD_H
170 m_snderror = wxSOUND_INVDEV;
171 return FALSE;
172 #else
173 wxSoundFormatPcm *pcm_format;
174
175 if (format.GetType() != wxSOUND_PCM) {
176 m_snderror = wxSOUND_INVFRMT;
177 return FALSE;
178 }
179
180 if (!m_esd_ok) {
181 m_snderror = wxSOUND_INVDEV;
182 return FALSE;
183 }
184
185 if (m_sndformat)
186 delete m_sndformat;
187
188 m_sndformat = format.Clone();
189 if (!m_sndformat) {
190 m_snderror = wxSOUND_MEMERROR;
191 return FALSE;
192 }
193 pcm_format = (wxSoundFormatPcm *)m_sndformat;
194
195 // Detect the best format
196 DetectBest(pcm_format);
197
198 m_snderror = wxSOUND_NOERROR;
199 if (*pcm_format != format) {
200 m_snderror = wxSOUND_NOEXACT;
201 return FALSE;
202 }
203 return TRUE;
204 #endif // defined HAVE_ESD_H
205 }
206
207 // --------------------------------------------------------------------------
208 // _wxSound_OSS_CBack (internal): it is called when the driver (ESD) is
209 // ready for a next buffer.
210 // --------------------------------------------------------------------------
211 #if defined(__WXGTK__) && defined(HAVE_ESD_H)
212 static void _wxSound_OSS_CBack(gpointer data, int source,
213 GdkInputCondition condition)
214 {
215 wxSoundStreamESD *esd = (wxSoundStreamESD *)data;
216
217 switch (condition) {
218 case GDK_INPUT_READ:
219 esd->WakeUpEvt(wxSOUND_INPUT);
220 break;
221 case GDK_INPUT_WRITE:
222 esd->WakeUpEvt(wxSOUND_OUTPUT);
223 break;
224 default:
225 break;
226 }
227 }
228 #endif
229
230
231 // --------------------------------------------------------------------------
232 // WakeUpEvt() (internal): it is called by _wxSound_OSS_CBack to bypass the
233 // C++ protection
234 // --------------------------------------------------------------------------
235 void wxSoundStreamESD::WakeUpEvt(int evt)
236 {
237 m_q_filled = FALSE;
238 OnSoundEvent(evt);
239 }
240
241 // --------------------------------------------------------------------------
242 // StartProduction(): see wxSoundStream
243 // --------------------------------------------------------------------------
244 bool wxSoundStreamESD::StartProduction(int evt)
245 {
246 #ifndef HAVE_ESD_H
247 m_snderror = wxSOUND_INVDEV;
248 return FALSE;
249 #else
250 wxSoundFormatPcm *pcm;
251 int flag = 0;
252
253 if (!m_esd_ok) {
254 m_snderror = wxSOUND_INVDEV;
255 return FALSE;
256 }
257
258 if (!m_esd_stop)
259 StopProduction();
260
261 pcm = (wxSoundFormatPcm *)m_sndformat;
262
263 flag |= (pcm->GetBPS() == 16) ? ESD_BITS16 : ESD_BITS8;
264 flag |= (pcm->GetChannels() == 2) ? ESD_STEREO : ESD_MONO;
265
266 if ((evt & wxSOUND_OUTPUT) != 0) {
267 flag |= ESD_PLAY | ESD_STREAM;
268 m_fd_output = esd_play_stream(flag, pcm->GetSampleRate(), NULL,
269 MY_ESD_NAME);
270 }
271
272 if ((evt & wxSOUND_INPUT) != 0) {
273 flag |= ESD_RECORD | ESD_STREAM;
274 m_fd_input = esd_record_stream(flag, pcm->GetSampleRate(), NULL,
275 MY_ESD_NAME);
276 }
277
278 #ifdef __WXGTK__
279 if ((evt & wxSOUND_OUTPUT) != 0) {
280 m_tag_output = gdk_input_add(m_fd_output, GDK_INPUT_WRITE,
281 _wxSound_OSS_CBack, (gpointer)this);
282 }
283 if ((evt & wxSOUND_INPUT) != 0) {
284 m_tag_input = gdk_input_add(m_fd_input, GDK_INPUT_READ,
285 _wxSound_OSS_CBack, (gpointer)this);
286 }
287 #endif
288
289 m_esd_stop = FALSE;
290 m_q_filled = FALSE;
291
292 return TRUE;
293 #endif // defined HAVE_ESD_H
294 }
295
296 // --------------------------------------------------------------------------
297 // StopProduction(): see wxSoundStream
298 // --------------------------------------------------------------------------
299 bool wxSoundStreamESD::StopProduction()
300 {
301 #ifndef HAVE_ESD_H
302 m_snderror = wxSOUND_INVDEV;
303 return FALSE;
304 #else
305 if (m_esd_stop)
306 return FALSE;
307
308 if (m_fd_input != -1) {
309 esd_close(m_fd_input);
310 #ifdef __WXGTK__
311 gdk_input_remove(m_tag_input);
312 #endif
313 }
314 if (m_fd_output != -1) {
315 esd_close(m_fd_output);
316 #ifdef __WXGTK__
317 gdk_input_remove(m_tag_output);
318 #endif
319 }
320
321 m_fd_input = -1;
322 m_fd_output= -1;
323 m_esd_stop = TRUE;
324 m_q_filled = TRUE;
325 return TRUE;
326 #endif // defined HAVE_ESD_H
327 }
328
329 //
330 // Detect the closest format (The best).
331 //
332 void wxSoundStreamESD::DetectBest(wxSoundFormatPcm *pcm)
333 {
334 #ifndef HAVE_ESD_H
335 m_snderror = wxSOUND_INVDEV;
336 return;
337 #else
338 wxSoundFormatPcm best_pcm;
339
340 // We change neither the number of channels nor the sample rate
341 // because ESD is clever.
342
343 best_pcm.SetSampleRate(pcm->GetSampleRate());
344 best_pcm.SetChannels(pcm->GetChannels());
345
346 // It supports 16 bits
347 if (pcm->GetBPS() >= 16)
348 best_pcm.SetBPS(16);
349 else
350 best_pcm.SetBPS(8);
351
352 best_pcm.SetOrder(wxLITTLE_ENDIAN);
353 best_pcm.Signed(TRUE);
354
355 // Finally recopy the new format
356 *pcm = best_pcm;
357 #endif // defined HAVE_ESD_H
358 }
359