]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia/snduss.cpp
1 ////////////////////////////////////////////////////////////////////////////////
4 // Author: Guilhem Lavaux
7 // Copyright: (C) 1997, 1998, Guilhem Lavaux
8 // License: wxWindows license
9 ////////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "snduss.h"
14 #include <sys/soundcard.h>
15 #include <sys/ioctl.h>
23 #define WXMMEDIA_INTERNAL
27 wxUssSound::wxUssSound()
29 m_srate(0), m_bps(0), m_stereo(0),
30 m_mode(wxSND_OTHER_IO
),
31 m_stop_thrd(TRUE
), m_sleeping(FALSE
)
34 m_ussformat
.SetCodecNo(WXSOUND_PCM
);
35 m_ussformat
.SetSign(wxSND_SAMPLE_SIGNED
);
36 m_ussformat
.SetByteOrder(wxSND_SAMPLE_LE
);
38 m_sndbuf
= new wxStreamBuffer(wxStreamBuffer::read_write
);
39 m_sndbuf
->Flushable(FALSE
);
40 m_sndbuf
->Fixed(TRUE
);
43 wxUssSound::~wxUssSound()
49 m_sleep_cond
.Signal();
60 bool wxUssSound::Wakeup(wxSndBuffer
& WXUNUSED(buf
))
62 printf("Waking up (wxUssSound::Wakeup) ...\n");
66 // wxThread::Create();
71 m_sleep_cond
.Signal();
78 void wxUssSound::StopBuffer(wxSndBuffer
& buf
)
81 buf
.Set(wxSND_BUFSTOP
);
83 while (buf
.IsSet(wxSND_BUFSTOP
))
88 void wxUssSound::USS_Sleep()
92 printf("Asleeping ...\n");
95 ret
= m_sleep_cond
.Wait(m_sleep_mtx
, 10, 0);
99 printf("Waking up ...\n");
104 bool wxUssSound::DoInput(wxSndBuffer
*buf
)
107 wxSoundCodec
*codec
= buf
->GetCurrentCodec();
109 m_sndbuf
->ResetBuffer();
111 bufsize
= codec
->Available();
112 if (bufsize
> m_max_bufsize
)
113 bufsize
= m_max_bufsize
;
116 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
119 read(m_fd
, m_sndbuf
->GetBufferStart(), bufsize
);
125 bool wxUssSound::DoOutput(wxSndBuffer
*buf
)
127 wxSoundCodec
*codec
= buf
->GetCurrentCodec();
129 m_sndbuf
->ResetBuffer();
131 if (!codec
->Available()) {
132 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
136 write(m_fd
, m_sndbuf
->GetBufferStart(), m_sndbuf
->GetIntPosition());
138 // Well ... it's not accurate ! :-|
139 buf
->OnBufferOutFinished();
144 bool wxUssSound::InitBuffer(wxSndBuffer
*buf
)
148 if (!OnSetupDriver(*buf
, buf
->GetMode())) {
149 if (buf
->IsNotSet(wxSND_BUFREADY
))
153 codec
= buf
->GetCurrentCodec();
156 codec
->SetInStream(m_sndbuf
);
157 codec
->InitIO(m_ussformat
);
158 codec
->InitMode(wxSoundCodec::ENCODING
);
161 codec
->SetOutStream(m_sndbuf
);
162 codec
->InitIO(m_ussformat
);
163 codec
->InitMode(wxSoundCodec::DECODING
);
172 void *wxUssSound::Entry()
177 node
= m_buffers
.First();
183 buf
= (wxSndBuffer
*)node
->Data();
186 while (!m_stop_thrd
) {
188 if (buf
->IsSet(wxSND_BUFSTOP
)) {
190 goto sound_clean_buffer
;
195 goto sound_clean_buffer
;
199 goto sound_clean_buffer
;
203 goto sound_clean_buffer
;
210 buf
->GetCurrentCodec()->ExitMode();
212 node
= m_buffers
.First();
216 buf
= (wxSndBuffer
*)node
->Data();
221 bool wxUssSound::OnSetupDriver(wxSndBuffer
& buf
, wxSndMode
WXUNUSED(mode
))
223 wxSoundDataFormat format
;
226 codec
= buf
.GetCurrentCodec();
227 format
= codec
->GetPreferredFormat(WXSOUND_PCM
);
229 if ((format
.GetSampleRate() != m_srate
) ||
230 (format
.GetBps() != m_bps
) ||
231 (format
.GetStereo() != m_stereo
)) {
233 if (!SetupSound(format
.GetSampleRate(), format
.GetBps(),
234 format
.GetStereo())) {
235 m_buffers
.DeleteObject(&buf
);
236 buf
.Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
237 buf
.SetError(wxSND_CANTSET
);
240 m_mode
= wxSND_OTHER_IO
;
243 if (buf
.GetMode() != m_mode
) {
244 m_mode
= buf
.GetMode();
251 wxUint32
wxUssSound::GetNbFragments()
253 struct audio_buf_info frag_info
;
255 ioctl(m_fd
, SNDCTL_DSP_GETOSPACE
, &frag_info
);
257 return frag_info
.fragstotal
;
260 wxUint32
wxUssSound::GetFragmentSize()
262 return m_max_bufsize
;
265 bool wxUssSound::SetupSound(wxUint16 srate
, wxUint8 bps
, bool stereo
)
268 unsigned long tmp_ul
;
276 m_fd
= open("/dev/dsp", O_RDWR
);
279 if (ioctl(m_fd
, SNDCTL_DSP_STEREO
, &tmp
) < 0)
284 if (ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp_ul
) < 0)
289 if (ioctl(m_fd
, SNDCTL_DSP_SAMPLESIZE
, &tmp
) < 0)
293 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &tmp
);
295 m_sndbuf
->SetBufferIO(m_max_bufsize
);
297 m_ussformat
.SetBps(m_bps
);
298 m_ussformat
.SetChannels((m_stereo
) ? 2 : 1);
299 m_ussformat
.SetSampleRate(m_srate
);