comctl32/treeview: Correctly draw cut items.
[wine/multimedia.git] / dlls / winenas.drv / audio.c
bloba5768fa01f5d9be1da2562748611c95b02afd241
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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>
50 #include <math.h>
52 #define FRAG_SIZE 1024
53 #define FRAG_COUNT 10
55 /* avoid type conflicts */
56 #define INT8 X_INT8
57 #define INT16 X_INT16
58 #define INT32 X_INT32
59 #define INT64 X_INT64
60 #define BOOL X_BOOL
61 #define BYTE X_BYTE
62 #ifdef HAVE_AUDIO_AUDIOLIB_H
63 #include <audio/audiolib.h>
64 #endif
65 #ifdef HAVE_AUDIO_SOUNDLIB_H
66 #include <audio/soundlib.h>
67 #endif
68 #undef INT8
69 #undef INT16
70 #undef INT32
71 #undef INT64
72 #undef LONG64
73 #undef BOOL
74 #undef BYTE
76 #include "windef.h"
77 #include "winbase.h"
78 #include "wingdi.h"
79 #include "winuser.h"
80 #include "winerror.h"
81 #include "mmddk.h"
82 #include "dsound.h"
83 #include "dsdriver.h"
84 #include "wine/unicode.h"
85 #include "wine/debug.h"
87 WINE_DEFAULT_DEBUG_CHANNEL(wave);
89 /* Allow 1% deviation for sample rates (some ES137x cards) */
90 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
92 static AuServer *AuServ;
94 #define MAX_WAVEOUTDRV (1)
96 /* state diagram for waveOut writing:
98 * +---------+-------------+---------------+---------------------------------+
99 * | state | function | event | new state |
100 * +---------+-------------+---------------+---------------------------------+
101 * | | open() | | STOPPED |
102 * | PAUSED | write() | | PAUSED |
103 * | STOPPED | write() | <thrd create> | PLAYING |
104 * | PLAYING | write() | HEADER | PLAYING |
105 * | (other) | write() | <error> | |
106 * | (any) | pause() | PAUSING | PAUSED |
107 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
108 * | (any) | reset() | RESETTING | STOPPED |
109 * | (any) | close() | CLOSING | CLOSED |
110 * +---------+-------------+---------------+---------------------------------+
113 /* states of the playing device */
114 #define WINE_WS_PLAYING 0
115 #define WINE_WS_PAUSED 1
116 #define WINE_WS_STOPPED 2
117 #define WINE_WS_CLOSED 3
119 /* events to be send to device */
120 enum win_wm_message {
121 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
122 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING
125 typedef struct {
126 enum win_wm_message msg; /* message identifier */
127 DWORD_PTR param; /* parameter for this message */
128 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
129 } RING_MSG;
131 /* implement an in-process message ring for better performance
132 * (compared to passing thru the server)
133 * this ring will be used by the input (resp output) record (resp playback) routine
135 #define NAS_RING_BUFFER_INCREMENT 64
136 typedef struct {
137 RING_MSG * messages;
138 int ring_buffer_size;
139 int msg_tosave;
140 int msg_toget;
141 HANDLE msg_event;
142 CRITICAL_SECTION msg_crst;
143 } MSG_RING;
145 typedef struct {
146 volatile int state; /* one of the WINE_WS_ manifest constants */
147 WAVEOPENDESC waveDesc;
148 WORD wFlags;
149 PCMWAVEFORMAT format;
150 WAVEOUTCAPSW caps;
151 int Id;
153 int open;
154 AuServer *AuServ;
155 AuDeviceID AuDev;
156 AuFlowID AuFlow;
157 BOOL FlowStarted;
159 DWORD writeBytes;
160 DWORD freeBytes;
161 DWORD sendBytes;
163 DWORD BufferSize; /* size of whole buffer in bytes */
165 char* SoundBuffer;
166 long BufferUsed;
168 DWORD volume_left; /* volume control information */
169 DWORD volume_right;
171 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
172 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
174 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
175 DWORD dwLoops; /* private copy of loop counter */
177 DWORD PlayedTotal; /* number of bytes actually played since opening */
178 DWORD WrittenTotal; /* number of bytes written to the audio device since opening */
180 /* synchronization stuff */
181 HANDLE hStartUpEvent;
182 HANDLE hThread;
183 DWORD dwThreadID;
184 MSG_RING msgRing;
185 } WINE_WAVEOUT;
187 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
189 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
190 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
193 /* NASFUNC */
194 static AuBool event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd);
195 static int nas_init(void);
196 static int nas_end(void);
198 static int nas_finddev(WINE_WAVEOUT* wwo);
199 static int nas_open(WINE_WAVEOUT* wwo);
200 static int nas_free(WINE_WAVEOUT* wwo);
201 static int nas_close(WINE_WAVEOUT* wwo);
202 static void buffer_resize(WINE_WAVEOUT* wwo, int len);
203 static int nas_add_buffer(WINE_WAVEOUT* wwo);
204 static int nas_send_buffer(WINE_WAVEOUT* wwo);
206 /* These strings used only for tracing */
207 static const char * const wodPlayerCmdString[] = {
208 "WINE_WM_PAUSING",
209 "WINE_WM_RESTARTING",
210 "WINE_WM_RESETTING",
211 "WINE_WM_HEADER",
212 "WINE_WM_UPDATE",
213 "WINE_WM_BREAKLOOP",
214 "WINE_WM_CLOSING",
217 static const char * const nas_elementnotify_kinds[] = {
218 "LowWater",
219 "HighWater",
220 "State",
221 "Unknown"
224 static const char * const nas_states[] = {
225 "Stop",
226 "Start",
227 "Pause",
228 "Any"
231 static const char * const nas_reasons[] = {
232 "User",
233 "Underrun",
234 "Overrun",
235 "EOF",
236 "Watermark",
237 "Hardware",
238 "Any"
241 static const char* nas_reason(unsigned int reason)
243 if (reason > 6) reason = 6;
244 return nas_reasons[reason];
247 static const char* nas_elementnotify_kind(unsigned int kind)
249 if (kind > 2) kind = 3;
250 return nas_elementnotify_kinds[kind];
254 #if 0
255 static const char* nas_event_type(unsigned int type)
257 static const char * const nas_event_types[] =
259 "Undefined",
260 "Undefined",
261 "ElementNotify",
262 "GrabNotify",
263 "MonitorNotify",
264 "BucketNotify",
265 "DeviceNotify"
268 if (type > 6) type = 0;
269 return nas_event_types[type];
271 #endif
274 static const char* nas_state(unsigned int state)
276 if (state > 3) state = 3;
277 return nas_states[state];
280 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
281 PCMWAVEFORMAT* format)
283 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
284 lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
285 format->wf.nChannels, format->wf.nAvgBytesPerSec);
286 TRACE("Position in bytes=%u\n", position);
288 switch (lpTime->wType) {
289 case TIME_SAMPLES:
290 lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
291 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
292 break;
293 case TIME_MS:
294 lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
295 TRACE("TIME_MS=%u\n", lpTime->u.ms);
296 break;
297 case TIME_SMPTE:
298 lpTime->u.smpte.fps = 30;
299 position = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
300 position += (format->wf.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
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 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
310 lpTime->u.smpte.hour, lpTime->u.smpte.min,
311 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
312 break;
313 default:
314 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
315 lpTime->wType = TIME_BYTES;
316 /* fall through */
317 case TIME_BYTES:
318 lpTime->u.cb = position;
319 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
320 break;
322 return MMSYSERR_NOERROR;
325 /*======================================================================*
326 * Low level WAVE implementation *
327 *======================================================================*/
328 #if 0
329 /* Volume functions derived from Alsaplayer source */
330 /* length is the number of 16 bit samples */
331 static void volume_effect16(void *bufin, void* bufout, int length, int left,
332 int right, int nChannels)
334 short *d_out = bufout;
335 short *d_in = bufin;
336 int i, v;
339 TRACE("length == %d, nChannels == %d\n", length, nChannels);
342 if (right == -1) right = left;
344 for(i = 0; i < length; i+=(nChannels))
346 v = (int) ((*(d_in++) * left) / 100);
347 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
348 if(nChannels == 2)
350 v = (int) ((*(d_in++) * right) / 100);
351 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
356 /* length is the number of 8 bit samples */
357 static void volume_effect8(void *bufin, void* bufout, int length, int left,
358 int right, int nChannels)
360 char *d_out = bufout;
361 char *d_in = bufin;
362 int i, v;
365 TRACE("length == %d, nChannels == %d\n", length, nChannels);
368 if (right == -1) right = left;
370 for(i = 0; i < length; i+=(nChannels))
372 v = (char) ((*(d_in++) * left) / 100);
373 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
374 if(nChannels == 2)
376 v = (char) ((*(d_in++) * right) / 100);
377 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
381 #endif
383 /******************************************************************
384 * NAS_CloseDevice
387 static void NAS_CloseDevice(WINE_WAVEOUT* wwo)
389 TRACE("NAS_CloseDevice\n");
390 nas_close(wwo);
393 /******************************************************************
394 * NAS_WaveClose
396 static LONG NAS_WaveClose(void)
398 nas_end(); /* free up nas server */
399 return 1;
402 /******************************************************************
403 * NAS_WaveInit
405 * Initialize internal structures from NAS server info
407 static LONG NAS_WaveInit(void)
409 int i;
410 if (!nas_init()) return MMSYSERR_ERROR;
412 /* initialize all device handles to -1 */
413 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
415 static const WCHAR ini[] = {'N','A','S',' ','W','A','V','E','O','U','T',' ','D','r','i','v','e','r',0};
416 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out caps values */
418 WOutDev[i].AuServ = AuServ;
419 WOutDev[i].AuDev = AuNone;
420 WOutDev[i].Id = i;
421 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
422 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
423 strcpyW(WOutDev[i].caps.szPname, ini);
424 WOutDev[i].AuFlow = 0;
425 WOutDev[i].caps.vDriverVersion = 0x0100;
426 WOutDev[i].caps.dwFormats = 0x00000000;
427 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
429 WOutDev[i].caps.wChannels = 2;
430 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
432 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
433 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
434 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
435 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
436 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
437 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
438 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
439 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
440 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
441 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
442 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
443 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
447 return 0;
450 /******************************************************************
451 * NAS_InitRingMessage
453 * Initialize the ring of messages for passing between driver's caller and playback/record
454 * thread
456 static int NAS_InitRingMessage(MSG_RING* mr)
458 mr->msg_toget = 0;
459 mr->msg_tosave = 0;
460 mr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
461 mr->ring_buffer_size = NAS_RING_BUFFER_INCREMENT;
462 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
463 InitializeCriticalSection(&mr->msg_crst);
464 mr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MSG_RING.msg_crst");
465 return 0;
468 /******************************************************************
469 * NAS_DestroyRingMessage
472 static int NAS_DestroyRingMessage(MSG_RING* mr)
474 CloseHandle(mr->msg_event);
475 HeapFree(GetProcessHeap(),0,mr->messages);
476 mr->msg_crst.DebugInfo->Spare[0] = 0;
477 DeleteCriticalSection(&mr->msg_crst);
478 return 0;
481 /******************************************************************
482 * NAS_AddRingMessage
484 * Inserts a new message into the ring (should be called from DriverProc derived routines)
486 static int NAS_AddRingMessage(MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
488 HANDLE hEvent = INVALID_HANDLE_VALUE;
490 EnterCriticalSection(&mr->msg_crst);
491 if (mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size))
493 int old_ring_buffer_size = mr->ring_buffer_size;
494 mr->ring_buffer_size += NAS_RING_BUFFER_INCREMENT;
495 TRACE("omr->ring_buffer_size=%d\n",mr->ring_buffer_size);
496 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
497 /* Now we need to rearrange the ring buffer so that the new
498 buffers just allocated are in between mr->msg_tosave and
499 mr->msg_toget.
501 if (mr->msg_tosave < mr->msg_toget)
503 memmove(&(mr->messages[mr->msg_toget + NAS_RING_BUFFER_INCREMENT]),
504 &(mr->messages[mr->msg_toget]),
505 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
507 mr->msg_toget += NAS_RING_BUFFER_INCREMENT;
510 if (wait)
512 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
513 if (hEvent == INVALID_HANDLE_VALUE)
515 ERR("can't create event !?\n");
516 LeaveCriticalSection(&mr->msg_crst);
517 return 0;
519 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
520 FIXME("two fast messages in the queue!!!!\n");
522 /* fast messages have to be added at the start of the queue */
523 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
525 mr->messages[mr->msg_toget].msg = msg;
526 mr->messages[mr->msg_toget].param = param;
527 mr->messages[mr->msg_toget].hEvent = hEvent;
529 else
531 mr->messages[mr->msg_tosave].msg = msg;
532 mr->messages[mr->msg_tosave].param = param;
533 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
534 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
537 LeaveCriticalSection(&mr->msg_crst);
539 SetEvent(mr->msg_event); /* signal a new message */
541 if (wait)
543 /* wait for playback/record thread to have processed the message */
544 WaitForSingleObject(hEvent, INFINITE);
545 CloseHandle(hEvent);
548 return 1;
551 /******************************************************************
552 * NAS_RetrieveRingMessage
554 * Get a message from the ring. Should be called by the playback/record thread.
556 static int NAS_RetrieveRingMessage(MSG_RING* mr, enum win_wm_message *msg,
557 DWORD_PTR *param, HANDLE *hEvent)
559 EnterCriticalSection(&mr->msg_crst);
561 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
563 LeaveCriticalSection(&mr->msg_crst);
564 return 0;
567 *msg = mr->messages[mr->msg_toget].msg;
568 mr->messages[mr->msg_toget].msg = 0;
569 *param = mr->messages[mr->msg_toget].param;
570 *hEvent = mr->messages[mr->msg_toget].hEvent;
571 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
572 LeaveCriticalSection(&mr->msg_crst);
573 return 1;
576 /*======================================================================*
577 * Low level WAVE OUT implementation *
578 *======================================================================*/
580 /**************************************************************************
581 * wodNotifyClient [internal]
583 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD_PTR dwParam1,
584 DWORD_PTR dwParam2)
586 TRACE("wMsg = 0x%04x dwParm1 = %08lX dwParam2 = %08lX\n", wMsg, dwParam1, dwParam2);
588 switch (wMsg) {
589 case WOM_OPEN:
590 case WOM_CLOSE:
591 case WOM_DONE:
592 if (wwo->wFlags != DCB_NULL &&
593 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
594 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
595 WARN("can't notify client !\n");
596 return MMSYSERR_ERROR;
598 break;
599 default:
600 FIXME("Unknown callback message %u\n", wMsg);
601 return MMSYSERR_INVALPARAM;
603 return MMSYSERR_NOERROR;
606 /**************************************************************************
607 * wodUpdatePlayedTotal [internal]
610 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
612 wwo->PlayedTotal = wwo->WrittenTotal;
613 return TRUE;
616 /**************************************************************************
617 * wodPlayer_BeginWaveHdr [internal]
619 * Makes the specified lpWaveHdr the currently playing wave header.
620 * If the specified wave header is a begin loop and we're not already in
621 * a loop, setup the loop.
623 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
625 wwo->lpPlayPtr = lpWaveHdr;
627 if (!lpWaveHdr) return;
629 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
630 if (wwo->lpLoopPtr) {
631 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
632 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
633 } else {
634 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
635 wwo->lpLoopPtr = lpWaveHdr;
636 /* Windows does not touch WAVEHDR.dwLoops,
637 * so we need to make an internal copy */
638 wwo->dwLoops = lpWaveHdr->dwLoops;
643 /**************************************************************************
644 * wodPlayer_PlayPtrNext [internal]
646 * Advance the play pointer to the next waveheader, looping if required.
648 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
650 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
652 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
653 /* We're at the end of a loop, loop if required */
654 if (--wwo->dwLoops > 0) {
655 wwo->lpPlayPtr = wwo->lpLoopPtr;
656 } else {
657 /* Handle overlapping loops correctly */
658 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
659 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
660 /* shall we consider the END flag for the closing loop or for
661 * the opening one or for both ???
662 * code assumes for closing loop only
664 } else {
665 lpWaveHdr = lpWaveHdr->lpNext;
667 wwo->lpLoopPtr = NULL;
668 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
670 } else {
671 /* We're not in a loop. Advance to the next wave header */
672 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
674 return lpWaveHdr;
677 /**************************************************************************
678 * wodPlayer_NotifyCompletions [internal]
680 * Notifies and remove from queue all wavehdrs which have been played to
681 * the speaker (ie. they have cleared the audio device). If force is true,
682 * we notify all wavehdrs and remove them all from the queue even if they
683 * are unplayed or part of a loop.
685 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
687 LPWAVEHDR lpWaveHdr;
689 /* Start from lpQueuePtr and keep notifying until:
690 * - we hit an unwritten wavehdr
691 * - we hit the beginning of a running loop
692 * - we hit a wavehdr which hasn't finished playing
694 wodUpdatePlayedTotal(wwo);
696 while ((lpWaveHdr = wwo->lpQueuePtr) && (force || (lpWaveHdr != wwo->lpPlayPtr &&
697 lpWaveHdr != wwo->lpLoopPtr && lpWaveHdr->reserved <= wwo->PlayedTotal))) {
699 wwo->lpQueuePtr = lpWaveHdr->lpNext;
701 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
702 lpWaveHdr->dwFlags |= WHDR_DONE;
704 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
706 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
707 1 : 1;
710 /**************************************************************************
711 * wodPlayer_Reset [internal]
713 * wodPlayer helper. Resets current output stream.
715 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
717 wodUpdatePlayedTotal(wwo);
718 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
720 /* we aren't able to flush any data that has already been written */
721 /* to nas, otherwise we would do the flushing here */
723 nas_free(wwo);
725 if (reset) {
726 enum win_wm_message msg;
727 DWORD_PTR param;
728 HANDLE ev;
730 /* remove any buffer */
731 wodPlayer_NotifyCompletions(wwo, TRUE);
733 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
734 wwo->state = WINE_WS_STOPPED;
735 wwo->PlayedTotal = wwo->WrittenTotal = 0;
737 /* remove any existing message in the ring */
738 EnterCriticalSection(&wwo->msgRing.msg_crst);
740 /* return all pending headers in queue */
741 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
743 TRACE("flushing msg\n");
744 if (msg != WINE_WM_HEADER)
746 FIXME("shouldn't have headers left\n");
747 SetEvent(ev);
748 continue;
750 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
751 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
753 wodNotifyClient(wwo, WOM_DONE, param, 0);
755 ResetEvent(wwo->msgRing.msg_event);
756 LeaveCriticalSection(&wwo->msgRing.msg_crst);
757 } else {
758 if (wwo->lpLoopPtr) {
759 /* complicated case, not handled yet (could imply modifying the loop counter */
760 FIXME("Pausing while in loop isn't correctly handled yet, expec strange results\n");
761 wwo->lpPlayPtr = wwo->lpLoopPtr;
762 wwo->WrittenTotal = wwo->PlayedTotal; /* this is wrong !!! */
763 } else {
764 /* the data already written is going to be played, so take */
765 /* this fact into account here */
766 wwo->PlayedTotal = wwo->WrittenTotal;
768 wwo->state = WINE_WS_PAUSED;
772 /**************************************************************************
773 * wodPlayer_ProcessMessages [internal]
775 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
777 LPWAVEHDR lpWaveHdr;
778 enum win_wm_message msg;
779 DWORD_PTR param;
780 HANDLE ev;
782 while (NAS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
783 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
784 switch (msg) {
785 case WINE_WM_PAUSING:
786 wodPlayer_Reset(wwo, FALSE);
787 SetEvent(ev);
788 break;
789 case WINE_WM_RESTARTING:
790 wwo->state = WINE_WS_PLAYING;
791 SetEvent(ev);
792 break;
793 case WINE_WM_HEADER:
794 lpWaveHdr = (LPWAVEHDR)param;
796 /* insert buffer at the end of queue */
798 LPWAVEHDR* wh;
799 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
800 *wh = lpWaveHdr;
802 if (!wwo->lpPlayPtr)
803 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
804 if (wwo->state == WINE_WS_STOPPED)
805 wwo->state = WINE_WS_PLAYING;
806 break;
807 case WINE_WM_RESETTING:
808 wodPlayer_Reset(wwo, TRUE);
809 SetEvent(ev);
810 break;
811 case WINE_WM_UPDATE:
812 wodUpdatePlayedTotal(wwo);
813 SetEvent(ev);
814 break;
815 case WINE_WM_BREAKLOOP:
816 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
817 /* ensure exit at end of current loop */
818 wwo->dwLoops = 1;
820 SetEvent(ev);
821 break;
822 case WINE_WM_CLOSING:
823 /* sanity check: this should not happen since the device must have been reset before */
824 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
825 wwo->hThread = 0;
826 wwo->state = WINE_WS_CLOSED;
827 SetEvent(ev);
828 ExitThread(0);
829 /* shouldn't go here */
830 default:
831 FIXME("unknown message %d\n", msg);
832 break;
837 /**************************************************************************
838 * wodPlayer [internal]
840 static DWORD CALLBACK wodPlayer(LPVOID pmt)
842 WORD uDevID = (DWORD_PTR)pmt;
843 WINE_WAVEOUT* wwo = &WOutDev[uDevID];
845 wwo->state = WINE_WS_STOPPED;
846 SetEvent(wwo->hStartUpEvent);
848 for (;;) {
850 if (wwo->FlowStarted) {
851 AuHandleEvents(wwo->AuServ);
853 if (wwo->state == WINE_WS_PLAYING && wwo->freeBytes && wwo->BufferUsed)
854 nas_send_buffer(wwo);
857 if (wwo->BufferUsed <= FRAG_SIZE && wwo->writeBytes > 0)
858 wodPlayer_NotifyCompletions(wwo, FALSE);
860 WaitForSingleObject(wwo->msgRing.msg_event, 20);
861 wodPlayer_ProcessMessages(wwo);
863 while(wwo->lpPlayPtr) {
864 wwo->lpPlayPtr->reserved = wwo->WrittenTotal + wwo->lpPlayPtr->dwBufferLength;
865 nas_add_buffer(wwo);
866 wodPlayer_PlayPtrNext(wwo);
868 return 0;
872 /**************************************************************************
873 * wodGetDevCaps [internal]
875 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
877 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
879 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
881 if (wDevID >= MAX_WAVEOUTDRV) {
882 TRACE("MAX_WAVOUTDRV reached !\n");
883 return MMSYSERR_BADDEVICEID;
886 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
887 return MMSYSERR_NOERROR;
890 /**************************************************************************
891 * wodOpen [internal]
893 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
895 WINE_WAVEOUT* wwo;
897 TRACE("wodOpen (%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
899 if (lpDesc == NULL) {
900 WARN("Invalid Parameter !\n");
901 return MMSYSERR_INVALPARAM;
903 if (wDevID >= MAX_WAVEOUTDRV) {
904 TRACE("MAX_WAVOUTDRV reached !\n");
905 return MMSYSERR_BADDEVICEID;
908 /* if this device is already open tell the app that it is allocated */
910 wwo = &WOutDev[wDevID];
912 if(wwo->open)
914 TRACE("device already allocated\n");
915 return MMSYSERR_ALLOCATED;
919 /* only PCM format is supported so far... */
920 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
921 lpDesc->lpFormat->nChannels == 0 ||
922 lpDesc->lpFormat->nSamplesPerSec == 0 ||
923 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
924 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
925 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
926 lpDesc->lpFormat->nSamplesPerSec);
927 return WAVERR_BADFORMAT;
930 if (dwFlags & WAVE_FORMAT_QUERY) {
931 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
932 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
933 lpDesc->lpFormat->nSamplesPerSec);
934 return MMSYSERR_NOERROR;
937 /* direct sound not supported, ignore the flag */
938 dwFlags &= ~WAVE_DIRECTSOUND;
940 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
942 wwo->waveDesc = *lpDesc;
943 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
945 if (wwo->format.wBitsPerSample == 0) {
946 WARN("Resetting zeroed wBitsPerSample\n");
947 wwo->format.wBitsPerSample = 8 *
948 (wwo->format.wf.nAvgBytesPerSec /
949 wwo->format.wf.nSamplesPerSec) /
950 wwo->format.wf.nChannels;
953 if (!nas_open(wwo))
954 return MMSYSERR_ALLOCATED;
956 NAS_InitRingMessage(&wwo->msgRing);
958 /* create player thread */
959 if (!(dwFlags & WAVE_DIRECTSOUND)) {
960 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
961 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID,
962 0, &(wwo->dwThreadID));
963 if (wwo->hThread)
964 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
965 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
966 CloseHandle(wwo->hStartUpEvent);
967 } else {
968 wwo->hThread = INVALID_HANDLE_VALUE;
969 wwo->dwThreadID = 0;
971 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
973 TRACE("stream=0x%lx, BufferSize=%d\n", (long)wwo->AuServ, wwo->BufferSize);
975 TRACE("wBitsPerSample=%u nAvgBytesPerSec=%u nSamplesPerSec=%u nChannels=%u nBlockAlign=%u\n",
976 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
977 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
978 wwo->format.wf.nBlockAlign);
980 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
983 /**************************************************************************
984 * wodClose [internal]
986 static DWORD wodClose(WORD wDevID)
988 DWORD ret = MMSYSERR_NOERROR;
989 WINE_WAVEOUT* wwo;
991 TRACE("(%u);\n", wDevID);
993 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
995 WARN("bad device ID !\n");
996 return MMSYSERR_BADDEVICEID;
999 wwo = &WOutDev[wDevID];
1000 if (wwo->lpQueuePtr) {
1001 WARN("buffers still playing !\n");
1002 ret = WAVERR_STILLPLAYING;
1003 } else {
1004 TRACE("imhere[3-close]\n");
1005 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1006 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1009 NAS_DestroyRingMessage(&wwo->msgRing);
1011 NAS_CloseDevice(wwo); /* close the stream and clean things up */
1013 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1015 return ret;
1018 /**************************************************************************
1019 * wodWrite [internal]
1022 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1024 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
1026 /* first, do the sanity checks... */
1027 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1029 WARN("bad dev ID !\n");
1030 return MMSYSERR_BADDEVICEID;
1033 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1035 TRACE("unprepared\n");
1036 return WAVERR_UNPREPARED;
1039 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1041 TRACE("still playing\n");
1042 return WAVERR_STILLPLAYING;
1045 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1046 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1047 lpWaveHdr->lpNext = 0;
1049 TRACE("adding ring message\n");
1050 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
1052 return MMSYSERR_NOERROR;
1055 /**************************************************************************
1056 * wodPause [internal]
1058 static DWORD wodPause(WORD wDevID)
1060 TRACE("(%u);!\n", wDevID);
1062 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1064 WARN("bad device ID !\n");
1065 return MMSYSERR_BADDEVICEID;
1068 TRACE("imhere[3-PAUSING]\n");
1069 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1071 return MMSYSERR_NOERROR;
1074 /**************************************************************************
1075 * wodRestart [internal]
1077 static DWORD wodRestart(WORD wDevID)
1079 TRACE("(%u);\n", wDevID);
1081 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1083 WARN("bad device ID !\n");
1084 return MMSYSERR_BADDEVICEID;
1087 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1088 TRACE("imhere[3-RESTARTING]\n");
1089 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1092 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1093 /* FIXME: Myst crashes with this ... hmm -MM
1094 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1097 return MMSYSERR_NOERROR;
1100 /**************************************************************************
1101 * wodReset [internal]
1103 static DWORD wodReset(WORD wDevID)
1105 TRACE("(%u);\n", wDevID);
1107 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1109 WARN("bad device ID !\n");
1110 return MMSYSERR_BADDEVICEID;
1113 TRACE("imhere[3-RESET]\n");
1114 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1116 return MMSYSERR_NOERROR;
1119 /**************************************************************************
1120 * wodGetPosition [internal]
1122 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1124 WINE_WAVEOUT* wwo;
1126 TRACE("%u, %p, %u);\n", wDevID, lpTime, uSize);
1128 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1130 WARN("bad device ID !\n");
1131 return MMSYSERR_BADDEVICEID;
1134 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1136 wwo = &WOutDev[wDevID];
1137 #if 0
1138 NAS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1139 #endif
1141 return bytes_to_mmtime(lpTime, wwo->WrittenTotal, &wwo->format);
1144 /**************************************************************************
1145 * wodBreakLoop [internal]
1147 static DWORD wodBreakLoop(WORD wDevID)
1149 TRACE("(%u);\n", wDevID);
1151 if (wDevID >= MAX_WAVEOUTDRV || AuServ == NULL)
1153 WARN("bad device ID !\n");
1154 return MMSYSERR_BADDEVICEID;
1156 NAS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1157 return MMSYSERR_NOERROR;
1160 /**************************************************************************
1161 * wodGetVolume [internal]
1163 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1165 DWORD left, right;
1167 left = WOutDev[wDevID].volume_left;
1168 right = WOutDev[wDevID].volume_right;
1170 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1172 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1174 return MMSYSERR_NOERROR;
1177 /**************************************************************************
1178 * wodSetVolume [internal]
1180 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1182 DWORD left, right;
1184 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1185 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1187 TRACE("(%u, %08X);\n", wDevID, dwParam);
1189 WOutDev[wDevID].volume_left = left;
1190 WOutDev[wDevID].volume_right = right;
1192 return MMSYSERR_NOERROR;
1195 /**************************************************************************
1196 * wodGetNumDevs [internal]
1198 static DWORD wodGetNumDevs(void)
1200 return MAX_WAVEOUTDRV;
1203 /**************************************************************************
1204 * wodMessage (WINENAS.@)
1206 DWORD WINAPI NAS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1207 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1209 TRACE("(%u, %04X, %08X, %08lX, %08lX);\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1211 switch (wMsg) {
1212 case DRVM_INIT:
1213 return NAS_WaveInit();
1214 case DRVM_EXIT:
1215 return NAS_WaveClose();
1216 case DRVM_ENABLE:
1217 case DRVM_DISABLE:
1218 /* FIXME: Pretend this is supported */
1219 return 0;
1220 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1221 case WODM_CLOSE: return wodClose (wDevID);
1222 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1223 case WODM_PAUSE: return wodPause (wDevID);
1224 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1225 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1226 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
1227 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
1228 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
1229 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1230 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1231 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1232 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1233 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1234 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1235 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1236 case WODM_RESTART: return wodRestart (wDevID);
1237 case WODM_RESET: return wodReset (wDevID);
1239 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1240 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1241 default:
1242 FIXME("unknown message %d!\n", wMsg);
1244 return MMSYSERR_NOTSUPPORTED;
1247 /*======================================================================*
1248 * Low level DSOUND implementation *
1249 *======================================================================*/
1250 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1252 /* we can't perform memory mapping as we don't have a file stream
1253 interface with nas like we do with oss */
1254 MESSAGE("This sound card s driver does not support direct access\n");
1255 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1256 return MMSYSERR_NOTSUPPORTED;
1259 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1261 memset(desc, 0, sizeof(*desc));
1262 strcpy(desc->szDesc, "Wine NAS DirectSound Driver");
1263 strcpy(desc->szDrvname, "winenas.drv");
1264 return MMSYSERR_NOERROR;
1267 static int nas_init(void) {
1268 TRACE("NAS INIT\n");
1269 if (!(AuServ = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL)))
1270 return 0;
1272 return 1;
1275 static int nas_finddev(WINE_WAVEOUT* wwo) {
1276 int i;
1278 for (i = 0; i < AuServerNumDevices(wwo->AuServ); i++) {
1279 if ((AuDeviceKind(AuServerDevice(wwo->AuServ, i)) ==
1280 AuComponentKindPhysicalOutput) &&
1281 AuDeviceNumTracks(AuServerDevice(wwo->AuServ, i)) == wwo->format.wf.nChannels)
1283 wwo->AuDev = AuDeviceIdentifier(AuServerDevice(wwo->AuServ, i));
1284 break;
1288 if (wwo->AuDev == AuNone)
1289 return 0;
1290 return 1;
1293 static int nas_open(WINE_WAVEOUT* wwo) {
1294 AuElement elements[3];
1296 if (!wwo->AuServ)
1297 return 0;
1299 if (!nas_finddev(wwo))
1300 return 0;
1302 if (!(wwo->AuFlow = AuCreateFlow(wwo->AuServ, NULL)))
1303 return 0;
1305 wwo->BufferSize = FRAG_SIZE * FRAG_COUNT;
1307 AuMakeElementImportClient(&elements[0], wwo->format.wf.nSamplesPerSec,
1308 wwo->format.wBitsPerSample == 16 ? AuFormatLinearSigned16LSB : AuFormatLinearUnsigned8,
1309 wwo->format.wf.nChannels, AuTrue, wwo->BufferSize, wwo->BufferSize / 2, 0, NULL);
1311 AuMakeElementExportDevice(&elements[1], 0, wwo->AuDev, wwo->format.wf.nSamplesPerSec,
1312 AuUnlimitedSamples, 0, NULL);
1314 AuSetElements(wwo->AuServ, wwo->AuFlow, AuTrue, 2, elements, NULL);
1316 AuRegisterEventHandler(wwo->AuServ, AuEventHandlerIDMask, 0, wwo->AuFlow,
1317 event_handler, (AuPointer) wwo);
1320 wwo->PlayedTotal = 0;
1321 wwo->WrittenTotal = 0;
1322 wwo->open = 1;
1324 wwo->BufferUsed = 0;
1325 wwo->writeBytes = 0;
1326 wwo->freeBytes = 0;
1327 wwo->sendBytes = 0;
1328 wwo->SoundBuffer = NULL;
1329 wwo->FlowStarted = 0;
1331 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1332 AuPauseFlow(wwo->AuServ, wwo->AuFlow, NULL);
1333 wwo->FlowStarted = 1;
1335 return 1;
1338 static AuBool
1339 event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
1341 WINE_WAVEOUT *wwo = hnd->data;
1342 switch (ev->type) {
1344 case AuEventTypeElementNotify: {
1345 AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
1348 switch (event->kind) {
1349 case AuElementNotifyKindLowWater:
1350 wwo->freeBytes += event->num_bytes;
1351 if (wwo->writeBytes > 0)
1352 wwo->sendBytes += event->num_bytes;
1353 if (wwo->freeBytes && wwo->BufferUsed)
1354 nas_send_buffer(wwo);
1355 break;
1357 case AuElementNotifyKindState:
1358 TRACE("ev: kind %s state %s->%s reason %s numbytes %ld freeB %u\n",
1359 nas_elementnotify_kind(event->kind),
1360 nas_state(event->prev_state),
1361 nas_state(event->cur_state),
1362 nas_reason(event->reason),
1363 event->num_bytes, wwo->freeBytes);
1365 if (event->cur_state == AuStatePause && event->reason != AuReasonUser) {
1366 wwo->freeBytes += event->num_bytes;
1367 if (wwo->writeBytes > 0)
1368 wwo->sendBytes += event->num_bytes;
1369 if (wwo->sendBytes > wwo->writeBytes)
1370 wwo->sendBytes = wwo->writeBytes;
1371 if (wwo->freeBytes && wwo->BufferUsed)
1372 nas_send_buffer(wwo);
1374 break;
1378 return AuTrue;
1381 static void
1382 buffer_resize(WINE_WAVEOUT* wwo, int len)
1384 void *newbuf = HeapAlloc(GetProcessHeap(), 0, wwo->BufferUsed + len);
1385 void *oldbuf = wwo->SoundBuffer;
1386 memcpy(newbuf, oldbuf, wwo->BufferUsed);
1387 wwo->SoundBuffer = newbuf;
1388 HeapFree(GetProcessHeap(), 0, oldbuf);
1391 static int nas_add_buffer(WINE_WAVEOUT* wwo) {
1392 int len = wwo->lpPlayPtr->dwBufferLength;
1394 buffer_resize(wwo, len);
1395 memcpy(wwo->SoundBuffer + wwo->BufferUsed, wwo->lpPlayPtr->lpData, len);
1396 wwo->BufferUsed += len;
1397 wwo->WrittenTotal += len;
1398 return len;
1401 static int nas_send_buffer(WINE_WAVEOUT* wwo) {
1402 int len = 0;
1403 char *ptr, *newdata;
1404 newdata = NULL;
1406 if (wwo->freeBytes <= 0)
1407 return 0;
1409 if (wwo->SoundBuffer == NULL || wwo->BufferUsed == 0) {
1410 return 0;
1413 if (wwo->BufferUsed <= wwo->freeBytes) {
1414 len = wwo->BufferUsed;
1415 ptr = wwo->SoundBuffer;
1416 } else {
1417 len = wwo->freeBytes;
1418 ptr = HeapAlloc(GetProcessHeap(), 0, len);
1419 memcpy(ptr,wwo->SoundBuffer,len);
1420 newdata = HeapAlloc(GetProcessHeap(), 0, wwo->BufferUsed - len);
1421 memcpy(newdata, wwo->SoundBuffer + len, wwo->BufferUsed - len);
1424 TRACE("envoye de %d bytes / %lu bytes / freeBytes %u\n", len, wwo->BufferUsed, wwo->freeBytes);
1426 AuWriteElement(wwo->AuServ, wwo->AuFlow, 0, len, ptr, AuFalse, NULL);
1428 wwo->BufferUsed -= len;
1429 wwo->freeBytes -= len;
1430 wwo->writeBytes += len;
1432 HeapFree(GetProcessHeap(), 0, ptr);
1434 wwo->SoundBuffer = NULL;
1436 if (newdata != NULL)
1437 wwo->SoundBuffer = newdata;
1439 return len;
1442 static int nas_free(WINE_WAVEOUT* wwo)
1445 if (!wwo->FlowStarted && wwo->BufferUsed) {
1446 AuStartFlow(wwo->AuServ, wwo->AuFlow, NULL);
1447 wwo->FlowStarted = 1;
1450 while (wwo->BufferUsed || wwo->writeBytes != wwo->sendBytes) {
1451 if (wwo->freeBytes)
1452 nas_send_buffer(wwo);
1453 AuHandleEvents(wwo->AuServ);
1456 AuFlush(wwo->AuServ);
1457 return TRUE;
1460 static int nas_close(WINE_WAVEOUT* wwo)
1462 AuEvent ev;
1464 nas_free(wwo);
1466 AuStopFlow(wwo->AuServ, wwo->AuFlow, NULL);
1467 AuDestroyFlow(wwo->AuServ, wwo->AuFlow, NULL);
1468 AuFlush(wwo->AuServ);
1469 AuNextEvent(wwo->AuServ, AuTrue, &ev);
1470 AuDispatchEvent(wwo->AuServ, &ev);
1472 wwo->AuFlow = 0;
1473 wwo->open = 0;
1474 wwo->BufferUsed = 0;
1475 wwo->freeBytes = 0;
1476 wwo->SoundBuffer = NULL;
1477 return 1;
1480 static int nas_end(void)
1482 if (AuServ)
1484 AuCloseServer(AuServ);
1485 AuServ = 0;
1487 return 1;
1491 /**************************************************************************
1492 * DriverProc (WINENAS.@)
1494 LRESULT CALLBACK NAS_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1495 LPARAM dwParam1, LPARAM dwParam2)
1497 switch(wMsg) {
1498 case DRV_LOAD:
1499 case DRV_FREE:
1500 case DRV_OPEN:
1501 case DRV_CLOSE:
1502 case DRV_ENABLE:
1503 case DRV_DISABLE:
1504 case DRV_QUERYCONFIGURE:
1505 return 1;
1506 case DRV_CONFIGURE: MessageBoxA(0, "NAS MultiMedia Driver !", "NAS Driver", MB_OK); return 1;
1507 case DRV_INSTALL:
1508 case DRV_REMOVE:
1509 return DRV_SUCCESS;
1510 default:
1511 return 0;