]>
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
->GetFormat().GetCodec();
108 m_sndbuf
->ResetBuffer();
109 codec
->SetInStream(m_sndbuf
);
110 codec
->InitIO(m_ussformat
);
112 bufsize
= codec
->Available();
113 if (bufsize
> m_max_bufsize
)
114 bufsize
= m_max_bufsize
;
117 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
120 read(m_fd
, m_sndbuf
, bufsize
);
126 bool wxUssSound::DoOutput(wxSndBuffer
*buf
)
128 wxSoundCodec
*codec
= buf
->GetCurrentCodec();
130 m_sndbuf
->ResetBuffer();
131 codec
->SetOutStream(m_sndbuf
);
132 codec
->InitIO(m_ussformat
);
134 if (!codec
->Available()) {
135 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
139 write(m_fd
, m_sndbuf
, m_sndbuf
->GetIntPosition());
141 // Well ... it's not accurate ! :-|
142 buf
->OnBufferOutFinished();
147 void *wxUssSound::Entry()
152 while (!m_stop_thrd
) {
153 node
= m_buffers
.First();
158 buf
= (wxSndBuffer
*)node
->Data();
159 if (!OnSetupDriver(*buf
, buf
->GetMode()))
163 if (buf
->IsSet(wxSND_BUFSTOP
)) {
186 bool wxUssSound::OnSetupDriver(wxSndBuffer
& buf
, wxSndMode
WXUNUSED(mode
))
188 wxSoundDataFormat format
;
191 codec
= buf
.GetFormat().GetCodec();
192 format
= codec
->GetPreferredFormat(WXSOUND_PCM
);
194 if ((format
.GetSampleRate() != m_srate
) ||
195 (format
.GetBps() != m_bps
) ||
196 (format
.GetStereo() != m_stereo
)) {
198 if (!SetupSound(format
.GetSampleRate(), format
.GetBps(),
199 format
.GetStereo())) {
200 m_buffers
.DeleteObject(&buf
);
201 buf
.Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
202 buf
.SetError(wxSND_CANTSET
);
205 m_mode
= wxSND_OTHER_IO
;
208 if (buf
.GetMode() != m_mode
) {
209 m_mode
= buf
.GetMode();
216 wxUint32
wxUssSound::GetNbFragments()
218 struct audio_buf_info frag_info
;
220 ioctl(m_fd
, SNDCTL_DSP_GETOSPACE
, &frag_info
);
222 return frag_info
.fragstotal
;
225 wxUint32
wxUssSound::GetFragmentSize()
227 return m_max_bufsize
;
230 bool wxUssSound::SetupSound(wxUint16 srate
, wxUint8 bps
, bool stereo
)
233 unsigned long tmp_ul
;
241 m_fd
= open("/dev/dsp", O_RDWR
);
244 if (ioctl(m_fd
, SNDCTL_DSP_STEREO
, &tmp
) < 0)
249 if (ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp_ul
) < 0)
254 if (ioctl(m_fd
, SNDCTL_DSP_SAMPLESIZE
, &tmp
) < 0)
258 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &tmp
);
260 m_sndbuf
->SetBufferIO(m_max_bufsize
);
262 m_ussformat
.SetBps(m_bps
);
263 m_ussformat
.SetChannels((m_stereo
) ? 2 : 1);
264 m_ussformat
.SetSampleRate(m_srate
);