Add a couple of missing spec files.
[wine.git] / dlls / winmm / winearts / audio.c
blob2f9a255adbda0f962e84dc2121d1d21afbf0c6f8
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Wine Driver for aRts Sound Server
4 * http://www.arts-project.org
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)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* NOTE:
26 * with arts we cannot stop the audio that is already in
27 * the servers buffer, so to reduce delays during starting
28 * and stoppping of audio streams adjust the
29 * audio buffer size in the kde control center or in the
30 * artsd startup script
32 * FIXME:
33 * pause in waveOut does not work correctly in loop mode
35 * TODO:
36 * implement wave-in support with artsc
39 /*#define EMULATE_SB16*/
41 #include "config.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <string.h>
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50 #include <fcntl.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "wingdi.h"
54 #include "winerror.h"
55 #include "wine/winuser16.h"
56 #include "mmddk.h"
57 #include "dsound.h"
58 #include "dsdriver.h"
59 #include "arts.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wave);
64 #ifdef HAVE_ARTS
66 #include <artsc.h>
68 #define BUFFER_SIZE 16 * 1024
69 #define SPACE_THRESHOLD 5 * 1024
71 #define MAX_WAVEOUTDRV (10)
73 /* state diagram for waveOut writing:
75 * +---------+-------------+---------------+---------------------------------+
76 * | state | function | event | new state |
77 * +---------+-------------+---------------+---------------------------------+
78 * | | open() | | STOPPED |
79 * | PAUSED | write() | | PAUSED |
80 * | STOPPED | write() | <thrd create> | PLAYING |
81 * | PLAYING | write() | HEADER | PLAYING |
82 * | (other) | write() | <error> | |
83 * | (any) | pause() | PAUSING | PAUSED |
84 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
85 * | (any) | reset() | RESETTING | STOPPED |
86 * | (any) | close() | CLOSING | CLOSED |
87 * +---------+-------------+---------------+---------------------------------+
90 /* states of the playing device */
91 #define WINE_WS_PLAYING 0
92 #define WINE_WS_PAUSED 1
93 #define WINE_WS_STOPPED 2
94 #define WINE_WS_CLOSED 3
96 /* events to be send to device */
97 enum win_wm_message {
98 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
99 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING
102 typedef struct {
103 enum win_wm_message msg; /* message identifier */
104 DWORD param; /* parameter for this message */
105 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
106 } RING_MSG;
108 /* implement an in-process message ring for better performance
109 * (compared to passing thru the server)
110 * this ring will be used by the input (resp output) record (resp playback) routine
112 #define ARTS_RING_BUFFER_INCREMENT 64
113 typedef struct {
114 RING_MSG * messages;
115 int ring_buffer_size;
116 int msg_tosave;
117 int msg_toget;
118 HANDLE msg_event;
119 CRITICAL_SECTION msg_crst;
120 } ARTS_MSG_RING;
122 typedef struct {
123 volatile int state; /* one of the WINE_WS_ manifest constants */
124 WAVEOPENDESC waveDesc;
125 WORD wFlags;
126 PCMWAVEFORMAT format;
127 WAVEOUTCAPSA caps;
129 /* arts information */
130 arts_stream_t play_stream; /* the stream structure we get from arts when opening a stream for playing */
131 DWORD dwBufferSize; /* size of whole buffer in bytes */
133 char* sound_buffer;
134 long buffer_size;
137 DWORD volume_left; /* volume control information */
138 DWORD volume_right;
140 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
141 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
142 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
144 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
145 DWORD dwLoops; /* private copy of loop counter */
147 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
148 DWORD dwWrittenTotal; /* number of bytes written to the audio device since opening */
150 /* synchronization stuff */
151 HANDLE hStartUpEvent;
152 HANDLE hThread;
153 DWORD dwThreadID;
154 ARTS_MSG_RING msgRing;
155 } WINE_WAVEOUT;
157 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
159 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
160 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
161 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
163 /* These strings used only for tracing */
164 static const char *wodPlayerCmdString[] = {
165 "WINE_WM_PAUSING",
166 "WINE_WM_RESTARTING",
167 "WINE_WM_RESETTING",
168 "WINE_WM_HEADER",
169 "WINE_WM_UPDATE",
170 "WINE_WM_BREAKLOOP",
171 "WINE_WM_CLOSING",
174 /*======================================================================*
175 * Low level WAVE implementation *
176 *======================================================================*/
178 /* Volume functions derived from Alsaplayer source */
179 /* length is the number of 16 bit samples */
180 void volume_effect16(void *bufin, void* bufout, int length, int left,
181 int right, int nChannels)
183 short *d_out = (short *)bufout;
184 short *d_in = (short *)bufin;
185 int i, v;
188 TRACE("length == %d, nChannels == %d\n", length, nChannels);
191 if (right == -1) right = left;
193 for(i = 0; i < length; i+=(nChannels))
195 v = (int) ((*(d_in++) * left) / 100);
196 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
197 if(nChannels == 2)
199 v = (int) ((*(d_in++) * right) / 100);
200 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
205 /* length is the number of 8 bit samples */
206 void volume_effect8(void *bufin, void* bufout, int length, int left,
207 int right, int nChannels)
209 BYTE *d_out = (BYTE *)bufout;
210 BYTE *d_in = (BYTE *)bufin;
211 int i, v;
214 TRACE("length == %d, nChannels == %d\n", length, nChannels);
217 if (right == -1) right = left;
219 for(i = 0; i < length; i+=(nChannels))
221 v = (BYTE) ((*(d_in++) * left) / 100);
222 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
223 if(nChannels == 2)
225 v = (BYTE) ((*(d_in++) * right) / 100);
226 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
231 /******************************************************************
232 * ARTS_CloseDevice
235 void ARTS_CloseDevice(WINE_WAVEOUT* wwo)
237 arts_close_stream(wwo->play_stream); /* close the arts stream */
238 wwo->play_stream = (arts_stream_t*)-1;
240 /* free up the buffer we use for volume and reset the size */
241 if(wwo->sound_buffer)
242 HeapFree(GetProcessHeap(), 0, wwo->sound_buffer);
244 wwo->buffer_size = 0;
247 /******************************************************************
248 * ARTS_Init
250 static int ARTS_Init(void)
252 return arts_init(); /* initialize arts and return errorcode */
255 /******************************************************************
256 * ARTS_WaveClose
258 LONG ARTS_WaveClose(void)
260 int iDevice;
262 /* close all open devices */
263 for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
265 if(WOutDev[iDevice].play_stream != (arts_stream_t*)-1)
267 ARTS_CloseDevice(&WOutDev[iDevice]);
271 arts_free(); /* free up arts */
272 return 1;
275 /******************************************************************
276 * ARTS_WaveInit
278 * Initialize internal structures from ARTS server info
280 LONG ARTS_WaveInit(void)
282 int i;
283 int errorcode;
285 TRACE("called\n");
287 if ((errorcode = ARTS_Init()) < 0)
289 ERR("arts_init() failed (%d)\n", errorcode);
290 return -1;
293 /* initialize all device handles to -1 */
294 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
296 WOutDev[i].play_stream = (arts_stream_t*)-1;
297 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
298 caps values */
299 /* FIXME: some programs compare this string against the content of the registry
300 * for MM drivers. The names have to match in order for the program to work
301 * (e.g. MS win9x mplayer.exe)
303 #ifdef EMULATE_SB16
304 WOutDev[i].caps.wMid = 0x0002;
305 WOutDev[i].caps.wPid = 0x0104;
306 strcpy(WOutDev[i].caps.szPname, "SB16 Wave Out");
307 #else
308 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
309 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
310 /* strcpy(WOutDev[i].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
311 strcpy(WOutDev[i].caps.szPname, "CS4236/37/38");
312 #endif
313 WOutDev[i].caps.vDriverVersion = 0x0100;
314 WOutDev[i].caps.dwFormats = 0x00000000;
315 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
317 WOutDev[i].caps.wChannels = 2;
318 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
320 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
321 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
322 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
323 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
324 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
325 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
326 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
327 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
328 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
329 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
330 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
331 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
335 return 0;
338 /******************************************************************
339 * ARTS_InitRingMessage
341 * Initialize the ring of messages for passing between driver's caller and playback/record
342 * thread
344 static int ARTS_InitRingMessage(ARTS_MSG_RING* mr)
346 mr->msg_toget = 0;
347 mr->msg_tosave = 0;
348 mr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
349 mr->ring_buffer_size = ARTS_RING_BUFFER_INCREMENT;
350 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
351 InitializeCriticalSection(&mr->msg_crst);
352 return 0;
355 /******************************************************************
356 * ARTS_DestroyRingMessage
359 static int ARTS_DestroyRingMessage(ARTS_MSG_RING* mr)
361 CloseHandle(mr->msg_event);
362 HeapFree(GetProcessHeap(),0,mr->messages);
363 DeleteCriticalSection(&mr->msg_crst);
364 return 0;
367 /******************************************************************
368 * ARTS_AddRingMessage
370 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
372 static int ARTS_AddRingMessage(ARTS_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
374 HANDLE hEvent = INVALID_HANDLE_VALUE;
376 EnterCriticalSection(&mr->msg_crst);
377 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
379 mr->ring_buffer_size += ARTS_RING_BUFFER_INCREMENT;
380 TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
381 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
383 if (wait)
385 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
386 if (hEvent == INVALID_HANDLE_VALUE)
388 ERR("can't create event !?\n");
389 LeaveCriticalSection(&mr->msg_crst);
390 return 0;
392 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
393 FIXME("two fast messages in the queue!!!!\n");
395 /* fast messages have to be added at the start of the queue */
396 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
398 mr->messages[mr->msg_toget].msg = msg;
399 mr->messages[mr->msg_toget].param = param;
400 mr->messages[mr->msg_toget].hEvent = hEvent;
402 else
404 mr->messages[mr->msg_tosave].msg = msg;
405 mr->messages[mr->msg_tosave].param = param;
406 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
407 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
410 LeaveCriticalSection(&mr->msg_crst);
412 SetEvent(mr->msg_event); /* signal a new message */
414 if (wait)
416 /* wait for playback/record thread to have processed the message */
417 WaitForSingleObject(hEvent, INFINITE);
418 CloseHandle(hEvent);
421 return 1;
424 /******************************************************************
425 * ARTS_RetrieveRingMessage
427 * Get a message from the ring. Should be called by the playback/record thread.
429 static int ARTS_RetrieveRingMessage(ARTS_MSG_RING* mr,
430 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
432 EnterCriticalSection(&mr->msg_crst);
434 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
436 LeaveCriticalSection(&mr->msg_crst);
437 return 0;
440 *msg = mr->messages[mr->msg_toget].msg;
441 mr->messages[mr->msg_toget].msg = 0;
442 *param = mr->messages[mr->msg_toget].param;
443 *hEvent = mr->messages[mr->msg_toget].hEvent;
444 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
445 LeaveCriticalSection(&mr->msg_crst);
446 return 1;
449 /*======================================================================*
450 * Low level WAVE OUT implementation *
451 *======================================================================*/
453 /**************************************************************************
454 * wodNotifyClient [internal]
456 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
458 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
460 switch (wMsg) {
461 case WOM_OPEN:
462 case WOM_CLOSE:
463 case WOM_DONE:
464 if (wwo->wFlags != DCB_NULL &&
465 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
466 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
467 WARN("can't notify client !\n");
468 return MMSYSERR_ERROR;
470 break;
471 default:
472 FIXME("Unknown callback message %u\n", wMsg);
473 return MMSYSERR_INVALPARAM;
475 return MMSYSERR_NOERROR;
478 /**************************************************************************
479 * wodUpdatePlayedTotal [internal]
482 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
484 /* total played is the bytes written less the bytes to write ;-) */
485 wwo->dwPlayedTotal = wwo->dwWrittenTotal -
486 (wwo->dwBufferSize -
487 arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE));
489 return TRUE;
492 /**************************************************************************
493 * wodPlayer_BeginWaveHdr [internal]
495 * Makes the specified lpWaveHdr the currently playing wave header.
496 * If the specified wave header is a begin loop and we're not already in
497 * a loop, setup the loop.
499 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
501 wwo->lpPlayPtr = lpWaveHdr;
503 if (!lpWaveHdr) return;
505 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
506 if (wwo->lpLoopPtr) {
507 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
508 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
509 } else {
510 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
511 wwo->lpLoopPtr = lpWaveHdr;
512 /* Windows does not touch WAVEHDR.dwLoops,
513 * so we need to make an internal copy */
514 wwo->dwLoops = lpWaveHdr->dwLoops;
517 wwo->dwPartialOffset = 0;
520 /**************************************************************************
521 * wodPlayer_PlayPtrNext [internal]
523 * Advance the play pointer to the next waveheader, looping if required.
525 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
527 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
529 wwo->dwPartialOffset = 0;
530 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
531 /* We're at the end of a loop, loop if required */
532 if (--wwo->dwLoops > 0) {
533 wwo->lpPlayPtr = wwo->lpLoopPtr;
534 } else {
535 /* Handle overlapping loops correctly */
536 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
537 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
538 /* shall we consider the END flag for the closing loop or for
539 * the opening one or for both ???
540 * code assumes for closing loop only
542 } else {
543 lpWaveHdr = lpWaveHdr->lpNext;
545 wwo->lpLoopPtr = NULL;
546 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
548 } else {
549 /* We're not in a loop. Advance to the next wave header */
550 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
553 return lpWaveHdr;
556 /**************************************************************************
557 * wodPlayer_DSPWait [internal]
558 * Returns the number of milliseconds to wait for the DSP buffer to clear.
559 * This is based on the number of fragments we want to be clear before
560 * writing and the number of free fragments we already have.
562 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
564 int waitvalue = (wwo->dwBufferSize - arts_stream_get(wwo->play_stream,
565 ARTS_P_BUFFER_SPACE)) / ((wwo->format.wf.nSamplesPerSec *
566 wwo->format.wBitsPerSample * wwo->format.wf.nChannels)
567 /1000);
569 TRACE("wait value of %d\n", waitvalue);
571 /* return the time left to play the buffer */
572 return waitvalue;
575 /**************************************************************************
576 * wodPlayer_NotifyWait [internal]
577 * Returns the number of milliseconds to wait before attempting to notify
578 * completion of the specified wavehdr.
579 * This is based on the number of bytes remaining to be written in the
580 * wave.
582 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
584 DWORD dwMillis;
586 if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
588 dwMillis = 1;
590 else
592 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
593 if(!dwMillis) dwMillis = 1;
596 TRACE("dwMillis = %ld\n", dwMillis);
598 return dwMillis;
602 /**************************************************************************
603 * wodPlayer_WriteMaxFrags [internal]
604 * Writes the maximum number of bytes possible to the DSP and returns
605 * the number of bytes written.
607 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
609 /* Only attempt to write to free bytes */
610 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
611 int toWrite = min(dwLength, *bytes);
612 int written;
614 TRACE("Writing wavehdr %p.%lu[%lu]\n",
615 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);
617 /* see if our buffer isn't large enough for the data we are writing */
618 if(wwo->buffer_size < toWrite)
620 if(wwo->sound_buffer)
621 HeapFree(GetProcessHeap(), 0, wwo->sound_buffer);
624 /* if we don't have a buffer then get one */
625 if(!wwo->sound_buffer)
627 /* allocate some memory for the buffer */
628 wwo->sound_buffer = HeapAlloc(GetProcessHeap(), 0, toWrite);
629 wwo->buffer_size = toWrite;
632 /* if we don't have a buffer then error out */
633 if(!wwo->sound_buffer)
635 ERR("error allocating sound_buffer memory\n");
636 return 0;
639 TRACE("toWrite == %d\n", toWrite);
641 /* apply volume to the bits */
642 /* for single channel audio streams we only use the LEFT volume */
643 if(wwo->format.wBitsPerSample == 16)
645 /* apply volume to the buffer we are about to send */
646 /* divide toWrite(bytes) by 2 as volume processes by 16 bits */
647 volume_effect16(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
648 wwo->sound_buffer, toWrite>>1, wwo->volume_left,
649 wwo->volume_right, wwo->format.wf.nChannels);
650 } else if(wwo->format.wBitsPerSample == 8)
652 /* apply volume to the buffer we are about to send */
653 volume_effect8(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
654 wwo->sound_buffer, toWrite, wwo->volume_left,
655 wwo->volume_right, wwo->format.wf.nChannels);
656 } else
658 FIXME("unsupported wwo->format.wBitsPerSample of %d\n",
659 wwo->format.wBitsPerSample);
662 /* send the audio data to arts for playing */
663 written = arts_write(wwo->play_stream, wwo->sound_buffer, toWrite);
665 TRACE("written = %d\n", written);
667 if (written <= 0) return written; /* if we wrote nothing just return */
669 if (written >= dwLength)
670 wodPlayer_PlayPtrNext(wwo); /* If we wrote all current wavehdr, skip to the next one */
671 else
672 wwo->dwPartialOffset += written; /* Remove the amount written */
674 *bytes -= written;
675 wwo->dwWrittenTotal += written; /* update stats on this wave device */
677 return written; /* return the number of bytes written */
681 /**************************************************************************
682 * wodPlayer_NotifyCompletions [internal]
684 * Notifies and remove from queue all wavehdrs which have been played to
685 * the speaker (ie. they have cleared the audio device). If force is true,
686 * we notify all wavehdrs and remove them all from the queue even if they
687 * are unplayed or part of a loop.
689 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
691 LPWAVEHDR lpWaveHdr;
693 /* Start from lpQueuePtr and keep notifying until:
694 * - we hit an unwritten wavehdr
695 * - we hit the beginning of a running loop
696 * - we hit a wavehdr which hasn't finished playing
698 while ((lpWaveHdr = wwo->lpQueuePtr) &&
699 (force ||
700 (lpWaveHdr != wwo->lpPlayPtr &&
701 lpWaveHdr != wwo->lpLoopPtr &&
702 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
704 wwo->lpQueuePtr = lpWaveHdr->lpNext;
706 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
707 lpWaveHdr->dwFlags |= WHDR_DONE;
709 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
711 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
712 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
715 /**************************************************************************
716 * wodPlayer_Reset [internal]
718 * wodPlayer helper. Resets current output stream.
720 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
722 wodUpdatePlayedTotal(wwo);
724 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
726 /* we aren't able to flush any data that has already been written */
727 /* to arts, otherwise we would do the flushing here */
729 if (reset) {
730 enum win_wm_message msg;
731 DWORD param;
732 HANDLE ev;
734 /* remove any buffer */
735 wodPlayer_NotifyCompletions(wwo, TRUE);
737 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
738 wwo->state = WINE_WS_STOPPED;
739 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
741 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
743 /* remove any existing message in the ring */
744 EnterCriticalSection(&wwo->msgRing.msg_crst);
746 /* return all pending headers in queue */
747 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
749 TRACE("flushing msg\n");
750 if (msg != WINE_WM_HEADER)
752 FIXME("shouldn't have headers left\n");
753 SetEvent(ev);
754 continue;
756 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
757 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
759 wodNotifyClient(wwo, WOM_DONE, param, 0);
761 ResetEvent(wwo->msgRing.msg_event);
762 LeaveCriticalSection(&wwo->msgRing.msg_crst);
763 } else {
764 if (wwo->lpLoopPtr) {
765 /* complicated case, not handled yet (could imply modifying the loop counter */
766 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
767 wwo->lpPlayPtr = wwo->lpLoopPtr;
768 wwo->dwPartialOffset = 0;
769 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
770 } else {
771 /* the data already written is going to be played, so take */
772 /* this fact into account here */
773 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
775 wwo->state = WINE_WS_PAUSED;
779 /**************************************************************************
780 * wodPlayer_ProcessMessages [internal]
782 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
784 LPWAVEHDR lpWaveHdr;
785 enum win_wm_message msg;
786 DWORD param;
787 HANDLE ev;
789 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
790 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
791 switch (msg) {
792 case WINE_WM_PAUSING:
793 wodPlayer_Reset(wwo, FALSE);
794 SetEvent(ev);
795 break;
796 case WINE_WM_RESTARTING:
797 wwo->state = WINE_WS_PLAYING;
798 SetEvent(ev);
799 break;
800 case WINE_WM_HEADER:
801 lpWaveHdr = (LPWAVEHDR)param;
803 /* insert buffer at the end of queue */
805 LPWAVEHDR* wh;
806 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
807 *wh = lpWaveHdr;
809 if (!wwo->lpPlayPtr)
810 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
811 if (wwo->state == WINE_WS_STOPPED)
812 wwo->state = WINE_WS_PLAYING;
813 break;
814 case WINE_WM_RESETTING:
815 wodPlayer_Reset(wwo, TRUE);
816 SetEvent(ev);
817 break;
818 case WINE_WM_UPDATE:
819 wodUpdatePlayedTotal(wwo);
820 SetEvent(ev);
821 break;
822 case WINE_WM_BREAKLOOP:
823 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
824 /* ensure exit at end of current loop */
825 wwo->dwLoops = 1;
827 SetEvent(ev);
828 break;
829 case WINE_WM_CLOSING:
830 /* sanity check: this should not happen since the device must have been reset before */
831 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
832 wwo->hThread = 0;
833 wwo->state = WINE_WS_CLOSED;
834 SetEvent(ev);
835 ExitThread(0);
836 /* shouldn't go here */
837 default:
838 FIXME("unknown message %d\n", msg);
839 break;
844 /**************************************************************************
845 * wodPlayer_FeedDSP [internal]
846 * Feed as much sound data as we can into the DSP and return the number of
847 * milliseconds before it will be necessary to feed the DSP again.
849 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
851 DWORD availInQ;
853 wodUpdatePlayedTotal(wwo);
854 availInQ = arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE);
855 TRACE("availInQ = %ld\n", availInQ);
857 /* input queue empty and output buffer with no space */
858 if (!wwo->lpPlayPtr && availInQ) {
859 TRACE("Run out of wavehdr:s... flushing\n");
860 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
861 return INFINITE;
864 /* no more room... no need to try to feed */
865 if(!availInQ)
867 TRACE("no more room, no need to try to feed\n");
868 return wodPlayer_DSPWait(wwo);
871 /* Feed from partial wavehdr */
872 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0)
874 TRACE("feeding from partial wavehdr\n");
875 wodPlayer_WriteMaxFrags(wwo, &availInQ);
878 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
879 if (!wwo->dwPartialOffset)
881 while(wwo->lpPlayPtr && availInQ > SPACE_THRESHOLD)
883 TRACE("feeding waveheaders until we run out of space\n");
884 /* note the value that dwPlayedTotal will return when this wave finishes playing */
885 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
886 wodPlayer_WriteMaxFrags(wwo, &availInQ);
890 return wodPlayer_DSPWait(wwo);
894 /**************************************************************************
895 * wodPlayer [internal]
897 static DWORD CALLBACK wodPlayer(LPVOID pmt)
899 WORD uDevID = (DWORD)pmt;
900 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
901 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
902 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
903 DWORD dwSleepTime;
905 wwo->state = WINE_WS_STOPPED;
906 SetEvent(wwo->hStartUpEvent);
908 for (;;) {
909 /** Wait for the shortest time before an action is required. If there
910 * are no pending actions, wait forever for a command.
912 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
913 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
914 WaitForSingleObject(wwo->msgRing.msg_event, dwSleepTime);
915 wodPlayer_ProcessMessages(wwo);
916 if (wwo->state == WINE_WS_PLAYING) {
917 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
918 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
919 } else {
920 dwNextFeedTime = dwNextNotifyTime = INFINITE;
925 /**************************************************************************
926 * wodGetDevCaps [internal]
928 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
930 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
932 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
934 if (wDevID >= MAX_WAVEOUTDRV) {
935 TRACE("MAX_WAVOUTDRV reached !\n");
936 return MMSYSERR_BADDEVICEID;
939 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
940 return MMSYSERR_NOERROR;
943 /**************************************************************************
944 * wodOpen [internal]
946 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
948 WINE_WAVEOUT* wwo;
950 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
951 if (lpDesc == NULL) {
952 WARN("Invalid Parameter !\n");
953 return MMSYSERR_INVALPARAM;
955 if (wDevID >= MAX_WAVEOUTDRV) {
956 TRACE("MAX_WAVOUTDRV reached !\n");
957 return MMSYSERR_BADDEVICEID;
960 /* if this device is already open tell the app that it is allocated */
961 if(WOutDev[wDevID].play_stream != (arts_stream_t*)-1)
963 TRACE("device already allocated\n");
964 return MMSYSERR_ALLOCATED;
967 /* only PCM format is supported so far... */
968 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
969 lpDesc->lpFormat->nChannels == 0 ||
970 lpDesc->lpFormat->nSamplesPerSec == 0) {
971 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
972 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
973 lpDesc->lpFormat->nSamplesPerSec);
974 return WAVERR_BADFORMAT;
977 if (dwFlags & WAVE_FORMAT_QUERY) {
978 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
979 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
980 lpDesc->lpFormat->nSamplesPerSec);
981 return MMSYSERR_NOERROR;
984 wwo = &WOutDev[wDevID];
986 /* direct sound not supported, ignore the flag */
987 dwFlags &= ~WAVE_DIRECTSOUND;
989 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
991 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
992 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
994 if (wwo->format.wBitsPerSample == 0) {
995 WARN("Resetting zeroed wBitsPerSample\n");
996 wwo->format.wBitsPerSample = 8 *
997 (wwo->format.wf.nAvgBytesPerSec /
998 wwo->format.wf.nSamplesPerSec) /
999 wwo->format.wf.nChannels;
1002 wwo->play_stream = arts_play_stream(wwo->format.wf.nSamplesPerSec,
1003 wwo->format.wBitsPerSample, wwo->format.wf.nChannels, "winearts");
1005 /* clear these so we don't have any confusion ;-) */
1006 wwo->sound_buffer = 0;
1007 wwo->buffer_size = 0;
1009 arts_stream_set(wwo->play_stream, ARTS_P_BLOCKING, 0); /* disable blocking on this stream */
1011 if(!wwo->play_stream) return MMSYSERR_ALLOCATED;
1013 /* Try to set buffer size from constant and store the value that it
1014 was set to for future use */
1015 wwo->dwBufferSize = arts_stream_set(wwo->play_stream,
1016 ARTS_P_BUFFER_SIZE, BUFFER_SIZE);
1017 TRACE("Tried to set BUFFER_SIZE of %d, wwo->dwBufferSize is actually %ld\n", BUFFER_SIZE, wwo->dwBufferSize);
1018 wwo->dwPlayedTotal = 0;
1019 wwo->dwWrittenTotal = 0;
1021 /* Initialize volume to full level */
1022 wwo->volume_left = 100;
1023 wwo->volume_right = 100;
1025 ARTS_InitRingMessage(&wwo->msgRing);
1027 /* create player thread */
1028 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1029 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1030 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1031 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1032 CloseHandle(wwo->hStartUpEvent);
1033 } else {
1034 wwo->hThread = INVALID_HANDLE_VALUE;
1035 wwo->dwThreadID = 0;
1037 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1039 TRACE("stream=0x%lx, dwBufferSize=%ld\n",
1040 (long)wwo->play_stream, wwo->dwBufferSize);
1042 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1043 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1044 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1045 wwo->format.wf.nBlockAlign);
1047 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1050 /**************************************************************************
1051 * wodClose [internal]
1053 static DWORD wodClose(WORD wDevID)
1055 DWORD ret = MMSYSERR_NOERROR;
1056 WINE_WAVEOUT* wwo;
1058 TRACE("(%u);\n", wDevID);
1060 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1061 (arts_stream_t*)-1)
1063 WARN("bad device ID !\n");
1064 return MMSYSERR_BADDEVICEID;
1067 wwo = &WOutDev[wDevID];
1068 if (wwo->lpQueuePtr) {
1069 WARN("buffers still playing !\n");
1070 ret = WAVERR_STILLPLAYING;
1071 } else {
1072 TRACE("imhere[3-close]\n");
1073 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1074 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1077 ARTS_DestroyRingMessage(&wwo->msgRing);
1079 ARTS_CloseDevice(wwo); /* close the stream and clean things up */
1081 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1083 return ret;
1086 /**************************************************************************
1087 * wodWrite [internal]
1090 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1092 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1094 /* first, do the sanity checks... */
1095 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1096 (arts_stream_t*)-1)
1098 WARN("bad dev ID !\n");
1099 return MMSYSERR_BADDEVICEID;
1102 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1104 TRACE("unprepared\n");
1105 return WAVERR_UNPREPARED;
1108 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1110 TRACE("still playing\n");
1111 return WAVERR_STILLPLAYING;
1114 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1115 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1116 lpWaveHdr->lpNext = 0;
1118 TRACE("adding ring message\n");
1119 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1121 return MMSYSERR_NOERROR;
1124 /**************************************************************************
1125 * wodPrepare [internal]
1127 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1129 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1131 if (wDevID >= MAX_WAVEOUTDRV) {
1132 WARN("bad device ID !\n");
1133 return MMSYSERR_BADDEVICEID;
1136 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1137 return WAVERR_STILLPLAYING;
1139 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1140 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1141 return MMSYSERR_NOERROR;
1144 /**************************************************************************
1145 * wodUnprepare [internal]
1147 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1149 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1151 if (wDevID >= MAX_WAVEOUTDRV) {
1152 WARN("bad device ID !\n");
1153 return MMSYSERR_BADDEVICEID;
1156 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1157 return WAVERR_STILLPLAYING;
1159 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1160 lpWaveHdr->dwFlags |= WHDR_DONE;
1162 return MMSYSERR_NOERROR;
1165 /**************************************************************************
1166 * wodPause [internal]
1168 static DWORD wodPause(WORD wDevID)
1170 TRACE("(%u);!\n", wDevID);
1172 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1173 (arts_stream_t*)-1)
1175 WARN("bad device ID !\n");
1176 return MMSYSERR_BADDEVICEID;
1179 TRACE("imhere[3-PAUSING]\n");
1180 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1182 return MMSYSERR_NOERROR;
1185 /**************************************************************************
1186 * wodRestart [internal]
1188 static DWORD wodRestart(WORD wDevID)
1190 TRACE("(%u);\n", wDevID);
1192 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1193 (arts_stream_t*)-1)
1195 WARN("bad device ID !\n");
1196 return MMSYSERR_BADDEVICEID;
1199 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1200 TRACE("imhere[3-RESTARTING]\n");
1201 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1204 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1205 /* FIXME: Myst crashes with this ... hmm -MM
1206 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1209 return MMSYSERR_NOERROR;
1212 /**************************************************************************
1213 * wodReset [internal]
1215 static DWORD wodReset(WORD wDevID)
1217 TRACE("(%u);\n", wDevID);
1219 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1220 (arts_stream_t*)-1)
1222 WARN("bad device ID !\n");
1223 return MMSYSERR_BADDEVICEID;
1226 TRACE("imhere[3-RESET]\n");
1227 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1229 return MMSYSERR_NOERROR;
1232 /**************************************************************************
1233 * wodGetPosition [internal]
1235 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1237 int time;
1238 DWORD val;
1239 WINE_WAVEOUT* wwo;
1241 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1243 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1244 (arts_stream_t*)-1)
1246 WARN("bad device ID !\n");
1247 return MMSYSERR_BADDEVICEID;
1250 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1252 wwo = &WOutDev[wDevID];
1253 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1254 val = wwo->dwPlayedTotal;
1256 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1257 lpTime->wType, wwo->format.wBitsPerSample,
1258 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1259 wwo->format.wf.nAvgBytesPerSec);
1260 TRACE("dwPlayedTotal=%lu\n", val);
1262 switch (lpTime->wType) {
1263 case TIME_BYTES:
1264 lpTime->u.cb = val;
1265 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1266 break;
1267 case TIME_SAMPLES:
1268 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1269 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1270 break;
1271 case TIME_SMPTE:
1272 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1273 lpTime->u.smpte.hour = time / 108000;
1274 time -= lpTime->u.smpte.hour * 108000;
1275 lpTime->u.smpte.min = time / 1800;
1276 time -= lpTime->u.smpte.min * 1800;
1277 lpTime->u.smpte.sec = time / 30;
1278 time -= lpTime->u.smpte.sec * 30;
1279 lpTime->u.smpte.frame = time;
1280 lpTime->u.smpte.fps = 30;
1281 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1282 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1283 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1284 break;
1285 default:
1286 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1287 lpTime->wType = TIME_MS;
1288 case TIME_MS:
1289 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1290 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1291 break;
1293 return MMSYSERR_NOERROR;
1296 /**************************************************************************
1297 * wodBreakLoop [internal]
1299 static DWORD wodBreakLoop(WORD wDevID)
1301 TRACE("(%u);\n", wDevID);
1303 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1304 (arts_stream_t*)-1)
1306 WARN("bad device ID !\n");
1307 return MMSYSERR_BADDEVICEID;
1309 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1310 return MMSYSERR_NOERROR;
1313 /**************************************************************************
1314 * wodGetVolume [internal]
1316 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1318 DWORD left, right;
1320 left = WOutDev[wDevID].volume_left;
1321 right = WOutDev[wDevID].volume_right;
1323 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1325 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) <<
1326 16);
1328 return MMSYSERR_NOERROR;
1331 /**************************************************************************
1332 * wodSetVolume [internal]
1334 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1336 DWORD left, right;
1338 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1339 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1341 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1343 WOutDev[wDevID].volume_left = left;
1344 WOutDev[wDevID].volume_right = right;
1346 return MMSYSERR_NOERROR;
1349 /**************************************************************************
1350 * wodGetNumDevs [internal]
1352 static DWORD wodGetNumDevs(void)
1354 return MAX_WAVEOUTDRV;
1357 /**************************************************************************
1358 * wodMessage (WINEARTS.@)
1360 DWORD WINAPI ARTS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1361 DWORD dwParam1, DWORD dwParam2)
1363 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1364 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1366 switch (wMsg) {
1367 case DRVM_INIT:
1368 case DRVM_EXIT:
1369 case DRVM_ENABLE:
1370 case DRVM_DISABLE:
1371 /* FIXME: Pretend this is supported */
1372 return 0;
1373 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1374 case WODM_CLOSE: return wodClose (wDevID);
1375 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1376 case WODM_PAUSE: return wodPause (wDevID);
1377 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1378 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1379 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1380 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1381 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1382 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1383 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1384 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1385 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1386 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1387 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1388 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1389 case WODM_RESTART: return wodRestart (wDevID);
1390 case WODM_RESET: return wodReset (wDevID);
1392 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1393 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1394 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1395 default:
1396 FIXME("unknown message %d!\n", wMsg);
1398 return MMSYSERR_NOTSUPPORTED;
1401 /*======================================================================*
1402 * Low level DSOUND implementation *
1403 *======================================================================*/
1404 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1406 /* we can't perform memory mapping as we don't have a file stream
1407 interface with arts like we do with oss */
1408 MESSAGE("This sound card's driver does not support direct access\n");
1409 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1410 return MMSYSERR_NOTSUPPORTED;
1413 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1415 memset(desc, 0, sizeof(*desc));
1416 strcpy(desc->szDesc, "Wine aRts DirectSound Driver");
1417 strcpy(desc->szDrvName, "winearts.drv");
1418 return MMSYSERR_NOERROR;
1421 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
1423 memcpy(pGuid, &DSDEVID_DefaultPlayback, sizeof(GUID));
1424 return MMSYSERR_NOERROR;
1427 #else /* !HAVE_ARTS */
1429 /**************************************************************************
1430 * wodMessage (WINEARTS.@)
1432 DWORD WINAPI ARTS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
1433 DWORD dwParam1, DWORD dwParam2)
1435 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1436 return MMSYSERR_NOTENABLED;
1439 #endif /* HAVE_ARTS */