Remove wine specific DRV_QUERYDSOUNDGUID message and calculate it in
[wine/multimedia.git] / dlls / winmm / winenas / audio.c
blob3204a0a3aec2ecac1868d3e112d8924ac639bc35
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Wine Driver for NAS Network Audio System
4 * http://radscan.com/nas.html
6 * Copyright 1994 Martin Ayotte
7 * 1999 Eric Pouech (async playing in waveOut/waveIn)
8 * 2000 Eric Pouech (loops in waveOut)
9 * 2002 Chris Morgan (aRts version of this file)
10 * 2002 Nicolas Escuder (NAS version of this file)
12 * Copyright 2002 Nicolas Escuder <n.escuder@alineanet.com>
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* NOTE:
29 * with nas we cannot stop the audio that is already in
30 * the servers buffer.
32 * FIXME:
33 * pause in waveOut does not work correctly in loop mode
37 #include "config.h"
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
48 #endif
49 #include <fcntl.h>
51 #if 0
52 #define EMULATE_SB16
53 #endif
54 #define FRAG_SIZE 1024
55 #define FRAG_COUNT 10
57 /* avoid type conflicts */
58 #define INT8 X_INT8
59 #define INT16 X_INT16
60 #define INT32 X_INT32
61 #define BOOL X_BOOL
62 #define BYTE X_BYTE
63 #ifdef HAVE_AUDIO_AUDIOLIB_H
64 #include <audio/audiolib.h>
65 #endif
66 #ifdef HAVE_AUDIO_SOUNDLIB_H
67 #include <audio/soundlib.h>
68 #endif
69 #undef INT8
70 #undef INT16
71 #undef INT32
72 #undef BOOL
73 #undef BYTE
75 #include "windef.h"
76 #include "winbase.h"
77 #include "wingdi.h"
78 #include "winerror.h"
79 #include "wine/winuser16.h"
80 #include "mmddk.h"
81 #include "dsound.h"
82 #include "dsdriver.h"
83 #include "nas.h"
84 #include "wine/debug.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(wave);
88 /* Allow 1% deviation for sample rates (some ES137x cards) */
89 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
91 #ifdef HAVE_NAS
93 static AuServer *AuServ;
95 #define MAX_WAVEOUTDRV (1)
97 /* state diagram for waveOut writing:
99 * +---------+-------------+---------------+---------------------------------+
100 * | state | function | event | new state |
101 * +---------+-------------+---------------+---------------------------------+
102 * | | open() | | STOPPED |
103 * | PAUSED | write() | | PAUSED |
104 * | STOPPED | write() | <thrd create> | PLAYING |
105 * | PLAYING | write() | HEADER | PLAYING |
106 * | (other) | write() | <error> | |
107 * | (any) | pause() | PAUSING | PAUSED |
108 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
109 * | (any) | reset() | RESETTING | STOPPED |
110 * | (any) | close() | CLOSING | CLOSED |
111 * +---------+-------------+---------------+---------------------------------+
114 /* states of the playing device */
115 #define WINE_WS_PLAYING 0
116 #define WINE_WS_PAUSED 1
117 #define WINE_WS_STOPPED 2
118 #define WINE_WS_CLOSED 3
120 /* events to be send to device */
121 enum win_wm_message {
122 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
123 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING
126 typedef struct {
127 enum win_wm_message msg; /* message identifier */
128 DWORD param; /* parameter for this message */
129 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
130 } RING_MSG;
132 /* implement an in-process message ring for better performance
133 * (compared to passing thru the server)
134 * this ring will be used by the input (resp output) record (resp playback) routine
136 #define NAS_RING_BUFFER_INCREMENT 64
137 typedef struct {
138 RING_MSG * messages;
139 int ring_buffer_size;
140 int msg_tosave;
141 int msg_toget;
142 HANDLE msg_event;
143 CRITICAL_SECTION msg_crst;
144 } MSG_RING;
146 typedef struct {
147 volatile int state; /* one of the WINE_WS_ manifest constants */
148 WAVEOPENDESC waveDesc;
149 WORD wFlags;
150 PCMWAVEFORMAT format;
151 WAVEOUTCAPSA caps;
152 int Id;
154 int open;
155 AuServer *AuServ;
156 AuDeviceID AuDev;
157 AuFlowID AuFlow;
158 BOOL FlowStarted;
160 DWORD writeBytes;
161 DWORD freeBytes;
162 DWORD sendBytes;
164 DWORD BufferSize; /* size of whole buffer in bytes */
166 char* SoundBuffer;
167 long BufferUsed;
169 DWORD volume_left; /* volume control information */
170 DWORD volume_right;
172 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
173 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
175 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
176 DWORD dwLoops; /* private copy of loop counter */
178 DWORD PlayedTotal; /* number of bytes actually played since opening */
179 DWORD WrittenTotal; /* number of bytes written to the audio device since opening */
181 /* synchronization stuff */
182 HANDLE hStartUpEvent;
183 HANDLE hThread;
184 DWORD dwThreadID;
185 MSG_RING msgRing;
186 } WINE_WAVEOUT;
188 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
190 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
191 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
194 /* NASFUNC */
195 static AuBool event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd);
196 static int nas_init(void);
197 static int nas_end(void);
199 static int nas_finddev(WINE_WAVEOUT* wwo);
200 static int nas_open(WINE_WAVEOUT* wwo);
201 static int nas_free(WINE_WAVEOUT* wwo);
202 static int nas_close(WINE_WAVEOUT* wwo);
203 static void buffer_resize(WINE_WAVEOUT* wwo, int len);
204 static int nas_add_buffer(WINE_WAVEOUT* wwo);
205 static int nas_send_buffer(WINE_WAVEOUT* wwo);
207 /* These strings used only for tracing */
208 static const char *wodPlayerCmdString[] = {
209 "WINE_WM_PAUSING",
210 "WINE_WM_RESTARTING",
211 "WINE_WM_RESETTING",
212 "WINE_WM_HEADER",
213 "WINE_WM_UPDATE",
214 "WINE_WM_BREAKLOOP",
215 "WINE_WM_CLOSING",
218 static char *nas_elementnotify_kinds[] = {
219 "LowWater",
220 "HighWater",
221 "State",
222 "Unknown"
225 static char *nas_states[] = {
226 "Stop",
227 "Start",
228 "Pause",
229 "Any"
232 static char *nas_reasons[] = {
233 "User",
234 "Underrun",
235 "Overrun",
236 "EOF",
237 "Watermark",
238 "Hardware",
239 "Any"
242 static char* nas_reason(unsigned int reason)
244 if (reason > 6) reason = 6;
245 return nas_reasons[reason];
248 static char* nas_elementnotify_kind(unsigned int kind)
250 if (kind > 2) kind = 3;
251 return nas_elementnotify_kinds[kind];
255 #if 0
256 static const char* nas_event_type(unsigned int type)
258 static const char * const nas_event_types[] =
260 "Undefined",
261 "Undefined",
262 "ElementNotify",
263 "GrabNotify",
264 "MonitorNotify",
265 "BucketNotify",
266 "DeviceNotify"
269 if (type > 6) type = 0;
270 return nas_event_types[type];
272 #endif
275 static char* nas_state(unsigned int state)
277 if (state > 3) state = 3;
278 return nas_states[state];
281 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
282 PCMWAVEFORMAT* format)
284 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
285 lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
286 format->wf.nChannels, format->wf.nAvgBytesPerSec);
287 TRACE("Position in bytes=%lu\n", position);
289 switch (lpTime->wType) {
290 case TIME_SAMPLES:
291 lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
292 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
293 break;
294 case TIME_MS:
295 lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
296 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
297 break;
298 case TIME_SMPTE:
299 position = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
300 lpTime->u.smpte.sec = position / format->wf.nSamplesPerSec;
301 position -= lpTime->u.smpte.sec * format->wf.nSamplesPerSec;
302 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
303 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
304 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
305 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
306 lpTime->u.smpte.fps = 30;
307 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->wf.nSamplesPerSec;
308 position -= lpTime->u.smpte.frame * format->wf.nSamplesPerSec / lpTime->u.smpte.fps;
309 if (position != 0)
311 /* Round up */
312 lpTime->u.smpte.frame++;
314 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
315 lpTime->u.smpte.hour, lpTime->u.smpte.min,
316 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
317 break;
318 default:
319 FIXME("Format %d not supported ! use TIME_BYTES !\n", lpTime->wType);
320 lpTime->wType = TIME_BYTES;
321 /* fall through */
322 case TIME_BYTES:
323 lpTime->u.cb = position;
324 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
325 break;
327 return MMSYSERR_NOERROR;
330 /*======================================================================*
331 * Low level WAVE implementation *
332 *======================================================================*/
334 /* Volume functions derived from Alsaplayer source */
335 /* length is the number of 16 bit samples */
336 void volume_effect16(void *bufin, void* bufout, int length, int left,
337 int right, int nChannels)
339 short *d_out = (short *)bufout;
340 short *d_in = (short *)bufin;
341 int i, v;
344 TRACE("length == %d, nChannels == %d\n", length, nChannels);
347 if (right == -1) right = left;
349 for(i = 0; i < length; i+=(nChannels))
351 v = (int) ((*(d_in++) * left) / 100);
352 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
353 if(nChannels == 2)
355 v = (int) ((*(d_in++) * right) / 100);
356 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
361 /* length is the number of 8 bit samples */
362 void volume_effect8(void *bufin, void* bufout, int length, int left,
363 int right, int nChannels)
365 char *d_out = (char *)bufout;
366 char *d_in = (char *)bufin;
367 int i, v;
370 TRACE("length == %d, nChannels == %d\n", length, nChannels);
373 if (right == -1) right = left;
375 for(i = 0; i < length; i+=(nChannels))
377 v = (char) ((*(d_in++) * left) / 100);
378 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
379 if(nChannels == 2)
381 v = (char) ((*(d_in++) * right) / 100);
382 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
387 /******************************************************************
388 * NAS_CloseDevice
391 void NAS_CloseDevice(WINE_WAVEOUT* wwo)
393 TRACE("NAS_CloseDevice\n");
394 nas_close(wwo);
397 /******************************************************************
398 * NAS_WaveClose
400 LONG NAS_WaveClose(void)
402 nas_end(); /* free up nas server */
403 return 1;
406 /******************************************************************
407 * NAS_WaveInit
409 * Initialize internal structures from NAS server info
411 LONG NAS_WaveInit(void)
413 int i;
414 nas_init();
416 /* initialize all device handles to -1 */
417 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
419 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out caps values */
421 WOutDev[i].AuServ = AuServ;
422 WOutDev[i].AuDev = AuNone;
423 WOutDev[i].Id = i;
424 /* FIXME: some programs compare this string against the content of the registry
425 * for MM drivers. The names have to match in order for the program to work
426 * (e.g. MS win9x mplayer.exe)
428 #ifdef EMULATE_SB16
429 WOutDev[i].caps.wMid = 0x0002;
430 WOutDev[i].caps.wPid = 0x0104;
431 strcpy(WOutDev[i].caps.szPname, "SB16 Wave Out");
432 #else
433 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
434 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
435 /* strcpy(WOutDev[i].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
436 strcpy(WOutDev[i].caps.szPname, "CS4236/37/38");
437 #endif
438 WOutDev[i].AuFlow = 0;
439 WOutDev[i].caps.vDriverVersion = 0x0100;
440 WOutDev[i].caps.dwFormats = 0x00000000;
441 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
443 WOutDev[i].caps.wChannels = 2;
444 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
446 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
447 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
448 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
449 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
450 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
451 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
452 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
453 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
454 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
455 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
456 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
457 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
461 return 0;
464 /******************************************************************
465 * NAS_InitRingMessage
467 * Initialize the ring of messages for passing between driver's caller and playback/record
468 * thread
470 static int NAS_InitRingMessage(MSG_RING* mr)
472 mr->msg_toget = 0;
473 mr->msg_tosave = 0;
474 mr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
475 mr->ring_buffer_size = NAS_RING_BUFFER_INCREMENT;
476 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
477 InitializeCriticalSection(&mr->msg_crst);
478 return 0;
481 /******************************************************************
482 * NAS_DestroyRingMessage
485 static int NAS_DestroyRingMessage(MSG_RING* mr)
487 CloseHandle(mr->msg_event);
488 HeapFree(GetProcessHeap(),0,mr->messages);
489 DeleteCriticalSection(&mr->msg_crst);
490 return 0;
493 /******************************************************************
494 * NAS_AddRingMessage
496 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
498 static int NAS_AddRingMessage(MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
500 HANDLE hEvent = INVALID_HANDLE_VALUE;
502 EnterCriticalSection(&mr->msg_crst);
503 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
505 int old_ring_buffer_size = mr->ring_buffer_size;
506 mr->ring_buffer_size += NAS_RING_BUFFER_INCREMENT;
507 TRACE("omr->ring_buffer_size=%d\n",mr->ring_buffer_size);
508 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
509 /* Now we need to rearrange the ring buffer so that the new
510 buffers just allocated are in between mr->msg_tosave and
511 mr->msg_toget.
513 if (mr->msg_tosave < mr->msg_toget)
515 memmove(&(mr->messages[mr->msg_toget + NAS_RING_BUFFER_INCREMENT]),
516 &(mr->messages[mr->msg_toget]),
517 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
519 mr->msg_toget += NAS_RING_BUFFER_INCREMENT;
522 if (wait)
524 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
525 if (hEvent == INVALID_HANDLE_VALUE)
527 ERR("can't create event !?\n");
528 LeaveCriticalSection(&mr->msg_crst);
529 return 0;
531 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
532 FIXME("two fast messages in the queue!!!!\n");
534 /* fast messages have to be added at the start of the queue */
535 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
537 mr->messages[mr->msg_toget].msg = msg;
538 mr->messages[mr->msg_toget].param = param;
539 mr->messages[mr->msg_toget].hEvent = hEvent;
541 else
543 mr->messages[mr->msg_tosave].msg = msg;
544 mr->messages[mr->msg_tosave].param = param;
545 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
546 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
549 LeaveCriticalSection(&mr->msg_crst);
551 SetEvent(mr->msg_event); /* signal a new message */
553 if (wait)
555 /* wait for playback/record thread to have processed the message */
556 WaitForSingleObject(hEvent, INFINITE);
557 CloseHandle(hEvent);
560 return 1;
563 /******************************************************************
564 * NAS_RetrieveRingMessage
566 * Get a message from the ring. Should be called by the playback/record thread.
568 static int NAS_RetrieveRingMessage(MSG_RING* mr,
569 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
571 EnterCriticalSection(&mr->msg_crst);
573 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
575 LeaveCriticalSection(&mr->msg_crst);
576 return 0;
579 *msg = mr->messages[mr->msg_toget].msg;
580 mr->messages[mr->msg_toget].msg = 0;
581 *param = mr->messages[mr->msg_toget].param;
582 *hEvent = mr->messages[mr->msg_toget].hEvent;
583 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
584 LeaveCriticalSection(&mr->msg_crst);
585 return 1;
588 /*======================================================================*
589 * Low level WAVE OUT implementation *
590 *======================================================================*/
592 /**************************************************************************
593 * wodNotifyClient [internal]
595 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
597 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
599 switch (wMsg) {
600 case WOM_OPEN:
601 case WOM_CLOSE:
602 case WOM_DONE:
603 if (wwo->wFlags != DCB_NULL &&
604 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
605 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
606 WARN("can't notify client !\n");
607 return MMSYSERR_ERROR;
609 break;
610 default:
611 FIXME("Unknown callback message %u\n", wMsg);
612 return MMSYSERR_INVALPARAM;
614 return MMSYSERR_NOERROR;
617 /**************************************************************************
618 * wodUpdatePlayedTotal [internal]
621 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
623 wwo->PlayedTotal = wwo->WrittenTotal;
624 return TRUE;
627 /**************************************************************************
628 * wodPlayer_BeginWaveHdr [internal]
630 * Makes the specified lpWaveHdr the currently playing wave header.
631 * If the specified wave header is a begin loop and we're not already in
632 * a loop, setup the loop.
634 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
636 wwo->lpPlayPtr = lpWaveHdr;
638 if (!lpWaveHdr) return;
640 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
641 if (wwo->lpLoopPtr) {
642 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
643 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
644 } else {
645 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
646 wwo->lpLoopPtr = lpWaveHdr;
647 /* Windows does not touch WAVEHDR.dwLoops,
648 * so we need to make an internal copy */
649 wwo->dwLoops = lpWaveHdr->dwLoops;
654 /**************************************************************************
655 * wodPlayer_PlayPtrNext [internal]
657 * Advance the play pointer to the next waveheader, looping if required.
659 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
661 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
663 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
664 /* We're at the end of a loop, loop if required */
665 if (--wwo->dwLoops > 0) {
666 wwo->lpPlayPtr = wwo->lpLoopPtr;
667 } else {
668 /* Handle overlapping loops correctly */
669 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
670 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
671 /* shall we consider the END flag for the closing loop or for
672 * the opening one or for both ???
673 * code assumes for closing loop only
675 } else {
676 lpWaveHdr = lpWaveHdr->lpNext;
678 wwo->lpLoopPtr = NULL;
679 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
681 } else {
682 /* We're not in a loop. Advance to the next wave header */
683 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
685 return lpWaveHdr;
688 /**************************************************************************
689 * wodPlayer_NotifyCompletions [internal]
691 * Notifies and remove from queue all wavehdrs which have been played to
692 * the speaker (ie. they have cleared the audio device). If force is true,
693 * we notify all wavehdrs and remove them all from the queue even if they
694 * are unplayed or part of a loop.
696 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
698 LPWAVEHDR lpWaveHdr;
700 /* Start from lpQueuePtr and keep notifying until:
701 * - we hit an unwritten wavehdr
702 * - we hit the beginning of a running loop
703 * - we hit a wavehdr which hasn't finished playing
705 wodUpdatePlayedTotal(wwo);
707 while ((lpWaveHdr = wwo->lpQueuePtr) && (force || (lpWaveHdr != wwo->lpPlayPtr &&
708 lpWaveHdr != wwo->lpLoopPtr && lpWaveHdr->reserved <= wwo->PlayedTotal))) {
710 wwo->lpQueuePtr = lpWaveHdr->lpNext;
712 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
713 lpWaveHdr->dwFlags |= WHDR_DONE;
715 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
717 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
718 1 : 1;
721 /**************************************************************************
722 * wodPlayer_Reset [internal]
724 * wodPlayer helper. Resets current output stream.
726 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
728 wodUpdatePlayedTotal(wwo);
729 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
731 /* we aren't able to flush any data that has already been written */
732 /* to nas, otherwise we would do the flushing here */
734 nas_free(wwo);
736 if (reset) {
737 enum win_wm_message msg;
738 DWORD param;
739 HANDLE ev;
741 /* remove any buffer */
742 wodPlayer_NotifyCompletions(wwo, TRUE);
744 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
745 wwo->state = WINE_WS_STOPPED;
746 wwo->PlayedTotal = wwo->WrittenTotal = 0;
748 /* remove any existing message in the ring */
749 EnterCriticalSection(&wwo->msgRing.msg_crst);
751 /* return all pending headers in queue */
752 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
754 TRACE("flushing msg\n");
755 if (msg != WINE_WM_HEADER)
757 FIXME("shouldn't have headers left\n");
758 SetEvent(ev);
759 continue;
761 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
762 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
764 wodNotifyClient(wwo, WOM_DONE, param, 0);
766 ResetEvent(wwo->msgRing.msg_event);
767 LeaveCriticalSection(&wwo->msgRing.msg_crst);
768 } else {
769 if (wwo->lpLoopPtr) {
770 /* complicated case, not handled yet (could imply modifying the loop counter */
771 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
772 wwo->lpPlayPtr = wwo->lpLoopPtr;
773 wwo->WrittenTotal = wwo->PlayedTotal; /* this is wrong !!! */
774 } else {
775 /* the data already written is going to be played, so take */
776 /* this fact into account here */
777 wwo->PlayedTotal = wwo->WrittenTotal;
779 wwo->state = WINE_WS_PAUSED;
783 /**************************************************************************
784 * wodPlayer_ProcessMessages [internal]
786 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
788 LPWAVEHDR lpWaveHdr;
789 enum win_wm_message msg;
790 DWORD param;
791 HANDLE ev;
793 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
794 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
795 switch (msg) {
796 case WINE_WM_PAUSING:
797 wodPlayer_Reset(wwo, FALSE);
798 SetEvent(ev);
799 break;
800 case WINE_WM_RESTARTING:
801 wwo->state = WINE_WS_PLAYING;
802 SetEvent(ev);
803 break;
804 case WINE_WM_HEADER:
805 lpWaveHdr = (LPWAVEHDR)param;
807 /* insert buffer at the end of queue */
809 LPWAVEHDR* wh;
810 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
811 *wh = lpWaveHdr;
813 if (!wwo->lpPlayPtr)
814 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
815 if (wwo->state == WINE_WS_STOPPED)
816 wwo->state = WINE_WS_PLAYING;
817 break;
818 case WINE_WM_RESETTING:
819 wodPlayer_Reset(wwo, TRUE);
820 SetEvent(ev);
821 break;
822 case WINE_WM_UPDATE:
823 wodUpdatePlayedTotal(wwo);
824 SetEvent(ev);
825 break;
826 case WINE_WM_BREAKLOOP:
827 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
828 /* ensure exit at end of current loop */
829 wwo->dwLoops = 1;
831 SetEvent(ev);
832 break;
833 case WINE_WM_CLOSING:
834 /* sanity check: this should not happen since the device must have been reset before */
835 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
836 wwo->hThread = 0;
837 wwo->state = WINE_WS_CLOSED;
838 SetEvent(ev);
839 ExitThread(0);
840 /* shouldn't go here */
841 default:
842 FIXME("unknown message %d\n", msg);
843 break;
848 /**************************************************************************
849 * wodPlayer [internal]
851 static DWORD CALLBACK wodPlayer(LPVOID pmt)
853 WORD uDevID = (DWORD)pmt;
854 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
856 wwo->state = WINE_WS_STOPPED;
857 SetEvent(wwo->hStartUpEvent);
859 for (;;) {
861 if (wwo->FlowStarted) {
862 AuHandleEvents(wwo->AuServ);
864 if (wwo->state == WINE_WS_PLAYING && wwo->freeBytes && wwo->BufferUsed)
865 nas_send_buffer(wwo);
868 if (wwo->BufferUsed <= FRAG_SIZE && wwo->writeBytes > 0)
869 wodPlayer_NotifyCompletions(wwo, FALSE);
871 WaitForSingleObject(wwo->msgRing.msg_event, 20);
872 wodPlayer_ProcessMessages(wwo);
874 while(wwo->lpPlayPtr) {
875 wwo->lpPlayPtr->reserved = wwo->WrittenTotal + wwo->lpPlayPtr->dwBufferLength;
876 nas_add_buffer(wwo);
877 wodPlayer_PlayPtrNext(wwo);
883 /**************************************************************************
884 * wodGetDevCaps [internal]
886 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
888 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
890 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
892 if (wDevID >= MAX_WAVEOUTDRV) {
893 TRACE("MAX_WAVOUTDRV reached !\n");
894 return MMSYSERR_BADDEVICEID;
897 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
898 return MMSYSERR_NOERROR;
901 /**************************************************************************
902 * wodOpen [internal]
904 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
906 WINE_WAVEOUT* wwo;
908 TRACE("wodOpen (%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
910 if (lpDesc == NULL) {
911 WARN("Invalid Parameter !\n");
912 return MMSYSERR_INVALPARAM;
914 if (wDevID >= MAX_WAVEOUTDRV) {
915 TRACE("MAX_WAVOUTDRV reached !\n");
916 return MMSYSERR_BADDEVICEID;
919 /* if this device is already open tell the app that it is allocated */
921 wwo = &WOutDev[wDevID];
923 if(wwo->open)
925 TRACE("device already allocated\n");
926 return MMSYSERR_ALLOCATED;
930 /* only PCM format is supported so far... */
931 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
932 lpDesc->lpFormat->nChannels == 0 ||
933 lpDesc->lpFormat->nSamplesPerSec == 0 ||
934 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
935 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
936 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
937 lpDesc->lpFormat->nSamplesPerSec);
938 return WAVERR_BADFORMAT;
941 if (dwFlags & WAVE_FORMAT_QUERY) {
942 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
943 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
944 lpDesc->lpFormat->nSamplesPerSec);
945 return MMSYSERR_NOERROR;
948 /* direct sound not supported, ignore the flag */
949 dwFlags &= ~WAVE_DIRECTSOUND;
951 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
953 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
954 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
956 if (wwo->format.wBitsPerSample == 0) {
957 WARN("Resetting zeroed wBitsPerSample\n");
958 wwo->format.wBitsPerSample = 8 *
959 (wwo->format.wf.nAvgBytesPerSec /
960 wwo->format.wf.nSamplesPerSec) /
961 wwo->format.wf.nChannels;
964 if (!nas_open(wwo))
965 return MMSYSERR_ALLOCATED;
967 NAS_InitRingMessage(&wwo->msgRing);
969 /* create player thread */
970 if (!(dwFlags & WAVE_DIRECTSOUND)) {
971 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
972 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
973 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
974 CloseHandle(wwo->hStartUpEvent);
975 } else {
976 wwo->hThread = INVALID_HANDLE_VALUE;
977 wwo->dwThreadID = 0;
979 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
981 TRACE("stream=0x%lx, BufferSize=%ld\n", (long)wwo->AuServ, wwo->BufferSize);
983 TRACE("wBitsPerSample=%u nAvgBytesPerSec=%lu nSamplesPerSec=%lu nChannels=%u nBlockAlign=%u\n",
984 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
985 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
986 wwo->format.wf.nBlockAlign);
988 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
991 /**************************************************************************
992 * wodClose [internal]
994 static DWORD wodClose(WORD wDevID)
996 DWORD ret = MMSYSERR_NOERROR;
997 WINE_WAVEOUT* wwo;
999 TRACE("(%u);\n", wDevID);
1001 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1003 WARN("bad device ID !\n");
1004 return MMSYSERR_BADDEVICEID;
1007 wwo = &WOutDev[wDevID];
1008 if (wwo->lpQueuePtr) {
1009 WARN("buffers still playing !\n");
1010 ret = WAVERR_STILLPLAYING;
1011 } else {
1012 TRACE("imhere[3-close]\n");
1013 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1014 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1017 NAS_DestroyRingMessage(&wwo->msgRing);
1019 NAS_CloseDevice(wwo); /* close the stream and clean things up */
1021 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1023 return ret;
1026 /**************************************************************************
1027 * wodWrite [internal]
1030 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1032 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1034 /* first, do the sanity checks... */
1035 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1037 WARN("bad dev ID !\n");
1038 return MMSYSERR_BADDEVICEID;
1041 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1043 TRACE("unprepared\n");
1044 return WAVERR_UNPREPARED;
1047 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1049 TRACE("still playing\n");
1050 return WAVERR_STILLPLAYING;
1053 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1054 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1055 lpWaveHdr->lpNext = 0;
1057 TRACE("adding ring message\n");
1058 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1060 return MMSYSERR_NOERROR;
1063 /**************************************************************************
1064 * wodPrepare [internal]
1066 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1068 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1070 if (wDevID >= MAX_WAVEOUTDRV) {
1071 WARN("bad device ID !\n");
1072 return MMSYSERR_BADDEVICEID;
1075 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1076 return WAVERR_STILLPLAYING;
1078 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1079 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1080 return MMSYSERR_NOERROR;
1083 /**************************************************************************
1084 * wodUnprepare [internal]
1086 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1088 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1090 if (wDevID >= MAX_WAVEOUTDRV) {
1091 WARN("bad device ID !\n");
1092 return MMSYSERR_BADDEVICEID;
1095 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1096 return WAVERR_STILLPLAYING;
1098 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1099 lpWaveHdr->dwFlags |= WHDR_DONE;
1101 return MMSYSERR_NOERROR;
1104 /**************************************************************************
1105 * wodPause [internal]
1107 static DWORD wodPause(WORD wDevID)
1109 TRACE("(%u);!\n", wDevID);
1111 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1113 WARN("bad device ID !\n");
1114 return MMSYSERR_BADDEVICEID;
1117 TRACE("imhere[3-PAUSING]\n");
1118 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1120 return MMSYSERR_NOERROR;
1123 /**************************************************************************
1124 * wodRestart [internal]
1126 static DWORD wodRestart(WORD wDevID)
1128 TRACE("(%u);\n", wDevID);
1130 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1132 WARN("bad device ID !\n");
1133 return MMSYSERR_BADDEVICEID;
1136 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1137 TRACE("imhere[3-RESTARTING]\n");
1138 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1141 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1142 /* FIXME: Myst crashes with this ... hmm -MM
1143 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1146 return MMSYSERR_NOERROR;
1149 /**************************************************************************
1150 * wodReset [internal]
1152 static DWORD wodReset(WORD wDevID)
1154 TRACE("(%u);\n", wDevID);
1156 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1158 WARN("bad device ID !\n");
1159 return MMSYSERR_BADDEVICEID;
1162 TRACE("imhere[3-RESET]\n");
1163 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1165 return MMSYSERR_NOERROR;
1168 /**************************************************************************
1169 * wodGetPosition [internal]
1171 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1173 WINE_WAVEOUT* wwo;
1175 TRACE("%u, %p, %lu);\n", wDevID, lpTime, uSize);
1177 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1179 WARN("bad device ID !\n");
1180 return MMSYSERR_BADDEVICEID;
1183 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1185 wwo = &WOutDev[wDevID];
1186 #if 0
1187 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1188 #endif
1190 return bytes_to_mmtime(lpTime, wwo->WrittenTotal, &wwo->format);
1193 /**************************************************************************
1194 * wodBreakLoop [internal]
1196 static DWORD wodBreakLoop(WORD wDevID)
1198 TRACE("(%u);\n", wDevID);
1200 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1202 WARN("bad device ID !\n");
1203 return MMSYSERR_BADDEVICEID;
1205 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1206 return MMSYSERR_NOERROR;
1209 /**************************************************************************
1210 * wodGetVolume [internal]
1212 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1214 DWORD left, right;
1216 left = WOutDev[wDevID].volume_left;
1217 right = WOutDev[wDevID].volume_right;
1219 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1221 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1223 return MMSYSERR_NOERROR;
1226 /**************************************************************************
1227 * wodSetVolume [internal]
1229 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1231 DWORD left, right;
1233 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1234 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1236 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1238 WOutDev[wDevID].volume_left = left;
1239 WOutDev[wDevID].volume_right = right;
1241 return MMSYSERR_NOERROR;
1244 /**************************************************************************
1245 * wodGetNumDevs [internal]
1247 static DWORD wodGetNumDevs(void)
1249 return MAX_WAVEOUTDRV;
1252 /**************************************************************************
1253 * wodMessage (WINENAS.@)
1255 DWORD WINAPI NAS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1256 DWORD dwParam1, DWORD dwParam2)
1258 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1260 switch (wMsg) {
1261 case DRVM_INIT:
1262 case DRVM_EXIT:
1263 case DRVM_ENABLE:
1264 case DRVM_DISABLE:
1265 /* FIXME: Pretend this is supported */
1266 return 0;
1267 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1268 case WODM_CLOSE: return wodClose (wDevID);
1269 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1270 case WODM_PAUSE: return wodPause (wDevID);
1271 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1272 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1273 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1274 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1275 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1276 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1277 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1278 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1279 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1280 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1281 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1282 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1283 case WODM_RESTART: return wodRestart (wDevID);
1284 case WODM_RESET: return wodReset (wDevID);
1286 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1287 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1288 default:
1289 FIXME("unknown message %d!\n", wMsg);
1291 return MMSYSERR_NOTSUPPORTED;
1294 /*======================================================================*
1295 * Low level DSOUND implementation *
1296 *======================================================================*/
1297 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1299 /* we can't perform memory mapping as we don't have a file stream
1300 interface with nas like we do with oss */
1301 MESSAGE("This sound card s driver does not support direct access\n");
1302 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1303 return MMSYSERR_NOTSUPPORTED;
1306 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1308 memset(desc, 0, sizeof(*desc));
1309 strcpy(desc->szDesc, "Wine NAS DirectSound Driver");
1310 strcpy(desc->szDrvName, "winenas.drv");
1311 return MMSYSERR_NOERROR;
1314 static int nas_init(void) {
1315 TRACE("NAS INIT\n");
1316 if (!(AuServ = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL)))
1317 return 0;
1319 return 1;
1322 static int nas_finddev(WINE_WAVEOUT* wwo) {
1323 int i;
1325 for (i = 0; i < AuServerNumDevices(wwo->AuServ); i++) {
1326 if ((AuDeviceKind(AuServerDevice(wwo->AuServ, i)) ==
1327 AuComponentKindPhysicalOutput) &&
1328 AuDeviceNumTracks(AuServerDevice(wwo->AuServ, i)) == wwo->format.wf.nChannels)
1330 wwo->AuDev = AuDeviceIdentifier(AuServerDevice(wwo->AuServ, i));
1331 break;
1335 if (wwo->AuDev == AuNone)
1336 return 0;
1337 return 1;
1340 static int nas_open(WINE_WAVEOUT* wwo) {
1341 AuElement elements[3];
1343 if (!wwo->AuServ)
1344 return 0;
1346 if (!nas_finddev(wwo))
1347 return 0;
1349 if (!(wwo->AuFlow = AuCreateFlow(wwo->AuServ, NULL)))
1350 return 0;
1352 wwo->BufferSize = FRAG_SIZE * FRAG_COUNT;
1354 AuMakeElementImportClient(&elements[0], wwo->format.wf.nSamplesPerSec,
1355 wwo->format.wBitsPerSample == 16 ? AuFormatLinearSigned16LSB : AuFormatLinearUnsigned8,
1356 wwo->format.wf.nChannels, AuTrue, wwo->BufferSize, wwo->BufferSize / 2, 0, NULL);
1358 AuMakeElementExportDevice(&elements[1], 0, wwo->AuDev, wwo->format.wf.nSamplesPerSec,
1359 AuUnlimitedSamples, 0, NULL);
1361 AuSetElements(wwo->AuServ, wwo->AuFlow, AuTrue, 2, elements, NULL);
1363 AuRegisterEventHandler(wwo->AuServ, AuEventHandlerIDMask, 0, wwo->AuFlow,
1364 event_handler, (AuPointer) wwo);
1367 wwo->PlayedTotal = 0;
1368 wwo->WrittenTotal = 0;
1369 wwo->open = 1;
1371 wwo->BufferUsed = 0;
1372 wwo->writeBytes = 0;
1373 wwo->freeBytes = 0;
1374 wwo->sendBytes = 0;
1375 wwo->SoundBuffer = NULL;
1376 wwo->FlowStarted = 0;
1378 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1379 AuPauseFlow(wwo->AuServ, wwo->AuFlow, NULL);
1380 wwo->FlowStarted = 1;
1382 return 1;
1385 static AuBool
1386 event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
1388 WINE_WAVEOUT *wwo = (WINE_WAVEOUT *)hnd->data;
1389 switch (ev->type) {
1391 case AuEventTypeElementNotify: {
1392 AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
1395 switch (event->kind) {
1396 case AuElementNotifyKindLowWater:
1397 wwo->freeBytes += event->num_bytes;
1398 if (wwo->writeBytes > 0)
1399 wwo->sendBytes += event->num_bytes;
1400 if (wwo->freeBytes && wwo->BufferUsed)
1401 nas_send_buffer(wwo);
1402 break;
1404 case AuElementNotifyKindState:
1405 TRACE("ev: kind %s state %s->%s reason %s numbytes %ld freeB %lu\n",
1406 nas_elementnotify_kind(event->kind),
1407 nas_state(event->prev_state),
1408 nas_state(event->cur_state),
1409 nas_reason(event->reason),
1410 event->num_bytes, wwo->freeBytes);
1412 if (event->cur_state == AuStatePause && event->reason != AuReasonUser) {
1413 wwo->freeBytes += event->num_bytes;
1414 if (wwo->writeBytes > 0)
1415 wwo->sendBytes += event->num_bytes;
1416 if (wwo->sendBytes > wwo->writeBytes)
1417 wwo->sendBytes = wwo->writeBytes;
1418 if (wwo->freeBytes && wwo->BufferUsed)
1419 nas_send_buffer(wwo);
1421 break;
1425 return AuTrue;
1428 static void
1429 buffer_resize(WINE_WAVEOUT* wwo, int len)
1431 void *newbuf = malloc(wwo->BufferUsed + len);
1432 void *oldbuf = wwo->SoundBuffer;
1433 memcpy(newbuf, oldbuf, wwo->BufferUsed);
1434 wwo->SoundBuffer = newbuf;
1435 if (oldbuf != NULL)
1436 free(oldbuf);
1439 static int nas_add_buffer(WINE_WAVEOUT* wwo) {
1440 int len = wwo->lpPlayPtr->dwBufferLength;
1442 buffer_resize(wwo, len);
1443 memcpy(wwo->SoundBuffer + wwo->BufferUsed, wwo->lpPlayPtr->lpData, len);
1444 wwo->BufferUsed += len;
1445 wwo->WrittenTotal += len;
1446 return len;
1449 static int nas_send_buffer(WINE_WAVEOUT* wwo) {
1450 int oldb , len;
1451 char *ptr, *newdata;
1452 newdata = NULL;
1453 oldb = len = 0;
1455 if (wwo->freeBytes <= 0)
1456 return 0;
1458 if (wwo->SoundBuffer == NULL || wwo->BufferUsed == 0) {
1459 return 0;
1462 if (wwo->BufferUsed <= wwo->freeBytes) {
1463 len = wwo->BufferUsed;
1464 ptr = wwo->SoundBuffer;
1465 } else {
1466 len = wwo->freeBytes;
1467 ptr = malloc(len);
1468 memcpy(ptr,wwo->SoundBuffer,len);
1469 newdata = malloc(wwo->BufferUsed - len);
1470 memcpy(newdata, wwo->SoundBuffer + len, wwo->BufferUsed - len);
1473 TRACE("envoye de %d bytes / %lu bytes / freeBytes %lu\n", len, wwo->BufferUsed, wwo->freeBytes);
1475 AuWriteElement(wwo->AuServ, wwo->AuFlow, 0, len, ptr, AuFalse, NULL);
1477 wwo->BufferUsed -= len;
1478 wwo->freeBytes -= len;
1479 wwo->writeBytes += len;
1481 free(ptr);
1483 wwo->SoundBuffer = NULL;
1485 if (newdata != NULL)
1486 wwo->SoundBuffer = newdata;
1488 return len;
1491 static int nas_free(WINE_WAVEOUT* wwo)
1494 if (!wwo->FlowStarted && wwo->BufferUsed) {
1495 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1496 wwo->FlowStarted = 1;
1499 while (wwo->BufferUsed || wwo->writeBytes != wwo->sendBytes) {
1500 if (wwo->freeBytes)
1501 nas_send_buffer(wwo);
1502 AuHandleEvents(wwo->AuServ);
1505 AuFlush(wwo->AuServ);
1506 return TRUE;
1509 static int nas_close(WINE_WAVEOUT* wwo)
1511 AuEvent ev;
1513 nas_free(wwo);
1515 AuStopFlow(wwo->AuServ, wwo->AuFlow, NULL);
1516 AuDestroyFlow(wwo->AuServ, wwo->AuFlow, NULL);
1517 AuFlush(wwo->AuServ);
1518 AuNextEvent(wwo->AuServ, AuTrue, &ev);
1519 AuDispatchEvent(wwo->AuServ, &ev);
1521 wwo->AuFlow = 0;
1522 wwo->open = 0;
1523 wwo->BufferUsed = 0;
1524 wwo->freeBytes = 0;
1525 wwo->SoundBuffer = NULL;
1526 return 1;
1529 static int nas_end(void)
1531 AuCloseServer(AuServ);
1532 AuServ = 0;
1533 return 1;
1536 #else /* !HAVE_NAS */
1538 /**************************************************************************
1539 * wodMessage (WINENAS.@)
1541 DWORD WINAPI NAS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
1543 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1544 return MMSYSERR_NOTENABLED;
1546 #endif