]>
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
);
169 void *wxUssSound::Entry()
174 node
= m_buffers
.First();
180 buf
= (wxSndBuffer
*)node
->Data();
183 while (!m_stop_thrd
) {
185 if (buf
->IsSet(wxSND_BUFSTOP
)) {
187 goto sound_clean_buffer
;
192 goto sound_clean_buffer
;
196 goto sound_clean_buffer
;
200 goto sound_clean_buffer
;
206 buf
->GetCurrentCodec()->ExitMode();
208 node
= m_buffers
.First();
212 buf
= (wxSndBuffer
*)node
->Data();
217 bool wxUssSound::OnSetupDriver(wxSndBuffer
& buf
, wxSndMode
WXUNUSED(mode
))
219 wxSoundDataFormat format
;
222 codec
= buf
.GetCurrentCodec();
223 format
= codec
->GetPreferredFormat(WXSOUND_PCM
);
225 if ((format
.GetSampleRate() != m_srate
) ||
226 (format
.GetBps() != m_bps
) ||
227 (format
.GetStereo() != m_stereo
)) {
229 if (!SetupSound(format
.GetSampleRate(), format
.GetBps(),
230 format
.GetStereo())) {
231 m_buffers
.DeleteObject(&buf
);
232 buf
.Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
233 buf
.SetError(wxSND_CANTSET
);
236 m_mode
= wxSND_OTHER_IO
;
239 if (buf
.GetMode() != m_mode
) {
240 m_mode
= buf
.GetMode();
247 wxUint32
wxUssSound::GetNbFragments()
249 struct audio_buf_info frag_info
;
251 ioctl(m_fd
, SNDCTL_DSP_GETOSPACE
, &frag_info
);
253 return frag_info
.fragstotal
;
256 wxUint32
wxUssSound::GetFragmentSize()
258 return m_max_bufsize
;
261 bool wxUssSound::SetupSound(wxUint16 srate
, wxUint8 bps
, bool stereo
)
264 unsigned long tmp_ul
;
272 m_fd
= open("/dev/dsp", O_RDWR
);
275 if (ioctl(m_fd
, SNDCTL_DSP_STEREO
, &tmp
) < 0)
280 if (ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp_ul
) < 0)
285 if (ioctl(m_fd
, SNDCTL_DSP_SAMPLESIZE
, &tmp
) < 0)
289 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &tmp
);
291 m_sndbuf
->SetBufferIO(m_max_bufsize
);
293 m_ussformat
.SetBps(m_bps
);
294 m_ussformat
.SetChannels((m_stereo
) ? 2 : 1);
295 m_ussformat
.SetSampleRate(m_srate
);