Make out-of-source-tree builds work with Solaris make.
[wine/dcerpc.git] / dlls / winmm / wineoss / audio.c
blob1e0984f041668a0bfe1f20ad03341a4dbba02208
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
8 */
9 /*
10 * FIXME:
11 * pause in waveOut does not work correctly
12 * full duplex (in/out) is not working (device is opened twice for Out
13 * and In) (OSS is known for its poor duplex capabilities, alsa is
14 * better)
17 /*#define EMULATE_SB16*/
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_MMAN_H
29 # include <sys/mman.h>
30 #endif
31 #include "windef.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "wine/winuser16.h"
35 #include "mmddk.h"
36 #include "dsound.h"
37 #include "dsdriver.h"
38 #include "oss.h"
39 #include "debugtools.h"
41 DEFAULT_DEBUG_CHANNEL(wave);
43 /* Allow 1% deviation for sample rates (some ES137x cards) */
44 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
46 #ifdef HAVE_OSS
48 #define SOUND_DEV "/dev/dsp"
49 #define MIXER_DEV "/dev/mixer"
51 #define MAX_WAVEOUTDRV (1)
52 #define MAX_WAVEINDRV (1)
54 /* state diagram for waveOut writing:
56 * +---------+-------------+---------------+---------------------------------+
57 * | state | function | event | new state |
58 * +---------+-------------+---------------+---------------------------------+
59 * | | open() | | STOPPED |
60 * | PAUSED | write() | | PAUSED |
61 * | STOPPED | write() | <thrd create> | PLAYING |
62 * | PLAYING | write() | HEADER | PLAYING |
63 * | (other) | write() | <error> | |
64 * | (any) | pause() | PAUSING | PAUSED |
65 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
66 * | (any) | reset() | RESETTING | STOPPED |
67 * | (any) | close() | CLOSING | CLOSED |
68 * +---------+-------------+---------------+---------------------------------+
71 /* states of the playing device */
72 #define WINE_WS_PLAYING 0
73 #define WINE_WS_PAUSED 1
74 #define WINE_WS_STOPPED 2
75 #define WINE_WS_CLOSED 3
77 /* events to be send to device */
78 #define WINE_WM_PAUSING (WM_USER + 1)
79 #define WINE_WM_RESTARTING (WM_USER + 2)
80 #define WINE_WM_RESETTING (WM_USER + 3)
81 #define WINE_WM_CLOSING (WM_USER + 4)
82 #define WINE_WM_HEADER (WM_USER + 5)
84 #define WINE_WM_FIRST WINE_WM_PAUSING
85 #define WINE_WM_LAST WINE_WM_HEADER
87 typedef struct {
88 int msg;
89 DWORD param;
90 } WWO_MSG;
92 typedef struct {
93 int unixdev;
94 volatile int state; /* one of the WINE_WS_ manifest constants */
95 DWORD dwFragmentSize; /* size of OSS buffer fragment */
96 WAVEOPENDESC waveDesc;
97 WORD wFlags;
98 PCMWAVEFORMAT format;
99 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
100 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
101 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
102 DWORD dwLoops; /* private copy of loop counter */
104 DWORD dwLastFragDone; /* time in ms, when last played fragment will be actually played */
105 DWORD dwPlayedTotal; /* number of bytes played since opening */
107 /* info on current lpQueueHdr->lpWaveHdr */
108 DWORD dwOffCurrHdr; /* offset in lpPlayPtr->lpData for fragments */
109 DWORD dwRemain; /* number of bytes to write to end the current fragment */
111 /* synchronization stuff */
112 HANDLE hThread;
113 DWORD dwThreadID;
114 HANDLE hEvent;
115 #define WWO_RING_BUFFER_SIZE 30
116 WWO_MSG messages[WWO_RING_BUFFER_SIZE];
117 int msg_tosave;
118 int msg_toget;
119 HANDLE msg_event;
120 CRITICAL_SECTION msg_crst;
121 WAVEOUTCAPSA caps;
123 /* DirectSound stuff */
124 LPBYTE mapping;
125 DWORD maplen;
126 } WINE_WAVEOUT;
128 typedef struct {
129 int unixdev;
130 volatile int state;
131 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
132 WAVEOPENDESC waveDesc;
133 WORD wFlags;
134 PCMWAVEFORMAT format;
135 LPWAVEHDR lpQueuePtr;
136 DWORD dwTotalRecorded;
137 WAVEINCAPSA caps;
138 BOOL bTriggerSupport;
140 /* synchronization stuff */
141 HANDLE hThread;
142 DWORD dwThreadID;
143 HANDLE hEvent;
144 } WINE_WAVEIN;
146 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
147 static WINE_WAVEIN WInDev [MAX_WAVEINDRV ];
149 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
151 /*======================================================================*
152 * Low level WAVE implementation *
153 *======================================================================*/
155 LONG OSS_WaveInit(void)
157 int audio;
158 int smplrate;
159 int samplesize = 16;
160 int dsp_stereo = 1;
161 int bytespersmpl;
162 int caps;
163 int mask;
164 int i;
167 /* start with output device */
169 /* initialize all device handles to -1 */
170 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
172 WOutDev[i].unixdev = -1;
175 /* FIXME: only one device is supported */
176 memset(&WOutDev[0].caps, 0, sizeof(WOutDev[0].caps));
178 if (access(SOUND_DEV,0) != 0 ||
179 (audio = open(SOUND_DEV, O_WRONLY|O_NDELAY, 0)) == -1) {
180 WARN("Couldn't open out %s (%s)\n", SOUND_DEV, strerror(errno));
181 return -1;
184 ioctl(audio, SNDCTL_DSP_RESET, 0);
186 /* FIXME: some programs compare this string against the content of the registry
187 * for MM drivers. The names have to match in order for the program to work
188 * (e.g. MS win9x mplayer.exe)
190 #ifdef EMULATE_SB16
191 WOutDev[0].caps.wMid = 0x0002;
192 WOutDev[0].caps.wPid = 0x0104;
193 strcpy(WOutDev[0].caps.szPname, "SB16 Wave Out");
194 #else
195 WOutDev[0].caps.wMid = 0x00FF; /* Manufac ID */
196 WOutDev[0].caps.wPid = 0x0001; /* Product ID */
197 /* strcpy(WOutDev[0].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
198 strcpy(WOutDev[0].caps.szPname, "CS4236/37/38");
199 #endif
200 WOutDev[0].caps.vDriverVersion = 0x0100;
201 WOutDev[0].caps.dwFormats = 0x00000000;
202 WOutDev[0].caps.dwSupport = WAVECAPS_VOLUME;
204 IOCTL(audio, SNDCTL_DSP_GETFMTS, mask);
205 TRACE("OSS dsp out mask=%08x\n", mask);
207 /* First bytespersampl, then stereo */
208 bytespersmpl = (IOCTL(audio, SNDCTL_DSP_SAMPLESIZE, samplesize) != 0) ? 1 : 2;
210 WOutDev[0].caps.wChannels = (IOCTL(audio, SNDCTL_DSP_STEREO, dsp_stereo) != 0) ? 1 : 2;
211 if (WOutDev[0].caps.wChannels > 1) WOutDev[0].caps.dwSupport |= WAVECAPS_LRVOLUME;
213 smplrate = 44100;
214 if (IOCTL(audio, SNDCTL_DSP_SPEED, smplrate) == 0) {
215 if (mask & AFMT_U8) {
216 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4M08;
217 if (WOutDev[0].caps.wChannels > 1)
218 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4S08;
220 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
221 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4M16;
222 if (WOutDev[0].caps.wChannels > 1)
223 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4S16;
226 smplrate = 22050;
227 if (IOCTL(audio, SNDCTL_DSP_SPEED, smplrate) == 0) {
228 if (mask & AFMT_U8) {
229 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_2M08;
230 if (WOutDev[0].caps.wChannels > 1)
231 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_2S08;
233 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
234 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_2M16;
235 if (WOutDev[0].caps.wChannels > 1)
236 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_2S16;
239 smplrate = 11025;
240 if (IOCTL(audio, SNDCTL_DSP_SPEED, smplrate) == 0) {
241 if (mask & AFMT_U8) {
242 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_1M08;
243 if (WOutDev[0].caps.wChannels > 1)
244 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_1S08;
246 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
247 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_1M16;
248 if (WOutDev[0].caps.wChannels > 1)
249 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_1S16;
252 if (IOCTL(audio, SNDCTL_DSP_GETCAPS, caps) == 0) {
253 TRACE("OSS dsp out caps=%08X\n", caps);
254 if ((caps & DSP_CAP_REALTIME) && !(caps & DSP_CAP_BATCH)) {
255 WOutDev[0].caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
257 /* well, might as well use the DirectSound cap flag for something */
258 if ((caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
259 !(caps & DSP_CAP_BATCH))
260 WOutDev[0].caps.dwSupport |= WAVECAPS_DIRECTSOUND;
262 close(audio);
263 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
264 WOutDev[0].caps.dwFormats, WOutDev[0].caps.dwSupport);
266 /* then do input device */
267 samplesize = 16;
268 dsp_stereo = 1;
270 for (i = 0; i < MAX_WAVEINDRV; ++i)
272 WInDev[i].unixdev = -1;
275 memset(&WInDev[0].caps, 0, sizeof(WInDev[0].caps));
277 if (access(SOUND_DEV,0) != 0 ||
278 (audio = open(SOUND_DEV, O_RDONLY|O_NDELAY, 0)) == -1) {
279 WARN("Couldn't open in %s (%s)\n", SOUND_DEV, strerror(errno));
280 return -1;
283 ioctl(audio, SNDCTL_DSP_RESET, 0);
285 #ifdef EMULATE_SB16
286 WInDev[0].caps.wMid = 0x0002;
287 WInDev[0].caps.wPid = 0x0004;
288 strcpy(WInDev[0].caps.szPname, "SB16 Wave In");
289 #else
290 WInDev[0].caps.wMid = 0x00FF; /* Manufac ID */
291 WInDev[0].caps.wPid = 0x0001; /* Product ID */
292 strcpy(WInDev[0].caps.szPname, "OpenSoundSystem WAVIN Driver");
293 #endif
294 WInDev[0].caps.dwFormats = 0x00000000;
295 WInDev[0].caps.wChannels = (IOCTL(audio, SNDCTL_DSP_STEREO, dsp_stereo) != 0) ? 1 : 2;
297 WInDev[0].bTriggerSupport = FALSE;
298 if (IOCTL(audio, SNDCTL_DSP_GETCAPS, caps) == 0) {
299 TRACE("OSS dsp in caps=%08X\n", caps);
300 if (caps & DSP_CAP_TRIGGER)
301 WInDev[0].bTriggerSupport = TRUE;
304 IOCTL(audio, SNDCTL_DSP_GETFMTS, mask);
305 TRACE("OSS in dsp mask=%08x\n", mask);
307 bytespersmpl = (IOCTL(audio, SNDCTL_DSP_SAMPLESIZE, samplesize) != 0) ? 1 : 2;
308 smplrate = 44100;
309 if (IOCTL(audio, SNDCTL_DSP_SPEED, smplrate) == 0) {
310 if (mask & AFMT_U8) {
311 WInDev[0].caps.dwFormats |= WAVE_FORMAT_4M08;
312 if (WInDev[0].caps.wChannels > 1)
313 WInDev[0].caps.dwFormats |= WAVE_FORMAT_4S08;
315 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
316 WInDev[0].caps.dwFormats |= WAVE_FORMAT_4M16;
317 if (WInDev[0].caps.wChannels > 1)
318 WInDev[0].caps.dwFormats |= WAVE_FORMAT_4S16;
321 smplrate = 22050;
322 if (IOCTL(audio, SNDCTL_DSP_SPEED, smplrate) == 0) {
323 if (mask & AFMT_U8) {
324 WInDev[0].caps.dwFormats |= WAVE_FORMAT_2M08;
325 if (WInDev[0].caps.wChannels > 1)
326 WInDev[0].caps.dwFormats |= WAVE_FORMAT_2S08;
328 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
329 WInDev[0].caps.dwFormats |= WAVE_FORMAT_2M16;
330 if (WInDev[0].caps.wChannels > 1)
331 WInDev[0].caps.dwFormats |= WAVE_FORMAT_2S16;
334 smplrate = 11025;
335 if (IOCTL(audio, SNDCTL_DSP_SPEED, smplrate) == 0) {
336 if (mask & AFMT_U8) {
337 WInDev[0].caps.dwFormats |= WAVE_FORMAT_1M08;
338 if (WInDev[0].caps.wChannels > 1)
339 WInDev[0].caps.dwFormats |= WAVE_FORMAT_1S08;
341 if ((mask & AFMT_S16_LE) && bytespersmpl > 1) {
342 WInDev[0].caps.dwFormats |= WAVE_FORMAT_1M16;
343 if (WInDev[0].caps.wChannels > 1)
344 WInDev[0].caps.dwFormats |= WAVE_FORMAT_1S16;
347 close(audio);
348 TRACE("in dwFormats = %08lX\n", WInDev[0].caps.dwFormats);
350 return 0;
353 /**************************************************************************
354 * OSS_NotifyClient [internal]
356 static DWORD OSS_NotifyClient(UINT wDevID, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
358 TRACE("wDevID = %04X wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n",wDevID, wMsg, dwParam1, dwParam2);
360 switch (wMsg) {
361 case WOM_OPEN:
362 case WOM_CLOSE:
363 case WOM_DONE:
364 if (wDevID >= MAX_WAVEOUTDRV) return MCIERR_INTERNAL;
366 if (WOutDev[wDevID].wFlags != DCB_NULL &&
367 !DriverCallback(WOutDev[wDevID].waveDesc.dwCallback,
368 WOutDev[wDevID].wFlags,
369 WOutDev[wDevID].waveDesc.hWave,
370 wMsg,
371 WOutDev[wDevID].waveDesc.dwInstance,
372 dwParam1,
373 dwParam2)) {
374 WARN("can't notify client !\n");
375 return MMSYSERR_NOERROR;
377 break;
379 case WIM_OPEN:
380 case WIM_CLOSE:
381 case WIM_DATA:
382 if (wDevID >= MAX_WAVEINDRV) return MCIERR_INTERNAL;
384 if (WInDev[wDevID].wFlags != DCB_NULL &&
385 !DriverCallback(WInDev[wDevID].waveDesc.dwCallback,
386 WInDev[wDevID].wFlags,
387 WInDev[wDevID].waveDesc.hWave,
388 wMsg,
389 WInDev[wDevID].waveDesc.dwInstance,
390 dwParam1,
391 dwParam2)) {
392 WARN("can't notify client !\n");
393 return MMSYSERR_NOERROR;
395 break;
396 default:
397 FIXME("Unknown CB message %u\n", wMsg);
398 break;
400 return 0;
403 /*======================================================================*
404 * Low level WAVE OUT implementation *
405 *======================================================================*/
407 /**************************************************************************
408 * wodPlayer_WriteFragments [internal]
410 * wodPlayer helper. Writes as many fragments as it can to unixdev.
411 * Returns TRUE in case of buffer underrun.
413 static BOOL wodPlayer_WriteFragments(WINE_WAVEOUT* wwo)
415 LPWAVEHDR lpWaveHdr;
416 LPBYTE lpData;
417 int count;
418 audio_buf_info info;
420 for (;;) {
421 if (ioctl(wwo->unixdev, SNDCTL_DSP_GETOSPACE, &info) < 0) {
422 ERR("ioctl failed (%s)\n", strerror(errno));
423 return FALSE;
426 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
428 if (!info.fragments) /* output queue is full, wait a bit */
429 return FALSE;
431 lpWaveHdr = wwo->lpPlayPtr;
432 if (!lpWaveHdr) {
433 if (wwo->dwRemain > 0 && /* still data to send to complete current fragment */
434 wwo->dwLastFragDone && /* first fragment has been played */
435 info.fragments + 2 > info.fragstotal) { /* done with all waveOutWrite()' fragments */
436 /* FIXME: should do better handling here */
437 WARN("Oooch, buffer underrun !\n");
438 return TRUE; /* force resetting of waveOut device */
440 return FALSE; /* wait a bit */
443 if (wwo->dwOffCurrHdr == 0) {
444 TRACE("Starting a new wavehdr %p of %ld bytes\n", lpWaveHdr, lpWaveHdr->dwBufferLength);
445 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
446 if (wwo->lpLoopPtr) {
447 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
448 } else {
449 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
450 wwo->lpLoopPtr = lpWaveHdr;
451 /* Windows does not touch WAVEHDR.dwLoops,
452 * so we need to make an internal copy */
453 wwo->dwLoops = lpWaveHdr->dwLoops;
458 lpData = lpWaveHdr->lpData;
460 /* finish current wave hdr ? */
461 if (wwo->dwOffCurrHdr + wwo->dwRemain >= lpWaveHdr->dwBufferLength) {
462 DWORD toWrite = lpWaveHdr->dwBufferLength - wwo->dwOffCurrHdr;
464 /* write end of current wave hdr */
465 count = write(wwo->unixdev, lpData + wwo->dwOffCurrHdr, toWrite);
466 TRACE("write(%p[%5lu], %5lu) => %d\n", lpData, wwo->dwOffCurrHdr, toWrite, count);
468 if (count > 0 || toWrite == 0) {
469 DWORD tc = GetTickCount();
471 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
472 wwo->dwLastFragDone = tc;
473 wwo->dwLastFragDone += (toWrite * 1000) / wwo->format.wf.nAvgBytesPerSec;
475 lpWaveHdr->reserved = wwo->dwLastFragDone;
476 TRACE("Tagging hdr %p with %08lx\n", lpWaveHdr, wwo->dwLastFragDone);
478 /* WAVEHDR written, go to next one */
479 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
480 if (--wwo->dwLoops > 0) {
481 wwo->lpPlayPtr = wwo->lpLoopPtr;
482 } else {
483 /* last one played */
484 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
485 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
486 /* shall we consider the END flag for the closing loop or for
487 * the opening one or for both ???
488 * code assumes for closing loop only
490 wwo->lpLoopPtr = lpWaveHdr;
491 } else {
492 wwo->lpLoopPtr = NULL;
494 wwo->lpPlayPtr = lpWaveHdr->lpNext;
496 } else {
497 wwo->lpPlayPtr = lpWaveHdr->lpNext;
499 wwo->dwOffCurrHdr = 0;
500 if ((wwo->dwRemain -= count) == 0) {
501 wwo->dwRemain = wwo->dwFragmentSize;
504 continue; /* try to go to use next wavehdr */
505 } else {
506 count = write(wwo->unixdev, lpData + wwo->dwOffCurrHdr, wwo->dwRemain);
507 TRACE("write(%p[%5lu], %5lu) => %d\n", lpData, wwo->dwOffCurrHdr, wwo->dwRemain, count);
508 if (count > 0) {
509 DWORD tc = GetTickCount();
511 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
512 wwo->dwLastFragDone = tc;
513 wwo->dwLastFragDone += (wwo->dwRemain * 1000) / wwo->format.wf.nAvgBytesPerSec;
515 TRACE("Tagging frag with %08lx\n", wwo->dwLastFragDone);
517 wwo->dwOffCurrHdr += count;
518 wwo->dwRemain = wwo->dwFragmentSize;
525 int wodPlayer_Message(WINE_WAVEOUT *wwo, int msg, DWORD param)
527 EnterCriticalSection(&wwo->msg_crst);
528 if ((wwo->msg_tosave == wwo->msg_toget) /* buffer overflow ? */
529 && (wwo->messages[wwo->msg_toget].msg))
531 ERR("buffer overflow !?\n");
532 LeaveCriticalSection(&wwo->msg_crst);
533 return 0;
536 wwo->messages[wwo->msg_tosave].msg = msg;
537 wwo->messages[wwo->msg_tosave].param = param;
538 wwo->msg_tosave++;
539 if (wwo->msg_tosave > WWO_RING_BUFFER_SIZE-1)
540 wwo->msg_tosave = 0;
541 LeaveCriticalSection(&wwo->msg_crst);
542 /* signal a new message */
543 SetEvent(wwo->msg_event);
544 return 1;
547 int wodPlayer_RetrieveMessage(WINE_WAVEOUT *wwo, int *msg, DWORD *param)
549 EnterCriticalSection(&wwo->msg_crst);
551 if (wwo->msg_toget == wwo->msg_tosave) /* buffer empty ? */
553 LeaveCriticalSection(&wwo->msg_crst);
554 return 0;
557 *msg = wwo->messages[wwo->msg_toget].msg;
558 wwo->messages[wwo->msg_toget].msg = 0;
559 *param = wwo->messages[wwo->msg_toget].param;
560 wwo->msg_toget++;
561 if (wwo->msg_toget > WWO_RING_BUFFER_SIZE-1)
562 wwo->msg_toget = 0;
563 LeaveCriticalSection(&wwo->msg_crst);
564 return 1;
567 /**************************************************************************
568 * wodPlayer_Notify [internal]
570 * wodPlayer helper. Notifies (and remove from queue) all the wavehdr which content
571 * have been played (actually to speaker, not to unixdev fd).
573 static void wodPlayer_Notify(WINE_WAVEOUT* wwo, WORD uDevID, BOOL force)
575 LPWAVEHDR lpWaveHdr;
576 DWORD tc = GetTickCount();
578 while (wwo->lpQueuePtr &&
579 (force ||
580 (wwo->lpQueuePtr != wwo->lpPlayPtr && wwo->lpQueuePtr != wwo->lpLoopPtr))) {
581 lpWaveHdr = wwo->lpQueuePtr;
583 if (lpWaveHdr->reserved > tc && !force) break;
585 wwo->dwPlayedTotal += lpWaveHdr->dwBufferLength;
586 wwo->lpQueuePtr = lpWaveHdr->lpNext;
588 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
589 lpWaveHdr->dwFlags |= WHDR_DONE;
591 TRACE("Notifying client with %p\n", lpWaveHdr);
592 if (OSS_NotifyClient(uDevID, WOM_DONE, (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR) {
593 WARN("can't notify client !\n");
598 /**************************************************************************
599 * wodPlayer_Reset [internal]
601 * wodPlayer helper. Resets current output stream.
603 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, WORD uDevID, BOOL reset)
605 /* updates current notify list */
606 wodPlayer_Notify(wwo, uDevID, FALSE);
608 /* flush all possible output */
609 if (ioctl(wwo->unixdev, SNDCTL_DSP_RESET, 0) == -1) {
610 perror("ioctl SNDCTL_DSP_RESET");
611 wwo->hThread = 0;
612 wwo->state = WINE_WS_STOPPED;
613 ExitThread(-1);
616 wwo->dwOffCurrHdr = 0;
617 wwo->dwRemain = wwo->dwFragmentSize;
618 if (reset) {
619 /* empty notify list */
620 wodPlayer_Notify(wwo, uDevID, TRUE);
622 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
623 wwo->state = WINE_WS_STOPPED;
624 wwo->dwPlayedTotal = 0;
625 } else {
626 /* FIXME: this is not accurate when looping, but can be do better ? */
627 wwo->lpPlayPtr = (wwo->lpLoopPtr) ? wwo->lpLoopPtr : wwo->lpQueuePtr;
628 wwo->state = WINE_WS_PAUSED;
632 /**************************************************************************
633 * wodPlayer [internal]
635 static DWORD CALLBACK wodPlayer(LPVOID pmt)
637 WORD uDevID = (DWORD)pmt;
638 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
639 WAVEHDR* lpWaveHdr;
640 DWORD dwSleepTime;
641 int msg;
642 DWORD param;
643 DWORD tc;
644 BOOL had_msg;
646 wwo->state = WINE_WS_STOPPED;
648 wwo->dwLastFragDone = 0;
649 wwo->dwOffCurrHdr = 0;
650 wwo->dwRemain = wwo->dwFragmentSize;
651 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
652 wwo->dwPlayedTotal = 0;
654 TRACE("imhere[0]\n");
655 SetEvent(wwo->hEvent);
657 for (;;) {
658 /* wait for dwSleepTime or an event in thread's queue
659 * FIXME:
660 * - is wait time calculation optimal ?
661 * - these 100 ms parts should be changed, but Eric reports
662 * that the wodPlayer thread might lock up if we use INFINITE
663 * (strange !), so I better don't change that now... */
664 if (wwo->state != WINE_WS_PLAYING)
665 dwSleepTime = 100;
666 else
668 tc = GetTickCount();
669 if (tc < wwo->dwLastFragDone)
671 /* calculate sleep time depending on when the last fragment
672 will be played */
673 dwSleepTime = (wwo->dwLastFragDone - tc)*7/10;
674 if (dwSleepTime > 100)
675 dwSleepTime = 100;
677 else
678 dwSleepTime = 0;
681 TRACE("imhere[1] tc = %08lx\n", GetTickCount());
682 if (dwSleepTime)
683 WaitForSingleObject(wwo->msg_event, dwSleepTime);
684 TRACE("imhere[2] (q=%p p=%p) tc = %08lx\n", wwo->lpQueuePtr,
685 wwo->lpPlayPtr, GetTickCount());
686 had_msg = FALSE;
687 while (wodPlayer_RetrieveMessage(wwo, &msg, &param)) {
688 had_msg = TRUE;
689 switch (msg) {
690 case WINE_WM_PAUSING:
691 wodPlayer_Reset(wwo, uDevID, FALSE);
692 wwo->state = WINE_WS_PAUSED;
693 SetEvent(wwo->hEvent);
694 break;
695 case WINE_WM_RESTARTING:
696 wwo->state = WINE_WS_PLAYING;
697 SetEvent(wwo->hEvent);
698 break;
699 case WINE_WM_HEADER:
700 lpWaveHdr = (LPWAVEHDR)param;
702 /* insert buffer at the end of queue */
704 LPWAVEHDR* wh;
705 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
706 *wh = lpWaveHdr;
708 if (!wwo->lpPlayPtr) wwo->lpPlayPtr = lpWaveHdr;
709 if (wwo->state == WINE_WS_STOPPED)
710 wwo->state = WINE_WS_PLAYING;
711 break;
712 case WINE_WM_RESETTING:
713 wodPlayer_Reset(wwo, uDevID, TRUE);
714 SetEvent(wwo->hEvent);
715 break;
716 case WINE_WM_CLOSING:
717 /* sanity check: this should not happen since the device must have been reset before */
718 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
719 wwo->hThread = 0;
720 wwo->state = WINE_WS_CLOSED;
721 SetEvent(wwo->hEvent);
722 ExitThread(0);
723 /* shouldn't go here */
724 default:
725 FIXME("unknown message %d\n", msg);
726 break;
728 if (wwo->state == WINE_WS_PLAYING) {
729 wodPlayer_WriteFragments(wwo);
731 wodPlayer_Notify(wwo, uDevID, FALSE);
734 if (!had_msg) { /* if we've received a msg we've just done this so we
735 won't repeat it */
736 if (wwo->state == WINE_WS_PLAYING) {
737 wodPlayer_WriteFragments(wwo);
739 wodPlayer_Notify(wwo, uDevID, FALSE);
742 ExitThread(0);
743 /* just for not generating compilation warnings... should never be executed */
744 return 0;
747 /**************************************************************************
748 * wodGetDevCaps [internal]
750 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
752 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
754 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
756 if (wDevID >= MAX_WAVEOUTDRV) {
757 TRACE("MAX_WAVOUTDRV reached !\n");
758 return MMSYSERR_BADDEVICEID;
761 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
762 return MMSYSERR_NOERROR;
765 /**************************************************************************
766 * wodOpen [internal]
768 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
770 int audio;
771 int format;
772 int sample_rate;
773 int dsp_stereo;
774 int fragment_size;
775 int audio_fragment;
776 WINE_WAVEOUT* wwo;
778 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
779 if (lpDesc == NULL) {
780 WARN("Invalid Parameter !\n");
781 return MMSYSERR_INVALPARAM;
783 if (wDevID >= MAX_WAVEOUTDRV) {
784 TRACE("MAX_WAVOUTDRV reached !\n");
785 return MMSYSERR_BADDEVICEID;
788 /* only PCM format is supported so far... */
789 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
790 lpDesc->lpFormat->nChannels == 0 ||
791 lpDesc->lpFormat->nSamplesPerSec == 0) {
792 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
793 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
794 lpDesc->lpFormat->nSamplesPerSec);
795 return WAVERR_BADFORMAT;
798 if (dwFlags & WAVE_FORMAT_QUERY) {
799 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
800 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
801 lpDesc->lpFormat->nSamplesPerSec);
802 return MMSYSERR_NOERROR;
805 wwo = &WOutDev[wDevID];
807 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->caps.dwSupport & WAVECAPS_DIRECTSOUND))
808 /* not supported, ignore it */
809 dwFlags &= ~WAVE_DIRECTSOUND;
811 if (access(SOUND_DEV, 0) != 0)
812 return MMSYSERR_NOTENABLED;
813 if (dwFlags & WAVE_DIRECTSOUND)
814 /* we want to be able to mmap() the device, which means it must be opened readable,
815 * otherwise mmap() will fail (at least under Linux) */
816 audio = open(SOUND_DEV, O_RDWR|O_NDELAY, 0);
817 else
818 audio = open(SOUND_DEV, O_WRONLY|O_NDELAY, 0);
819 if (audio == -1) {
820 WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
821 return MMSYSERR_ALLOCATED;
823 fcntl(audio, F_SETFD, 1); /* set close on exec flag */
824 wwo->unixdev = audio;
825 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
827 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
828 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
830 if (wwo->format.wBitsPerSample == 0) {
831 WARN("Resetting zeroed wBitsPerSample\n");
832 wwo->format.wBitsPerSample = 8 *
833 (wwo->format.wf.nAvgBytesPerSec /
834 wwo->format.wf.nSamplesPerSec) /
835 wwo->format.wf.nChannels;
838 if (dwFlags & WAVE_DIRECTSOUND) {
839 if (wwo->caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
840 /* we have realtime DirectSound, fragments just waste our time,
841 * but a large buffer is good, so choose 64KB (32 * 2^11) */
842 audio_fragment = 0x0020000B;
843 else
844 /* to approximate realtime, we must use small fragments,
845 * let's try to fragment the above 64KB (256 * 2^8) */
846 audio_fragment = 0x01000008;
847 } else {
848 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
849 * thus leading to 46ms per fragment, and a turnaround time of 185ms
851 /* 16 fragments max, 2^10=1024 bytes per fragment */
852 audio_fragment = 0x000F000A;
854 sample_rate = wwo->format.wf.nSamplesPerSec;
855 dsp_stereo = (wwo->format.wf.nChannels > 1) ? 1 : 0;
856 format = (wwo->format.wBitsPerSample == 16) ? AFMT_S16_LE : AFMT_U8;
858 IOCTL(audio, SNDCTL_DSP_SETFRAGMENT, audio_fragment);
859 /* First size and stereo then samplerate */
860 IOCTL(audio, SNDCTL_DSP_SETFMT, format);
861 IOCTL(audio, SNDCTL_DSP_STEREO, dsp_stereo);
862 IOCTL(audio, SNDCTL_DSP_SPEED, sample_rate);
864 /* paranoid checks */
865 if (format != ((wwo->format.wBitsPerSample == 16) ? AFMT_S16_LE : AFMT_U8))
866 ERR("Can't set format to %d (%d)\n",
867 (wwo->format.wBitsPerSample == 16) ? AFMT_S16_LE : AFMT_U8, format);
868 if (dsp_stereo != (wwo->format.wf.nChannels > 1) ? 1 : 0)
869 ERR("Can't set stereo to %u (%d)\n",
870 (wwo->format.wf.nChannels > 1) ? 1 : 0, dsp_stereo);
871 if (!NEAR_MATCH(sample_rate,wwo->format.wf.nSamplesPerSec))
872 ERR("Can't set sample_rate to %lu (%d)\n",
873 wwo->format.wf.nSamplesPerSec, sample_rate);
875 /* even if we set fragment size above, read it again, just in case */
876 IOCTL(audio, SNDCTL_DSP_GETBLKSIZE, fragment_size);
877 if (fragment_size == -1) {
878 ERR("IOCTL can't 'SNDCTL_DSP_GETBLKSIZE' !\n");
879 close(audio);
880 wwo->unixdev = -1;
881 return MMSYSERR_NOTENABLED;
883 if ((fragment_size > 1024) && (LOWORD(audio_fragment) <= 10)) {
884 /* we've tried to set 1K fragments or less, but it didn't work */
885 ERR("fragment size set failed, size is now %d\n", fragment_size);
886 MESSAGE("Your Open Sound System driver did not let us configure small enough sound fragments.\n");
887 MESSAGE("This may cause delays and other problems in audio playback with certain applications.\n");
889 wwo->dwFragmentSize = fragment_size;
891 wwo->msg_toget = 0;
892 wwo->msg_tosave = 0;
893 wwo->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
894 memset(wwo->messages, 0, sizeof(WWO_MSG)*WWO_RING_BUFFER_SIZE);
895 InitializeCriticalSection(&wwo->msg_crst);
897 if (!(dwFlags & WAVE_DIRECTSOUND)) {
898 wwo->hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
899 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
900 WaitForSingleObject(wwo->hEvent, INFINITE);
901 } else {
902 wwo->hEvent = INVALID_HANDLE_VALUE;
903 wwo->hThread = INVALID_HANDLE_VALUE;
904 wwo->dwThreadID = 0;
907 TRACE("fd=%d fragmentSize=%ld\n",
908 wwo->unixdev, wwo->dwFragmentSize);
909 if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
910 ERR("Fragment doesn't contain an integral number of data blocks\n");
912 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
913 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
914 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
915 wwo->format.wf.nBlockAlign);
917 if (OSS_NotifyClient(wDevID, WOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
918 WARN("can't notify client !\n");
919 return MMSYSERR_INVALPARAM;
921 return MMSYSERR_NOERROR;
924 /**************************************************************************
925 * wodClose [internal]
927 static DWORD wodClose(WORD wDevID)
929 DWORD ret = MMSYSERR_NOERROR;
930 WINE_WAVEOUT* wwo;
932 TRACE("(%u);\n", wDevID);
934 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
935 WARN("bad device ID !\n");
936 return MMSYSERR_BADDEVICEID;
939 wwo = &WOutDev[wDevID];
940 if (wwo->lpQueuePtr) {
941 WARN("buffers still playing !\n");
942 ret = WAVERR_STILLPLAYING;
943 } else {
944 TRACE("imhere[3-close]\n");
945 if (wwo->hEvent != INVALID_HANDLE_VALUE) {
946 wodPlayer_Message(wwo, WINE_WM_CLOSING, 0);
947 WaitForSingleObject(wwo->hEvent, INFINITE);
948 CloseHandle(wwo->hEvent);
950 if (wwo->mapping) {
951 munmap(wwo->mapping, wwo->maplen);
952 wwo->mapping = NULL;
955 close(wwo->unixdev);
956 wwo->unixdev = -1;
957 wwo->dwFragmentSize = 0;
958 if (OSS_NotifyClient(wDevID, WOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
959 WARN("can't notify client !\n");
960 ret = MMSYSERR_INVALPARAM;
963 return ret;
966 /**************************************************************************
967 * wodWrite [internal]
970 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
972 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
974 /* first, do the sanity checks... */
975 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
976 WARN("bad dev ID !\n");
977 return MMSYSERR_BADDEVICEID;
980 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
981 return WAVERR_UNPREPARED;
983 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
984 return WAVERR_STILLPLAYING;
986 lpWaveHdr->dwFlags &= ~WHDR_DONE;
987 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
988 lpWaveHdr->lpNext = 0;
990 TRACE("imhere[3-HEADER]\n");
991 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_HEADER, (DWORD)lpWaveHdr);
993 return MMSYSERR_NOERROR;
996 /**************************************************************************
997 * wodPrepare [internal]
999 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1001 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1003 if (wDevID >= MAX_WAVEOUTDRV) {
1004 WARN("bad device ID !\n");
1005 return MMSYSERR_BADDEVICEID;
1008 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1009 return WAVERR_STILLPLAYING;
1011 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1012 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1013 return MMSYSERR_NOERROR;
1016 /**************************************************************************
1017 * wodUnprepare [internal]
1019 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1021 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1023 if (wDevID >= MAX_WAVEOUTDRV) {
1024 WARN("bad device ID !\n");
1025 return MMSYSERR_BADDEVICEID;
1028 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1029 return WAVERR_STILLPLAYING;
1031 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1032 lpWaveHdr->dwFlags |= WHDR_DONE;
1034 return MMSYSERR_NOERROR;
1037 /**************************************************************************
1038 * wodPause [internal]
1040 static DWORD wodPause(WORD wDevID)
1042 TRACE("(%u);!\n", wDevID);
1044 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1045 WARN("bad device ID !\n");
1046 return MMSYSERR_BADDEVICEID;
1049 TRACE("imhere[3-PAUSING]\n");
1050 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_PAUSING, 0);
1051 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1053 return MMSYSERR_NOERROR;
1056 /**************************************************************************
1057 * wodRestart [internal]
1059 static DWORD wodRestart(WORD wDevID)
1061 TRACE("(%u);\n", wDevID);
1063 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1064 WARN("bad device ID !\n");
1065 return MMSYSERR_BADDEVICEID;
1068 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1069 TRACE("imhere[3-RESTARTING]\n");
1070 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_RESTARTING, 0);
1071 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1074 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1075 /* FIXME: Myst crashes with this ... hmm -MM
1076 if (OSS_NotifyClient(wDevID, WOM_DONE, 0L, 0L) != MMSYSERR_NOERROR) {
1077 WARN("can't notify client !\n");
1078 return MMSYSERR_INVALPARAM;
1082 return MMSYSERR_NOERROR;
1085 /**************************************************************************
1086 * wodReset [internal]
1088 static DWORD wodReset(WORD wDevID)
1090 TRACE("(%u);\n", wDevID);
1092 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1093 WARN("bad device ID !\n");
1094 return MMSYSERR_BADDEVICEID;
1097 TRACE("imhere[3-RESET]\n");
1098 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_RESETTING, 0);
1099 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1101 return MMSYSERR_NOERROR;
1105 /**************************************************************************
1106 * wodGetPosition [internal]
1108 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1110 int time;
1111 DWORD val;
1112 WINE_WAVEOUT* wwo;
1114 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1116 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1117 WARN("bad device ID !\n");
1118 return MMSYSERR_BADDEVICEID;
1121 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1123 wwo = &WOutDev[wDevID];
1124 val = wwo->dwPlayedTotal;
1126 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1127 lpTime->wType, wwo->format.wBitsPerSample,
1128 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1129 wwo->format.wf.nAvgBytesPerSec);
1130 TRACE("dwTotalPlayed=%lu\n", val);
1132 switch (lpTime->wType) {
1133 case TIME_BYTES:
1134 lpTime->u.cb = val;
1135 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1136 break;
1137 case TIME_SAMPLES:
1138 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample;
1139 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1140 break;
1141 case TIME_SMPTE:
1142 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1143 lpTime->u.smpte.hour = time / 108000;
1144 time -= lpTime->u.smpte.hour * 108000;
1145 lpTime->u.smpte.min = time / 1800;
1146 time -= lpTime->u.smpte.min * 1800;
1147 lpTime->u.smpte.sec = time / 30;
1148 time -= lpTime->u.smpte.sec * 30;
1149 lpTime->u.smpte.frame = time;
1150 lpTime->u.smpte.fps = 30;
1151 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1152 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1153 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1154 break;
1155 default:
1156 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1157 lpTime->wType = TIME_MS;
1158 case TIME_MS:
1159 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1160 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1161 break;
1163 return MMSYSERR_NOERROR;
1166 /**************************************************************************
1167 * wodGetVolume [internal]
1169 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1171 int mixer;
1172 int volume;
1173 DWORD left, right;
1175 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1177 if (lpdwVol == NULL)
1178 return MMSYSERR_NOTENABLED;
1179 if ((mixer = open(MIXER_DEV, O_RDONLY|O_NDELAY)) < 0) {
1180 WARN("mixer device not available !\n");
1181 return MMSYSERR_NOTENABLED;
1183 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
1184 WARN("unable read mixer !\n");
1185 return MMSYSERR_NOTENABLED;
1187 close(mixer);
1188 left = LOBYTE(volume);
1189 right = HIBYTE(volume);
1190 TRACE("left=%ld right=%ld !\n", left, right);
1191 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1192 return MMSYSERR_NOERROR;
1196 /**************************************************************************
1197 * wodSetVolume [internal]
1199 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1201 int mixer;
1202 int volume;
1203 DWORD left, right;
1205 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1207 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1208 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1209 volume = left + (right << 8);
1211 if ((mixer = open(MIXER_DEV, O_WRONLY|O_NDELAY)) < 0) {
1212 WARN("mixer device not available !\n");
1213 return MMSYSERR_NOTENABLED;
1215 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
1216 WARN("unable set mixer !\n");
1217 return MMSYSERR_NOTENABLED;
1218 } else {
1219 TRACE("volume=%04x\n", (unsigned)volume);
1221 close(mixer);
1222 return MMSYSERR_NOERROR;
1225 /**************************************************************************
1226 * wodGetNumDevs [internal]
1228 static DWORD wodGetNumDevs(void)
1230 DWORD ret = 1;
1232 /* FIXME: For now, only one sound device (SOUND_DEV) is allowed */
1233 int audio = open(SOUND_DEV, O_WRONLY|O_NDELAY, 0);
1235 if (audio == -1) {
1236 if (errno != EBUSY)
1237 ret = 0;
1238 } else {
1239 close(audio);
1241 return ret;
1244 /**************************************************************************
1245 * wodMessage (WINEOSS.7)
1247 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1248 DWORD dwParam1, DWORD dwParam2)
1250 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1251 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1253 switch (wMsg) {
1254 case DRVM_INIT:
1255 case DRVM_EXIT:
1256 case DRVM_ENABLE:
1257 case DRVM_DISABLE:
1258 /* FIXME: Pretend this is supported */
1259 return 0;
1260 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1261 case WODM_CLOSE: return wodClose (wDevID);
1262 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1263 case WODM_PAUSE: return wodPause (wDevID);
1264 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1265 case WODM_BREAKLOOP: return MMSYSERR_NOTSUPPORTED;
1266 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1267 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1268 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1269 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1270 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1271 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1272 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1273 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1274 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1275 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1276 case WODM_RESTART: return wodRestart (wDevID);
1277 case WODM_RESET: return wodReset (wDevID);
1279 case DRV_QUERYDSOUNDIFACE: return wodDsCreate(wDevID, (PIDSDRIVER*)dwParam1);
1280 default:
1281 FIXME("unknown message %d!\n", wMsg);
1283 return MMSYSERR_NOTSUPPORTED;
1286 /*======================================================================*
1287 * Low level DSOUND implementation *
1288 *======================================================================*/
1290 typedef struct IDsDriverImpl IDsDriverImpl;
1291 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1293 struct IDsDriverImpl
1295 /* IUnknown fields */
1296 ICOM_VFIELD(IDsDriver);
1297 DWORD ref;
1298 /* IDsDriverImpl fields */
1299 UINT wDevID;
1300 IDsDriverBufferImpl*primary;
1303 struct IDsDriverBufferImpl
1305 /* IUnknown fields */
1306 ICOM_VFIELD(IDsDriverBuffer);
1307 DWORD ref;
1308 /* IDsDriverBufferImpl fields */
1309 IDsDriverImpl* drv;
1310 DWORD buflen;
1313 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1315 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1316 if (!wwo->mapping) {
1317 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1318 wwo->unixdev, 0);
1319 if (wwo->mapping == (LPBYTE)-1) {
1320 ERR("(%p): Could not map sound device for direct access (errno=%d)\n", dsdb, errno);
1321 return DSERR_GENERIC;
1323 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1325 /* for some reason, es1371 and sblive! sometimes have junk in here.
1326 * clear it, or we get junk noise */
1327 /* some libc implementations are buggy: their memset reads from the buffer...
1328 * to work around it, we have the 0 the block by hand and do not call:
1329 * memset(wwo->mapping,0,wwo->maplen);
1332 char* p1 = wwo->mapping;
1333 unsigned len = wwo->maplen;
1335 if (len >= 16) /* so we can have at least a 4 longs to store... */
1337 /* the mmap:ed value is (at least) dword aligned
1338 * so, start filling the complete unsigned long:s
1340 int b = len >> 2;
1341 unsigned long* p4 = (unsigned long*)p1;
1343 while (b--) *p4++ = 0;
1344 /* prepare for filling the rest */
1345 len &= 3;
1346 p1 = (unsigned char*)p4;
1348 /* in all cases, fill the remaining bytes */
1349 while (len-- != 0) *p1++ = 0;
1352 return DS_OK;
1355 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
1357 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1358 if (wwo->mapping) {
1359 if (munmap(wwo->mapping, wwo->maplen) < 0) {
1360 ERR("(%p): Could not unmap sound device (errno=%d)\n", dsdb, errno);
1361 return DSERR_GENERIC;
1363 wwo->mapping = NULL;
1364 TRACE("(%p): sound device unmapped\n", dsdb);
1366 return DS_OK;
1369 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1371 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1372 FIXME("(): stub!\n");
1373 return DSERR_UNSUPPORTED;
1376 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1378 ICOM_THIS(IDsDriverBufferImpl,iface);
1379 This->ref++;
1380 return This->ref;
1383 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1385 ICOM_THIS(IDsDriverBufferImpl,iface);
1386 if (--This->ref)
1387 return This->ref;
1388 if (This == This->drv->primary)
1389 This->drv->primary = NULL;
1390 DSDB_UnmapPrimary(This);
1391 HeapFree(GetProcessHeap(),0,This);
1392 return 0;
1395 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1396 LPVOID*ppvAudio1,LPDWORD pdwLen1,
1397 LPVOID*ppvAudio2,LPDWORD pdwLen2,
1398 DWORD dwWritePosition,DWORD dwWriteLen,
1399 DWORD dwFlags)
1401 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1402 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
1403 * and that we don't support secondary buffers, this method will never be called */
1404 TRACE("(%p): stub\n",iface);
1405 return DSERR_UNSUPPORTED;
1408 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1409 LPVOID pvAudio1,DWORD dwLen1,
1410 LPVOID pvAudio2,DWORD dwLen2)
1412 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1413 TRACE("(%p): stub\n",iface);
1414 return DSERR_UNSUPPORTED;
1417 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1418 LPWAVEFORMATEX pwfx)
1420 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1422 TRACE("(%p,%p)\n",iface,pwfx);
1423 /* On our request (GetDriverDesc flags), DirectSound has by now used
1424 * waveOutClose/waveOutOpen to set the format...
1425 * unfortunately, this means our mmap() is now gone...
1426 * so we need to somehow signal to our DirectSound implementation
1427 * that it should completely recreate this HW buffer...
1428 * this unexpected error code should do the trick... */
1429 return DSERR_BUFFERLOST;
1432 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1434 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1435 TRACE("(%p,%ld): stub\n",iface,dwFreq);
1436 return DSERR_UNSUPPORTED;
1439 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1441 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1442 FIXME("(%p,%p): stub!\n",iface,pVolPan);
1443 return DSERR_UNSUPPORTED;
1446 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1448 /* ICOM_THIS(IDsDriverImpl,iface); */
1449 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1450 return DSERR_UNSUPPORTED;
1453 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1454 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1456 ICOM_THIS(IDsDriverBufferImpl,iface);
1457 count_info info;
1458 DWORD ptr;
1460 TRACE("(%p)\n",iface);
1461 if (WOutDev[This->drv->wDevID].unixdev == -1) {
1462 ERR("device not open, but accessing?\n");
1463 return DSERR_UNINITIALIZED;
1465 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_GETOPTR, &info) < 0) {
1466 ERR("ioctl failed (%d)\n", errno);
1467 return DSERR_GENERIC;
1469 ptr = info.ptr & ~3; /* align the pointer, just in case */
1470 if (lpdwPlay) *lpdwPlay = ptr;
1471 if (lpdwWrite) {
1472 /* add some safety margin (not strictly necessary, but...) */
1473 if (WOutDev[This->drv->wDevID].caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
1474 *lpdwWrite = ptr + 32;
1475 else
1476 *lpdwWrite = ptr + WOutDev[This->drv->wDevID].dwFragmentSize;
1477 while (*lpdwWrite > This->buflen)
1478 *lpdwWrite -= This->buflen;
1480 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
1481 return DS_OK;
1484 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
1486 ICOM_THIS(IDsDriverBufferImpl,iface);
1487 int enable = PCM_ENABLE_OUTPUT;
1488 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
1489 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1490 ERR("ioctl failed (%d)\n", errno);
1491 return DSERR_GENERIC;
1493 return DS_OK;
1496 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1498 ICOM_THIS(IDsDriverBufferImpl,iface);
1499 int enable = 0;
1500 TRACE("(%p)\n",iface);
1501 /* no more playing */
1502 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1503 ERR("ioctl failed (%d)\n", errno);
1504 return DSERR_GENERIC;
1506 #if 0
1507 /* the play position must be reset to the beginning of the buffer */
1508 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_RESET, 0) < 0) {
1509 ERR("ioctl failed (%d)\n", errno);
1510 return DSERR_GENERIC;
1512 #endif
1513 /* Most OSS drivers just can't stop the playback without closing the device...
1514 * so we need to somehow signal to our DirectSound implementation
1515 * that it should completely recreate this HW buffer...
1516 * this unexpected error code should do the trick... */
1517 return DSERR_BUFFERLOST;
1520 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
1522 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1523 IDsDriverBufferImpl_QueryInterface,
1524 IDsDriverBufferImpl_AddRef,
1525 IDsDriverBufferImpl_Release,
1526 IDsDriverBufferImpl_Lock,
1527 IDsDriverBufferImpl_Unlock,
1528 IDsDriverBufferImpl_SetFormat,
1529 IDsDriverBufferImpl_SetFrequency,
1530 IDsDriverBufferImpl_SetVolumePan,
1531 IDsDriverBufferImpl_SetPosition,
1532 IDsDriverBufferImpl_GetPosition,
1533 IDsDriverBufferImpl_Play,
1534 IDsDriverBufferImpl_Stop
1537 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
1539 /* ICOM_THIS(IDsDriverImpl,iface); */
1540 FIXME("(%p): stub!\n",iface);
1541 return DSERR_UNSUPPORTED;
1544 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
1546 ICOM_THIS(IDsDriverImpl,iface);
1547 This->ref++;
1548 return This->ref;
1551 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
1553 ICOM_THIS(IDsDriverImpl,iface);
1554 if (--This->ref)
1555 return This->ref;
1556 HeapFree(GetProcessHeap(),0,This);
1557 return 0;
1560 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
1562 ICOM_THIS(IDsDriverImpl,iface);
1563 TRACE("(%p,%p)\n",iface,pDesc);
1564 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
1565 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
1566 strcpy(pDesc->szDesc,"WineOSS DirectSound Driver");
1567 strcpy(pDesc->szDrvName,"wineoss.drv");
1568 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
1569 pDesc->wVxdId = 0;
1570 pDesc->wReserved = 0;
1571 pDesc->ulDeviceNum = This->wDevID;
1572 pDesc->dwHeapType = DSDHEAP_NOHEAP;
1573 pDesc->pvDirectDrawHeap = NULL;
1574 pDesc->dwMemStartAddress = 0;
1575 pDesc->dwMemEndAddress = 0;
1576 pDesc->dwMemAllocExtra = 0;
1577 pDesc->pvReserved1 = NULL;
1578 pDesc->pvReserved2 = NULL;
1579 return DS_OK;
1582 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
1584 ICOM_THIS(IDsDriverImpl,iface);
1585 int enable = 0;
1587 TRACE("(%p)\n",iface);
1588 /* make sure the card doesn't start playing before we want it to */
1589 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1590 ERR("ioctl failed (%d)\n", errno);
1591 return DSERR_GENERIC;
1593 return DS_OK;
1596 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
1598 ICOM_THIS(IDsDriverImpl,iface);
1599 TRACE("(%p)\n",iface);
1600 if (This->primary) {
1601 ERR("problem with DirectSound: primary not released\n");
1602 return DSERR_GENERIC;
1604 return DS_OK;
1607 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
1609 /* ICOM_THIS(IDsDriverImpl,iface); */
1610 TRACE("(%p,%p)\n",iface,pCaps);
1611 memset(pCaps, 0, sizeof(*pCaps));
1612 /* FIXME: need to check actual capabilities */
1613 pCaps->dwFlags = DSCAPS_PRIMARYMONO | DSCAPS_PRIMARYSTEREO |
1614 DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARY16BIT;
1615 pCaps->dwPrimaryBuffers = 1;
1616 /* the other fields only apply to secondary buffers, which we don't support
1617 * (unless we want to mess with wavetable synthesizers and MIDI) */
1618 return DS_OK;
1621 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
1622 LPWAVEFORMATEX pwfx,
1623 DWORD dwFlags, DWORD dwCardAddress,
1624 LPDWORD pdwcbBufferSize,
1625 LPBYTE *ppbBuffer,
1626 LPVOID *ppvObj)
1628 ICOM_THIS(IDsDriverImpl,iface);
1629 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
1630 HRESULT err;
1631 audio_buf_info info;
1632 int enable = 0;
1634 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
1635 /* we only support primary buffers */
1636 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
1637 return DSERR_UNSUPPORTED;
1638 if (This->primary)
1639 return DSERR_ALLOCATED;
1640 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
1641 return DSERR_CONTROLUNAVAIL;
1643 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
1644 if (*ippdsdb == NULL)
1645 return DSERR_OUTOFMEMORY;
1646 ICOM_VTBL(*ippdsdb) = &dsdbvt;
1647 (*ippdsdb)->ref = 1;
1648 (*ippdsdb)->drv = This;
1650 /* check how big the DMA buffer is now */
1651 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1652 ERR("ioctl failed (%d)\n", errno);
1653 HeapFree(GetProcessHeap(),0,*ippdsdb);
1654 *ippdsdb = NULL;
1655 return DSERR_GENERIC;
1657 WOutDev[This->wDevID].maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
1659 /* map the DMA buffer */
1660 err = DSDB_MapPrimary(*ippdsdb);
1661 if (err != DS_OK) {
1662 HeapFree(GetProcessHeap(),0,*ippdsdb);
1663 *ippdsdb = NULL;
1664 return err;
1667 /* primary buffer is ready to go */
1668 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
1669 *ppbBuffer = WOutDev[This->wDevID].mapping;
1671 /* some drivers need some extra nudging after mapping */
1672 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1673 ERR("ioctl failed (%d)\n", errno);
1674 return DSERR_GENERIC;
1677 This->primary = *ippdsdb;
1679 return DS_OK;
1682 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
1683 PIDSDRIVERBUFFER pBuffer,
1684 LPVOID *ppvObj)
1686 /* ICOM_THIS(IDsDriverImpl,iface); */
1687 TRACE("(%p,%p): stub\n",iface,pBuffer);
1688 return DSERR_INVALIDCALL;
1691 static ICOM_VTABLE(IDsDriver) dsdvt =
1693 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1694 IDsDriverImpl_QueryInterface,
1695 IDsDriverImpl_AddRef,
1696 IDsDriverImpl_Release,
1697 IDsDriverImpl_GetDriverDesc,
1698 IDsDriverImpl_Open,
1699 IDsDriverImpl_Close,
1700 IDsDriverImpl_GetCaps,
1701 IDsDriverImpl_CreateSoundBuffer,
1702 IDsDriverImpl_DuplicateSoundBuffer
1705 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1707 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
1709 /* the HAL isn't much better than the HEL if we can't do mmap() */
1710 if (!(WOutDev[wDevID].caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
1711 ERR("DirectSound flag not set\n");
1712 MESSAGE("This sound card's driver does not support direct access\n");
1713 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1714 return MMSYSERR_NOTSUPPORTED;
1717 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
1718 if (!*idrv)
1719 return MMSYSERR_NOMEM;
1720 ICOM_VTBL(*idrv) = &dsdvt;
1721 (*idrv)->ref = 1;
1723 (*idrv)->wDevID = wDevID;
1724 (*idrv)->primary = NULL;
1725 return MMSYSERR_NOERROR;
1728 /*======================================================================*
1729 * Low level WAVE IN implementation *
1730 *======================================================================*/
1732 /**************************************************************************
1733 * widGetDevCaps [internal]
1735 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
1737 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1739 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1741 if (wDevID >= MAX_WAVEINDRV) {
1742 TRACE("MAX_WAVINDRV reached !\n");
1743 return MMSYSERR_BADDEVICEID;
1746 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1747 return MMSYSERR_NOERROR;
1750 /**************************************************************************
1751 * widRecorder [internal]
1753 static DWORD CALLBACK widRecorder(LPVOID pmt)
1755 WORD uDevID = (DWORD)pmt;
1756 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1757 WAVEHDR* lpWaveHdr;
1758 DWORD dwSleepTime;
1759 MSG msg;
1760 DWORD bytesRead;
1762 audio_buf_info info;
1763 int xs;
1765 LPVOID buffer = HeapAlloc(GetProcessHeap(),
1766 HEAP_ZERO_MEMORY,
1767 wwi->dwFragmentSize);
1769 LPVOID pOffset = buffer;
1771 PeekMessageA(&msg, 0, 0, 0, 0);
1772 wwi->state = WINE_WS_STOPPED;
1773 wwi->dwTotalRecorded = 0;
1775 SetEvent(wwi->hEvent);
1777 /* the soundblaster live needs a micro wake to get its recording started
1778 * (or GETISPACE will have 0 frags all the time)
1780 read(wwi->unixdev,&xs,4);
1782 /* make sleep time to be # of ms to output a fragment */
1783 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
1784 TRACE("sleeptime=%ld ms\n", dwSleepTime);
1786 for (; ; ) {
1787 /* wait for dwSleepTime or an event in thread's queue */
1788 /* FIXME: could improve wait time depending on queue state,
1789 * ie, number of queued fragments
1792 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
1794 lpWaveHdr = wwi->lpQueuePtr;
1796 ioctl(wwi->unixdev, SNDCTL_DSP_GETISPACE, &info);
1797 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", info.fragments, info.fragsize, info.fragstotal, info.bytes);
1799 /* read all the fragments accumulated so far */
1800 while ((info.fragments > 0) && (wwi->lpQueuePtr))
1802 info.fragments --;
1804 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
1806 /* directly read fragment in wavehdr */
1807 bytesRead = read(wwi->unixdev,
1808 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1809 wwi->dwFragmentSize);
1811 TRACE("bytesRead=%ld (direct)\n", bytesRead);
1812 if (bytesRead != (DWORD) -1)
1814 /* update number of bytes recorded in current buffer and by this device */
1815 lpWaveHdr->dwBytesRecorded += bytesRead;
1816 wwi->dwTotalRecorded += bytesRead;
1818 /* buffer is full. notify client */
1819 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
1821 /* must copy the value of next waveHdr, because we have no idea of what
1822 * will be done with the content of lpWaveHdr in callback
1824 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1826 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1827 lpWaveHdr->dwFlags |= WHDR_DONE;
1829 if (OSS_NotifyClient(uDevID, WIM_DATA,
1830 (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR)
1832 WARN("can't notify client !\n");
1834 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1838 else
1840 /* read the fragment in a local buffer */
1841 bytesRead = read(wwi->unixdev, buffer, wwi->dwFragmentSize);
1842 pOffset = buffer;
1844 TRACE("bytesRead=%ld (local)\n", bytesRead);
1846 /* copy data in client buffers */
1847 while (bytesRead != (DWORD) -1 && bytesRead > 0)
1849 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1851 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1852 pOffset,
1853 dwToCopy);
1855 /* update number of bytes recorded in current buffer and by this device */
1856 lpWaveHdr->dwBytesRecorded += dwToCopy;
1857 wwi->dwTotalRecorded += dwToCopy;
1858 bytesRead -= dwToCopy;
1859 pOffset += dwToCopy;
1861 /* client buffer is full. notify client */
1862 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
1864 /* must copy the value of next waveHdr, because we have no idea of what
1865 * will be done with the content of lpWaveHdr in callback
1867 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1868 TRACE("lpNext=%p\n", lpNext);
1870 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1871 lpWaveHdr->dwFlags |= WHDR_DONE;
1873 if (OSS_NotifyClient(uDevID, WIM_DATA,
1874 (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR)
1876 WARN("can't notify client !\n");
1879 wwi->lpQueuePtr = lpWaveHdr = lpNext;
1880 if (!lpNext && bytesRead) {
1881 /* no more buffer to copy data to, but we did read more.
1882 * what hasn't been copied will be dropped
1884 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
1885 wwi->lpQueuePtr = NULL;
1886 break;
1894 MsgWaitForMultipleObjects(0, NULL, FALSE, dwSleepTime, QS_POSTMESSAGE);
1896 while (PeekMessageA(&msg, 0, WINE_WM_FIRST, WINE_WM_LAST, PM_REMOVE)) {
1898 TRACE("msg=0x%x wParam=0x%x lParam=0x%lx\n", msg.message, msg.wParam, msg.lParam);
1899 switch (msg.message) {
1900 case WINE_WM_PAUSING:
1901 wwi->state = WINE_WS_PAUSED;
1902 /*FIXME("Device should stop recording\n");*/
1903 SetEvent(wwi->hEvent);
1904 break;
1905 case WINE_WM_RESTARTING:
1907 int enable = PCM_ENABLE_INPUT;
1908 wwi->state = WINE_WS_PLAYING;
1910 if (wwi->bTriggerSupport)
1912 /* start the recording */
1913 if (ioctl(wwi->unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0)
1915 ERR("ioctl(SNDCTL_DSP_SETTRIGGER) failed (%d)\n", errno);
1918 else
1920 unsigned char data[4];
1921 /* read 4 bytes to start the recording */
1922 read(wwi->unixdev, data, 4);
1925 SetEvent(wwi->hEvent);
1926 break;
1928 case WINE_WM_HEADER:
1929 lpWaveHdr = (LPWAVEHDR)msg.lParam;
1930 lpWaveHdr->lpNext = 0;
1932 /* insert buffer at the end of queue */
1934 LPWAVEHDR* wh;
1935 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1936 *wh = lpWaveHdr;
1938 break;
1939 case WINE_WM_RESETTING:
1940 wwi->state = WINE_WS_STOPPED;
1941 /* return all buffers to the app */
1942 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1943 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1944 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1945 lpWaveHdr->dwFlags |= WHDR_DONE;
1947 if (OSS_NotifyClient(uDevID, WIM_DATA,
1948 (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR) {
1949 WARN("can't notify client !\n");
1952 wwi->lpQueuePtr = NULL;
1953 SetEvent(wwi->hEvent);
1954 break;
1955 case WINE_WM_CLOSING:
1956 wwi->hThread = 0;
1957 wwi->state = WINE_WS_CLOSED;
1958 SetEvent(wwi->hEvent);
1959 HeapFree(GetProcessHeap(), 0, buffer);
1960 ExitThread(0);
1961 /* shouldn't go here */
1962 default:
1963 FIXME("unknown message %d\n", msg.message);
1964 break;
1968 ExitThread(0);
1969 /* just for not generating compilation warnings... should never be executed */
1970 return 0;
1974 /**************************************************************************
1975 * widOpen [internal]
1977 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1979 int audio;
1980 int fragment_size;
1981 int sample_rate;
1982 int format;
1983 int dsp_stereo;
1984 WINE_WAVEIN* wwi;
1985 int audio_fragment;
1987 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1988 if (lpDesc == NULL) {
1989 WARN("Invalid Parameter !\n");
1990 return MMSYSERR_INVALPARAM;
1992 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_BADDEVICEID;
1994 /* only PCM format is supported so far... */
1995 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1996 lpDesc->lpFormat->nChannels == 0 ||
1997 lpDesc->lpFormat->nSamplesPerSec == 0) {
1998 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1999 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2000 lpDesc->lpFormat->nSamplesPerSec);
2001 return WAVERR_BADFORMAT;
2004 if (dwFlags & WAVE_FORMAT_QUERY) {
2005 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2006 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2007 lpDesc->lpFormat->nSamplesPerSec);
2008 return MMSYSERR_NOERROR;
2011 if (access(SOUND_DEV,0) != 0) return MMSYSERR_NOTENABLED;
2012 audio = open(SOUND_DEV, O_RDONLY|O_NDELAY, 0);
2013 if (audio == -1) {
2014 WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
2015 return MMSYSERR_ALLOCATED;
2017 fcntl(audio, F_SETFD, 1); /* set close on exec flag */
2019 wwi = &WInDev[wDevID];
2020 if (wwi->lpQueuePtr) {
2021 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2022 wwi->lpQueuePtr = NULL;
2024 wwi->unixdev = audio;
2025 wwi->dwTotalRecorded = 0;
2026 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2028 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2029 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2031 if (wwi->format.wBitsPerSample == 0) {
2032 WARN("Resetting zeroed wBitsPerSample\n");
2033 wwi->format.wBitsPerSample = 8 *
2034 (wwi->format.wf.nAvgBytesPerSec /
2035 wwi->format.wf.nSamplesPerSec) /
2036 wwi->format.wf.nChannels;
2039 sample_rate = wwi->format.wf.nSamplesPerSec;
2040 dsp_stereo = (wwi->format.wf.nChannels > 1) ? TRUE : FALSE;
2041 format = (wwi->format.wBitsPerSample == 16) ? AFMT_S16_LE : AFMT_U8;
2044 IOCTL(audio, SNDCTL_DSP_SETFMT, format);
2045 IOCTL(audio, SNDCTL_DSP_STEREO, dsp_stereo);
2046 IOCTL(audio, SNDCTL_DSP_SPEED, sample_rate);
2049 /* This is actually hand tuned to work so that my SB Live:
2050 * - does not skip
2051 * - does not buffer too much
2052 * when sending with the Shoutcast winamp plugin
2054 /* 7 fragments max, 2^10 = 1024 bytes per fragment */
2055 audio_fragment = 0x0007000A;
2056 IOCTL(audio, SNDCTL_DSP_SETFRAGMENT, audio_fragment);
2058 /* paranoid checks */
2059 if (format != ((wwi->format.wBitsPerSample == 16) ? AFMT_S16_LE : AFMT_U8))
2060 ERR("Can't set format to %d (%d)\n",
2061 (wwi->format.wBitsPerSample == 16) ? AFMT_S16_LE : AFMT_U8, format);
2062 if (dsp_stereo != (wwi->format.wf.nChannels > 1) ? 1 : 0)
2063 ERR("Can't set stereo to %u (%d)\n",
2064 (wwi->format.wf.nChannels > 1) ? 1 : 0, dsp_stereo);
2065 if (!NEAR_MATCH(sample_rate, wwi->format.wf.nSamplesPerSec))
2066 ERR("Can't set sample_rate to %lu (%d)\n",
2067 wwi->format.wf.nSamplesPerSec, sample_rate);
2069 IOCTL(audio, SNDCTL_DSP_GETBLKSIZE, fragment_size);
2070 if (fragment_size == -1) {
2071 WARN("IOCTL can't 'SNDCTL_DSP_GETBLKSIZE' !\n");
2072 close(audio);
2073 wwi->unixdev = -1;
2074 return MMSYSERR_NOTENABLED;
2076 wwi->dwFragmentSize = fragment_size;
2078 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2079 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
2080 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
2081 wwi->format.wf.nBlockAlign);
2083 wwi->hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2084 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2085 WaitForSingleObject(wwi->hEvent, INFINITE);
2087 if (OSS_NotifyClient(wDevID, WIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
2088 WARN("can't notify client !\n");
2089 return MMSYSERR_INVALPARAM;
2091 return MMSYSERR_NOERROR;
2094 /**************************************************************************
2095 * widClose [internal]
2097 static DWORD widClose(WORD wDevID)
2099 WINE_WAVEIN* wwi;
2101 TRACE("(%u);\n", wDevID);
2102 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2103 WARN("can't close !\n");
2104 return MMSYSERR_INVALHANDLE;
2107 wwi = &WInDev[wDevID];
2109 if (wwi->lpQueuePtr != NULL) {
2110 WARN("still buffers open !\n");
2111 return WAVERR_STILLPLAYING;
2114 PostThreadMessageA(wwi->dwThreadID, WINE_WM_CLOSING, 0, 0);
2115 WaitForSingleObject(wwi->hEvent, INFINITE);
2116 CloseHandle(wwi->hEvent);
2117 close(wwi->unixdev);
2118 wwi->unixdev = -1;
2119 wwi->dwFragmentSize = 0;
2120 if (OSS_NotifyClient(wDevID, WIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
2121 WARN("can't notify client !\n");
2122 return MMSYSERR_INVALPARAM;
2124 return MMSYSERR_NOERROR;
2127 /**************************************************************************
2128 * widAddBuffer [internal]
2130 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2132 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2134 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2135 WARN("can't do it !\n");
2136 return MMSYSERR_INVALHANDLE;
2138 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2139 TRACE("never been prepared !\n");
2140 return WAVERR_UNPREPARED;
2142 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2143 TRACE("header already in use !\n");
2144 return WAVERR_STILLPLAYING;
2147 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2148 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2149 lpWaveHdr->dwBytesRecorded = 0;
2150 lpWaveHdr->lpNext = NULL;
2152 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_HEADER, 0, (DWORD)lpWaveHdr);
2153 return MMSYSERR_NOERROR;
2156 /**************************************************************************
2157 * widPrepare [internal]
2159 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2161 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2163 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
2165 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2166 return WAVERR_STILLPLAYING;
2168 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2169 lpWaveHdr->dwFlags &= ~(WHDR_INQUEUE|WHDR_DONE);
2170 lpWaveHdr->dwBytesRecorded = 0;
2171 TRACE("header prepared !\n");
2172 return MMSYSERR_NOERROR;
2175 /**************************************************************************
2176 * widUnprepare [internal]
2178 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2180 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2181 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
2183 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2184 return WAVERR_STILLPLAYING;
2186 lpWaveHdr->dwFlags &= ~(WHDR_PREPARED|WHDR_INQUEUE);
2187 lpWaveHdr->dwFlags |= WHDR_DONE;
2189 return MMSYSERR_NOERROR;
2192 /**************************************************************************
2193 * widStart [internal]
2195 static DWORD widStart(WORD wDevID)
2197 TRACE("(%u);\n", wDevID);
2198 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2199 WARN("can't start recording !\n");
2200 return MMSYSERR_INVALHANDLE;
2203 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESTARTING, 0, 0);
2204 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2205 return MMSYSERR_NOERROR;
2208 /**************************************************************************
2209 * widStop [internal]
2211 static DWORD widStop(WORD wDevID)
2213 TRACE("(%u);\n", wDevID);
2214 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2215 WARN("can't stop !\n");
2216 return MMSYSERR_INVALHANDLE;
2218 /* FIXME: reset aint stop */
2219 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
2220 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2222 return MMSYSERR_NOERROR;
2225 /**************************************************************************
2226 * widReset [internal]
2228 static DWORD widReset(WORD wDevID)
2230 TRACE("(%u);\n", wDevID);
2231 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2232 WARN("can't reset !\n");
2233 return MMSYSERR_INVALHANDLE;
2235 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
2236 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2237 return MMSYSERR_NOERROR;
2240 /**************************************************************************
2241 * widGetPosition [internal]
2243 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2245 int time;
2246 WINE_WAVEIN* wwi;
2248 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2250 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2251 WARN("can't get pos !\n");
2252 return MMSYSERR_INVALHANDLE;
2254 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
2256 wwi = &WInDev[wDevID];
2258 TRACE("wType=%04X !\n", lpTime->wType);
2259 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
2260 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
2261 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
2262 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
2263 switch (lpTime->wType) {
2264 case TIME_BYTES:
2265 lpTime->u.cb = wwi->dwTotalRecorded;
2266 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
2267 break;
2268 case TIME_SAMPLES:
2269 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
2270 wwi->format.wBitsPerSample;
2271 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
2272 break;
2273 case TIME_SMPTE:
2274 time = wwi->dwTotalRecorded /
2275 (wwi->format.wf.nAvgBytesPerSec / 1000);
2276 lpTime->u.smpte.hour = time / 108000;
2277 time -= lpTime->u.smpte.hour * 108000;
2278 lpTime->u.smpte.min = time / 1800;
2279 time -= lpTime->u.smpte.min * 1800;
2280 lpTime->u.smpte.sec = time / 30;
2281 time -= lpTime->u.smpte.sec * 30;
2282 lpTime->u.smpte.frame = time;
2283 lpTime->u.smpte.fps = 30;
2284 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
2285 lpTime->u.smpte.hour, lpTime->u.smpte.min,
2286 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
2287 break;
2288 case TIME_MS:
2289 lpTime->u.ms = wwi->dwTotalRecorded /
2290 (wwi->format.wf.nAvgBytesPerSec / 1000);
2291 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
2292 break;
2293 default:
2294 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
2295 lpTime->wType = TIME_MS;
2297 return MMSYSERR_NOERROR;
2300 /**************************************************************************
2301 * widMessage (WINEOSS.6)
2303 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2304 DWORD dwParam1, DWORD dwParam2)
2306 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2307 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2309 switch (wMsg) {
2310 case DRVM_INIT:
2311 case DRVM_EXIT:
2312 case DRVM_ENABLE:
2313 case DRVM_DISABLE:
2314 /* FIXME: Pretend this is supported */
2315 return 0;
2316 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2317 case WIDM_CLOSE: return widClose (wDevID);
2318 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2319 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2320 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2321 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2322 case WIDM_GETNUMDEVS: return wodGetNumDevs (); /* same number of devices in output as in input */
2323 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
2324 case WIDM_RESET: return widReset (wDevID);
2325 case WIDM_START: return widStart (wDevID);
2326 case WIDM_STOP: return widStop (wDevID);
2327 default:
2328 FIXME("unknown message %u!\n", wMsg);
2330 return MMSYSERR_NOTSUPPORTED;
2333 #else /* !HAVE_OSS */
2335 /**************************************************************************
2336 * wodMessage (WINEOSS.7)
2338 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2339 DWORD dwParam1, DWORD dwParam2)
2341 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2342 return MMSYSERR_NOTENABLED;
2345 /**************************************************************************
2346 * widMessage (WINEOSS.6)
2348 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2349 DWORD dwParam1, DWORD dwParam2)
2351 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2352 return MMSYSERR_NOTENABLED;
2355 #endif /* HAVE_OSS */