]> git.saurik.com Git - wxWidgets.git/blob - utils/wxMMedia/snduss.cpp
* Fixes (WAV works on Linux, AIFF following)
[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 Join();
53 }
54
55 if (m_fd != -1)
56 close(m_fd);
57 }
58
59 bool wxUssSound::Wakeup(wxSndBuffer& WXUNUSED(buf))
60 {
61 printf("Waking up (wxUssSound::Wakeup) ...\n");
62 if (m_stop_thrd) {
63 m_stop_thrd = FALSE;
64 Entry();
65 // wxThread::Create();
66 }
67
68 if (m_sleeping) {
69 m_sleep_mtx.Lock();
70 m_sleep_cond.Signal();
71 m_sleep_mtx.Unlock();
72 }
73
74 return TRUE;
75 }
76
77 void wxUssSound::StopBuffer(wxSndBuffer& buf)
78 {
79 buf.HardLock();
80 buf.Set(wxSND_BUFSTOP);
81 buf.HardUnlock();
82 while (buf.IsSet(wxSND_BUFSTOP))
83 wxYield();
84 // usleep(0);
85 }
86
87 void wxUssSound::USS_Sleep()
88 {
89 bool ret;
90
91 printf("Asleeping ...\n");
92 m_sleeping = TRUE;
93 m_sleep_mtx.Lock();
94 ret = m_sleep_cond.Wait(m_sleep_mtx, 10, 0);
95 m_sleep_mtx.Unlock();
96 m_sleeping = FALSE;
97
98 printf("Waking up ...\n");
99 if (!ret)
100 m_stop_thrd = TRUE;
101 }
102
103 bool wxUssSound::DoInput(wxSndBuffer *buf)
104 {
105 wxUint32 bufsize;
106 wxSoundCodec *codec = buf->GetCurrentCodec();
107
108 m_sndbuf->ResetBuffer();
109 codec->SetInStream(m_sndbuf);
110 codec->InitIO(m_ussformat);
111
112 bufsize = codec->Available();
113 if (bufsize > m_max_bufsize)
114 bufsize = m_max_bufsize;
115
116 if (!bufsize) {
117 buf->Clear(wxSND_BUFLOCKED | wxSND_BUFREADY);
118 return false;
119 }
120 read(m_fd, m_sndbuf->GetBufferStart(), bufsize);
121 codec->Encode();
122
123 return true;
124 }
125
126 bool wxUssSound::DoOutput(wxSndBuffer *buf)
127 {
128 wxSoundCodec *codec = buf->GetCurrentCodec();
129
130 m_sndbuf->ResetBuffer();
131
132 if (!codec->Available()) {
133 buf->Clear(wxSND_BUFLOCKED | wxSND_BUFREADY);
134 return FALSE;
135 }
136 codec->Decode();
137 write(m_fd, m_sndbuf->GetBufferStart(), m_sndbuf->GetIntPosition());
138
139 // Well ... it's not accurate ! :-|
140 buf->OnBufferOutFinished();
141
142 return TRUE;
143 }
144
145 bool wxUssSound::InitBuffer(wxSndBuffer *buf)
146 {
147 wxSoundCodec *codec;
148
149 if (!OnSetupDriver(*buf, buf->GetMode())) {
150 if (buf->IsNotSet(wxSND_BUFREADY))
151 return FALSE;
152 }
153
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);
159
160 return TRUE;
161 }
162
163 void *wxUssSound::Entry()
164 {
165 wxNode *node;
166 wxSndBuffer *buf;
167
168 node = m_buffers.First();
169 if (!node) {
170 m_stop_thrd = FALSE;
171 return NULL;
172 }
173
174 buf = (wxSndBuffer *)node->Data();
175 InitBuffer(buf);
176
177 while (!m_stop_thrd) {
178 buf->HardLock();
179 if (buf->IsSet(wxSND_BUFSTOP)) {
180 buf->HardUnlock();
181 goto sound_clean_buffer;
182 }
183 switch(m_mode) {
184 case wxSND_INPUT:
185 if (!DoInput(buf))
186 goto sound_clean_buffer;
187 break;
188 case wxSND_OUTPUT:
189 if (!DoOutput(buf))
190 goto sound_clean_buffer;
191 break;
192 case wxSND_DUPLEX:
193 case wxSND_OTHER_IO:
194 goto sound_clean_buffer;
195 break;
196 }
197 buf->HardUnlock();
198 continue;
199 sound_clean_buffer:
200 buf->GetCurrentCodec()->ExitMode();
201 delete node;
202 node = m_buffers.First();
203 if (!node)
204 USS_Sleep();
205 if (node)
206 buf = (wxSndBuffer *)node->Data();
207 }
208 return NULL;
209 }
210
211 bool wxUssSound::OnSetupDriver(wxSndBuffer& buf, wxSndMode WXUNUSED(mode))
212 {
213 wxSoundDataFormat format;
214 wxSoundCodec *codec;
215
216 codec = buf.GetCurrentCodec();
217 format = codec->GetPreferredFormat(WXSOUND_PCM);
218
219 if ((format.GetSampleRate() != m_srate) ||
220 (format.GetBps() != m_bps) ||
221 (format.GetStereo() != m_stereo)) {
222
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);
228 return FALSE;
229 }
230 m_mode = wxSND_OTHER_IO;
231 }
232
233 if (buf.GetMode() != m_mode) {
234 m_mode = buf.GetMode();
235 return FALSE;
236 }
237
238 return TRUE;
239 }
240
241 wxUint32 wxUssSound::GetNbFragments()
242 {
243 struct audio_buf_info frag_info;
244
245 ioctl(m_fd, SNDCTL_DSP_GETOSPACE, &frag_info);
246
247 return frag_info.fragstotal;
248 }
249
250 wxUint32 wxUssSound::GetFragmentSize()
251 {
252 return m_max_bufsize;
253 }
254
255 bool wxUssSound::SetupSound(wxUint16 srate, wxUint8 bps, bool stereo)
256 {
257 int tmp;
258 unsigned long tmp_ul;
259
260 if (m_fd != -1) {
261 delete m_sndbuf;
262 fsync(m_fd);
263 close(m_fd);
264 }
265
266 m_fd = open("/dev/dsp", O_RDWR);
267
268 tmp = stereo;
269 if (ioctl(m_fd, SNDCTL_DSP_STEREO, &tmp) < 0)
270 return FALSE;
271 m_stereo = tmp;
272
273 tmp_ul = srate;
274 if (ioctl(m_fd, SNDCTL_DSP_SPEED, &tmp_ul) < 0)
275 return FALSE;
276 m_srate = tmp_ul;
277
278 tmp = bps;
279 if (ioctl(m_fd, SNDCTL_DSP_SAMPLESIZE, &tmp) < 0)
280 return FALSE;
281 m_bps = tmp;
282
283 ioctl(m_fd, SNDCTL_DSP_GETBLKSIZE, &tmp);
284 m_max_bufsize = tmp;
285 m_sndbuf->SetBufferIO(m_max_bufsize);
286
287 m_ussformat.SetBps(m_bps);
288 m_ussformat.SetChannels((m_stereo) ? 2 : 1);
289 m_ussformat.SetSampleRate(m_srate);
290
291 return TRUE;
292 }