Define new propsheet messages.
[wine/multimedia.git] / dlls / winmm / winenas / audio.c
blobb4c0ab2927172f51674a0d8c9214a99489ddec82
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 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
283 PCMWAVEFORMAT* format)
285 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
286 lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
287 format->wf.nChannels, format->wf.nAvgBytesPerSec);
288 TRACE("Position in bytes=%lu\n", position);
290 switch (lpTime->wType) {
291 case TIME_SAMPLES:
292 lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
293 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
294 break;
295 case TIME_MS:
296 lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
297 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
298 break;
299 case TIME_SMPTE:
300 position = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
301 lpTime->u.smpte.sec = position / format->wf.nSamplesPerSec;
302 position -= lpTime->u.smpte.sec * format->wf.nSamplesPerSec;
303 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
304 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
305 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
306 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
307 lpTime->u.smpte.fps = 30;
308 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->wf.nSamplesPerSec;
309 position -= lpTime->u.smpte.frame * format->wf.nSamplesPerSec / lpTime->u.smpte.fps;
310 if (position != 0)
312 /* Round up */
313 lpTime->u.smpte.frame++;
315 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
316 lpTime->u.smpte.hour, lpTime->u.smpte.min,
317 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
318 break;
319 default:
320 FIXME("Format %d not supported ! use TIME_BYTES !\n", lpTime->wType);
321 lpTime->wType = TIME_BYTES;
322 /* fall through */
323 case TIME_BYTES:
324 lpTime->u.cb = position;
325 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
326 break;
328 return MMSYSERR_NOERROR;
331 /*======================================================================*
332 * Low level WAVE implementation *
333 *======================================================================*/
335 /* Volume functions derived from Alsaplayer source */
336 /* length is the number of 16 bit samples */
337 void volume_effect16(void *bufin, void* bufout, int length, int left,
338 int right, int nChannels)
340 short *d_out = (short *)bufout;
341 short *d_in = (short *)bufin;
342 int i, v;
345 TRACE("length == %d, nChannels == %d\n", length, nChannels);
348 if (right == -1) right = left;
350 for(i = 0; i < length; i+=(nChannels))
352 v = (int) ((*(d_in++) * left) / 100);
353 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
354 if(nChannels == 2)
356 v = (int) ((*(d_in++) * right) / 100);
357 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
362 /* length is the number of 8 bit samples */
363 void volume_effect8(void *bufin, void* bufout, int length, int left,
364 int right, int nChannels)
366 char *d_out = (char *)bufout;
367 char *d_in = (char *)bufin;
368 int i, v;
371 TRACE("length == %d, nChannels == %d\n", length, nChannels);
374 if (right == -1) right = left;
376 for(i = 0; i < length; i+=(nChannels))
378 v = (char) ((*(d_in++) * left) / 100);
379 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
380 if(nChannels == 2)
382 v = (char) ((*(d_in++) * right) / 100);
383 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
388 /******************************************************************
389 * NAS_CloseDevice
392 void NAS_CloseDevice(WINE_WAVEOUT* wwo)
394 TRACE("NAS_CloseDevice\n");
395 nas_close(wwo);
398 /******************************************************************
399 * NAS_WaveClose
401 LONG NAS_WaveClose(void)
403 nas_end(); /* free up nas server */
404 return 1;
407 /******************************************************************
408 * NAS_WaveInit
410 * Initialize internal structures from NAS server info
412 LONG NAS_WaveInit(void)
414 int i;
415 nas_init();
417 /* initialize all device handles to -1 */
418 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
420 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out caps values */
422 WOutDev[i].AuServ = AuServ;
423 WOutDev[i].AuDev = AuNone;
424 WOutDev[i].Id = i;
425 /* FIXME: some programs compare this string against the content of the registry
426 * for MM drivers. The names have to match in order for the program to work
427 * (e.g. MS win9x mplayer.exe)
429 #ifdef EMULATE_SB16
430 WOutDev[i].caps.wMid = 0x0002;
431 WOutDev[i].caps.wPid = 0x0104;
432 strcpy(WOutDev[i].caps.szPname, "SB16 Wave Out");
433 #else
434 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
435 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
436 /* strcpy(WOutDev[i].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
437 strcpy(WOutDev[i].caps.szPname, "CS4236/37/38");
438 #endif
439 WOutDev[i].AuFlow = 0;
440 WOutDev[i].caps.vDriverVersion = 0x0100;
441 WOutDev[i].caps.dwFormats = 0x00000000;
442 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
444 WOutDev[i].caps.wChannels = 2;
445 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
447 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
448 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
449 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
450 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
451 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
452 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
453 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
454 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
455 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
456 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
457 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
458 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
462 return 0;
465 /******************************************************************
466 * NAS_InitRingMessage
468 * Initialize the ring of messages for passing between driver's caller and playback/record
469 * thread
471 static int NAS_InitRingMessage(MSG_RING* mr)
473 mr->msg_toget = 0;
474 mr->msg_tosave = 0;
475 mr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
476 mr->ring_buffer_size = NAS_RING_BUFFER_INCREMENT;
477 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
478 InitializeCriticalSection(&mr->msg_crst);
479 return 0;
482 /******************************************************************
483 * NAS_DestroyRingMessage
486 static int NAS_DestroyRingMessage(MSG_RING* mr)
488 CloseHandle(mr->msg_event);
489 HeapFree(GetProcessHeap(),0,mr->messages);
490 DeleteCriticalSection(&mr->msg_crst);
491 return 0;
494 /******************************************************************
495 * NAS_AddRingMessage
497 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
499 static int NAS_AddRingMessage(MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
501 HANDLE hEvent = INVALID_HANDLE_VALUE;
503 EnterCriticalSection(&mr->msg_crst);
504 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
506 int old_ring_buffer_size = mr->ring_buffer_size;
507 mr->ring_buffer_size += NAS_RING_BUFFER_INCREMENT;
508 TRACE("omr->ring_buffer_size=%d\n",mr->ring_buffer_size);
509 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
510 /* Now we need to rearrange the ring buffer so that the new
511 buffers just allocated are in between mr->msg_tosave and
512 mr->msg_toget.
514 if (mr->msg_tosave < mr->msg_toget)
516 memmove(&(mr->messages[mr->msg_toget + NAS_RING_BUFFER_INCREMENT]),
517 &(mr->messages[mr->msg_toget]),
518 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
520 mr->msg_toget += NAS_RING_BUFFER_INCREMENT;
523 if (wait)
525 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
526 if (hEvent == INVALID_HANDLE_VALUE)
528 ERR("can't create event !?\n");
529 LeaveCriticalSection(&mr->msg_crst);
530 return 0;
532 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
533 FIXME("two fast messages in the queue!!!!\n");
535 /* fast messages have to be added at the start of the queue */
536 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
538 mr->messages[mr->msg_toget].msg = msg;
539 mr->messages[mr->msg_toget].param = param;
540 mr->messages[mr->msg_toget].hEvent = hEvent;
542 else
544 mr->messages[mr->msg_tosave].msg = msg;
545 mr->messages[mr->msg_tosave].param = param;
546 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
547 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
550 LeaveCriticalSection(&mr->msg_crst);
552 SetEvent(mr->msg_event); /* signal a new message */
554 if (wait)
556 /* wait for playback/record thread to have processed the message */
557 WaitForSingleObject(hEvent, INFINITE);
558 CloseHandle(hEvent);
561 return 1;
564 /******************************************************************
565 * NAS_RetrieveRingMessage
567 * Get a message from the ring. Should be called by the playback/record thread.
569 static int NAS_RetrieveRingMessage(MSG_RING* mr,
570 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
572 EnterCriticalSection(&mr->msg_crst);
574 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
576 LeaveCriticalSection(&mr->msg_crst);
577 return 0;
580 *msg = mr->messages[mr->msg_toget].msg;
581 mr->messages[mr->msg_toget].msg = 0;
582 *param = mr->messages[mr->msg_toget].param;
583 *hEvent = mr->messages[mr->msg_toget].hEvent;
584 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
585 LeaveCriticalSection(&mr->msg_crst);
586 return 1;
589 /*======================================================================*
590 * Low level WAVE OUT implementation *
591 *======================================================================*/
593 /**************************************************************************
594 * wodNotifyClient [internal]
596 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
598 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
600 switch (wMsg) {
601 case WOM_OPEN:
602 case WOM_CLOSE:
603 case WOM_DONE:
604 if (wwo->wFlags != DCB_NULL &&
605 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
606 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
607 WARN("can't notify client !\n");
608 return MMSYSERR_ERROR;
610 break;
611 default:
612 FIXME("Unknown callback message %u\n", wMsg);
613 return MMSYSERR_INVALPARAM;
615 return MMSYSERR_NOERROR;
618 /**************************************************************************
619 * wodUpdatePlayedTotal [internal]
622 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
624 wwo->PlayedTotal = wwo->WrittenTotal;
625 return TRUE;
628 /**************************************************************************
629 * wodPlayer_BeginWaveHdr [internal]
631 * Makes the specified lpWaveHdr the currently playing wave header.
632 * If the specified wave header is a begin loop and we're not already in
633 * a loop, setup the loop.
635 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
637 wwo->lpPlayPtr = lpWaveHdr;
639 if (!lpWaveHdr) return;
641 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
642 if (wwo->lpLoopPtr) {
643 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
644 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
645 } else {
646 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
647 wwo->lpLoopPtr = lpWaveHdr;
648 /* Windows does not touch WAVEHDR.dwLoops,
649 * so we need to make an internal copy */
650 wwo->dwLoops = lpWaveHdr->dwLoops;
655 /**************************************************************************
656 * wodPlayer_PlayPtrNext [internal]
658 * Advance the play pointer to the next waveheader, looping if required.
660 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
662 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
664 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
665 /* We're at the end of a loop, loop if required */
666 if (--wwo->dwLoops > 0) {
667 wwo->lpPlayPtr = wwo->lpLoopPtr;
668 } else {
669 /* Handle overlapping loops correctly */
670 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
671 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
672 /* shall we consider the END flag for the closing loop or for
673 * the opening one or for both ???
674 * code assumes for closing loop only
676 } else {
677 lpWaveHdr = lpWaveHdr->lpNext;
679 wwo->lpLoopPtr = NULL;
680 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
682 } else {
683 /* We're not in a loop. Advance to the next wave header */
684 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
686 return lpWaveHdr;
689 /**************************************************************************
690 * wodPlayer_NotifyCompletions [internal]
692 * Notifies and remove from queue all wavehdrs which have been played to
693 * the speaker (ie. they have cleared the audio device). If force is true,
694 * we notify all wavehdrs and remove them all from the queue even if they
695 * are unplayed or part of a loop.
697 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
699 LPWAVEHDR lpWaveHdr;
701 /* Start from lpQueuePtr and keep notifying until:
702 * - we hit an unwritten wavehdr
703 * - we hit the beginning of a running loop
704 * - we hit a wavehdr which hasn't finished playing
706 wodUpdatePlayedTotal(wwo);
708 while ((lpWaveHdr = wwo->lpQueuePtr) && (force || (lpWaveHdr != wwo->lpPlayPtr &&
709 lpWaveHdr != wwo->lpLoopPtr && lpWaveHdr->reserved <= wwo->PlayedTotal))) {
711 wwo->lpQueuePtr = lpWaveHdr->lpNext;
713 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
714 lpWaveHdr->dwFlags |= WHDR_DONE;
716 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
718 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
719 1 : 1;
722 /**************************************************************************
723 * wodPlayer_Reset [internal]
725 * wodPlayer helper. Resets current output stream.
727 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
729 wodUpdatePlayedTotal(wwo);
730 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
732 /* we aren't able to flush any data that has already been written */
733 /* to nas, otherwise we would do the flushing here */
735 nas_free(wwo);
737 if (reset) {
738 enum win_wm_message msg;
739 DWORD param;
740 HANDLE ev;
742 /* remove any buffer */
743 wodPlayer_NotifyCompletions(wwo, TRUE);
745 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
746 wwo->state = WINE_WS_STOPPED;
747 wwo->PlayedTotal = wwo->WrittenTotal = 0;
749 /* remove any existing message in the ring */
750 EnterCriticalSection(&wwo->msgRing.msg_crst);
752 /* return all pending headers in queue */
753 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
755 TRACE("flushing msg\n");
756 if (msg != WINE_WM_HEADER)
758 FIXME("shouldn't have headers left\n");
759 SetEvent(ev);
760 continue;
762 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
763 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
765 wodNotifyClient(wwo, WOM_DONE, param, 0);
767 ResetEvent(wwo->msgRing.msg_event);
768 LeaveCriticalSection(&wwo->msgRing.msg_crst);
769 } else {
770 if (wwo->lpLoopPtr) {
771 /* complicated case, not handled yet (could imply modifying the loop counter */
772 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
773 wwo->lpPlayPtr = wwo->lpLoopPtr;
774 wwo->WrittenTotal = wwo->PlayedTotal; /* this is wrong !!! */
775 } else {
776 /* the data already written is going to be played, so take */
777 /* this fact into account here */
778 wwo->PlayedTotal = wwo->WrittenTotal;
780 wwo->state = WINE_WS_PAUSED;
784 /**************************************************************************
785 * wodPlayer_ProcessMessages [internal]
787 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
789 LPWAVEHDR lpWaveHdr;
790 enum win_wm_message msg;
791 DWORD param;
792 HANDLE ev;
794 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
795 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
796 switch (msg) {
797 case WINE_WM_PAUSING:
798 wodPlayer_Reset(wwo, FALSE);
799 SetEvent(ev);
800 break;
801 case WINE_WM_RESTARTING:
802 wwo->state = WINE_WS_PLAYING;
803 SetEvent(ev);
804 break;
805 case WINE_WM_HEADER:
806 lpWaveHdr = (LPWAVEHDR)param;
808 /* insert buffer at the end of queue */
810 LPWAVEHDR* wh;
811 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
812 *wh = lpWaveHdr;
814 if (!wwo->lpPlayPtr)
815 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
816 if (wwo->state == WINE_WS_STOPPED)
817 wwo->state = WINE_WS_PLAYING;
818 break;
819 case WINE_WM_RESETTING:
820 wodPlayer_Reset(wwo, TRUE);
821 SetEvent(ev);
822 break;
823 case WINE_WM_UPDATE:
824 wodUpdatePlayedTotal(wwo);
825 SetEvent(ev);
826 break;
827 case WINE_WM_BREAKLOOP:
828 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
829 /* ensure exit at end of current loop */
830 wwo->dwLoops = 1;
832 SetEvent(ev);
833 break;
834 case WINE_WM_CLOSING:
835 /* sanity check: this should not happen since the device must have been reset before */
836 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
837 wwo->hThread = 0;
838 wwo->state = WINE_WS_CLOSED;
839 SetEvent(ev);
840 ExitThread(0);
841 /* shouldn't go here */
842 default:
843 FIXME("unknown message %d\n", msg);
844 break;
849 /**************************************************************************
850 * wodPlayer [internal]
852 static DWORD CALLBACK wodPlayer(LPVOID pmt)
854 WORD uDevID = (DWORD)pmt;
855 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
857 wwo->state = WINE_WS_STOPPED;
858 SetEvent(wwo->hStartUpEvent);
860 for (;;) {
862 if (wwo->FlowStarted) {
863 AuHandleEvents(wwo->AuServ);
865 if (wwo->state == WINE_WS_PLAYING && wwo->freeBytes && wwo->BufferUsed)
866 nas_send_buffer(wwo);
869 if (wwo->BufferUsed <= FRAG_SIZE && wwo->writeBytes > 0)
870 wodPlayer_NotifyCompletions(wwo, FALSE);
872 WaitForSingleObject(wwo->msgRing.msg_event, 20);
873 wodPlayer_ProcessMessages(wwo);
875 while(wwo->lpPlayPtr) {
876 wwo->lpPlayPtr->reserved = wwo->WrittenTotal + wwo->lpPlayPtr->dwBufferLength;
877 nas_add_buffer(wwo);
878 wodPlayer_PlayPtrNext(wwo);
884 /**************************************************************************
885 * wodGetDevCaps [internal]
887 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
889 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
891 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
893 if (wDevID >= MAX_WAVEOUTDRV) {
894 TRACE("MAX_WAVOUTDRV reached !\n");
895 return MMSYSERR_BADDEVICEID;
898 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
899 return MMSYSERR_NOERROR;
902 /**************************************************************************
903 * wodOpen [internal]
905 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
907 WINE_WAVEOUT* wwo;
909 TRACE("wodOpen (%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
911 if (lpDesc == NULL) {
912 WARN("Invalid Parameter !\n");
913 return MMSYSERR_INVALPARAM;
915 if (wDevID >= MAX_WAVEOUTDRV) {
916 TRACE("MAX_WAVOUTDRV reached !\n");
917 return MMSYSERR_BADDEVICEID;
920 /* if this device is already open tell the app that it is allocated */
922 wwo = &WOutDev[wDevID];
924 if(wwo->open)
926 TRACE("device already allocated\n");
927 return MMSYSERR_ALLOCATED;
931 /* only PCM format is supported so far... */
932 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
933 lpDesc->lpFormat->nChannels == 0 ||
934 lpDesc->lpFormat->nSamplesPerSec == 0 ||
935 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
936 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
937 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
938 lpDesc->lpFormat->nSamplesPerSec);
939 return WAVERR_BADFORMAT;
942 if (dwFlags & WAVE_FORMAT_QUERY) {
943 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
944 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
945 lpDesc->lpFormat->nSamplesPerSec);
946 return MMSYSERR_NOERROR;
949 /* direct sound not supported, ignore the flag */
950 dwFlags &= ~WAVE_DIRECTSOUND;
952 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
954 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
955 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
957 if (wwo->format.wBitsPerSample == 0) {
958 WARN("Resetting zeroed wBitsPerSample\n");
959 wwo->format.wBitsPerSample = 8 *
960 (wwo->format.wf.nAvgBytesPerSec /
961 wwo->format.wf.nSamplesPerSec) /
962 wwo->format.wf.nChannels;
965 if (!nas_open(wwo))
966 return MMSYSERR_ALLOCATED;
968 NAS_InitRingMessage(&wwo->msgRing);
970 /* create player thread */
971 if (!(dwFlags & WAVE_DIRECTSOUND)) {
972 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
973 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
974 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
975 CloseHandle(wwo->hStartUpEvent);
976 } else {
977 wwo->hThread = INVALID_HANDLE_VALUE;
978 wwo->dwThreadID = 0;
980 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
982 TRACE("stream=0x%lx, BufferSize=%ld\n", (long)wwo->AuServ, wwo->BufferSize);
984 TRACE("wBitsPerSample=%u nAvgBytesPerSec=%lu nSamplesPerSec=%lu nChannels=%u nBlockAlign=%u\n",
985 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
986 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
987 wwo->format.wf.nBlockAlign);
989 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
992 /**************************************************************************
993 * wodClose [internal]
995 static DWORD wodClose(WORD wDevID)
997 DWORD ret = MMSYSERR_NOERROR;
998 WINE_WAVEOUT* wwo;
1000 TRACE("(%u);\n", wDevID);
1002 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1004 WARN("bad device ID !\n");
1005 return MMSYSERR_BADDEVICEID;
1008 wwo = &WOutDev[wDevID];
1009 if (wwo->lpQueuePtr) {
1010 WARN("buffers still playing !\n");
1011 ret = WAVERR_STILLPLAYING;
1012 } else {
1013 TRACE("imhere[3-close]\n");
1014 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1015 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1018 NAS_DestroyRingMessage(&wwo->msgRing);
1020 NAS_CloseDevice(wwo); /* close the stream and clean things up */
1022 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1024 return ret;
1027 /**************************************************************************
1028 * wodWrite [internal]
1031 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1033 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1035 /* first, do the sanity checks... */
1036 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1038 WARN("bad dev ID !\n");
1039 return MMSYSERR_BADDEVICEID;
1042 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1044 TRACE("unprepared\n");
1045 return WAVERR_UNPREPARED;
1048 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1050 TRACE("still playing\n");
1051 return WAVERR_STILLPLAYING;
1054 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1055 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1056 lpWaveHdr->lpNext = 0;
1058 TRACE("adding ring message\n");
1059 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1061 return MMSYSERR_NOERROR;
1064 /**************************************************************************
1065 * wodPrepare [internal]
1067 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1069 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1071 if (wDevID >= MAX_WAVEOUTDRV) {
1072 WARN("bad device ID !\n");
1073 return MMSYSERR_BADDEVICEID;
1076 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1077 return WAVERR_STILLPLAYING;
1079 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1080 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1081 return MMSYSERR_NOERROR;
1084 /**************************************************************************
1085 * wodUnprepare [internal]
1087 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1089 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1091 if (wDevID >= MAX_WAVEOUTDRV) {
1092 WARN("bad device ID !\n");
1093 return MMSYSERR_BADDEVICEID;
1096 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1097 return WAVERR_STILLPLAYING;
1099 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1100 lpWaveHdr->dwFlags |= WHDR_DONE;
1102 return MMSYSERR_NOERROR;
1105 /**************************************************************************
1106 * wodPause [internal]
1108 static DWORD wodPause(WORD wDevID)
1110 TRACE("(%u);!\n", wDevID);
1112 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1114 WARN("bad device ID !\n");
1115 return MMSYSERR_BADDEVICEID;
1118 TRACE("imhere[3-PAUSING]\n");
1119 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1121 return MMSYSERR_NOERROR;
1124 /**************************************************************************
1125 * wodRestart [internal]
1127 static DWORD wodRestart(WORD wDevID)
1129 TRACE("(%u);\n", wDevID);
1131 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1133 WARN("bad device ID !\n");
1134 return MMSYSERR_BADDEVICEID;
1137 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1138 TRACE("imhere[3-RESTARTING]\n");
1139 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1142 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1143 /* FIXME: Myst crashes with this ... hmm -MM
1144 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1147 return MMSYSERR_NOERROR;
1150 /**************************************************************************
1151 * wodReset [internal]
1153 static DWORD wodReset(WORD wDevID)
1155 TRACE("(%u);\n", wDevID);
1157 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1159 WARN("bad device ID !\n");
1160 return MMSYSERR_BADDEVICEID;
1163 TRACE("imhere[3-RESET]\n");
1164 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1166 return MMSYSERR_NOERROR;
1169 /**************************************************************************
1170 * wodGetPosition [internal]
1172 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1174 WINE_WAVEOUT* wwo;
1176 TRACE("%u, %p, %lu);\n", wDevID, lpTime, uSize);
1178 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1180 WARN("bad device ID !\n");
1181 return MMSYSERR_BADDEVICEID;
1184 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1186 wwo = &WOutDev[wDevID];
1187 #if 0
1188 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1189 #endif
1191 return bytes_to_mmtime(lpTime, wwo->WrittenTotal, &wwo->format);
1194 /**************************************************************************
1195 * wodBreakLoop [internal]
1197 static DWORD wodBreakLoop(WORD wDevID)
1199 TRACE("(%u);\n", wDevID);
1201 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1203 WARN("bad device ID !\n");
1204 return MMSYSERR_BADDEVICEID;
1206 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1207 return MMSYSERR_NOERROR;
1210 /**************************************************************************
1211 * wodGetVolume [internal]
1213 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1215 DWORD left, right;
1217 left = WOutDev[wDevID].volume_left;
1218 right = WOutDev[wDevID].volume_right;
1220 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1222 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1224 return MMSYSERR_NOERROR;
1227 /**************************************************************************
1228 * wodSetVolume [internal]
1230 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1232 DWORD left, right;
1234 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1235 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1237 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1239 WOutDev[wDevID].volume_left = left;
1240 WOutDev[wDevID].volume_right = right;
1242 return MMSYSERR_NOERROR;
1245 /**************************************************************************
1246 * wodGetNumDevs [internal]
1248 static DWORD wodGetNumDevs(void)
1250 return MAX_WAVEOUTDRV;
1253 /**************************************************************************
1254 * wodMessage (WINENAS.@)
1256 DWORD WINAPI NAS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1257 DWORD dwParam1, DWORD dwParam2)
1259 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1261 switch (wMsg) {
1262 case DRVM_INIT:
1263 case DRVM_EXIT:
1264 case DRVM_ENABLE:
1265 case DRVM_DISABLE:
1266 /* FIXME: Pretend this is supported */
1267 return 0;
1268 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1269 case WODM_CLOSE: return wodClose (wDevID);
1270 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1271 case WODM_PAUSE: return wodPause (wDevID);
1272 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1273 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1274 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1275 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1276 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1277 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1278 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1279 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1280 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1281 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1282 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1283 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1284 case WODM_RESTART: return wodRestart (wDevID);
1285 case WODM_RESET: return wodReset (wDevID);
1287 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1288 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1289 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1290 default:
1291 FIXME("unknown message %d!\n", wMsg);
1293 return MMSYSERR_NOTSUPPORTED;
1296 /*======================================================================*
1297 * Low level DSOUND implementation *
1298 *======================================================================*/
1299 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1301 /* we can't perform memory mapping as we don't have a file stream
1302 interface with nas like we do with oss */
1303 MESSAGE("This sound card s driver does not support direct access\n");
1304 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1305 return MMSYSERR_NOTSUPPORTED;
1308 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1310 memset(desc, 0, sizeof(*desc));
1311 strcpy(desc->szDesc, "Wine NAS DirectSound Driver");
1312 strcpy(desc->szDrvName, "winenas.drv");
1313 return MMSYSERR_NOERROR;
1316 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
1318 memcpy(pGuid, &DSDEVID_DefaultPlayback, sizeof(GUID));
1319 return MMSYSERR_NOERROR;
1322 static int nas_init(void) {
1323 TRACE("NAS INIT\n");
1324 if (!(AuServ = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL)))
1325 return 0;
1327 return 1;
1330 static int nas_finddev(WINE_WAVEOUT* wwo) {
1331 int i;
1333 for (i = 0; i < AuServerNumDevices(wwo->AuServ); i++) {
1334 if ((AuDeviceKind(AuServerDevice(wwo->AuServ, i)) ==
1335 AuComponentKindPhysicalOutput) &&
1336 AuDeviceNumTracks(AuServerDevice(wwo->AuServ, i)) == wwo->format.wf.nChannels)
1338 wwo->AuDev = AuDeviceIdentifier(AuServerDevice(wwo->AuServ, i));
1339 break;
1343 if (wwo->AuDev == AuNone)
1344 return 0;
1345 return 1;
1348 static int nas_open(WINE_WAVEOUT* wwo) {
1349 AuElement elements[3];
1351 if (!wwo->AuServ)
1352 return 0;
1354 if (!nas_finddev(wwo))
1355 return 0;
1357 if (!(wwo->AuFlow = AuCreateFlow(wwo->AuServ, NULL)))
1358 return 0;
1360 wwo->BufferSize = FRAG_SIZE * FRAG_COUNT;
1362 AuMakeElementImportClient(&elements[0], wwo->format.wf.nSamplesPerSec,
1363 wwo->format.wBitsPerSample == 16 ? AuFormatLinearSigned16LSB : AuFormatLinearUnsigned8,
1364 wwo->format.wf.nChannels, AuTrue, wwo->BufferSize, wwo->BufferSize / 2, 0, NULL);
1366 AuMakeElementExportDevice(&elements[1], 0, wwo->AuDev, wwo->format.wf.nSamplesPerSec,
1367 AuUnlimitedSamples, 0, NULL);
1369 AuSetElements(wwo->AuServ, wwo->AuFlow, AuTrue, 2, elements, NULL);
1371 AuRegisterEventHandler(wwo->AuServ, AuEventHandlerIDMask, 0, wwo->AuFlow,
1372 event_handler, (AuPointer) wwo);
1375 wwo->PlayedTotal = 0;
1376 wwo->WrittenTotal = 0;
1377 wwo->open = 1;
1379 wwo->BufferUsed = 0;
1380 wwo->writeBytes = 0;
1381 wwo->freeBytes = 0;
1382 wwo->sendBytes = 0;
1383 wwo->SoundBuffer = NULL;
1384 wwo->FlowStarted = 0;
1386 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1387 AuPauseFlow(wwo->AuServ, wwo->AuFlow, NULL);
1388 wwo->FlowStarted = 1;
1390 return 1;
1393 static AuBool
1394 event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
1396 WINE_WAVEOUT *wwo = (WINE_WAVEOUT *)hnd->data;
1397 switch (ev->type) {
1399 case AuEventTypeElementNotify: {
1400 AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
1403 switch (event->kind) {
1404 case AuElementNotifyKindLowWater:
1405 wwo->freeBytes += event->num_bytes;
1406 if (wwo->writeBytes > 0)
1407 wwo->sendBytes += event->num_bytes;
1408 if (wwo->freeBytes && wwo->BufferUsed)
1409 nas_send_buffer(wwo);
1410 break;
1412 case AuElementNotifyKindState:
1413 TRACE("ev: kind %s state %s->%s reason %s numbytes %ld freeB %lu\n",
1414 nas_elementnotify_kind(event->kind),
1415 nas_state(event->prev_state),
1416 nas_state(event->cur_state),
1417 nas_reason(event->reason),
1418 event->num_bytes, wwo->freeBytes);
1420 if (event->cur_state == AuStatePause && event->reason != AuReasonUser) {
1421 wwo->freeBytes += event->num_bytes;
1422 if (wwo->writeBytes > 0)
1423 wwo->sendBytes += event->num_bytes;
1424 if (wwo->sendBytes > wwo->writeBytes)
1425 wwo->sendBytes = wwo->writeBytes;
1426 if (wwo->freeBytes && wwo->BufferUsed)
1427 nas_send_buffer(wwo);
1429 break;
1433 return AuTrue;
1436 static void
1437 buffer_resize(WINE_WAVEOUT* wwo, int len)
1439 void *newbuf = malloc(wwo->BufferUsed + len);
1440 void *oldbuf = wwo->SoundBuffer;
1441 memcpy(newbuf, oldbuf, wwo->BufferUsed);
1442 wwo->SoundBuffer = newbuf;
1443 if (oldbuf != NULL)
1444 free(oldbuf);
1447 static int nas_add_buffer(WINE_WAVEOUT* wwo) {
1448 int len = wwo->lpPlayPtr->dwBufferLength;
1450 buffer_resize(wwo, len);
1451 memcpy(wwo->SoundBuffer + wwo->BufferUsed, wwo->lpPlayPtr->lpData, len);
1452 wwo->BufferUsed += len;
1453 wwo->WrittenTotal += len;
1454 return len;
1457 static int nas_send_buffer(WINE_WAVEOUT* wwo) {
1458 int oldb , len;
1459 char *ptr, *newdata;
1460 newdata = NULL;
1461 oldb = len = 0;
1463 if (wwo->freeBytes <= 0)
1464 return 0;
1466 if (wwo->SoundBuffer == NULL || wwo->BufferUsed == 0) {
1467 return 0;
1470 if (wwo->BufferUsed <= wwo->freeBytes) {
1471 len = wwo->BufferUsed;
1472 ptr = wwo->SoundBuffer;
1473 } else {
1474 len = wwo->freeBytes;
1475 ptr = malloc(len);
1476 memcpy(ptr,wwo->SoundBuffer,len);
1477 newdata = malloc(wwo->BufferUsed - len);
1478 memcpy(newdata, wwo->SoundBuffer + len, wwo->BufferUsed - len);
1481 TRACE("envoye de %d bytes / %lu bytes / freeBytes %lu\n", len, wwo->BufferUsed, wwo->freeBytes);
1483 AuWriteElement(wwo->AuServ, wwo->AuFlow, 0, len, ptr, AuFalse, NULL);
1485 wwo->BufferUsed -= len;
1486 wwo->freeBytes -= len;
1487 wwo->writeBytes += len;
1489 free(ptr);
1491 wwo->SoundBuffer = NULL;
1493 if (newdata != NULL)
1494 wwo->SoundBuffer = newdata;
1496 return len;
1499 static int nas_free(WINE_WAVEOUT* wwo)
1502 if (!wwo->FlowStarted && wwo->BufferUsed) {
1503 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1504 wwo->FlowStarted = 1;
1507 while (wwo->BufferUsed || wwo->writeBytes != wwo->sendBytes) {
1508 if (wwo->freeBytes)
1509 nas_send_buffer(wwo);
1510 AuHandleEvents(wwo->AuServ);
1513 AuFlush(wwo->AuServ);
1514 return TRUE;
1517 static int nas_close(WINE_WAVEOUT* wwo)
1519 AuEvent ev;
1521 nas_free(wwo);
1523 AuStopFlow(wwo->AuServ, wwo->AuFlow, NULL);
1524 AuDestroyFlow(wwo->AuServ, wwo->AuFlow, NULL);
1525 AuFlush(wwo->AuServ);
1526 AuNextEvent(wwo->AuServ, AuTrue, &ev);
1527 AuDispatchEvent(wwo->AuServ, &ev);
1529 wwo->AuFlow = 0;
1530 wwo->open = 0;
1531 wwo->BufferUsed = 0;
1532 wwo->freeBytes = 0;
1533 wwo->SoundBuffer = NULL;
1534 return 1;
1537 static int nas_end(void)
1539 AuCloseServer(AuServ);
1540 AuServ = 0;
1541 return 1;
1544 #else /* !HAVE_NAS */
1546 /**************************************************************************
1547 * wodMessage (WINENAS.@)
1549 DWORD WINAPI NAS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
1551 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1552 return MMSYSERR_NOTENABLED;
1554 #endif