1 // --------------------------------------------------------------------------
5 // Author: Guilhem Lavaux <lavaux@easynet.fr> (C) 1999
7 // --------------------------------------------------------------------------
9 #pragma implementation "sndoss.cpp"
12 #include <sys/soundcard.h>
13 #include <sys/types.h>
15 #include <sys/ioctl.h>
19 #include <wx/string.h>
27 wxSoundStreamOSS::wxSoundStreamOSS(const wxString
& dev_name
)
29 wxSoundFormatPcm pcm_default
;
31 m_fd
= open(dev_name
.mb_str(), O_RDWR
);
34 m_snderror
= wxSOUND_INVDEV
;
40 wxSoundStreamOSS::SetSoundFormat(pcm_default
);
42 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &m_bufsize
);
44 m_snderror
= wxSOUND_NOERR
;
51 wxSoundStreamOSS::~wxSoundStreamOSS()
57 wxUint32
wxSoundStreamOSS::GetBestSize() const
62 wxSoundStream
& wxSoundStreamOSS::Read(void *buffer
, size_t len
)
66 m_lastcount
= (size_t)ret
= read(m_fd
, buffer
, len
);
69 m_snderror
= wxSOUND_IOERR
;
71 m_snderror
= wxSOUND_NOERR
;
76 wxSoundStream
& wxSoundStreamOSS::Write(const void *buffer
, size_t len
)
80 m_lastcount
= (size_t)ret
= write(m_fd
, buffer
, len
);
83 m_snderror
= wxSOUND_IOERR
;
85 m_snderror
= wxSOUND_NOERR
;
90 bool wxSoundStreamOSS::SetSoundFormat(const wxSoundFormatBase
& format
)
93 wxSoundFormatPcm
*pcm_format
;
95 if (format
.GetType() != wxSOUND_PCM
) {
96 m_snderror
= wxSOUND_INVFRMT
;
101 m_snderror
= wxSOUND_INVDEV
;
108 m_sndformat
= format
.Clone();
110 m_snderror
= wxSOUND_MEMERR
;
113 pcm_format
= (wxSoundFormatPcm
*)m_sndformat
;
115 // Set the sample rate field.
116 tmp
= pcm_format
->GetSampleRate();
117 ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp
);
119 pcm_format
->SetSampleRate(tmp
);
121 // Detect the best format
122 DetectBest(pcm_format
);
123 SetupFormat(pcm_format
);
125 tmp
= pcm_format
->GetChannels();
126 ioctl(m_fd
, SNDCTL_DSP_CHANNELS
, &tmp
);
127 pcm_format
->SetChannels(tmp
);
129 m_snderror
= wxSOUND_NOERR
;
130 if (*pcm_format
!= format
) {
131 m_snderror
= wxSOUND_NOTEXACT
;
137 bool wxSoundStreamOSS::SetupFormat(wxSoundFormatPcm
*pcm_format
)
141 switch(pcm_format
->GetBPS()) {
143 if (pcm_format
->Signed())
149 switch (pcm_format
->GetOrder()) {
151 if (pcm_format
->Signed())
156 case wxLITTLE_ENDIAN
:
157 if (pcm_format
->Signed())
166 ioctl(m_fd
, SNDCTL_DSP_SETFMT
, &tmp
);
171 pcm_format
->SetBPS(8);
172 pcm_format
->Signed(FALSE
);
175 pcm_format
->SetBPS(8);
176 pcm_format
->Signed(TRUE
);
179 pcm_format
->SetBPS(16);
180 pcm_format
->Signed(FALSE
);
181 pcm_format
->SetOrder(wxLITTLE_ENDIAN
);
184 pcm_format
->SetBPS(16);
185 pcm_format
->Signed(FALSE
);
186 pcm_format
->SetOrder(wxBIG_ENDIAN
);
189 pcm_format
->SetBPS(16);
190 pcm_format
->Signed(TRUE
);
191 pcm_format
->SetOrder(wxLITTLE_ENDIAN
);
194 pcm_format
->SetBPS(16);
195 pcm_format
->Signed(TRUE
);
196 pcm_format
->SetOrder(wxBIG_ENDIAN
);
203 static void _wxSound_OSS_CBack(gpointer data
, int source
,
204 GdkInputCondition condition
)
206 wxSoundStreamOSS
*oss
= (wxSoundStreamOSS
*)data
;
210 oss
->WakeUpEvt(wxSOUND_INPUT
);
212 case GDK_INPUT_WRITE
:
213 oss
->WakeUpEvt(wxSOUND_OUTPUT
);
221 void wxSoundStreamOSS::WakeUpEvt(int evt
)
226 bool wxSoundStreamOSS::StartProduction(int evt
)
228 wxSoundFormatBase
*old_frmt
;
233 old_frmt
= m_sndformat
->Clone();
235 if (evt
== wxSOUND_OUTPUT
)
236 m_fd
= open(m_devname
.mb_str(), O_WRONLY
);
237 else if (evt
== wxSOUND_INPUT
)
238 m_fd
= open(m_devname
.mb_str(), O_RDONLY
);
241 m_snderror
= wxSOUND_INVDEV
;
245 SetSoundFormat(*old_frmt
);
251 if (evt
== wxSOUND_OUTPUT
) {
252 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_WRITE
, _wxSound_OSS_CBack
, (gpointer
)this);
253 trig
= PCM_ENABLE_OUTPUT
;
255 m_tag
= gdk_input_add(m_fd
, GDK_INPUT_READ
, _wxSound_OSS_CBack
, (gpointer
)this);
256 trig
= PCM_ENABLE_INPUT
;
263 ioctl(m_fd
, SNDCTL_DSP_SETTRIGGER
, &trig
);
270 bool wxSoundStreamOSS::StopProduction()
276 gdk_input_remove(m_tag
);
285 // Detect the closest format (The best).
287 void wxSoundStreamOSS::DetectBest(wxSoundFormatPcm
*pcm
)
289 #define MASK_16BITS (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
292 wxSoundFormatPcm best_pcm
;
294 // We change neither the number of channels nor the sample rate
296 best_pcm
.SetSampleRate(pcm
->GetSampleRate());
297 best_pcm
.SetChannels(pcm
->GetChannels());
299 // Get the supported format by the sound card
300 ioctl(m_fd
, SNDCTL_DSP_GETFMTS
, &fmt_mask
);
302 // It supports 16 bits
303 if (pcm
->GetBPS() == 16 && ((fmt_mask
& MASK_16BITS
) != 0))
306 // It supports big endianness
307 if (pcm
->GetOrder() == wxBIG_ENDIAN
&& ((fmt_mask
& (AFMT_S16_BE
| AFMT_U16_BE
)) != 0))
308 best_pcm
.SetOrder(wxBIG_ENDIAN
);
310 // It supports little endianness
311 if (pcm
->GetOrder() == wxLITTLE_ENDIAN
&& ((fmt_mask
& (AFMT_S16_LE
| AFMT_U16_LE
)) != 0))
312 best_pcm
.SetOrder(wxLITTLE_ENDIAN
);
314 // It supports signed samples
315 if (pcm
->Signed() && ((fmt_mask
& (AFMT_S16_LE
| AFMT_S16_BE
| AFMT_S8
)) != 0))
316 best_pcm
.Signed(TRUE
);
318 // It supports unsigned samples
319 if (!pcm
->Signed() && ((fmt_mask
& (AFMT_U16_LE
| AFMT_U16_BE
| AFMT_U8
)) != 0))
320 best_pcm
.Signed(FALSE
);
322 // Finally recopy the new format