]>
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>
24 #define WXMMEDIA_INTERNAL
28 wxUssSound::wxUssSound()
30 m_srate(0), m_bps(0), m_stereo(0),
31 m_mode(wxSND_OTHER_IO
),
32 m_stop_thrd(TRUE
), m_sleeping(FALSE
)
35 m_ussformat
.SetCodecNo(WXSOUND_PCM
);
36 m_ussformat
.SetSign(wxSND_SAMPLE_SIGNED
);
37 m_ussformat
.SetByteOrder(wxSND_SAMPLE_LE
);
39 m_sndbuf
= new wxStreamBuffer(wxStreamBuffer::read_write
);
40 m_sndbuf
->Flushable(FALSE
);
41 m_sndbuf
->Fixed(TRUE
);
44 wxUssSound::~wxUssSound()
50 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
->GetFormat().GetCodec();
109 m_sndbuf
->ResetBuffer();
110 codec
->SetInStream(m_sndbuf
);
111 codec
->InitIO(m_ussformat
);
113 bufsize
= codec
->Available();
114 if (bufsize
> m_max_bufsize
)
115 bufsize
= m_max_bufsize
;
118 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
121 read(m_fd
, m_sndbuf
, bufsize
);
127 bool wxUssSound::DoOutput(wxSndBuffer
*buf
)
129 wxSoundCodec
*codec
= buf
->GetCurrentCodec();
131 m_sndbuf
->ResetBuffer();
132 codec
->SetOutStream(m_sndbuf
);
133 codec
->InitIO(m_ussformat
);
135 if (!codec
->Available()) {
136 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
140 write(m_fd
, m_sndbuf
, m_sndbuf
->GetIntPosition());
142 // Well ... it's not accurate ! :-|
143 buf
->OnBufferOutFinished();
148 void *wxUssSound::Entry()
153 while (!m_stop_thrd
) {
154 node
= m_buffers
.First();
159 buf
= (wxSndBuffer
*)node
->Data();
160 if (!OnSetupDriver(*buf
, buf
->GetMode()))
164 if (buf
->IsSet(wxSND_BUFSTOP
)) {
187 bool wxUssSound::OnSetupDriver(wxSndBuffer
& buf
, wxSndMode
WXUNUSED(mode
))
189 wxSoundDataFormat format
;
192 codec
= buf
.GetFormat().GetCodec();
193 format
= codec
->GetPreferredFormat(WXSOUND_PCM
);
195 if ((format
.GetSampleRate() != m_srate
) ||
196 (format
.GetBps() != m_bps
) ||
197 (format
.GetStereo() != m_stereo
)) {
199 if (!SetupSound(format
.GetSampleRate(), format
.GetBps(),
200 format
.GetStereo())) {
201 m_buffers
.DeleteObject(&buf
);
202 buf
.Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
203 buf
.SetError(wxSND_CANTSET
);
206 m_mode
= wxSND_OTHER_IO
;
209 if (buf
.GetMode() != m_mode
) {
210 m_mode
= buf
.GetMode();
217 wxUint32
wxUssSound::GetNbFragments()
219 struct audio_buf_info frag_info
;
221 ioctl(m_fd
, SNDCTL_DSP_GETOSPACE
, &frag_info
);
223 return frag_info
.fragstotal
;
226 wxUint32
wxUssSound::GetFragmentSize()
228 return m_max_bufsize
;
231 bool wxUssSound::SetupSound(wxUint16 srate
, wxUint8 bps
, bool stereo
)
234 unsigned long tmp_ul
;
242 m_fd
= open("/dev/dsp", O_RDWR
);
245 if (ioctl(m_fd
, SNDCTL_DSP_STEREO
, &tmp
) < 0)
250 if (ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp_ul
) < 0)
255 if (ioctl(m_fd
, SNDCTL_DSP_SAMPLESIZE
, &tmp
) < 0)
259 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &tmp
);
261 m_sndbuf
->SetBufferIO(m_max_bufsize
);
263 m_ussformat
.SetBps(m_bps
);
264 m_ussformat
.SetChannels((m_stereo
) ? 2 : 1);
265 m_ussformat
.SetSampleRate(m_srate
);