Removed some more uses of the non-standard ICOM_THIS macro.
[wine/multimedia.git] / dlls / winmm / wineaudioio / audio.c
blobc2b425812fc4ce8bc41d4726dd25804803c377e5
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Wine Driver for Libaudioio
4 * Derived from the Wine OSS Sample Driver
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
8 * 2002 Robert Lunnon (Modifications for libaudioio)
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Note large hacks done to effectively disable DSound altogether
26 * Also Input is not yet working (Lots more work to be done there)
27 * But this does make a reasonable output driver for solaris until the arts
28 * driver comes up to speed
32 * FIXME:
33 * pause in waveOut does not work correctly
34 * full duplex (in/out) is not working (device is opened twice for Out
35 * and In) (OSS is known for its poor duplex capabilities, alsa is
36 * better)
39 /*#define EMULATE_SB16*/
41 #include "config.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <string.h>
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50 #include <errno.h>
51 #include <fcntl.h>
52 #ifdef HAVE_SYS_IOCTL_H
53 # include <sys/ioctl.h>
54 #endif
55 #ifdef HAVE_SYS_MMAN_H
56 # include <sys/mman.h>
57 #endif
58 #ifdef HAVE_LIBAUDIOIO_H
59 #include <libaudioio.h>
60 #endif
61 #include "windef.h"
62 #include "winbase.h"
63 #include "wingdi.h"
64 #include "winerror.h"
65 #include "wine/winuser16.h"
66 #include "mmddk.h"
67 #include "dsound.h"
68 #include "dsdriver.h"
69 #include "wine/debug.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(wave);
73 #ifdef HAVE_LIBAUDIOIO
75 /* Allow 1% deviation for sample rates (some ES137x cards) */
76 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
78 #define SOUND_DEV "/dev/audio"
79 #define DEFAULT_FRAGMENT_SIZE 4096
83 #define MAX_WAVEOUTDRV (1)
84 #define MAX_WAVEINDRV (1)
86 /* state diagram for waveOut writing:
88 * +---------+-------------+---------------+---------------------------------+
89 * | state | function | event | new state |
90 * +---------+-------------+---------------+---------------------------------+
91 * | | open() | | STOPPED |
92 * | PAUSED | write() | | PAUSED |
93 * | STOPPED | write() | <thrd create> | PLAYING |
94 * | PLAYING | write() | HEADER | PLAYING |
95 * | (other) | write() | <error> | |
96 * | (any) | pause() | PAUSING | PAUSED |
97 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
98 * | (any) | reset() | RESETTING | STOPPED |
99 * | (any) | close() | CLOSING | CLOSED |
100 * +---------+-------------+---------------+---------------------------------+
103 /* states of the playing device */
104 #define WINE_WS_PLAYING 0
105 #define WINE_WS_PAUSED 1
106 #define WINE_WS_STOPPED 2
107 #define WINE_WS_CLOSED 3
109 /* events to be send to device */
110 #define WINE_WM_PAUSING (WM_USER + 1)
111 #define WINE_WM_RESTARTING (WM_USER + 2)
112 #define WINE_WM_RESETTING (WM_USER + 3)
113 #define WINE_WM_CLOSING (WM_USER + 4)
114 #define WINE_WM_HEADER (WM_USER + 5)
116 #define WINE_WM_FIRST WINE_WM_PAUSING
117 #define WINE_WM_LAST WINE_WM_HEADER
119 typedef struct {
120 int msg;
121 DWORD param;
122 } WWO_MSG;
124 typedef struct {
125 int unixdev;
126 volatile int state; /* one of the WINE_WS_ manifest constants */
127 DWORD dwFragmentSize; /* size of OSS buffer fragment */
128 WAVEOPENDESC waveDesc;
129 WORD wFlags;
130 PCMWAVEFORMAT format;
131 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
132 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
133 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
134 DWORD dwLoops; /* private copy of loop counter */
136 DWORD dwLastFragDone; /* time in ms, when last played fragment will be actually played */
137 DWORD dwPlayedTotal; /* number of bytes played since opening */
139 /* info on current lpQueueHdr->lpWaveHdr */
140 DWORD dwOffCurrHdr; /* offset in lpPlayPtr->lpData for fragments */
141 DWORD dwRemain; /* number of bytes to write to end the current fragment */
143 /* synchronization stuff */
144 HANDLE hThread;
145 DWORD dwThreadID;
146 HANDLE hEvent;
147 #define WWO_RING_BUFFER_SIZE 30
148 WWO_MSG messages[WWO_RING_BUFFER_SIZE];
149 int msg_tosave;
150 int msg_toget;
151 HANDLE msg_event;
152 CRITICAL_SECTION msg_crst;
153 WAVEOUTCAPSA caps;
155 /* DirectSound stuff */
156 LPBYTE mapping;
157 DWORD maplen;
158 } WINE_WAVEOUT;
160 typedef struct {
161 int unixdev;
162 volatile int state;
163 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
164 WAVEOPENDESC waveDesc;
165 WORD wFlags;
166 PCMWAVEFORMAT format;
167 LPWAVEHDR lpQueuePtr;
168 DWORD dwTotalRecorded;
169 WAVEINCAPSA caps;
170 BOOL bTriggerSupport;
172 /* synchronization stuff */
173 HANDLE hThread;
174 DWORD dwThreadID;
175 HANDLE hEvent;
176 } WINE_WAVEIN;
178 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
179 static WINE_WAVEIN WInDev [MAX_WAVEINDRV ];
181 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
182 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv);
183 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
184 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc);
186 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
187 PCMWAVEFORMAT* format)
189 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
190 lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
191 format->wf.nChannels, format->wf.nAvgBytesPerSec);
192 TRACE("Position in bytes=%lu\n", position);
194 switch (lpTime->wType) {
195 case TIME_SAMPLES:
196 lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
197 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
198 break;
199 case TIME_MS:
200 lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
201 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
202 break;
203 case TIME_SMPTE:
204 position = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
205 lpTime->u.smpte.sec = position / format->wf.nSamplesPerSec;
206 position -= lpTime->u.smpte.sec * format->wf.nSamplesPerSec;
207 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
208 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
209 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
210 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
211 lpTime->u.smpte.fps = 30;
212 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->wf.nSamplesPerSec;
213 position -= lpTime->u.smpte.frame * format->wf.nSamplesPerSec / lpTime->u.smpte.fps;
214 if (position != 0)
216 /* Round up */
217 lpTime->u.smpte.frame++;
219 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
220 lpTime->u.smpte.hour, lpTime->u.smpte.min,
221 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
222 break;
223 default:
224 FIXME("Format %d not supported ! use TIME_BYTES !\n", lpTime->wType);
225 lpTime->wType = TIME_BYTES;
226 /* fall through */
227 case TIME_BYTES:
228 lpTime->u.cb = position;
229 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
230 break;
232 return MMSYSERR_NOERROR;
235 /*======================================================================*
236 * Low level WAVE implementation *
237 *======================================================================*/
238 SampleSpec_t spec[2];
242 LONG LIBAUDIOIO_WaveInit(void)
244 int audio;
245 int smplrate;
246 int samplesize = 16;
247 int dsp_stereo = 1;
248 int bytespersmpl;
249 int caps;
250 int mask;
251 int i;
253 int audio_fd;
254 int mode;
255 TRACE("Init ENTERED rate = %d\n",spec[PLAYBACK].rate);
256 spec[RECORD].channels=spec[PLAYBACK].channels=2;
257 spec[RECORD].max_blocks=spec[PLAYBACK].max_blocks=16;
258 spec[RECORD].rate=spec[PLAYBACK].rate=44100;
259 spec[RECORD].encoding=spec[PLAYBACK].encoding= ENCODE_PCM;
260 spec[RECORD].precision=spec[PLAYBACK].precision=16 ;
261 spec[RECORD].endian=spec[PLAYBACK].endian=ENDIAN_INTEL;
262 spec[RECORD].disable_threads=spec[PLAYBACK].disable_threads=1;
263 spec[RECORD].type=spec[PLAYBACK].type=TYPE_SIGNED; /* in 16 bit mode this is what typical PC hardware expects */
265 mode = O_WRONLY|O_NDELAY;
267 /* start with output device */
269 /* initialize all device handles to -1 */
270 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
272 WOutDev[i].unixdev = -1;
275 /* FIXME: only one device is supported */
276 memset(&WOutDev[0].caps, 0, sizeof(WOutDev[0].caps));
280 /* FIXME: some programs compare this string against the content of the registry
281 * for MM drivers. The names have to match in order for the program to work
282 * (e.g. MS win9x mplayer.exe)
284 #ifdef EMULATE_SB16
285 WOutDev[0].caps.wMid = 0x0002;
286 WOutDev[0].caps.wPid = 0x0104;
287 strcpy(WOutDev[0].caps.szPname, "SB16 Wave Out");
288 #else
289 WOutDev[0].caps.wMid = 0x00FF; /* Manufac ID */
290 WOutDev[0].caps.wPid = 0x0001; /* Product ID */
291 /* strcpy(WOutDev[0].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
292 strcpy(WOutDev[0].caps.szPname, "CS4236/37/38");
293 #endif
294 WOutDev[0].caps.vDriverVersion = 0x0100;
295 WOutDev[0].caps.dwFormats = 0x00000000;
296 WOutDev[0].caps.dwSupport = WAVECAPS_VOLUME;
299 * Libaudioio works differently, you tell it what spec audio you want to write
300 * and it guarantees to match it by converting to the final format on the fly.
301 * So we don't need to read back and compare.
304 bytespersmpl = spec[PLAYBACK].precision/8;
306 WOutDev[0].caps.wChannels = spec[PLAYBACK].channels;
309 /* Fixme: Libaudioio 0.2 doesn't support balance yet (Libaudioio 0.3 does so this must be fixed later)*/
311 /* if (WOutDev[0].caps.wChannels > 1) WOutDev[0].caps.dwSupport |= WAVECAPS_LRVOLUME;*/
312 TRACE("Init sammplerate= %d\n",spec[PLAYBACK].rate);
314 smplrate = spec[PLAYBACK].rate;
316 * We have set up the data format to be 16 bit signed in intel format
317 * For Big Endian machines libaudioio will convert the data to bigendian for us
320 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4M16;
321 if (WOutDev[0].caps.wChannels > 1)
322 WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4S16;
324 /* Don't understand this yet, but I don't think this functionality is portable, leave it here for future evaluation
325 * if (IOCTL(audio, SNDCTL_DSP_GETCAPS, caps) == 0) {
326 * TRACE("OSS dsp out caps=%08X\n", caps);
327 * if ((caps & DSP_CAP_REALTIME) && !(caps & DSP_CAP_BATCH)) {
328 * WOutDev[0].caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
330 */ /* well, might as well use the DirectSound cap flag for something */
331 /* if ((caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
332 * !(caps & DSP_CAP_BATCH))
333 * WOutDev[0].caps.dwSupport |= WAVECAPS_DIRECTSOUND;
339 * Note that libaudioio audio capture is not proven yet, in our open call
340 * set the spec for input audio the same as for output
342 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
343 WOutDev[0].caps.dwFormats, WOutDev[0].caps.dwSupport);
345 for (i = 0; i < MAX_WAVEINDRV; ++i)
347 WInDev[i].unixdev = -1;
350 memset(&WInDev[0].caps, 0, sizeof(WInDev[0].caps));
354 #ifdef EMULATE_SB16
355 WInDev[0].caps.wMid = 0x0002;
356 WInDev[0].caps.wPid = 0x0004;
357 strcpy(WInDev[0].caps.szPname, "SB16 Wave In");
358 #else
359 WInDev[0].caps.wMid = 0x00FF; /* Manufac ID */
360 WInDev[0].caps.wPid = 0x0001; /* Product ID */
361 strcpy(WInDev[0].caps.szPname, "OpenSoundSystem WAVIN Driver");
362 #endif
363 WInDev[0].caps.dwFormats = 0x00000000;
364 WInDev[0].caps.wChannels = spec[RECORD].channels;
366 WInDev[0].bTriggerSupport = TRUE; /* Maybe :-) */
368 bytespersmpl = spec[RECORD].precision/8;
369 smplrate = spec[RECORD].rate;
371 WInDev[0].caps.dwFormats |= WAVE_FORMAT_4M16;
372 if (WInDev[0].caps.wChannels > 1)
373 WInDev[0].caps.dwFormats |= WAVE_FORMAT_4S16;
376 TRACE("in dwFormats = %08lX\n", WInDev[0].caps.dwFormats);
378 return 0;
381 /**************************************************************************
382 * LIBAUDIOIO_NotifyClient [internal]
384 static DWORD LIBAUDIOIO_NotifyClient(UINT wDevID, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
386 TRACE("wDevID = %04X wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n",wDevID, wMsg, dwParam1, dwParam2);
388 switch (wMsg) {
389 case WOM_OPEN:
390 case WOM_CLOSE:
391 case WOM_DONE:
392 if (wDevID >= MAX_WAVEOUTDRV) return MCIERR_INTERNAL;
394 if (WOutDev[wDevID].wFlags != DCB_NULL &&
395 !DriverCallback(WOutDev[wDevID].waveDesc.dwCallback,
396 WOutDev[wDevID].wFlags,
397 WOutDev[wDevID].waveDesc.hWave,
398 wMsg,
399 WOutDev[wDevID].waveDesc.dwInstance,
400 dwParam1,
401 dwParam2)) {
402 WARN("can't notify client !\n");
403 return MMSYSERR_NOERROR;
405 break;
407 case WIM_OPEN:
408 case WIM_CLOSE:
409 case WIM_DATA:
410 if (wDevID >= MAX_WAVEINDRV) return MCIERR_INTERNAL;
412 if (WInDev[wDevID].wFlags != DCB_NULL &&
413 !DriverCallback(WInDev[wDevID].waveDesc.dwCallback,
414 WInDev[wDevID].wFlags,
415 WInDev[wDevID].waveDesc.hWave,
416 wMsg,
417 WInDev[wDevID].waveDesc.dwInstance,
418 dwParam1,
419 dwParam2)) {
420 WARN("can't notify client !\n");
421 return MMSYSERR_NOERROR;
423 break;
424 default:
425 FIXME("Unknown CB message %u\n", wMsg);
426 break;
428 return 0;
431 /*======================================================================*
432 * Low level WAVE OUT implementation *
433 *======================================================================*/
435 /**************************************************************************
436 * wodPlayer_WriteFragments [internal]
438 * wodPlayer helper. Writes as many fragments as it can to unixdev.
439 * Returns TRUE in case of buffer underrun.
441 static BOOL wodPlayer_WriteFragments(WINE_WAVEOUT* wwo)
443 LPWAVEHDR lpWaveHdr;
444 LPBYTE lpData;
445 int count;
447 TRACE("wodPlayer_WriteFragments sammplerate= %d\n",spec[PLAYBACK].rate);
448 for (;;) {
452 * Libaudioio doesn't buffer the same way as linux, you can write data is any block size
453 *And libaudioio just tracks the number of blocks in the streams queue to control latency
456 if (!AudioIOCheckWriteReady()) return FALSE; /* Returns false if you have execeeded your specified latency (No space left)*/
458 lpWaveHdr = wwo->lpPlayPtr;
459 if (!lpWaveHdr) {
460 if (wwo->dwRemain > 0 && /* still data to send to complete current fragment */
461 wwo->dwLastFragDone && /* first fragment has been played */
462 AudioIOCheckUnderrun()) { /* done with all waveOutWrite()' fragments */
463 /* FIXME: should do better handling here */
464 WARN("Oooch, buffer underrun !\n");
465 return TRUE; /* force resetting of waveOut device */
467 return FALSE; /* wait a bit */
470 if (wwo->dwOffCurrHdr == 0) {
471 TRACE("Starting a new wavehdr %p of %ld bytes\n", lpWaveHdr, lpWaveHdr->dwBufferLength);
472 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
473 if (wwo->lpLoopPtr) {
474 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
475 } else {
476 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
477 wwo->lpLoopPtr = lpWaveHdr;
478 /* Windows does not touch WAVEHDR.dwLoops,
479 * so we need to make an internal copy */
480 wwo->dwLoops = lpWaveHdr->dwLoops;
485 lpData = lpWaveHdr->lpData;
487 /* finish current wave hdr ? */
488 if (wwo->dwOffCurrHdr + wwo->dwRemain >= lpWaveHdr->dwBufferLength) {
489 DWORD toWrite = lpWaveHdr->dwBufferLength - wwo->dwOffCurrHdr;
491 /* write end of current wave hdr */
492 count = AudioIOWrite(lpData + wwo->dwOffCurrHdr, toWrite);
493 TRACE("write(%p[%5lu], %5lu) => %d\n", lpData, wwo->dwOffCurrHdr, toWrite, count);
495 if (count > 0 || toWrite == 0) {
496 DWORD tc = GetTickCount();
498 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
499 wwo->dwLastFragDone = tc;
500 wwo->dwLastFragDone += (toWrite * 1000) / wwo->format.wf.nAvgBytesPerSec;
502 lpWaveHdr->reserved = wwo->dwLastFragDone;
503 TRACE("Tagging hdr %p with %08lx\n", lpWaveHdr, wwo->dwLastFragDone);
505 /* WAVEHDR written, go to next one */
506 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
507 if (--wwo->dwLoops > 0) {
508 wwo->lpPlayPtr = wwo->lpLoopPtr;
509 } else {
510 /* last one played */
511 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
512 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
513 /* shall we consider the END flag for the closing loop or for
514 * the opening one or for both ???
515 * code assumes for closing loop only
517 wwo->lpLoopPtr = lpWaveHdr;
518 } else {
519 wwo->lpLoopPtr = NULL;
521 wwo->lpPlayPtr = lpWaveHdr->lpNext;
523 } else {
524 wwo->lpPlayPtr = lpWaveHdr->lpNext;
526 wwo->dwOffCurrHdr = 0;
527 if ((wwo->dwRemain -= count) == 0) {
528 wwo->dwRemain = wwo->dwFragmentSize;
531 continue; /* try to go to use next wavehdr */
532 } else {
533 count = AudioIOWrite( lpData + wwo->dwOffCurrHdr, wwo->dwRemain);
534 TRACE("write(%p[%5lu], %5lu) => %d\n", lpData, wwo->dwOffCurrHdr, wwo->dwRemain, count);
535 if (count > 0) {
536 DWORD tc = GetTickCount();
538 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
539 wwo->dwLastFragDone = tc;
540 wwo->dwLastFragDone += (wwo->dwRemain * 1000) / wwo->format.wf.nAvgBytesPerSec;
542 TRACE("Tagging frag with %08lx\n", wwo->dwLastFragDone);
544 wwo->dwOffCurrHdr += count;
545 wwo->dwRemain = wwo->dwFragmentSize;
552 int wodPlayer_Message(WINE_WAVEOUT *wwo, int msg, DWORD param)
554 TRACE("wodPlayerMessage sammplerate= %d msg=%d\n",spec[PLAYBACK].rate,msg);
555 EnterCriticalSection(&wwo->msg_crst);
556 if ((wwo->msg_tosave == wwo->msg_toget) /* buffer overflow ? */
557 && (wwo->messages[wwo->msg_toget].msg))
559 ERR("buffer overflow !?\n");
560 LeaveCriticalSection(&wwo->msg_crst);
561 return 0;
564 wwo->messages[wwo->msg_tosave].msg = msg;
565 wwo->messages[wwo->msg_tosave].param = param;
566 wwo->msg_tosave++;
567 if (wwo->msg_tosave > WWO_RING_BUFFER_SIZE-1)
568 wwo->msg_tosave = 0;
569 LeaveCriticalSection(&wwo->msg_crst);
570 /* signal a new message */
571 SetEvent(wwo->msg_event);
572 return 1;
575 int wodPlayer_RetrieveMessage(WINE_WAVEOUT *wwo, int *msg, DWORD *param)
577 EnterCriticalSection(&wwo->msg_crst);
579 if (wwo->msg_toget == wwo->msg_tosave) /* buffer empty ? */
581 LeaveCriticalSection(&wwo->msg_crst);
582 return 0;
585 *msg = wwo->messages[wwo->msg_toget].msg;
586 wwo->messages[wwo->msg_toget].msg = 0;
587 *param = wwo->messages[wwo->msg_toget].param;
588 wwo->msg_toget++;
589 if (wwo->msg_toget > WWO_RING_BUFFER_SIZE-1)
590 wwo->msg_toget = 0;
591 LeaveCriticalSection(&wwo->msg_crst);
592 return 1;
595 /**************************************************************************
596 * wodPlayer_Notify [internal]
598 * wodPlayer helper. Notifies (and remove from queue) all the wavehdr which content
599 * have been played (actually to speaker, not to unixdev fd).
601 static void wodPlayer_Notify(WINE_WAVEOUT* wwo, WORD uDevID, BOOL force)
603 LPWAVEHDR lpWaveHdr;
604 DWORD tc = GetTickCount();
606 while (wwo->lpQueuePtr &&
607 (force ||
608 (wwo->lpQueuePtr != wwo->lpPlayPtr && wwo->lpQueuePtr != wwo->lpLoopPtr))) {
609 lpWaveHdr = wwo->lpQueuePtr;
611 if (lpWaveHdr->reserved > tc && !force) break;
613 wwo->dwPlayedTotal += lpWaveHdr->dwBufferLength;
614 wwo->lpQueuePtr = lpWaveHdr->lpNext;
616 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
617 lpWaveHdr->dwFlags |= WHDR_DONE;
619 TRACE("Notifying client with %p\n", lpWaveHdr);
620 if (LIBAUDIOIO_NotifyClient(uDevID, WOM_DONE, (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR) {
621 WARN("can't notify client !\n");
626 /**************************************************************************
627 * wodPlayer_Reset [internal]
629 * wodPlayer helper. Resets current output stream.
631 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, WORD uDevID, BOOL reset)
633 /* updates current notify list */
634 wodPlayer_Notify(wwo, uDevID, FALSE);
636 /* flush all possible output */
638 *FIXME In the original code I think this aborted IO. With Libaudioio you have to wait
639 * The following function just blocks until I/O is complete
640 * There is possibly a way to abort the blocks buffered in streams
641 * but this approach seems to work OK
643 AudioIOFlush();
647 wwo->dwOffCurrHdr = 0;
648 wwo->dwRemain = wwo->dwFragmentSize;
649 if (reset) {
650 /* empty notify list */
651 wodPlayer_Notify(wwo, uDevID, TRUE);
653 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
654 wwo->state = WINE_WS_STOPPED;
655 wwo->dwPlayedTotal = 0;
656 } else {
657 /* FIXME: this is not accurate when looping, but can be do better ? */
658 wwo->lpPlayPtr = (wwo->lpLoopPtr) ? wwo->lpLoopPtr : wwo->lpQueuePtr;
659 wwo->state = WINE_WS_PAUSED;
663 /**************************************************************************
664 * wodPlayer [internal]
666 static DWORD CALLBACK wodPlayer(LPVOID pmt)
668 WORD uDevID = (DWORD)pmt;
669 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
670 WAVEHDR* lpWaveHdr;
671 DWORD dwSleepTime;
672 int msg;
673 DWORD param;
674 DWORD tc;
675 BOOL had_msg;
677 wwo->state = WINE_WS_STOPPED;
679 wwo->dwLastFragDone = 0;
680 wwo->dwOffCurrHdr = 0;
681 wwo->dwRemain = wwo->dwFragmentSize;
682 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
683 wwo->dwPlayedTotal = 0;
685 TRACE("imhere[0]\n");
686 SetEvent(wwo->hEvent);
688 for (;;) {
689 /* wait for dwSleepTime or an event in thread's queue
690 * FIXME:
691 * - is wait time calculation optimal ?
692 * - these 100 ms parts should be changed, but Eric reports
693 * that the wodPlayer thread might lock up if we use INFINITE
694 * (strange !), so I better don't change that now... */
695 if (wwo->state != WINE_WS_PLAYING)
696 dwSleepTime = 100;
697 else
699 tc = GetTickCount();
700 if (tc < wwo->dwLastFragDone)
702 /* calculate sleep time depending on when the last fragment
703 will be played */
704 dwSleepTime = (wwo->dwLastFragDone - tc)*7/10;
705 if (dwSleepTime > 100)
706 dwSleepTime = 100;
708 else
709 dwSleepTime = 0;
712 TRACE("imhere[1] tc = %08lx\n", GetTickCount());
713 if (dwSleepTime)
714 WaitForSingleObject(wwo->msg_event, dwSleepTime);
715 TRACE("imhere[2] (q=%p p=%p) tc = %08lx\n", wwo->lpQueuePtr,
716 wwo->lpPlayPtr, GetTickCount());
717 had_msg = FALSE;
718 while (wodPlayer_RetrieveMessage(wwo, &msg, &param)) {
719 had_msg = TRUE;
720 switch (msg) {
721 case WINE_WM_PAUSING:
722 wodPlayer_Reset(wwo, uDevID, FALSE);
723 wwo->state = WINE_WS_PAUSED;
724 SetEvent(wwo->hEvent);
725 break;
726 case WINE_WM_RESTARTING:
727 wwo->state = WINE_WS_PLAYING;
728 SetEvent(wwo->hEvent);
729 break;
730 case WINE_WM_HEADER:
731 lpWaveHdr = (LPWAVEHDR)param;
733 /* insert buffer at the end of queue */
735 LPWAVEHDR* wh;
736 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
737 *wh = lpWaveHdr;
739 if (!wwo->lpPlayPtr) wwo->lpPlayPtr = lpWaveHdr;
740 if (wwo->state == WINE_WS_STOPPED)
741 wwo->state = WINE_WS_PLAYING;
742 break;
743 case WINE_WM_RESETTING:
744 wodPlayer_Reset(wwo, uDevID, TRUE);
745 SetEvent(wwo->hEvent);
746 break;
747 case WINE_WM_CLOSING:
748 /* sanity check: this should not happen since the device must have been reset before */
749 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
750 wwo->hThread = 0;
751 wwo->state = WINE_WS_CLOSED;
752 SetEvent(wwo->hEvent);
753 ExitThread(0);
754 /* shouldn't go here */
755 default:
756 FIXME("unknown message %d\n", msg);
757 break;
759 if (wwo->state == WINE_WS_PLAYING) {
760 wodPlayer_WriteFragments(wwo);
762 wodPlayer_Notify(wwo, uDevID, FALSE);
765 if (!had_msg) { /* if we've received a msg we've just done this so we
766 won't repeat it */
767 if (wwo->state == WINE_WS_PLAYING) {
768 wodPlayer_WriteFragments(wwo);
770 wodPlayer_Notify(wwo, uDevID, FALSE);
773 ExitThread(0);
774 /* just for not generating compilation warnings... should never be executed */
775 return 0;
778 /**************************************************************************
779 * wodGetDevCaps [internal]
781 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
783 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
785 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
787 if (wDevID >= MAX_WAVEOUTDRV) {
788 TRACE("MAX_WAVOUTDRV reached !\n");
789 return MMSYSERR_BADDEVICEID;
792 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
793 return MMSYSERR_NOERROR;
796 /**************************************************************************
797 * wodOpen [internal]
799 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
801 int audio;
802 int format;
803 int sample_rate;
804 int dsp_stereo;
805 int fragment_size;
806 int audio_fragment;
807 WINE_WAVEOUT* wwo;
809 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
810 if (lpDesc == NULL) {
811 WARN("Invalid Parameter !\n");
812 return MMSYSERR_INVALPARAM;
814 if (wDevID >= MAX_WAVEOUTDRV) {
815 TRACE("MAX_WAVOUTDRV reached !\n");
816 return MMSYSERR_BADDEVICEID;
819 /* only PCM format is supported so far... */
820 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
821 lpDesc->lpFormat->nChannels == 0 ||
822 lpDesc->lpFormat->nSamplesPerSec == 0 ||
823 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
824 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
825 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
826 lpDesc->lpFormat->nSamplesPerSec);
827 return WAVERR_BADFORMAT;
830 if (dwFlags & WAVE_FORMAT_QUERY) {
831 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
832 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
833 lpDesc->lpFormat->nSamplesPerSec);
834 return MMSYSERR_NOERROR;
837 wwo = &WOutDev[wDevID];
839 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->caps.dwSupport & WAVECAPS_DIRECTSOUND))
840 /* not supported, ignore it */
841 dwFlags &= ~WAVE_DIRECTSOUND;
843 if (access(SOUND_DEV, 0) != 0)
844 return MMSYSERR_NOTENABLED;
846 audio = AudioIOOpenX( O_WRONLY|O_NDELAY,&spec[PLAYBACK],&spec[PLAYBACK]);
848 if (audio == -1) {
849 WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
850 return MMSYSERR_ALLOCATED;
852 /* fcntl(audio, F_SETFD, 1); *//* set close on exec flag - Dunno about this (RL)*/
853 wwo->unixdev = audio;
854 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
856 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
857 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
859 if (wwo->format.wBitsPerSample == 0) {
860 WARN("Resetting zeroed wBitsPerSample\n");
861 wwo->format.wBitsPerSample = 8 *
862 (wwo->format.wf.nAvgBytesPerSec /
863 wwo->format.wf.nSamplesPerSec) /
864 wwo->format.wf.nChannels;
867 if (dwFlags & WAVE_DIRECTSOUND) {
868 if (wwo->caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
869 /* we have realtime DirectSound, fragments just waste our time,
870 * but a large buffer is good, so choose 64KB (32 * 2^11) */
871 audio_fragment = 0x0020000B;
872 else
873 /* to approximate realtime, we must use small fragments,
874 * let's try to fragment the above 64KB (256 * 2^8) */
875 audio_fragment = 0x01000008;
876 } else {
877 /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
878 * thus leading to 46ms per fragment, and a turnaround time of 185ms
880 /* 16 fragments max, 2^10=1024 bytes per fragment */
881 audio_fragment = 0x000F000A;
883 sample_rate = wwo->format.wf.nSamplesPerSec;
884 dsp_stereo = (wwo->format.wf.nChannels > 1) ? 1 : 0;
888 /*Set the sample rate*/
889 spec[PLAYBACK].rate=sample_rate;
891 /*And the size and signedness*/
892 spec[PLAYBACK].precision=(wwo->format.wBitsPerSample );
893 if (spec[PLAYBACK].precision==16) spec[PLAYBACK].type=TYPE_SIGNED; else spec[PLAYBACK].type=TYPE_UNSIGNED;
894 spec[PLAYBACK].channels=(wwo->format.wf.nChannels);
895 spec[PLAYBACK].encoding=ENCODE_PCM;
896 spec[PLAYBACK].endian=ENDIAN_INTEL;
897 spec[PLAYBACK].max_blocks=16; /*FIXME This is the libaudioio equivalent to fragments, it controls latency*/
899 audio = AudioIOOpenX( O_WRONLY|O_NDELAY,&spec[PLAYBACK],&spec[PLAYBACK]);
901 if (audio == -1) {
902 WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
903 return MMSYSERR_ALLOCATED;
905 /* fcntl(audio, F_SETFD, 1); *//* set close on exec flag */
906 wwo->unixdev = audio;
909 /* even if we set fragment size above, read it again, just in case */
911 wwo->dwFragmentSize = DEFAULT_FRAGMENT_SIZE;
913 wwo->msg_toget = 0;
914 wwo->msg_tosave = 0;
915 wwo->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
916 memset(wwo->messages, 0, sizeof(WWO_MSG)*WWO_RING_BUFFER_SIZE);
917 InitializeCriticalSection(&wwo->msg_crst);
919 if (!(dwFlags & WAVE_DIRECTSOUND)) {
920 wwo->hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
921 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
922 WaitForSingleObject(wwo->hEvent, INFINITE);
923 } else {
924 wwo->hEvent = INVALID_HANDLE_VALUE;
925 wwo->hThread = INVALID_HANDLE_VALUE;
926 wwo->dwThreadID = 0;
929 TRACE("fd=%d fragmentSize=%ld\n",
930 wwo->unixdev, wwo->dwFragmentSize);
931 if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
932 ERR("Fragment doesn't contain an integral number of data blocks\n");
934 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
935 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
936 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
937 wwo->format.wf.nBlockAlign);
939 if (LIBAUDIOIO_NotifyClient(wDevID, WOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
940 WARN("can't notify client !\n");
941 return MMSYSERR_INVALPARAM;
943 return MMSYSERR_NOERROR;
946 /**************************************************************************
947 * wodClose [internal]
949 static DWORD wodClose(WORD wDevID)
951 DWORD ret = MMSYSERR_NOERROR;
952 WINE_WAVEOUT* wwo;
954 TRACE("(%u);\n", wDevID);
956 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
957 WARN("bad device ID !\n");
958 return MMSYSERR_BADDEVICEID;
961 wwo = &WOutDev[wDevID];
962 if (wwo->lpQueuePtr) {
963 WARN("buffers still playing !\n");
964 ret = WAVERR_STILLPLAYING;
965 } else {
966 TRACE("imhere[3-close]\n");
967 if (wwo->hEvent != INVALID_HANDLE_VALUE) {
968 wodPlayer_Message(wwo, WINE_WM_CLOSING, 0);
969 WaitForSingleObject(wwo->hEvent, INFINITE);
970 CloseHandle(wwo->hEvent);
972 if (wwo->mapping) {
973 munmap(wwo->mapping, wwo->maplen);
974 wwo->mapping = NULL;
977 AudioIOClose();
978 wwo->unixdev = -1;
979 wwo->dwFragmentSize = 0;
980 if (LIBAUDIOIO_NotifyClient(wDevID, WOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
981 WARN("can't notify client !\n");
982 ret = MMSYSERR_INVALPARAM;
985 return ret;
988 /**************************************************************************
989 * wodWrite [internal]
992 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
994 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
996 /* first, do the sanity checks... */
997 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
998 WARN("bad dev ID !\n");
999 return MMSYSERR_BADDEVICEID;
1002 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1003 return WAVERR_UNPREPARED;
1005 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1006 return WAVERR_STILLPLAYING;
1008 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1009 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1010 lpWaveHdr->lpNext = 0;
1012 TRACE("imhere[3-HEADER]\n");
1013 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_HEADER, (DWORD)lpWaveHdr);
1015 return MMSYSERR_NOERROR;
1018 /**************************************************************************
1019 * wodPrepare [internal]
1021 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1023 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1025 if (wDevID >= MAX_WAVEOUTDRV) {
1026 WARN("bad device ID !\n");
1027 return MMSYSERR_BADDEVICEID;
1030 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1031 return WAVERR_STILLPLAYING;
1033 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1034 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1035 return MMSYSERR_NOERROR;
1038 /**************************************************************************
1039 * wodUnprepare [internal]
1041 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1043 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1045 if (wDevID >= MAX_WAVEOUTDRV) {
1046 WARN("bad device ID !\n");
1047 return MMSYSERR_BADDEVICEID;
1050 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1051 return WAVERR_STILLPLAYING;
1053 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1054 lpWaveHdr->dwFlags |= WHDR_DONE;
1056 return MMSYSERR_NOERROR;
1059 /**************************************************************************
1060 * wodPause [internal]
1062 static DWORD wodPause(WORD wDevID)
1064 TRACE("(%u);!\n", wDevID);
1066 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1067 WARN("bad device ID !\n");
1068 return MMSYSERR_BADDEVICEID;
1071 TRACE("imhere[3-PAUSING]\n");
1072 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_PAUSING, 0);
1073 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1075 return MMSYSERR_NOERROR;
1078 /**************************************************************************
1079 * wodRestart [internal]
1081 static DWORD wodRestart(WORD wDevID)
1083 TRACE("(%u);\n", wDevID);
1085 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1086 WARN("bad device ID !\n");
1087 return MMSYSERR_BADDEVICEID;
1090 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1091 TRACE("imhere[3-RESTARTING]\n");
1092 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_RESTARTING, 0);
1093 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1096 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1097 /* FIXME: Myst crashes with this ... hmm -MM
1098 if (LIBAUDIOIO_NotifyClient(wDevID, WOM_DONE, 0L, 0L) != MMSYSERR_NOERROR) {
1099 WARN("can't notify client !\n");
1100 return MMSYSERR_INVALPARAM;
1104 return MMSYSERR_NOERROR;
1107 /**************************************************************************
1108 * wodReset [internal]
1110 static DWORD wodReset(WORD wDevID)
1112 TRACE("(%u);\n", wDevID);
1114 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1115 WARN("bad device ID !\n");
1116 return MMSYSERR_BADDEVICEID;
1119 TRACE("imhere[3-RESET]\n");
1120 wodPlayer_Message(&WOutDev[wDevID], WINE_WM_RESETTING, 0);
1121 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1123 return MMSYSERR_NOERROR;
1127 /**************************************************************************
1128 * wodGetPosition [internal]
1130 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1132 WINE_WAVEOUT* wwo;
1134 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1136 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1137 WARN("bad device ID !\n");
1138 return MMSYSERR_BADDEVICEID;
1141 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1143 wwo = &WOutDev[wDevID];
1145 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->format);
1148 /**************************************************************************
1149 * wodGetVolume [internal]
1151 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1153 int mixer;
1154 int vol,bal;
1155 DWORD left, right;
1157 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1159 if (lpdwVol == NULL)
1160 return MMSYSERR_NOTENABLED;
1162 vol=AudioIOGetPlaybackVolume();
1163 bal=AudioIOGetPlaybackBalance();
1166 if(bal<0) {
1167 left = vol;
1168 right=-(vol*(-100+bal)/100);
1170 else
1172 right = vol;
1173 left=(vol*(100-bal)/100);
1176 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1177 return MMSYSERR_NOERROR;
1181 /**************************************************************************
1182 * wodSetVolume [internal]
1184 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1186 int mixer;
1187 int volume,bal;
1188 DWORD left, right;
1190 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1192 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1193 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1194 volume = max(left , right );
1195 bal=min(left,right);
1196 bal=bal*100/volume;
1197 if(right>left) bal=-100+bal; else bal=100-bal;
1199 AudioIOSetPlaybackVolume(volume);
1200 AudioIOSetPlaybackBalance(bal);
1202 return MMSYSERR_NOERROR;
1205 /**************************************************************************
1206 * wodGetNumDevs [internal]
1208 static DWORD wodGetNumDevs(void)
1210 DWORD ret = 1;
1212 /* FIXME: For now, only one sound device (SOUND_DEV) is allowed */
1213 int audio = open(SOUND_DEV, O_WRONLY|O_NDELAY, 0);
1215 if (audio == -1) {
1216 if (errno != EBUSY)
1217 ret = 0;
1218 } else {
1219 close(audio);
1222 TRACE("NumDrivers = %d\n",ret);
1223 return ret;
1226 /**************************************************************************
1227 * wodMessage (WINEAUDIOIO.@)
1229 DWORD WINAPI LIBAUDIOIO_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1230 DWORD dwParam1, DWORD dwParam2)
1232 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1233 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1235 switch (wMsg) {
1236 case DRVM_INIT:
1237 case DRVM_EXIT:
1238 case DRVM_ENABLE:
1239 case DRVM_DISABLE:
1240 /* FIXME: Pretend this is supported */
1241 return 0;
1242 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1243 case WODM_CLOSE: return wodClose (wDevID);
1244 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1245 case WODM_PAUSE: return wodPause (wDevID);
1246 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1247 case WODM_BREAKLOOP: return MMSYSERR_NOTSUPPORTED;
1248 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1249 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1250 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1251 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1252 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1253 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1254 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1255 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1256 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1257 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1258 case WODM_RESTART: return wodRestart (wDevID);
1259 case WODM_RESET: return wodReset (wDevID);
1261 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1262 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1263 default:
1264 FIXME("unknown message %d!\n", wMsg);
1266 return MMSYSERR_NOTSUPPORTED;
1269 /*======================================================================*
1270 * Low level DSOUND implementation *
1271 * While I have tampered somewhat with this code it is wholely unlikely that it works
1272 * Elsewhere the driver returns Not Implemented for DIrectSound
1273 * While it may be possible to map the sound device on Solaris
1274 * Doing so would bypass the libaudioio library and therefore break any conversions
1275 * that the libaudioio sample specification converter is doing
1276 * **** All this is untested so far
1277 *======================================================================*/
1279 typedef struct IDsDriverImpl IDsDriverImpl;
1280 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1282 struct IDsDriverImpl
1284 /* IUnknown fields */
1285 IDsDriverVtbl *lpVtbl;
1286 DWORD ref;
1287 /* IDsDriverImpl fields */
1288 UINT wDevID;
1289 IDsDriverBufferImpl*primary;
1292 struct IDsDriverBufferImpl
1294 /* IUnknown fields */
1295 IDsDriverBufferVtbl *lpVtbl;
1296 DWORD ref;
1297 /* IDsDriverBufferImpl fields */
1298 IDsDriverImpl* drv;
1299 DWORD buflen;
1302 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1304 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1305 if (!wwo->mapping) {
1306 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1307 wwo->unixdev, 0);
1308 if (wwo->mapping == (LPBYTE)-1) {
1309 ERR("(%p): Could not map sound device for direct access (errno=%d)\n", dsdb, errno);
1310 return DSERR_GENERIC;
1312 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1314 /* for some reason, es1371 and sblive! sometimes have junk in here. */
1315 memset(wwo->mapping,0,wwo->maplen); /* clear it, or we get junk noise */
1317 return DS_OK;
1320 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
1322 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1323 if (wwo->mapping) {
1324 if (munmap(wwo->mapping, wwo->maplen) < 0) {
1325 ERR("(%p): Could not unmap sound device (errno=%d)\n", dsdb, errno);
1326 return DSERR_GENERIC;
1328 wwo->mapping = NULL;
1329 TRACE("(%p): sound device unmapped\n", dsdb);
1331 return DS_OK;
1334 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1336 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1337 FIXME("(): stub!\n");
1338 return DSERR_UNSUPPORTED;
1341 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1343 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1344 This->ref++;
1345 return This->ref;
1348 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1350 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1351 if (--This->ref)
1352 return This->ref;
1353 if (This == This->drv->primary)
1354 This->drv->primary = NULL;
1355 DSDB_UnmapPrimary(This);
1356 HeapFree(GetProcessHeap(),0,This);
1357 return 0;
1360 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1361 LPVOID*ppvAudio1,LPDWORD pdwLen1,
1362 LPVOID*ppvAudio2,LPDWORD pdwLen2,
1363 DWORD dwWritePosition,DWORD dwWriteLen,
1364 DWORD dwFlags)
1366 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1367 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
1368 * and that we don't support secondary buffers, this method will never be called */
1369 TRACE("(%p): stub\n",iface);
1370 return DSERR_UNSUPPORTED;
1373 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1374 LPVOID pvAudio1,DWORD dwLen1,
1375 LPVOID pvAudio2,DWORD dwLen2)
1377 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1378 TRACE("(%p): stub\n",iface);
1379 return DSERR_UNSUPPORTED;
1382 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1383 LPWAVEFORMATEX pwfx)
1385 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1387 TRACE("(%p,%p)\n",iface,pwfx);
1388 /* On our request (GetDriverDesc flags), DirectSound has by now used
1389 * waveOutClose/waveOutOpen to set the format...
1390 * unfortunately, this means our mmap() is now gone...
1391 * so we need to somehow signal to our DirectSound implementation
1392 * that it should completely recreate this HW buffer...
1393 * this unexpected error code should do the trick... */
1394 return DSERR_BUFFERLOST;
1397 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1399 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1400 TRACE("(%p,%ld): stub\n",iface,dwFreq);
1401 return DSERR_UNSUPPORTED;
1404 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1406 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1407 FIXME("(%p,%p): stub!\n",iface,pVolPan);
1408 return DS_OK;
1411 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1413 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1414 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1415 return DSERR_UNSUPPORTED;
1418 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1419 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1421 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1422 #if 0
1423 count_info info;
1424 #endif
1425 DWORD ptr;
1427 TRACE("(%p)\n",iface);
1428 if (WOutDev[This->drv->wDevID].unixdev == -1) {
1429 ERR("device not open, but accessing?\n");
1430 return DSERR_UNINITIALIZED;
1432 /*Libaudioio doesn't support this (Yet anyway)*/
1433 return DSERR_UNSUPPORTED;
1437 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
1439 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1440 #if 0
1441 int enable = PCM_ENABLE_OUTPUT;
1442 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
1443 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1444 ERR("ioctl failed (%d)\n", errno);
1445 return DSERR_GENERIC;
1447 #endif
1448 return DS_OK;
1451 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1453 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1454 int enable = 0;
1455 #if 0
1456 TRACE("(%p)\n",iface);
1457 /* no more playing */
1458 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1459 ERR("ioctl failed (%d)\n", errno);
1460 return DSERR_GENERIC;
1462 #endif
1463 #if 0
1464 /* the play position must be reset to the beginning of the buffer */
1465 if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_RESET, 0) < 0) {
1466 ERR("ioctl failed (%d)\n", errno);
1467 return DSERR_GENERIC;
1469 #endif
1470 /* Most OSS drivers just can't stop the playback without closing the device...
1471 * so we need to somehow signal to our DirectSound implementation
1472 * that it should completely recreate this HW buffer...
1473 * this unexpected error code should do the trick... */
1474 return DSERR_BUFFERLOST;
1477 static IDsDriverBufferVtbl dsdbvt =
1479 IDsDriverBufferImpl_QueryInterface,
1480 IDsDriverBufferImpl_AddRef,
1481 IDsDriverBufferImpl_Release,
1482 IDsDriverBufferImpl_Lock,
1483 IDsDriverBufferImpl_Unlock,
1484 IDsDriverBufferImpl_SetFormat,
1485 IDsDriverBufferImpl_SetFrequency,
1486 IDsDriverBufferImpl_SetVolumePan,
1487 IDsDriverBufferImpl_SetPosition,
1488 IDsDriverBufferImpl_GetPosition,
1489 IDsDriverBufferImpl_Play,
1490 IDsDriverBufferImpl_Stop
1493 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
1495 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1496 FIXME("(%p): stub!\n",iface);
1497 return DSERR_UNSUPPORTED;
1500 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
1502 IDsDriverImpl *This = (IDsDriverImpl *)iface;
1503 This->ref++;
1504 return This->ref;
1507 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
1509 IDsDriverImpl *This = (IDsDriverImpl *)iface;
1510 if (--This->ref)
1511 return This->ref;
1512 HeapFree(GetProcessHeap(),0,This);
1513 return 0;
1516 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
1518 IDsDriverImpl *This = (IDsDriverImpl *)iface;
1519 TRACE("(%p,%p)\n",iface,pDesc);
1520 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
1521 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
1522 strcpy(pDesc->szDesc,"Wine AudioIO DirectSound Driver");
1523 strcpy(pDesc->szDrvName,"wineaudioio.drv");
1524 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
1525 pDesc->wVxdId = 0;
1526 pDesc->wReserved = 0;
1527 pDesc->ulDeviceNum = This->wDevID;
1528 pDesc->dwHeapType = DSDHEAP_NOHEAP;
1529 pDesc->pvDirectDrawHeap = NULL;
1530 pDesc->dwMemStartAddress = 0;
1531 pDesc->dwMemEndAddress = 0;
1532 pDesc->dwMemAllocExtra = 0;
1533 pDesc->pvReserved1 = NULL;
1534 pDesc->pvReserved2 = NULL;
1535 return DS_OK;
1538 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
1540 IDsDriverImpl *This = (IDsDriverImpl *)iface;
1541 int enable = 0;
1543 TRACE("(%p)\n",iface);
1544 /* make sure the card doesn't start playing before we want it to */
1545 #if 0
1546 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1547 ERR("ioctl failed (%d)\n", errno);
1548 return DSERR_GENERIC;
1550 #endif
1551 return DS_OK;
1554 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
1556 IDsDriverImpl *This = (IDsDriverImpl *)iface;
1557 TRACE("(%p)\n",iface);
1558 if (This->primary) {
1559 ERR("problem with DirectSound: primary not released\n");
1560 return DSERR_GENERIC;
1562 return DS_OK;
1565 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
1567 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1568 TRACE("(%p,%p)\n",iface,pCaps);
1569 memset(pCaps, 0, sizeof(*pCaps));
1570 /* FIXME: need to check actual capabilities */
1571 pCaps->dwFlags = DSCAPS_PRIMARYMONO | DSCAPS_PRIMARYSTEREO |
1572 DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARY16BIT;
1573 pCaps->dwPrimaryBuffers = 1;
1574 pCaps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
1575 pCaps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
1576 /* the other fields only apply to secondary buffers, which we don't support
1577 * (unless we want to mess with wavetable synthesizers and MIDI) */
1578 return DS_OK;
1581 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
1582 LPWAVEFORMATEX pwfx,
1583 DWORD dwFlags, DWORD dwCardAddress,
1584 LPDWORD pdwcbBufferSize,
1585 LPBYTE *ppbBuffer,
1586 LPVOID *ppvObj)
1588 IDsDriverImpl *This = (IDsDriverImpl *)iface;
1589 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
1590 HRESULT err;
1591 #if 0
1592 audio_buf_info info;
1593 #endif
1594 int enable = 0;
1596 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
1597 /* we only support primary buffers */
1598 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
1599 return DSERR_UNSUPPORTED;
1600 if (This->primary)
1601 return DSERR_ALLOCATED;
1602 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
1603 return DSERR_CONTROLUNAVAIL;
1605 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
1606 if (*ippdsdb == NULL)
1607 return DSERR_OUTOFMEMORY;
1608 (*ippdsdb)->lpVtbl = &dsdbvt;
1609 (*ippdsdb)->ref = 1;
1610 (*ippdsdb)->drv = This;
1612 /* check how big the DMA buffer is now */
1613 #if 0
1614 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1615 ERR("ioctl failed (%d)\n", errno);
1616 HeapFree(GetProcessHeap(),0,*ippdsdb);
1617 *ippdsdb = NULL;
1618 return DSERR_GENERIC;
1620 #endif
1621 WOutDev[This->wDevID].maplen =64*1024; /* Map 64 K at a time */
1623 #if 0
1624 (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
1625 #endif
1626 /* map the DMA buffer */
1627 err = DSDB_MapPrimary(*ippdsdb);
1628 if (err != DS_OK) {
1629 HeapFree(GetProcessHeap(),0,*ippdsdb);
1630 *ippdsdb = NULL;
1631 return err;
1634 /* primary buffer is ready to go */
1635 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
1636 *ppbBuffer = WOutDev[This->wDevID].mapping;
1638 /* some drivers need some extra nudging after mapping */
1639 #if 0
1640 if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1641 ERR("ioctl failed (%d)\n", errno);
1642 return DSERR_GENERIC;
1644 #endif
1646 This->primary = *ippdsdb;
1648 return DS_OK;
1651 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
1652 PIDSDRIVERBUFFER pBuffer,
1653 LPVOID *ppvObj)
1655 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1656 TRACE("(%p,%p): stub\n",iface,pBuffer);
1657 return DSERR_INVALIDCALL;
1660 static IDsDriverVtbl dsdvt =
1662 IDsDriverImpl_QueryInterface,
1663 IDsDriverImpl_AddRef,
1664 IDsDriverImpl_Release,
1665 IDsDriverImpl_GetDriverDesc,
1666 IDsDriverImpl_Open,
1667 IDsDriverImpl_Close,
1668 IDsDriverImpl_GetCaps,
1669 IDsDriverImpl_CreateSoundBuffer,
1670 IDsDriverImpl_DuplicateSoundBuffer
1673 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1675 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
1677 /* the HAL isn't much better than the HEL if we can't do mmap() */
1678 if (!(WOutDev[wDevID].caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
1679 ERR("DirectSound flag not set\n");
1680 MESSAGE("This sound card's driver does not support direct access\n");
1681 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1682 return MMSYSERR_NOTSUPPORTED;
1685 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
1686 if (!*idrv)
1687 return MMSYSERR_NOMEM;
1688 (*idrv)->lpVtbl = &dsdvt;
1689 (*idrv)->ref = 1;
1691 (*idrv)->wDevID = wDevID;
1692 (*idrv)->primary = NULL;
1693 return MMSYSERR_NOERROR;
1696 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1698 memset(desc, 0, sizeof(*desc));
1699 strcpy(desc->szDesc, "Wine LIBAUDIOIO DirectSound Driver");
1700 strcpy(desc->szDrvName, "wineaudioio.drv");
1701 return MMSYSERR_NOERROR;
1704 /*======================================================================*
1705 * Low level WAVE IN implementation *
1706 *======================================================================*/
1708 /**************************************************************************
1709 * widGetDevCaps [internal]
1711 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
1713 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1715 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1717 if (wDevID >= MAX_WAVEINDRV) {
1718 TRACE("MAX_WAVINDRV reached !\n");
1719 return MMSYSERR_BADDEVICEID;
1722 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1723 return MMSYSERR_NOERROR;
1726 /**************************************************************************
1727 * widRecorder [internal]
1729 static DWORD CALLBACK widRecorder(LPVOID pmt)
1731 WORD uDevID = (DWORD)pmt;
1732 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1733 WAVEHDR* lpWaveHdr;
1734 DWORD dwSleepTime;
1735 MSG msg;
1736 DWORD bytesRead;
1739 int fragments;
1740 int fragsize;
1741 int fragstotal;
1742 int bytes;
1745 int xs;
1747 LPVOID buffer = HeapAlloc(GetProcessHeap(),
1748 HEAP_ZERO_MEMORY,
1749 wwi->dwFragmentSize);
1751 LPVOID pOffset = buffer;
1753 PeekMessageA(&msg, 0, 0, 0, 0);
1754 wwi->state = WINE_WS_STOPPED;
1755 wwi->dwTotalRecorded = 0;
1757 SetEvent(wwi->hEvent);
1760 /* make sleep time to be # of ms to output a fragment */
1761 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
1762 TRACE("sleeptime=%ld ms\n", dwSleepTime);
1764 for (; ; ) {
1765 /* wait for dwSleepTime or an event in thread's queue */
1766 /* FIXME: could improve wait time depending on queue state,
1767 * ie, number of queued fragments
1770 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
1772 lpWaveHdr = wwi->lpQueuePtr;
1774 bytes=fragsize=AudioIORecordingAvailable();
1775 fragments=fragstotal=1;
1777 TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", fragments, fragsize, fragstotal, bytes);
1780 /* read all the fragments accumulated so far */
1781 while ((fragments > 0) && (wwi->lpQueuePtr))
1783 fragments --;
1785 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
1787 /* directly read fragment in wavehdr */
1788 bytesRead = AudioIORead(
1789 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1790 wwi->dwFragmentSize);
1792 TRACE("bytesRead=%ld (direct)\n", bytesRead);
1793 if (bytesRead != (DWORD) -1)
1795 /* update number of bytes recorded in current buffer and by this device */
1796 lpWaveHdr->dwBytesRecorded += bytesRead;
1797 wwi->dwTotalRecorded += bytesRead;
1799 /* buffer is full. notify client */
1800 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
1802 /* must copy the value of next waveHdr, because we have no idea of what
1803 * will be done with the content of lpWaveHdr in callback
1805 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1807 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1808 lpWaveHdr->dwFlags |= WHDR_DONE;
1810 if (LIBAUDIOIO_NotifyClient(uDevID, WIM_DATA,
1811 (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR)
1813 WARN("can't notify client !\n");
1815 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1819 else
1821 /* read the fragment in a local buffer */
1822 bytesRead = AudioIORead( buffer, wwi->dwFragmentSize);
1823 pOffset = buffer;
1825 TRACE("bytesRead=%ld (local)\n", bytesRead);
1827 /* copy data in client buffers */
1828 while (bytesRead != (DWORD) -1 && bytesRead > 0)
1830 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1832 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1833 pOffset,
1834 dwToCopy);
1836 /* update number of bytes recorded in current buffer and by this device */
1837 lpWaveHdr->dwBytesRecorded += dwToCopy;
1838 wwi->dwTotalRecorded += dwToCopy;
1839 bytesRead -= dwToCopy;
1840 pOffset += dwToCopy;
1842 /* client buffer is full. notify client */
1843 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
1845 /* must copy the value of next waveHdr, because we have no idea of what
1846 * will be done with the content of lpWaveHdr in callback
1848 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1849 TRACE("lpNext=%p\n", lpNext);
1851 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1852 lpWaveHdr->dwFlags |= WHDR_DONE;
1854 if (LIBAUDIOIO_NotifyClient(uDevID, WIM_DATA,
1855 (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR)
1857 WARN("can't notify client !\n");
1860 wwi->lpQueuePtr = lpWaveHdr = lpNext;
1861 if (!lpNext && bytesRead) {
1862 /* no more buffer to copy data to, but we did read more.
1863 * what hasn't been copied will be dropped
1865 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
1866 wwi->lpQueuePtr = NULL;
1867 break;
1875 MsgWaitForMultipleObjects(0, NULL, FALSE, dwSleepTime, QS_POSTMESSAGE);
1877 while (PeekMessageA(&msg, 0, WINE_WM_FIRST, WINE_WM_LAST, PM_REMOVE)) {
1879 TRACE("msg=0x%x wParam=0x%x lParam=0x%lx\n", msg.message, msg.wParam, msg.lParam);
1880 switch (msg.message) {
1881 case WINE_WM_PAUSING:
1882 wwi->state = WINE_WS_PAUSED;
1884 AudioIORecordingPause();
1885 SetEvent(wwi->hEvent);
1886 break;
1887 case WINE_WM_RESTARTING:
1890 wwi->state = WINE_WS_PLAYING;
1892 if (wwi->bTriggerSupport)
1894 /* start the recording */
1895 AudioIORecordingResume();
1897 else
1899 unsigned char data[4];
1900 /* read 4 bytes to start the recording */
1901 AudioIORead( data, 4);
1904 SetEvent(wwi->hEvent);
1905 break;
1907 case WINE_WM_HEADER:
1908 lpWaveHdr = (LPWAVEHDR)msg.lParam;
1909 lpWaveHdr->lpNext = 0;
1911 /* insert buffer at the end of queue */
1913 LPWAVEHDR* wh;
1914 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1915 *wh = lpWaveHdr;
1917 break;
1918 case WINE_WM_RESETTING:
1919 wwi->state = WINE_WS_STOPPED;
1920 /* return all buffers to the app */
1921 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1922 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1923 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1924 lpWaveHdr->dwFlags |= WHDR_DONE;
1926 if (LIBAUDIOIO_NotifyClient(uDevID, WIM_DATA,
1927 (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR) {
1928 WARN("can't notify client !\n");
1931 wwi->lpQueuePtr = NULL;
1932 SetEvent(wwi->hEvent);
1933 break;
1934 case WINE_WM_CLOSING:
1935 wwi->hThread = 0;
1936 wwi->state = WINE_WS_CLOSED;
1937 SetEvent(wwi->hEvent);
1938 HeapFree(GetProcessHeap(), 0, buffer);
1939 ExitThread(0);
1940 /* shouldn't go here */
1941 default:
1942 FIXME("unknown message %d\n", msg.message);
1943 break;
1947 ExitThread(0);
1948 /* just for not generating compilation warnings... should never be executed */
1949 return 0;
1953 /**************************************************************************
1954 * widOpen [internal]
1956 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1958 int audio;
1959 int fragment_size;
1960 int sample_rate;
1961 int format;
1962 int dsp_stereo;
1963 WINE_WAVEIN* wwi;
1964 int audio_fragment;
1966 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1967 if (lpDesc == NULL) {
1968 WARN("Invalid Parameter !\n");
1969 return MMSYSERR_INVALPARAM;
1971 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_BADDEVICEID;
1973 /* only PCM format is supported so far... */
1974 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1975 lpDesc->lpFormat->nChannels == 0 ||
1976 lpDesc->lpFormat->nSamplesPerSec == 0 ||
1977 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1978 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1979 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1980 lpDesc->lpFormat->nSamplesPerSec);
1981 return WAVERR_BADFORMAT;
1984 if (dwFlags & WAVE_FORMAT_QUERY) {
1985 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1986 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1987 lpDesc->lpFormat->nSamplesPerSec);
1988 return MMSYSERR_NOERROR;
1991 if (access(SOUND_DEV,0) != 0) return MMSYSERR_NOTENABLED;
1992 audio = AudioIOOpenX( O_RDONLY|O_NDELAY, &spec[RECORD],&spec[RECORD]);
1993 if (audio == -1) {
1994 WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
1995 return MMSYSERR_ALLOCATED;
1997 fcntl(audio, F_SETFD, 1); /* set close on exec flag */
1999 wwi = &WInDev[wDevID];
2000 if (wwi->lpQueuePtr) {
2001 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
2002 wwi->lpQueuePtr = NULL;
2004 wwi->unixdev = audio;
2005 wwi->dwTotalRecorded = 0;
2006 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2008 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2009 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2011 if (wwi->format.wBitsPerSample == 0) {
2012 WARN("Resetting zeroed wBitsPerSample\n");
2013 wwi->format.wBitsPerSample = 8 *
2014 (wwi->format.wf.nAvgBytesPerSec /
2015 wwi->format.wf.nSamplesPerSec) /
2016 wwi->format.wf.nChannels;
2019 spec[RECORD].rate=sample_rate = wwi->format.wf.nSamplesPerSec;
2020 dsp_stereo = ((spec[RECORD].channels=wwi->format.wf.nChannels) > 1) ? TRUE : FALSE;
2021 spec[RECORD].precision= wwi->format.wBitsPerSample;
2022 spec[RECORD].type=(spec[RECORD].precision==16)?TYPE_SIGNED:TYPE_UNSIGNED;
2024 /* This is actually hand tuned to work so that my SB Live:
2025 * - does not skip
2026 * - does not buffer too much
2027 * when sending with the Shoutcast winamp plugin
2029 /* 7 fragments max, 2^10 = 1024 bytes per fragment */
2030 audio_fragment = 0x0007000A;
2031 fragment_size=4096;
2032 if (fragment_size == -1) {
2033 AudioIOClose();
2034 wwi->unixdev = -1;
2035 return MMSYSERR_NOTENABLED;
2037 wwi->dwFragmentSize = fragment_size;
2039 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
2040 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
2041 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
2042 wwi->format.wf.nBlockAlign);
2044 wwi->hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
2045 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
2046 WaitForSingleObject(wwi->hEvent, INFINITE);
2048 if (LIBAUDIOIO_NotifyClient(wDevID, WIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
2049 WARN("can't notify client !\n");
2050 return MMSYSERR_INVALPARAM;
2052 return MMSYSERR_NOERROR;
2055 /**************************************************************************
2056 * widClose [internal]
2058 static DWORD widClose(WORD wDevID)
2060 WINE_WAVEIN* wwi;
2062 TRACE("(%u);\n", wDevID);
2063 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2064 WARN("can't close !\n");
2065 return MMSYSERR_INVALHANDLE;
2068 wwi = &WInDev[wDevID];
2070 if (wwi->lpQueuePtr != NULL) {
2071 WARN("still buffers open !\n");
2072 return WAVERR_STILLPLAYING;
2075 PostThreadMessageA(wwi->dwThreadID, WINE_WM_CLOSING, 0, 0);
2076 WaitForSingleObject(wwi->hEvent, INFINITE);
2077 CloseHandle(wwi->hEvent);
2078 AudioIOClose();
2079 wwi->unixdev = -1;
2080 wwi->dwFragmentSize = 0;
2081 if (LIBAUDIOIO_NotifyClient(wDevID, WIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
2082 WARN("can't notify client !\n");
2083 return MMSYSERR_INVALPARAM;
2085 return MMSYSERR_NOERROR;
2088 /**************************************************************************
2089 * widAddBuffer [internal]
2091 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2093 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2095 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2096 WARN("can't do it !\n");
2097 return MMSYSERR_INVALHANDLE;
2099 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2100 TRACE("never been prepared !\n");
2101 return WAVERR_UNPREPARED;
2103 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2104 TRACE("header already in use !\n");
2105 return WAVERR_STILLPLAYING;
2108 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2109 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2110 lpWaveHdr->dwBytesRecorded = 0;
2111 lpWaveHdr->lpNext = NULL;
2113 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_HEADER, 0, (DWORD)lpWaveHdr);
2114 return MMSYSERR_NOERROR;
2117 /**************************************************************************
2118 * widPrepare [internal]
2120 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2122 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2124 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
2126 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2127 return WAVERR_STILLPLAYING;
2129 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2130 lpWaveHdr->dwFlags &= ~(WHDR_INQUEUE|WHDR_DONE);
2131 lpWaveHdr->dwBytesRecorded = 0;
2132 TRACE("header prepared !\n");
2133 return MMSYSERR_NOERROR;
2136 /**************************************************************************
2137 * widUnprepare [internal]
2139 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2141 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2142 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
2144 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2145 return WAVERR_STILLPLAYING;
2147 lpWaveHdr->dwFlags &= ~(WHDR_PREPARED|WHDR_INQUEUE);
2148 lpWaveHdr->dwFlags |= WHDR_DONE;
2150 return MMSYSERR_NOERROR;
2153 /**************************************************************************
2154 * widStart [internal]
2156 static DWORD widStart(WORD wDevID)
2158 TRACE("(%u);\n", wDevID);
2159 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2160 WARN("can't start recording !\n");
2161 return MMSYSERR_INVALHANDLE;
2164 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESTARTING, 0, 0);
2165 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2166 return MMSYSERR_NOERROR;
2169 /**************************************************************************
2170 * widStop [internal]
2172 static DWORD widStop(WORD wDevID)
2174 TRACE("(%u);\n", wDevID);
2175 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2176 WARN("can't stop !\n");
2177 return MMSYSERR_INVALHANDLE;
2179 /* FIXME: reset aint stop */
2180 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
2181 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2183 return MMSYSERR_NOERROR;
2186 /**************************************************************************
2187 * widReset [internal]
2189 static DWORD widReset(WORD wDevID)
2191 TRACE("(%u);\n", wDevID);
2192 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2193 WARN("can't reset !\n");
2194 return MMSYSERR_INVALHANDLE;
2196 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
2197 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2198 return MMSYSERR_NOERROR;
2201 /**************************************************************************
2202 * widGetPosition [internal]
2204 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2206 WINE_WAVEIN* wwi;
2208 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2210 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2211 WARN("can't get pos !\n");
2212 return MMSYSERR_INVALHANDLE;
2214 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
2216 wwi = &WInDev[wDevID];
2218 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->format);
2221 /**************************************************************************
2222 * widMessage (WINEAUDIOIO.@)
2224 DWORD WINAPI LIBAUDIOIO_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2225 DWORD dwParam1, DWORD dwParam2)
2227 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2228 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2230 switch (wMsg) {
2231 case DRVM_INIT:
2232 case DRVM_EXIT:
2233 case DRVM_ENABLE:
2234 case DRVM_DISABLE:
2235 /* FIXME: Pretend this is supported */
2236 return 0;
2237 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2238 case WIDM_CLOSE: return widClose (wDevID);
2239 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2240 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2241 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2242 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2243 case WIDM_GETNUMDEVS: return wodGetNumDevs (); /* same number of devices in output as in input */
2244 case WIDM_GETPOS: return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
2245 case WIDM_RESET: return widReset (wDevID);
2246 case WIDM_START: return widStart (wDevID);
2247 case WIDM_STOP: return widStop (wDevID);
2248 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
2249 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2250 default:
2251 FIXME("unknown message %u!\n", wMsg);
2253 return MMSYSERR_NOTSUPPORTED;
2256 /*======================================================================*
2257 * Low level DSOUND capture implementation *
2258 *======================================================================*/
2259 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
2261 /* we can't perform memory mapping as we don't have a file stream
2262 interface with arts like we do with oss */
2263 MESSAGE("This sound card's driver does not support direct access\n");
2264 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2265 return MMSYSERR_NOTSUPPORTED;
2268 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2270 memset(desc, 0, sizeof(*desc));
2271 strcpy(desc->szDesc, "Wine LIBAUDIOIO DirectSound Driver");
2272 strcpy(desc->szDrvName, "wineaudioio.drv");
2273 return MMSYSERR_NOERROR;
2276 #else /* HAVE_LIBAUDIOIO */
2278 /**************************************************************************
2279 * wodMessage (WINEAUDIOIO.@)
2281 DWORD WINAPI LIBAUDIOIO_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2282 DWORD dwParam1, DWORD dwParam2)
2284 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2285 return MMSYSERR_NOTENABLED;
2288 /**************************************************************************
2289 * widMessage (WINEAUDIOIO.@)
2291 DWORD WINAPI LIBAUDIOIO_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2292 DWORD dwParam1, DWORD dwParam2)
2294 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2295 return MMSYSERR_NOTENABLED;
2298 #endif /* HAVE_LIBAUDIOIO */