]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia/snduss.cpp
9a03817bedc3dced341ff122dca241a6826afb55
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();
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
->GetBufferStart(), bufsize
);
126 bool wxUssSound::DoOutput(wxSndBuffer
*buf
)
128 wxSoundCodec
*codec
= buf
->GetCurrentCodec();
130 m_sndbuf
->ResetBuffer();
132 if (!codec
->Available()) {
133 buf
->Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
137 write(m_fd
, m_sndbuf
->GetBufferStart(), m_sndbuf
->GetIntPosition());
139 // Well ... it's not accurate ! :-|
140 buf
->OnBufferOutFinished();
145 bool wxUssSound::InitBuffer(wxSndBuffer
*buf
)
149 if (!OnSetupDriver(*buf
, buf
->GetMode())) {
150 if (buf
->IsNotSet(wxSND_BUFREADY
))
154 codec
= buf
->GetCurrentCodec();
155 codec
->SetOutStream(m_sndbuf
);
156 codec
->InitIO(m_ussformat
);
157 // TODO: We need more tests here.
158 codec
->InitMode((m_mode
== wxSND_OUTPUT
) ? wxSoundCodec::DECODING
: wxSoundCodec::ENCODING
);
163 void *wxUssSound::Entry()
168 node
= m_buffers
.First();
174 buf
= (wxSndBuffer
*)node
->Data();
177 while (!m_stop_thrd
) {
179 if (buf
->IsSet(wxSND_BUFSTOP
)) {
181 goto sound_clean_buffer
;
186 goto sound_clean_buffer
;
190 goto sound_clean_buffer
;
194 goto sound_clean_buffer
;
200 buf
->GetCurrentCodec()->ExitMode();
202 node
= m_buffers
.First();
206 buf
= (wxSndBuffer
*)node
->Data();
211 bool wxUssSound::OnSetupDriver(wxSndBuffer
& buf
, wxSndMode
WXUNUSED(mode
))
213 wxSoundDataFormat format
;
216 codec
= buf
.GetCurrentCodec();
217 format
= codec
->GetPreferredFormat(WXSOUND_PCM
);
219 if ((format
.GetSampleRate() != m_srate
) ||
220 (format
.GetBps() != m_bps
) ||
221 (format
.GetStereo() != m_stereo
)) {
223 if (!SetupSound(format
.GetSampleRate(), format
.GetBps(),
224 format
.GetStereo())) {
225 m_buffers
.DeleteObject(&buf
);
226 buf
.Clear(wxSND_BUFLOCKED
| wxSND_BUFREADY
);
227 buf
.SetError(wxSND_CANTSET
);
230 m_mode
= wxSND_OTHER_IO
;
233 if (buf
.GetMode() != m_mode
) {
234 m_mode
= buf
.GetMode();
241 wxUint32
wxUssSound::GetNbFragments()
243 struct audio_buf_info frag_info
;
245 ioctl(m_fd
, SNDCTL_DSP_GETOSPACE
, &frag_info
);
247 return frag_info
.fragstotal
;
250 wxUint32
wxUssSound::GetFragmentSize()
252 return m_max_bufsize
;
255 bool wxUssSound::SetupSound(wxUint16 srate
, wxUint8 bps
, bool stereo
)
258 unsigned long tmp_ul
;
266 m_fd
= open("/dev/dsp", O_RDWR
);
269 if (ioctl(m_fd
, SNDCTL_DSP_STEREO
, &tmp
) < 0)
274 if (ioctl(m_fd
, SNDCTL_DSP_SPEED
, &tmp_ul
) < 0)
279 if (ioctl(m_fd
, SNDCTL_DSP_SAMPLESIZE
, &tmp
) < 0)
283 ioctl(m_fd
, SNDCTL_DSP_GETBLKSIZE
, &tmp
);
285 m_sndbuf
->SetBufferIO(m_max_bufsize
);
287 m_ussformat
.SetBps(m_bps
);
288 m_ussformat
.SetChannels((m_stereo
) ? 2 : 1);
289 m_ussformat
.SetSampleRate(m_srate
);