]>
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();
59 bool wxUssSound::Wakeup(wxSndBuffer
& WXUNUSED(buf
))
61 printf("Waking up (wxUssSound::Wakeup) ...\n");
65 // wxThread::Create();
70 m_sleep_cond
.Signal();
77 void wxUssSound::StopBuffer(wxSndBuffer
& buf
)
80 buf
.Set(wxSND_BUFSTOP
);
82 while (buf
.IsSet(wxSND_BUFSTOP
))
87 void wxUssSound::USS_Sleep()
91 printf("Asleeping ...\n");
94 ret
= m_sleep_cond
.Wait(m_sleep_mtx
, 10, 0);
98 printf("Waking up ...\n");
103 bool wxUssSound::DoInput(wxSndBuffer
*buf
)
106 wxSoundCodec
*codec
= buf
->GetCurrentCodec();
108 m_sndbuf
->ResetBuffer();
110 bufsize
= codec
->Available();
111 if (bufsize
> m_max_bufsize
)
112 bufsize
= m_max_bufsize
;
115 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
118 read(m_fd
, m_sndbuf
->GetBufferStart(), bufsize
);
124 bool wxUssSound::DoOutput(wxSndBuffer
*buf
)
126 wxSoundCodec
*codec
= buf
->GetCurrentCodec();
128 m_sndbuf
->ResetBuffer();
130 if (!codec
->Available()) {
131 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
135 write(m_fd
, m_sndbuf
->GetBufferStart(), m_sndbuf
->GetIntPosition());
137 // Well ... it's not accurate ! :-|
138 buf
->OnBufferOutFinished();
143 bool wxUssSound::InitBuffer(wxSndBuffer
*buf
)
147 if (!OnSetupDriver(*buf
, buf
->GetMode())) {
148 if (buf
->IsNotSet(wxSND_BUFREADY
))
152 codec
= buf
->GetCurrentCodec();
155 codec
->SetInStream(m_sndbuf
);
156 codec
->InitIO(m_ussformat
);
157 codec
->InitMode(wxSoundCodec::ENCODING
);
160 codec
->SetOutStream(m_sndbuf
);
161 codec
->InitIO(m_ussformat
);
162 codec
->InitMode(wxSoundCodec::DECODING
);
168 void *wxUssSound::Entry()
173 node
= m_buffers
.First();
179 buf
= (wxSndBuffer
*)node
->Data();
182 while (!m_stop_thrd
) {
184 if (buf
->IsSet(wxSND_BUFSTOP
)) {
186 goto sound_clean_buffer
;
191 goto sound_clean_buffer
;
195 goto sound_clean_buffer
;
199 goto sound_clean_buffer
;
205 buf
->GetCurrentCodec()->ExitMode();
207 node
= m_buffers
.First();
211 buf
= (wxSndBuffer
*)node
->Data();
216 bool wxUssSound::OnSetupDriver(wxSndBuffer
& buf
, wxSndMode
WXUNUSED(mode
))
218 wxSoundDataFormat format
;
221 codec
= buf
.GetCurrentCodec();
222 format
= codec
->GetPreferredFormat(WXSOUND_PCM
);
224 if ((format
.GetSampleRate() != m_srate
) ||
225 (format
.GetBps() != m_bps
) ||
226 (format
.GetStereo() != m_stereo
)) {
228 if (!SetupSound(format
.GetSampleRate(), format
.GetBps(),
229 format
.GetStereo())) {
230 m_buffers
.DeleteObject(&buf
);
231 buf
.Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
232 buf
.SetError(wxSND_CANTSET
);
235 m_mode
= wxSND_OTHER_IO
;
238 if (buf
.GetMode() != m_mode
) {
239 m_mode
= buf
.GetMode();
246 wxUint32
wxUssSound::GetNbFragments()
248 struct audio_buf_info frag_info
;
250 ioctl(m_fd
, SNDCTL_DSP_GETOSPACE
, &frag_info
);
252 return frag_info
.fragstotal
;
255 wxUint32
wxUssSound::GetFragmentSize()
257 return m_max_bufsize
;
260 bool wxUssSound::SetupSound(wxUint16 srate
, wxUint8 bps
, bool stereo
)
263 unsigned long tmp_ul
;
271 m_fd
= open("/dev/dsp", O_RDWR
);
274 if (ioctl(m_fd
, SNDCTL_DSP_STEREO
, &tmp
) < 0)
279 if (ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp_ul
) < 0)
284 if (ioctl(m_fd
, SNDCTL_DSP_SAMPLESIZE
, &tmp
) < 0)
288 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &tmp
);
290 m_sndbuf
->SetBufferIO(m_max_bufsize
);
292 m_ussformat
.SetBps(m_bps
);
293 m_ussformat
.SetChannels((m_stereo
) ? 2 : 1);
294 m_ussformat
.SetSampleRate(m_srate
);