Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / winmm / winenas / audio.c
blob303d5e6f342d13eff68322a98b4d7d88ed8e11e5
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);
192 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
195 /* NASFUNC */
196 static AuBool event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd);
197 static int nas_init(void);
198 static int nas_end(void);
200 static int nas_finddev(WINE_WAVEOUT* wwo);
201 static int nas_open(WINE_WAVEOUT* wwo);
202 static int nas_free(WINE_WAVEOUT* wwo);
203 static int nas_close(WINE_WAVEOUT* wwo);
204 static void buffer_resize(WINE_WAVEOUT* wwo, int len);
205 static int nas_add_buffer(WINE_WAVEOUT* wwo);
206 static int nas_send_buffer(WINE_WAVEOUT* wwo);
208 /* These strings used only for tracing */
209 static const char *wodPlayerCmdString[] = {
210 "WINE_WM_PAUSING",
211 "WINE_WM_RESTARTING",
212 "WINE_WM_RESETTING",
213 "WINE_WM_HEADER",
214 "WINE_WM_UPDATE",
215 "WINE_WM_BREAKLOOP",
216 "WINE_WM_CLOSING",
219 static char *nas_elementnotify_kinds[] = {
220 "LowWater",
221 "HighWater",
222 "State",
223 "Unknown"
226 static char *nas_states[] = {
227 "Stop",
228 "Start",
229 "Pause",
230 "Any"
233 static char *nas_reasons[] = {
234 "User",
235 "Underrun",
236 "Overrun",
237 "EOF",
238 "Watermark",
239 "Hardware",
240 "Any"
243 static char* nas_reason(unsigned int reason)
245 if (reason > 6) reason = 6;
246 return nas_reasons[reason];
249 static char* nas_elementnotify_kind(unsigned int kind)
251 if (kind > 2) kind = 3;
252 return nas_elementnotify_kinds[kind];
256 #if 0
257 static const char* nas_event_type(unsigned int type)
259 static const char * const nas_event_types[] =
261 "Undefined",
262 "Undefined",
263 "ElementNotify",
264 "GrabNotify",
265 "MonitorNotify",
266 "BucketNotify",
267 "DeviceNotify"
270 if (type > 6) type = 0;
271 return nas_event_types[type];
273 #endif
276 static char* nas_state(unsigned int state)
278 if (state > 3) state = 3;
279 return nas_states[state];
282 /*======================================================================*
283 * Low level WAVE implementation *
284 *======================================================================*/
286 /* Volume functions derived from Alsaplayer source */
287 /* length is the number of 16 bit samples */
288 void volume_effect16(void *bufin, void* bufout, int length, int left,
289 int right, int nChannels)
291 short *d_out = (short *)bufout;
292 short *d_in = (short *)bufin;
293 int i, v;
296 TRACE("length == %d, nChannels == %d\n", length, nChannels);
299 if (right == -1) right = left;
301 for(i = 0; i < length; i+=(nChannels))
303 v = (int) ((*(d_in++) * left) / 100);
304 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
305 if(nChannels == 2)
307 v = (int) ((*(d_in++) * right) / 100);
308 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
313 /* length is the number of 8 bit samples */
314 void volume_effect8(void *bufin, void* bufout, int length, int left,
315 int right, int nChannels)
317 char *d_out = (char *)bufout;
318 char *d_in = (char *)bufin;
319 int i, v;
322 TRACE("length == %d, nChannels == %d\n", length, nChannels);
325 if (right == -1) right = left;
327 for(i = 0; i < length; i+=(nChannels))
329 v = (char) ((*(d_in++) * left) / 100);
330 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
331 if(nChannels == 2)
333 v = (char) ((*(d_in++) * right) / 100);
334 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
339 /******************************************************************
340 * NAS_CloseDevice
343 void NAS_CloseDevice(WINE_WAVEOUT* wwo)
345 TRACE("NAS_CloseDevice\n");
346 nas_close(wwo);
349 /******************************************************************
350 * NAS_WaveClose
352 LONG NAS_WaveClose(void)
354 nas_end(); /* free up nas server */
355 return 1;
358 /******************************************************************
359 * NAS_WaveInit
361 * Initialize internal structures from NAS server info
363 LONG NAS_WaveInit(void)
365 int i;
366 nas_init();
368 /* initialize all device handles to -1 */
369 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
371 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out caps values */
373 WOutDev[i].AuServ = AuServ;
374 WOutDev[i].AuDev = AuNone;
375 WOutDev[i].Id = i;
376 /* FIXME: some programs compare this string against the content of the registry
377 * for MM drivers. The names have to match in order for the program to work
378 * (e.g. MS win9x mplayer.exe)
380 #ifdef EMULATE_SB16
381 WOutDev[i].caps.wMid = 0x0002;
382 WOutDev[i].caps.wPid = 0x0104;
383 strcpy(WOutDev[i].caps.szPname, "SB16 Wave Out");
384 #else
385 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
386 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
387 /* strcpy(WOutDev[i].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
388 strcpy(WOutDev[i].caps.szPname, "CS4236/37/38");
389 #endif
390 WOutDev[i].AuFlow = 0;
391 WOutDev[i].caps.vDriverVersion = 0x0100;
392 WOutDev[i].caps.dwFormats = 0x00000000;
393 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
395 WOutDev[i].caps.wChannels = 2;
396 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
398 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
399 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
400 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
401 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
402 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
403 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
404 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
405 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
406 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
407 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
408 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
409 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
413 return 0;
416 /******************************************************************
417 * NAS_InitRingMessage
419 * Initialize the ring of messages for passing between driver's caller and playback/record
420 * thread
422 static int NAS_InitRingMessage(MSG_RING* mr)
424 mr->msg_toget = 0;
425 mr->msg_tosave = 0;
426 mr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
427 mr->ring_buffer_size = NAS_RING_BUFFER_INCREMENT;
428 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
429 InitializeCriticalSection(&mr->msg_crst);
430 return 0;
433 /******************************************************************
434 * NAS_DestroyRingMessage
437 static int NAS_DestroyRingMessage(MSG_RING* mr)
439 CloseHandle(mr->msg_event);
440 HeapFree(GetProcessHeap(),0,mr->messages);
441 DeleteCriticalSection(&mr->msg_crst);
442 return 0;
445 /******************************************************************
446 * NAS_AddRingMessage
448 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
450 static int NAS_AddRingMessage(MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
452 HANDLE hEvent = INVALID_HANDLE_VALUE;
454 EnterCriticalSection(&mr->msg_crst);
455 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
457 mr->ring_buffer_size += NAS_RING_BUFFER_INCREMENT;
458 TRACE("omr->ring_buffer_size=%d\n",mr->ring_buffer_size);
459 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
461 if (wait)
463 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
464 if (hEvent == INVALID_HANDLE_VALUE)
466 ERR("can't create event !?\n");
467 LeaveCriticalSection(&mr->msg_crst);
468 return 0;
470 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
471 FIXME("two fast messages in the queue!!!!\n");
473 /* fast messages have to be added at the start of the queue */
474 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
476 mr->messages[mr->msg_toget].msg = msg;
477 mr->messages[mr->msg_toget].param = param;
478 mr->messages[mr->msg_toget].hEvent = hEvent;
480 else
482 mr->messages[mr->msg_tosave].msg = msg;
483 mr->messages[mr->msg_tosave].param = param;
484 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
485 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
488 LeaveCriticalSection(&mr->msg_crst);
490 SetEvent(mr->msg_event); /* signal a new message */
492 if (wait)
494 /* wait for playback/record thread to have processed the message */
495 WaitForSingleObject(hEvent, INFINITE);
496 CloseHandle(hEvent);
499 return 1;
502 /******************************************************************
503 * NAS_RetrieveRingMessage
505 * Get a message from the ring. Should be called by the playback/record thread.
507 static int NAS_RetrieveRingMessage(MSG_RING* mr,
508 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
510 EnterCriticalSection(&mr->msg_crst);
512 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
514 LeaveCriticalSection(&mr->msg_crst);
515 return 0;
518 *msg = mr->messages[mr->msg_toget].msg;
519 mr->messages[mr->msg_toget].msg = 0;
520 *param = mr->messages[mr->msg_toget].param;
521 *hEvent = mr->messages[mr->msg_toget].hEvent;
522 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
523 LeaveCriticalSection(&mr->msg_crst);
524 return 1;
527 /*======================================================================*
528 * Low level WAVE OUT implementation *
529 *======================================================================*/
531 /**************************************************************************
532 * wodNotifyClient [internal]
534 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
536 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
538 switch (wMsg) {
539 case WOM_OPEN:
540 case WOM_CLOSE:
541 case WOM_DONE:
542 if (wwo->wFlags != DCB_NULL &&
543 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
544 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
545 WARN("can't notify client !\n");
546 return MMSYSERR_ERROR;
548 break;
549 default:
550 FIXME("Unknown callback message %u\n", wMsg);
551 return MMSYSERR_INVALPARAM;
553 return MMSYSERR_NOERROR;
556 /**************************************************************************
557 * wodUpdatePlayedTotal [internal]
560 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
562 wwo->PlayedTotal = wwo->WrittenTotal;
563 return TRUE;
566 /**************************************************************************
567 * wodPlayer_BeginWaveHdr [internal]
569 * Makes the specified lpWaveHdr the currently playing wave header.
570 * If the specified wave header is a begin loop and we're not already in
571 * a loop, setup the loop.
573 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
575 wwo->lpPlayPtr = lpWaveHdr;
577 if (!lpWaveHdr) return;
579 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
580 if (wwo->lpLoopPtr) {
581 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
582 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
583 } else {
584 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
585 wwo->lpLoopPtr = lpWaveHdr;
586 /* Windows does not touch WAVEHDR.dwLoops,
587 * so we need to make an internal copy */
588 wwo->dwLoops = lpWaveHdr->dwLoops;
593 /**************************************************************************
594 * wodPlayer_PlayPtrNext [internal]
596 * Advance the play pointer to the next waveheader, looping if required.
598 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
600 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
602 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
603 /* We're at the end of a loop, loop if required */
604 if (--wwo->dwLoops > 0) {
605 wwo->lpPlayPtr = wwo->lpLoopPtr;
606 } else {
607 /* Handle overlapping loops correctly */
608 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
609 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
610 /* shall we consider the END flag for the closing loop or for
611 * the opening one or for both ???
612 * code assumes for closing loop only
614 } else {
615 lpWaveHdr = lpWaveHdr->lpNext;
617 wwo->lpLoopPtr = NULL;
618 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
620 } else {
621 /* We're not in a loop. Advance to the next wave header */
622 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
624 return lpWaveHdr;
627 /**************************************************************************
628 * wodPlayer_NotifyCompletions [internal]
630 * Notifies and remove from queue all wavehdrs which have been played to
631 * the speaker (ie. they have cleared the audio device). If force is true,
632 * we notify all wavehdrs and remove them all from the queue even if they
633 * are unplayed or part of a loop.
635 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
637 LPWAVEHDR lpWaveHdr;
639 /* Start from lpQueuePtr and keep notifying until:
640 * - we hit an unwritten wavehdr
641 * - we hit the beginning of a running loop
642 * - we hit a wavehdr which hasn't finished playing
644 wodUpdatePlayedTotal(wwo);
646 while ((lpWaveHdr = wwo->lpQueuePtr) && (force || (lpWaveHdr != wwo->lpPlayPtr &&
647 lpWaveHdr != wwo->lpLoopPtr && lpWaveHdr->reserved <= wwo->PlayedTotal))) {
649 wwo->lpQueuePtr = lpWaveHdr->lpNext;
651 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
652 lpWaveHdr->dwFlags |= WHDR_DONE;
654 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
656 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
657 1 : 1;
660 /**************************************************************************
661 * wodPlayer_Reset [internal]
663 * wodPlayer helper. Resets current output stream.
665 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
667 wodUpdatePlayedTotal(wwo);
668 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
670 /* we aren't able to flush any data that has already been written */
671 /* to nas, otherwise we would do the flushing here */
673 nas_free(wwo);
675 if (reset) {
676 enum win_wm_message msg;
677 DWORD param;
678 HANDLE ev;
680 /* remove any buffer */
681 wodPlayer_NotifyCompletions(wwo, TRUE);
683 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
684 wwo->state = WINE_WS_STOPPED;
685 wwo->PlayedTotal = wwo->WrittenTotal = 0;
687 /* remove any existing message in the ring */
688 EnterCriticalSection(&wwo->msgRing.msg_crst);
690 /* return all pending headers in queue */
691 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
693 TRACE("flushing msg\n");
694 if (msg != WINE_WM_HEADER)
696 FIXME("shouldn't have headers left\n");
697 SetEvent(ev);
698 continue;
700 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
701 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
703 wodNotifyClient(wwo, WOM_DONE, param, 0);
705 ResetEvent(wwo->msgRing.msg_event);
706 LeaveCriticalSection(&wwo->msgRing.msg_crst);
707 } else {
708 if (wwo->lpLoopPtr) {
709 /* complicated case, not handled yet (could imply modifying the loop counter */
710 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
711 wwo->lpPlayPtr = wwo->lpLoopPtr;
712 wwo->WrittenTotal = wwo->PlayedTotal; /* this is wrong !!! */
713 } else {
714 /* the data already written is going to be played, so take */
715 /* this fact into account here */
716 wwo->PlayedTotal = wwo->WrittenTotal;
718 wwo->state = WINE_WS_PAUSED;
722 /**************************************************************************
723 * wodPlayer_ProcessMessages [internal]
725 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
727 LPWAVEHDR lpWaveHdr;
728 enum win_wm_message msg;
729 DWORD param;
730 HANDLE ev;
732 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
733 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
734 switch (msg) {
735 case WINE_WM_PAUSING:
736 wodPlayer_Reset(wwo, FALSE);
737 SetEvent(ev);
738 break;
739 case WINE_WM_RESTARTING:
740 wwo->state = WINE_WS_PLAYING;
741 SetEvent(ev);
742 break;
743 case WINE_WM_HEADER:
744 lpWaveHdr = (LPWAVEHDR)param;
746 /* insert buffer at the end of queue */
748 LPWAVEHDR* wh;
749 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
750 *wh = lpWaveHdr;
752 if (!wwo->lpPlayPtr)
753 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
754 if (wwo->state == WINE_WS_STOPPED)
755 wwo->state = WINE_WS_PLAYING;
756 break;
757 case WINE_WM_RESETTING:
758 wodPlayer_Reset(wwo, TRUE);
759 SetEvent(ev);
760 break;
761 case WINE_WM_UPDATE:
762 wodUpdatePlayedTotal(wwo);
763 SetEvent(ev);
764 break;
765 case WINE_WM_BREAKLOOP:
766 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
767 /* ensure exit at end of current loop */
768 wwo->dwLoops = 1;
770 SetEvent(ev);
771 break;
772 case WINE_WM_CLOSING:
773 /* sanity check: this should not happen since the device must have been reset before */
774 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
775 wwo->hThread = 0;
776 wwo->state = WINE_WS_CLOSED;
777 SetEvent(ev);
778 ExitThread(0);
779 /* shouldn't go here */
780 default:
781 FIXME("unknown message %d\n", msg);
782 break;
787 /**************************************************************************
788 * wodPlayer [internal]
790 static DWORD CALLBACK wodPlayer(LPVOID pmt)
792 WORD uDevID = (DWORD)pmt;
793 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
795 wwo->state = WINE_WS_STOPPED;
796 SetEvent(wwo->hStartUpEvent);
798 for (;;) {
800 if (wwo->FlowStarted) {
801 AuHandleEvents(wwo->AuServ);
803 if (wwo->state == WINE_WS_PLAYING && wwo->freeBytes && wwo->BufferUsed)
804 nas_send_buffer(wwo);
807 if (wwo->BufferUsed <= FRAG_SIZE && wwo->writeBytes > 0)
808 wodPlayer_NotifyCompletions(wwo, FALSE);
810 WaitForSingleObject(wwo->msgRing.msg_event, 20);
811 wodPlayer_ProcessMessages(wwo);
813 while(wwo->lpPlayPtr) {
814 wwo->lpPlayPtr->reserved = wwo->WrittenTotal + wwo->lpPlayPtr->dwBufferLength;
815 nas_add_buffer(wwo);
816 wodPlayer_PlayPtrNext(wwo);
822 /**************************************************************************
823 * wodGetDevCaps [internal]
825 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
827 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
829 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
831 if (wDevID >= MAX_WAVEOUTDRV) {
832 TRACE("MAX_WAVOUTDRV reached !\n");
833 return MMSYSERR_BADDEVICEID;
836 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
837 return MMSYSERR_NOERROR;
840 /**************************************************************************
841 * wodOpen [internal]
843 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
845 WINE_WAVEOUT* wwo;
847 TRACE("wodOpen (%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
849 if (lpDesc == NULL) {
850 WARN("Invalid Parameter !\n");
851 return MMSYSERR_INVALPARAM;
853 if (wDevID >= MAX_WAVEOUTDRV) {
854 TRACE("MAX_WAVOUTDRV reached !\n");
855 return MMSYSERR_BADDEVICEID;
858 /* if this device is already open tell the app that it is allocated */
860 wwo = &WOutDev[wDevID];
862 if(wwo->open)
864 TRACE("device already allocated\n");
865 return MMSYSERR_ALLOCATED;
869 /* only PCM format is supported so far... */
870 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
871 lpDesc->lpFormat->nChannels == 0 ||
872 lpDesc->lpFormat->nSamplesPerSec == 0) {
873 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
874 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
875 lpDesc->lpFormat->nSamplesPerSec);
876 return WAVERR_BADFORMAT;
879 if (dwFlags & WAVE_FORMAT_QUERY) {
880 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
881 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
882 lpDesc->lpFormat->nSamplesPerSec);
883 return MMSYSERR_NOERROR;
886 /* direct sound not supported, ignore the flag */
887 dwFlags &= ~WAVE_DIRECTSOUND;
889 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
891 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
892 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
894 if (wwo->format.wBitsPerSample == 0) {
895 WARN("Resetting zeroed wBitsPerSample\n");
896 wwo->format.wBitsPerSample = 8 *
897 (wwo->format.wf.nAvgBytesPerSec /
898 wwo->format.wf.nSamplesPerSec) /
899 wwo->format.wf.nChannels;
902 if (!nas_open(wwo))
903 return MMSYSERR_ALLOCATED;
905 NAS_InitRingMessage(&wwo->msgRing);
907 /* create player thread */
908 if (!(dwFlags & WAVE_DIRECTSOUND)) {
909 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
910 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
911 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
912 CloseHandle(wwo->hStartUpEvent);
913 } else {
914 wwo->hThread = INVALID_HANDLE_VALUE;
915 wwo->dwThreadID = 0;
917 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
919 TRACE("stream=0x%lx, BufferSize=%ld\n", (long)wwo->AuServ, wwo->BufferSize);
921 TRACE("wBitsPerSample=%u nAvgBytesPerSec=%lu nSamplesPerSec=%lu nChannels=%u nBlockAlign=%u\n",
922 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
923 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
924 wwo->format.wf.nBlockAlign);
926 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
929 /**************************************************************************
930 * wodClose [internal]
932 static DWORD wodClose(WORD wDevID)
934 DWORD ret = MMSYSERR_NOERROR;
935 WINE_WAVEOUT* wwo;
937 TRACE("(%u);\n", wDevID);
939 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
941 WARN("bad device ID !\n");
942 return MMSYSERR_BADDEVICEID;
945 wwo = &WOutDev[wDevID];
946 if (wwo->lpQueuePtr) {
947 WARN("buffers still playing !\n");
948 ret = WAVERR_STILLPLAYING;
949 } else {
950 TRACE("imhere[3-close]\n");
951 if (wwo->hThread != INVALID_HANDLE_VALUE) {
952 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
955 NAS_DestroyRingMessage(&wwo->msgRing);
957 NAS_CloseDevice(wwo); /* close the stream and clean things up */
959 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
961 return ret;
964 /**************************************************************************
965 * wodWrite [internal]
968 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
970 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
972 /* first, do the sanity checks... */
973 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
975 WARN("bad dev ID !\n");
976 return MMSYSERR_BADDEVICEID;
979 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
981 TRACE("unprepared\n");
982 return WAVERR_UNPREPARED;
985 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
987 TRACE("still playing\n");
988 return WAVERR_STILLPLAYING;
991 lpWaveHdr->dwFlags &= ~WHDR_DONE;
992 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
993 lpWaveHdr->lpNext = 0;
995 TRACE("adding ring message\n");
996 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
998 return MMSYSERR_NOERROR;
1001 /**************************************************************************
1002 * wodPrepare [internal]
1004 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1006 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1008 if (wDevID >= MAX_WAVEOUTDRV) {
1009 WARN("bad device ID !\n");
1010 return MMSYSERR_BADDEVICEID;
1013 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1014 return WAVERR_STILLPLAYING;
1016 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1017 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1018 return MMSYSERR_NOERROR;
1021 /**************************************************************************
1022 * wodUnprepare [internal]
1024 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1026 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1028 if (wDevID >= MAX_WAVEOUTDRV) {
1029 WARN("bad device ID !\n");
1030 return MMSYSERR_BADDEVICEID;
1033 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1034 return WAVERR_STILLPLAYING;
1036 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1037 lpWaveHdr->dwFlags |= WHDR_DONE;
1039 return MMSYSERR_NOERROR;
1042 /**************************************************************************
1043 * wodPause [internal]
1045 static DWORD wodPause(WORD wDevID)
1047 TRACE("(%u);!\n", wDevID);
1049 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1051 WARN("bad device ID !\n");
1052 return MMSYSERR_BADDEVICEID;
1055 TRACE("imhere[3-PAUSING]\n");
1056 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1058 return MMSYSERR_NOERROR;
1061 /**************************************************************************
1062 * wodRestart [internal]
1064 static DWORD wodRestart(WORD wDevID)
1066 TRACE("(%u);\n", wDevID);
1068 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1070 WARN("bad device ID !\n");
1071 return MMSYSERR_BADDEVICEID;
1074 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1075 TRACE("imhere[3-RESTARTING]\n");
1076 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1079 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1080 /* FIXME: Myst crashes with this ... hmm -MM
1081 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1084 return MMSYSERR_NOERROR;
1087 /**************************************************************************
1088 * wodReset [internal]
1090 static DWORD wodReset(WORD wDevID)
1092 TRACE("(%u);\n", wDevID);
1094 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1096 WARN("bad device ID !\n");
1097 return MMSYSERR_BADDEVICEID;
1100 TRACE("imhere[3-RESET]\n");
1101 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1103 return MMSYSERR_NOERROR;
1106 /**************************************************************************
1107 * wodGetPosition [internal]
1109 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1111 int time;
1112 DWORD val;
1113 WINE_WAVEOUT* wwo;
1115 TRACE("%u, %p, %lu);\n", wDevID, lpTime, uSize);
1117 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1119 WARN("bad device ID !\n");
1120 return MMSYSERR_BADDEVICEID;
1123 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1125 wwo = &WOutDev[wDevID];
1126 #if 0
1127 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1128 #endif
1129 val = wwo->WrittenTotal;
1131 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1132 lpTime->wType, wwo->format.wBitsPerSample,
1133 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1134 wwo->format.wf.nAvgBytesPerSec);
1135 TRACE("PlayedTotal=%lu\n", val);
1137 switch (lpTime->wType) {
1138 case TIME_BYTES:
1139 lpTime->u.cb = val;
1140 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1141 break;
1142 case TIME_SAMPLES:
1143 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample / wwo->format.wf.nChannels;
1144 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1145 break;
1146 case TIME_SMPTE:
1147 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1148 lpTime->u.smpte.hour = time / 108000;
1149 time -= lpTime->u.smpte.hour * 108000;
1150 lpTime->u.smpte.min = time / 1800;
1151 time -= lpTime->u.smpte.min * 1800;
1152 lpTime->u.smpte.sec = time / 30;
1153 time -= lpTime->u.smpte.sec * 30;
1154 lpTime->u.smpte.frame = time;
1155 lpTime->u.smpte.fps = 30;
1156 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1157 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1158 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1159 break;
1160 default:
1161 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1162 lpTime->wType = TIME_MS;
1163 case TIME_MS:
1164 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1165 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1166 break;
1168 return MMSYSERR_NOERROR;
1171 /**************************************************************************
1172 * wodBreakLoop [internal]
1174 static DWORD wodBreakLoop(WORD wDevID)
1176 TRACE("(%u);\n", wDevID);
1178 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1180 WARN("bad device ID !\n");
1181 return MMSYSERR_BADDEVICEID;
1183 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1184 return MMSYSERR_NOERROR;
1187 /**************************************************************************
1188 * wodGetVolume [internal]
1190 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1192 DWORD left, right;
1194 left = WOutDev[wDevID].volume_left;
1195 right = WOutDev[wDevID].volume_right;
1197 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1199 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1201 return MMSYSERR_NOERROR;
1204 /**************************************************************************
1205 * wodSetVolume [internal]
1207 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1209 DWORD left, right;
1211 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1212 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1214 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1216 WOutDev[wDevID].volume_left = left;
1217 WOutDev[wDevID].volume_right = right;
1219 return MMSYSERR_NOERROR;
1222 /**************************************************************************
1223 * wodGetNumDevs [internal]
1225 static DWORD wodGetNumDevs(void)
1227 return MAX_WAVEOUTDRV;
1230 /**************************************************************************
1231 * wodMessage (WINENAS.@)
1233 DWORD WINAPI NAS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1234 DWORD dwParam1, DWORD dwParam2)
1236 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1238 switch (wMsg) {
1239 case DRVM_INIT:
1240 case DRVM_EXIT:
1241 case DRVM_ENABLE:
1242 case DRVM_DISABLE:
1243 /* FIXME: Pretend this is supported */
1244 return 0;
1245 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1246 case WODM_CLOSE: return wodClose (wDevID);
1247 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1248 case WODM_PAUSE: return wodPause (wDevID);
1249 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1250 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1251 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1252 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1253 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1254 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1255 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1256 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1257 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1258 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1259 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1260 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1261 case WODM_RESTART: return wodRestart (wDevID);
1262 case WODM_RESET: return wodReset (wDevID);
1264 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1265 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1266 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1267 default:
1268 FIXME("unknown message %d!\n", wMsg);
1270 return MMSYSERR_NOTSUPPORTED;
1273 /*======================================================================*
1274 * Low level DSOUND implementation *
1275 *======================================================================*/
1276 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1278 /* we can't perform memory mapping as we don't have a file stream
1279 interface with nas like we do with oss */
1280 MESSAGE("This sound card s driver does not support direct access\n");
1281 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1282 return MMSYSERR_NOTSUPPORTED;
1285 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1287 memset(desc, 0, sizeof(*desc));
1288 strcpy(desc->szDesc, "Wine NAS DirectSound Driver");
1289 strcpy(desc->szDrvName, "winenas.drv");
1290 return MMSYSERR_NOERROR;
1293 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
1295 memcpy(pGuid, &DSDEVID_DefaultPlayback, sizeof(GUID));
1296 return MMSYSERR_NOERROR;
1299 static int nas_init(void) {
1300 TRACE("NAS INIT\n");
1301 if (!(AuServ = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL)))
1302 return 0;
1304 return 1;
1307 static int nas_finddev(WINE_WAVEOUT* wwo) {
1308 int i;
1310 for (i = 0; i < AuServerNumDevices(wwo->AuServ); i++) {
1311 if ((AuDeviceKind(AuServerDevice(wwo->AuServ, i)) ==
1312 AuComponentKindPhysicalOutput) &&
1313 AuDeviceNumTracks(AuServerDevice(wwo->AuServ, i)) == wwo->format.wf.nChannels)
1315 wwo->AuDev = AuDeviceIdentifier(AuServerDevice(wwo->AuServ, i));
1316 break;
1320 if (wwo->AuDev == AuNone)
1321 return 0;
1322 return 1;
1325 static int nas_open(WINE_WAVEOUT* wwo) {
1326 AuElement elements[3];
1328 if (!wwo->AuServ)
1329 return 0;
1331 if (!nas_finddev(wwo))
1332 return 0;
1334 if (!(wwo->AuFlow = AuCreateFlow(wwo->AuServ, NULL)))
1335 return 0;
1337 wwo->BufferSize = FRAG_SIZE * FRAG_COUNT;
1339 AuMakeElementImportClient(&elements[0], wwo->format.wf.nSamplesPerSec,
1340 wwo->format.wBitsPerSample == 16 ? AuFormatLinearSigned16LSB : AuFormatLinearUnsigned8,
1341 wwo->format.wf.nChannels, AuTrue, wwo->BufferSize, wwo->BufferSize / 2, 0, NULL);
1343 AuMakeElementExportDevice(&elements[1], 0, wwo->AuDev, wwo->format.wf.nSamplesPerSec,
1344 AuUnlimitedSamples, 0, NULL);
1346 AuSetElements(wwo->AuServ, wwo->AuFlow, AuTrue, 2, elements, NULL);
1348 AuRegisterEventHandler(wwo->AuServ, AuEventHandlerIDMask, 0, wwo->AuFlow,
1349 event_handler, (AuPointer) wwo);
1352 wwo->PlayedTotal = 0;
1353 wwo->WrittenTotal = 0;
1354 wwo->open = 1;
1356 wwo->BufferUsed = 0;
1357 wwo->writeBytes = 0;
1358 wwo->freeBytes = 0;
1359 wwo->sendBytes = 0;
1360 wwo->SoundBuffer = NULL;
1361 wwo->FlowStarted = 0;
1363 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1364 AuPauseFlow(wwo->AuServ, wwo->AuFlow, NULL);
1365 wwo->FlowStarted = 1;
1367 return 1;
1370 static AuBool
1371 event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
1373 WINE_WAVEOUT *wwo = (WINE_WAVEOUT *)hnd->data;
1374 switch (ev->type) {
1376 case AuEventTypeElementNotify: {
1377 AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
1380 switch (event->kind) {
1381 case AuElementNotifyKindLowWater:
1382 wwo->freeBytes += event->num_bytes;
1383 if (wwo->writeBytes > 0)
1384 wwo->sendBytes += event->num_bytes;
1385 if (wwo->freeBytes && wwo->BufferUsed)
1386 nas_send_buffer(wwo);
1387 break;
1389 case AuElementNotifyKindState:
1390 TRACE("ev: kind %s state %s->%s reason %s numbytes %ld freeB %lu\n",
1391 nas_elementnotify_kind(event->kind),
1392 nas_state(event->prev_state),
1393 nas_state(event->cur_state),
1394 nas_reason(event->reason),
1395 event->num_bytes, wwo->freeBytes);
1397 if (event->cur_state == AuStatePause && event->reason != AuReasonUser) {
1398 wwo->freeBytes += event->num_bytes;
1399 if (wwo->writeBytes > 0)
1400 wwo->sendBytes += event->num_bytes;
1401 if (wwo->sendBytes > wwo->writeBytes)
1402 wwo->sendBytes = wwo->writeBytes;
1403 if (wwo->freeBytes && wwo->BufferUsed)
1404 nas_send_buffer(wwo);
1406 break;
1410 return AuTrue;
1413 static void
1414 buffer_resize(WINE_WAVEOUT* wwo, int len)
1416 void *newbuf = malloc(wwo->BufferUsed + len);
1417 void *oldbuf = wwo->SoundBuffer;
1418 memcpy(newbuf, oldbuf, wwo->BufferUsed);
1419 wwo->SoundBuffer = newbuf;
1420 if (oldbuf != NULL)
1421 free(oldbuf);
1424 static int nas_add_buffer(WINE_WAVEOUT* wwo) {
1425 int len = wwo->lpPlayPtr->dwBufferLength;
1427 buffer_resize(wwo, len);
1428 memcpy(wwo->SoundBuffer + wwo->BufferUsed, wwo->lpPlayPtr->lpData, len);
1429 wwo->BufferUsed += len;
1430 wwo->WrittenTotal += len;
1431 return len;
1434 static int nas_send_buffer(WINE_WAVEOUT* wwo) {
1435 int oldb , len;
1436 char *ptr, *newdata;
1437 newdata = NULL;
1438 oldb = len = 0;
1440 if (wwo->freeBytes <= 0)
1441 return 0;
1443 if (wwo->SoundBuffer == NULL || wwo->BufferUsed == 0) {
1444 return 0;
1447 if (wwo->BufferUsed <= wwo->freeBytes) {
1448 len = wwo->BufferUsed;
1449 ptr = wwo->SoundBuffer;
1450 } else {
1451 len = wwo->freeBytes;
1452 ptr = malloc(len);
1453 memcpy(ptr,wwo->SoundBuffer,len);
1454 newdata = malloc(wwo->BufferUsed - len);
1455 memcpy(newdata, wwo->SoundBuffer + len, wwo->BufferUsed - len);
1458 TRACE("envoye de %d bytes / %lu bytes / freeBytes %lu\n", len, wwo->BufferUsed, wwo->freeBytes);
1460 AuWriteElement(wwo->AuServ, wwo->AuFlow, 0, len, ptr, AuFalse, NULL);
1462 wwo->BufferUsed -= len;
1463 wwo->freeBytes -= len;
1464 wwo->writeBytes += len;
1466 free(ptr);
1468 wwo->SoundBuffer = NULL;
1470 if (newdata != NULL)
1471 wwo->SoundBuffer = newdata;
1473 return len;
1476 static int nas_free(WINE_WAVEOUT* wwo)
1479 if (!wwo->FlowStarted && wwo->BufferUsed) {
1480 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1481 wwo->FlowStarted = 1;
1484 while (wwo->BufferUsed || wwo->writeBytes != wwo->sendBytes) {
1485 if (wwo->freeBytes)
1486 nas_send_buffer(wwo);
1487 AuHandleEvents(wwo->AuServ);
1490 AuFlush(wwo->AuServ);
1491 return TRUE;
1494 static int nas_close(WINE_WAVEOUT* wwo)
1496 AuEvent ev;
1498 nas_free(wwo);
1500 AuStopFlow(wwo->AuServ, wwo->AuFlow, NULL);
1501 AuDestroyFlow(wwo->AuServ, wwo->AuFlow, NULL);
1502 AuFlush(wwo->AuServ);
1503 AuNextEvent(wwo->AuServ, AuTrue, &ev);
1504 AuDispatchEvent(wwo->AuServ, &ev);
1506 wwo->AuFlow = 0;
1507 wwo->open = 0;
1508 wwo->BufferUsed = 0;
1509 wwo->freeBytes = 0;
1510 wwo->SoundBuffer = NULL;
1511 return 1;
1514 static int nas_end(void)
1516 AuCloseServer(AuServ);
1517 AuServ = 0;
1518 return 1;
1521 #else /* !HAVE_NAS */
1523 /**************************************************************************
1524 * wodMessage (WINENAS.@)
1526 DWORD WINAPI NAS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
1528 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1529 return MMSYSERR_NOTENABLED;
1531 #endif