1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999, 2000
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndoss.cpp"
12 // --------------------------------------------------------------------------
13 // System dependent headers
14 // --------------------------------------------------------------------------
16 #include <sys/soundcard.h>
17 #include <sys/types.h>
19 #include <sys/ioctl.h>
26 // --------------------------------------------------------------------------
28 // --------------------------------------------------------------------------
30 #include "wx/string.h"
31 #include "wx/mmedia/sndbase.h"
32 #include "wx/mmedia/sndoss.h"
33 #include "wx/mmedia/sndpcm.h"
35 wxSoundStreamOSS::wxSoundStreamOSS(const wxString
& dev_name
)
37 wxSoundFormatPcm pcm_default
;
39 // Open the OSS device
40 m_fd
= open(dev_name
.mb_str(), O_WRONLY
);
44 m_snderror
= wxSOUND_INVDEV
;
48 // Remember the device name
51 // Initialize the default format
52 wxSoundStreamOSS::SetSoundFormat(pcm_default
);
54 // Get the default best size for OSS
55 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &m_bufsize
);
57 m_snderror
= wxSOUND_NOERROR
;
67 wxSoundStreamOSS::~wxSoundStreamOSS()
73 wxUint32
wxSoundStreamOSS::GetBestSize() const
78 wxSoundStream
& wxSoundStreamOSS::Read(void *buffer
, wxUint32 len
)
83 m_snderror
= wxSOUND_NOTSTARTED
;
88 ret
= read(m_fd
, buffer
, len
);
89 m_lastcount
= (wxUint32
)ret
;
93 m_snderror
= wxSOUND_IOERROR
;
95 m_snderror
= wxSOUND_NOERROR
;
100 wxSoundStream
& wxSoundStreamOSS::Write(const void *buffer
, wxUint32 len
)
105 m_snderror
= wxSOUND_NOTSTARTED
;
110 ret
= write(m_fd
, buffer
, len
);
115 m_snderror
= wxSOUND_IOERROR
;
117 m_snderror
= wxSOUND_NOERROR
;
118 m_lastcount
= (wxUint32
)ret
;
124 bool wxSoundStreamOSS::SetSoundFormat(const wxSoundFormatBase
& format
)
127 wxSoundFormatPcm
*pcm_format
;
129 if (format
.GetType() != wxSOUND_PCM
) {
130 m_snderror
= wxSOUND_INVFRMT
;
135 m_snderror
= wxSOUND_INVDEV
;
142 m_sndformat
= format
.Clone();
144 m_snderror
= wxSOUND_MEMERROR
;
147 pcm_format
= (wxSoundFormatPcm
*)m_sndformat
;
149 // We temporary open the OSS device
151 m_fd
= open(m_devname
.mb_str(), O_WRONLY
);
153 m_snderror
= wxSOUND_INVDEV
;
158 // Set the sample rate field.
159 tmp
= pcm_format
->GetSampleRate();
160 ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp
);
162 pcm_format
->SetSampleRate(tmp
);
164 // Detect the best format
165 DetectBest(pcm_format
);
167 SetupFormat(pcm_format
);
169 tmp
= pcm_format
->GetChannels();
170 ioctl(m_fd
, SNDCTL_DSP_CHANNELS
, &tmp
);
171 pcm_format
->SetChannels(tmp
);
173 // Close the OSS device
177 m_snderror
= wxSOUND_NOERROR
;
178 if (*pcm_format
!= format
) {
179 m_snderror
= wxSOUND_NOEXACT
;
186 bool wxSoundStreamOSS::SetupFormat(wxSoundFormatPcm
*pcm_format
)
190 switch(pcm_format
->GetBPS()) {
192 if (pcm_format
->Signed())
198 switch (pcm_format
->GetOrder()) {
200 if (pcm_format
->Signed())
205 case wxLITTLE_ENDIAN
:
206 if (pcm_format
->Signed())
215 ioctl(m_fd
, SNDCTL_DSP_SETFMT
, &tmp
);
220 pcm_format
->SetBPS(8);
221 pcm_format
->Signed(false);
224 pcm_format
->SetBPS(8);
225 pcm_format
->Signed(true);
228 pcm_format
->SetBPS(16);
229 pcm_format
->Signed(false);
230 pcm_format
->SetOrder(wxLITTLE_ENDIAN
);
233 pcm_format
->SetBPS(16);
234 pcm_format
->Signed(false);
235 pcm_format
->SetOrder(wxBIG_ENDIAN
);
238 pcm_format
->SetBPS(16);
239 pcm_format
->Signed(true);
240 pcm_format
->SetOrder(wxLITTLE_ENDIAN
);
243 pcm_format
->SetBPS(16);
244 pcm_format
->Signed(true);
245 pcm_format
->SetOrder(wxBIG_ENDIAN
);
252 static void _wxSound_OSS_CBack(gpointer data
, int source
,
253 GdkInputCondition condition
)
255 wxSoundStreamOSS
*oss
= (wxSoundStreamOSS
*)data
;
259 oss
->WakeUpEvt(wxSOUND_INPUT
);
261 case GDK_INPUT_WRITE
:
262 oss
->WakeUpEvt(wxSOUND_OUTPUT
);
270 void wxSoundStreamOSS::WakeUpEvt(int evt
)
276 bool wxSoundStreamOSS::StartProduction(int evt
)
278 wxSoundFormatBase
*old_frmt
;
283 old_frmt
= m_sndformat
->Clone();
285 m_snderror
= wxSOUND_MEMERROR
;
289 if (evt
== wxSOUND_OUTPUT
)
290 m_fd
= open(m_devname
.mb_str(), O_WRONLY
);
291 else if (evt
== wxSOUND_INPUT
)
292 m_fd
= open(m_devname
.mb_str(), O_RDONLY
);
295 m_snderror
= wxSOUND_INVDEV
;
299 SetSoundFormat(*old_frmt
);
304 if (evt
== wxSOUND_OUTPUT
) {
306 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_WRITE
, _wxSound_OSS_CBack
, (gpointer
)this);
308 trig
= PCM_ENABLE_OUTPUT
;
311 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_READ
, _wxSound_OSS_CBack
, (gpointer
)this);
313 trig
= PCM_ENABLE_INPUT
;
316 ioctl(m_fd
, SNDCTL_DSP_SETTRIGGER
, &trig
);
324 bool wxSoundStreamOSS::StopProduction()
330 gdk_input_remove(m_tag
);
339 bool wxSoundStreamOSS::QueueFilled() const
345 // Detect the closest format (The best).
347 void wxSoundStreamOSS::DetectBest(wxSoundFormatPcm
*pcm
)
349 #define MASK_16BITS (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
352 wxSoundFormatPcm best_pcm
;
354 // We change neither the number of channels nor the sample rate
356 best_pcm
.SetSampleRate(pcm
->GetSampleRate());
357 best_pcm
.SetChannels(pcm
->GetChannels());
359 // Get the supported format by the sound card
360 ioctl(m_fd
, SNDCTL_DSP_GETFMTS
, &fmt_mask
);
362 // It supports 16 bits
363 if (pcm
->GetBPS() == 16 && ((fmt_mask
& MASK_16BITS
) != 0))
366 // It supports big endianness
367 if (pcm
->GetOrder() == wxBIG_ENDIAN
&&
368 ((fmt_mask
& (AFMT_S16_BE
| AFMT_U16_BE
)) != 0))
369 best_pcm
.SetOrder(wxBIG_ENDIAN
);
371 // It supports little endianness
372 if (pcm
->GetOrder() == wxLITTLE_ENDIAN
&&
373 ((fmt_mask
& (AFMT_S16_LE
| AFMT_U16_LE
)) != 0))
374 best_pcm
.SetOrder(wxLITTLE_ENDIAN
);
376 // It supports signed samples
378 ((fmt_mask
& (AFMT_S16_LE
| AFMT_S16_BE
| AFMT_S8
)) != 0))
379 best_pcm
.Signed(true);
381 // It supports unsigned samples
382 if (!pcm
->Signed() &&
383 ((fmt_mask
& (AFMT_U16_LE
| AFMT_U16_BE
| AFMT_U8
)) != 0))
384 best_pcm
.Signed(false);
386 // Finally recopy the new format