]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia/snduss.cpp
* Stream: update in doc, fix in code.
[wxWidgets.git] / utils / wxMMedia / snduss.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 // Name: snduss.cpp
3 // Purpose: wxMMedia
4 // Author: Guilhem Lavaux
5 // Created: 1997
6 // Updated: 1998
7 // Copyright: (C) 1997, 1998, Guilhem Lavaux
8 // License: wxWindows license
9 ////////////////////////////////////////////////////////////////////////////////
10 #ifdef __GNUG__
11 #pragma implementation "snduss.h"
12 #endif
13
14 #include <sys/soundcard.h>
15 #include <sys/ioctl.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <time.h>
19
20 #include "wx/app.h"
21 #include "wx/utils.h"
22
23 #define WXMMEDIA_INTERNAL
24 #include "snduss.h"
25 #include "sndfrmt.h"
26
27 wxUssSound::wxUssSound()
28 : wxSound(),
29 m_srate(0), m_bps(0), m_stereo(0),
30 m_mode(wxSND_OTHER_IO),
31 m_stop_thrd(TRUE), m_sleeping(FALSE)
32 {
33 m_fd = -1;
34 m_ussformat.SetCodecNo(WXSOUND_PCM);
35 m_ussformat.SetSign(wxSND_SAMPLE_SIGNED);
36 m_ussformat.SetByteOrder(wxSND_SAMPLE_LE);
37
38 m_sndbuf = new wxStreamBuffer(wxStreamBuffer::read_write);
39 m_sndbuf->Flushable(FALSE);
40 m_sndbuf->Fixed(TRUE);
41 }
42
43 wxUssSound::~wxUssSound()
44 {
45 if (!m_stop_thrd) {
46 m_stop_thrd = TRUE;
47 if (m_sleeping) {
48 m_sleep_mtx.Lock();
49 m_sleep_cond.Signal();
50 m_sleep_mtx.Unlock();
51 }
52 while (IsAlive())
53 Yield();
54 }
55
56 if (m_fd != -1)
57 close(m_fd);
58 }
59
60 bool wxUssSound::Wakeup(wxSndBuffer& WXUNUSED(buf))
61 {
62 printf("Waking up (wxUssSound::Wakeup) ...\n");
63 if (m_stop_thrd) {
64 m_stop_thrd = FALSE;
65 Entry();
66 // wxThread::Create();
67 }
68
69 if (m_sleeping) {
70 m_sleep_mtx.Lock();
71 m_sleep_cond.Signal();
72 m_sleep_mtx.Unlock();
73 }
74
75 return TRUE;
76 }
77
78 void wxUssSound::StopBuffer(wxSndBuffer& buf)
79 {
80 buf.HardLock();
81 buf.Set(wxSND_BUFSTOP);
82 buf.HardUnlock();
83 while (buf.IsSet(wxSND_BUFSTOP))
84 wxYield();
85 // usleep(0);
86 }
87
88 void wxUssSound::USS_Sleep()
89 {
90 bool ret;
91
92 printf("Asleeping ...\n");
93 m_sleeping = TRUE;
94 m_sleep_mtx.Lock();
95 ret = m_sleep_cond.Wait(m_sleep_mtx, 10, 0);
96 m_sleep_mtx.Unlock();
97 m_sleeping = FALSE;
98
99 printf("Waking up ...\n");
100 if (!ret)
101 m_stop_thrd = TRUE;
102 }
103
104 bool wxUssSound::DoInput(wxSndBuffer *buf)
105 {
106 wxUint32 bufsize;
107 wxSoundCodec *codec = buf->GetCurrentCodec();
108
109 m_sndbuf->ResetBuffer();
110
111 bufsize = codec->Available();
112 if (bufsize > m_max_bufsize)
113 bufsize = m_max_bufsize;
114
115 if (!bufsize) {
116 buf->Clear(wxSND_BUFLOCKED | wxSND_BUFREADY);
117 return false;
118 }
119 read(m_fd, m_sndbuf->GetBufferStart(), bufsize);
120 codec->Encode();
121
122 return true;
123 }
124
125 bool wxUssSound::DoOutput(wxSndBuffer *buf)
126 {
127 wxSoundCodec *codec = buf->GetCurrentCodec();
128
129 m_sndbuf->ResetBuffer();
130
131 if (!codec->Available()) {
132 buf->Clear(wxSND_BUFLOCKED | wxSND_BUFREADY);
133 return FALSE;
134 }
135 codec->Decode();
136 write(m_fd, m_sndbuf->GetBufferStart(), m_sndbuf->GetIntPosition());
137
138 // Well ... it's not accurate ! :-|
139 buf->OnBufferOutFinished();
140
141 return TRUE;
142 }
143
144 bool wxUssSound::InitBuffer(wxSndBuffer *buf)
145 {
146 wxSoundCodec *codec;
147
148 if (!OnSetupDriver(*buf, buf->GetMode())) {
149 if (buf->IsNotSet(wxSND_BUFREADY))
150 return FALSE;
151 }
152
153 codec = buf->GetCurrentCodec();
154 switch (m_mode) {
155 case wxSND_INPUT:
156 codec->SetInStream(m_sndbuf);
157 codec->InitIO(m_ussformat);
158 codec->InitMode(wxSoundCodec::ENCODING);
159 break;
160 case wxSND_OUTPUT:
161 codec->SetOutStream(m_sndbuf);
162 codec->InitIO(m_ussformat);
163 codec->InitMode(wxSoundCodec::DECODING);
164 break;
165 }
166 return TRUE;
167 }
168
169 void *wxUssSound::Entry()
170 {
171 wxNode *node;
172 wxSndBuffer *buf;
173
174 node = m_buffers.First();
175 if (!node) {
176 m_stop_thrd = FALSE;
177 return NULL;
178 }
179
180 buf = (wxSndBuffer *)node->Data();
181 InitBuffer(buf);
182
183 while (!m_stop_thrd) {
184 buf->HardLock();
185 if (buf->IsSet(wxSND_BUFSTOP)) {
186 buf->HardUnlock();
187 goto sound_clean_buffer;
188 }
189 switch(m_mode) {
190 case wxSND_INPUT:
191 if (!DoInput(buf))
192 goto sound_clean_buffer;
193 break;
194 case wxSND_OUTPUT:
195 if (!DoOutput(buf))
196 goto sound_clean_buffer;
197 break;
198 case wxSND_DUPLEX:
199 case wxSND_OTHER_IO:
200 goto sound_clean_buffer;
201 break;
202 }
203 buf->HardUnlock();
204 continue;
205 sound_clean_buffer:
206 buf->GetCurrentCodec()->ExitMode();
207 delete node;
208 node = m_buffers.First();
209 if (!node)
210 USS_Sleep();
211 if (node)
212 buf = (wxSndBuffer *)node->Data();
213 }
214 return NULL;
215 }
216
217 bool wxUssSound::OnSetupDriver(wxSndBuffer& buf, wxSndMode WXUNUSED(mode))
218 {
219 wxSoundDataFormat format;
220 wxSoundCodec *codec;
221
222 codec = buf.GetCurrentCodec();
223 format = codec->GetPreferredFormat(WXSOUND_PCM);
224
225 if ((format.GetSampleRate() != m_srate) ||
226 (format.GetBps() != m_bps) ||
227 (format.GetStereo() != m_stereo)) {
228
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);
234 return FALSE;
235 }
236 m_mode = wxSND_OTHER_IO;
237 }
238
239 if (buf.GetMode() != m_mode) {
240 m_mode = buf.GetMode();
241 return FALSE;
242 }
243
244 return TRUE;
245 }
246
247 wxUint32 wxUssSound::GetNbFragments()
248 {
249 struct audio_buf_info frag_info;
250
251 ioctl(m_fd, SNDCTL_DSP_GETOSPACE, &frag_info);
252
253 return frag_info.fragstotal;
254 }
255
256 wxUint32 wxUssSound::GetFragmentSize()
257 {
258 return m_max_bufsize;
259 }
260
261 bool wxUssSound::SetupSound(wxUint16 srate, wxUint8 bps, bool stereo)
262 {
263 int tmp;
264 unsigned long tmp_ul;
265
266 if (m_fd != -1) {
267 delete m_sndbuf;
268 fsync(m_fd);
269 close(m_fd);
270 }
271
272 m_fd = open("/dev/dsp", O_RDWR);
273
274 tmp = stereo;
275 if (ioctl(m_fd, SNDCTL_DSP_STEREO, &tmp) < 0)
276 return FALSE;
277 m_stereo = tmp;
278
279 tmp_ul = srate;
280 if (ioctl(m_fd, SNDCTL_DSP_SPEED, &tmp_ul) < 0)
281 return FALSE;
282 m_srate = tmp_ul;
283
284 tmp = bps;
285 if (ioctl(m_fd, SNDCTL_DSP_SAMPLESIZE, &tmp) < 0)
286 return FALSE;
287 m_bps = tmp;
288
289 ioctl(m_fd, SNDCTL_DSP_GETBLKSIZE, &tmp);
290 m_max_bufsize = tmp;
291 m_sndbuf->SetBufferIO(m_max_bufsize);
292
293 m_ussformat.SetBps(m_bps);
294 m_ussformat.SetChannels((m_stereo) ? 2 : 1);
295 m_ussformat.SetSampleRate(m_srate);
296
297 return TRUE;
298 }