1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
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
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
33 * pause in waveOut does not work correctly in loop mode
35 * does something need to be done in for WaveIn DirectSound?
38 /*#define EMULATE_SB16*/
41 #include "wine/port.h"
56 #include "wine/winuser16.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wave);
69 /* The following four #defines allow you to fine-tune the packet
70 * settings in arts for better low-latency support. You must also
71 * adjust the latency in the KDE arts control panel. I recommend 4
72 * fragments, 1024 bytes.
74 * The following is from the ARTS documentation and explains what CCCC
77 * @li ARTS_P_PACKET_SETTINGS (rw) This is a way to configure packet
78 * size & packet count at the same time. The format is 0xCCCCSSSS,
79 * where 2^SSSS is the packet size, and CCCC is the packet count. Note
80 * that when writing this, you don't necessarily get the settings you
83 #define WAVEOUT_PACKET_CCCC 0x000C
84 #define WAVEOUT_PACKET_SSSS 0x0008
85 #define WAVEIN_PACKET_CCCC 0x000C
86 #define WAVEIN_PACKET_SSSS 0x0008
88 #define BUFFER_REFILL_THRESHOLD 4
90 #define WAVEOUT_PACKET_SETTINGS ((WAVEOUT_PACKET_CCCC << 16) | (WAVEOUT_PACKET_SSSS))
91 #define WAVEIN_PACKET_SETTINGS ((WAVEIN_PACKET_CCCC << 16) | (WAVEIN_PACKET_SSSS))
93 #define MAX_WAVEOUTDRV (10)
94 #define MAX_WAVEINDRV (10)
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, WINE_WM_STARTING, WINE_WM_STOPPING
126 enum win_wm_message msg; /* message identifier */
127 DWORD param; /* parameter for this message */
128 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
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 ARTS_RING_BUFFER_INCREMENT 64
138 int ring_buffer_size;
142 CRITICAL_SECTION msg_crst;
146 volatile int state; /* one of the WINE_WS_ manifest constants */
147 WAVEOPENDESC waveDesc;
149 PCMWAVEFORMAT format;
151 char interface_name[32];
153 DWORD dwSleepTime; /* Num of milliseconds to sleep between filling the dsp buffers */
155 /* arts information */
156 arts_stream_t play_stream; /* the stream structure we get from arts when opening a stream for playing */
157 DWORD dwBufferSize; /* size of whole buffer in bytes */
163 DWORD volume_left; /* volume control information */
166 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
167 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
168 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
170 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
171 DWORD dwLoops; /* private copy of loop counter */
173 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
174 DWORD dwWrittenTotal; /* number of bytes written to the audio device since opening */
176 /* synchronization stuff */
177 HANDLE hStartUpEvent;
180 ARTS_MSG_RING msgRing;
184 volatile int state; /* one of the WINE_WS_ manifest constants */
185 WAVEOPENDESC waveDesc;
187 PCMWAVEFORMAT format;
189 char interface_name[32];
191 /* arts information */
192 arts_stream_t record_stream; /* the stream structure we get from arts when opening a stream for recording */
195 LPWAVEHDR lpQueuePtr;
196 DWORD dwRecordedTotal;
198 /* synchronization stuff */
199 HANDLE hStartUpEvent;
202 ARTS_MSG_RING msgRing;
205 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
206 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
208 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
209 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
210 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
212 /* These strings used only for tracing */
213 static const char *wodPlayerCmdString[] = {
215 "WINE_WM_RESTARTING",
225 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
226 PCMWAVEFORMAT* format)
228 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
229 lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
230 format->wf.nChannels, format->wf.nAvgBytesPerSec);
231 TRACE("Position in bytes=%lu\n", position);
233 switch (lpTime->wType) {
235 lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
236 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
239 lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
240 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
243 position = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
244 lpTime->u.smpte.sec = position / format->wf.nSamplesPerSec;
245 position -= lpTime->u.smpte.sec * format->wf.nSamplesPerSec;
246 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
247 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
248 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
249 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
250 lpTime->u.smpte.fps = 30;
251 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->wf.nSamplesPerSec;
252 position -= lpTime->u.smpte.frame * format->wf.nSamplesPerSec / lpTime->u.smpte.fps;
256 lpTime->u.smpte.frame++;
258 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
259 lpTime->u.smpte.hour, lpTime->u.smpte.min,
260 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
263 FIXME("Format %d not supported ! use TIME_BYTES !\n", lpTime->wType);
264 lpTime->wType = TIME_BYTES;
267 lpTime->u.cb = position;
268 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
271 return MMSYSERR_NOERROR;
274 /*======================================================================*
275 * Low level WAVE implementation *
276 *======================================================================*/
278 /* Volume functions derived from Alsaplayer source */
279 /* length is the number of 16 bit samples */
280 void volume_effect16(void *bufin, void* bufout, int length, int left,
281 int right, int nChannels)
283 short *d_out = (short *)bufout;
284 short *d_in = (short *)bufin;
288 TRACE("length == %d, nChannels == %d\n", length, nChannels);
291 if (right == -1) right = left;
293 for(i = 0; i < length; i+=(nChannels))
295 v = (int) ((*(d_in++) * left) / 100);
296 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
299 v = (int) ((*(d_in++) * right) / 100);
300 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
305 /* length is the number of 8 bit samples */
306 void volume_effect8(void *bufin, void* bufout, int length, int left,
307 int right, int nChannels)
309 BYTE *d_out = (BYTE *)bufout;
310 BYTE *d_in = (BYTE *)bufin;
314 TRACE("length == %d, nChannels == %d\n", length, nChannels);
317 if (right == -1) right = left;
319 for(i = 0; i < length; i+=(nChannels))
321 v = (BYTE) ((*(d_in++) * left) / 100);
322 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
325 v = (BYTE) ((*(d_in++) * right) / 100);
326 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
331 /******************************************************************
332 * ARTS_CloseWaveOutDevice
335 void ARTS_CloseWaveOutDevice(WINE_WAVEOUT* wwo)
337 arts_close_stream(wwo->play_stream); /* close the arts stream */
338 wwo->play_stream = (arts_stream_t*)-1;
340 /* free up the buffer we use for volume and reset the size */
341 if(wwo->sound_buffer)
343 HeapFree(GetProcessHeap(), 0, wwo->sound_buffer);
344 wwo->sound_buffer = NULL;
347 wwo->buffer_size = 0;
350 /******************************************************************
351 * ARTS_CloseWaveInDevice
354 void ARTS_CloseWaveInDevice(WINE_WAVEIN* wwi)
356 arts_close_stream(wwi->record_stream); /* close the arts stream */
357 wwi->record_stream = (arts_stream_t*)-1;
360 /******************************************************************
363 static int ARTS_Init(void)
365 return arts_init(); /* initialize arts and return errorcode */
368 /******************************************************************
371 LONG ARTS_WaveClose(void)
375 /* close all open devices */
376 for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
378 if(WOutDev[iDevice].play_stream != (arts_stream_t*)-1)
380 ARTS_CloseWaveOutDevice(&WOutDev[iDevice]);
384 for(iDevice = 0; iDevice < MAX_WAVEINDRV; iDevice++)
386 if(WInDev[iDevice].record_stream != (arts_stream_t*)-1)
388 ARTS_CloseWaveInDevice(&WInDev[iDevice]);
392 arts_free(); /* free up arts */
396 /******************************************************************
399 * Initialize internal structures from ARTS server info
401 LONG ARTS_WaveInit(void)
408 if ((errorcode = ARTS_Init()) < 0)
410 WARN("arts_init() failed (%d)\n", errorcode);
414 /* initialize all device handles to -1 */
415 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
417 WOutDev[i].play_stream = (arts_stream_t*)-1;
418 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
420 /* FIXME: some programs compare this string against the content of the registry
421 * for MM drivers. The names have to match in order for the program to work
422 * (e.g. MS win9x mplayer.exe)
425 WOutDev[i].caps.wMid = 0x0002;
426 WOutDev[i].caps.wPid = 0x0104;
427 strcpy(WOutDev[i].caps.szPname, "SB16 Wave Out");
429 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
430 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
431 /* strcpy(WOutDev[i].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
432 strcpy(WOutDev[i].caps.szPname, "CS4236/37/38");
434 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "winearts: %d", i);
436 WOutDev[i].caps.vDriverVersion = 0x0100;
437 WOutDev[i].caps.dwFormats = 0x00000000;
438 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
440 WOutDev[i].caps.wChannels = 2;
441 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
443 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
444 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
445 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
446 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
447 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
448 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
449 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
450 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
451 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
452 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
453 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
454 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
457 for (i = 0; i < MAX_WAVEINDRV; ++i)
459 WInDev[i].record_stream = (arts_stream_t*)-1;
460 memset(&WInDev[i].caps, 0, sizeof(WInDev[i].caps)); /* zero out
462 /* FIXME: some programs compare this string against the content of the registry
463 * for MM drivers. The names have to match in order for the program to work
464 * (e.g. MS win9x mplayer.exe)
467 WInDev[i].caps.wMid = 0x0002;
468 WInDev[i].caps.wPid = 0x0104;
469 strcpy(WInDev[i].caps.szPname, "SB16 Wave In");
471 WInDev[i].caps.wMid = 0x00FF;
472 WInDev[i].caps.wPid = 0x0001;
473 strcpy(WInDev[i].caps.szPname,"CS4236/37/38");
475 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "winearts: %d", i);
477 WInDev[i].caps.vDriverVersion = 0x0100;
478 WInDev[i].caps.dwFormats = 0x00000000;
480 WInDev[i].caps.wChannels = 2;
482 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
483 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
484 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
485 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
486 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
487 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
488 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
489 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
490 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
491 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
492 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
493 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
495 WInDev[i].caps.wReserved1 = 0;
500 /******************************************************************
501 * ARTS_InitRingMessage
503 * Initialize the ring of messages for passing between driver's caller and playback/record
506 static int ARTS_InitRingMessage(ARTS_MSG_RING* mr)
510 mr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
511 mr->ring_buffer_size = ARTS_RING_BUFFER_INCREMENT;
512 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
513 InitializeCriticalSection(&mr->msg_crst);
517 /******************************************************************
518 * ARTS_DestroyRingMessage
521 static int ARTS_DestroyRingMessage(ARTS_MSG_RING* mr)
523 CloseHandle(mr->msg_event);
524 HeapFree(GetProcessHeap(),0,mr->messages);
526 DeleteCriticalSection(&mr->msg_crst);
530 /******************************************************************
531 * ARTS_AddRingMessage
533 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
535 static int ARTS_AddRingMessage(ARTS_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
537 HANDLE hEvent = INVALID_HANDLE_VALUE;
539 EnterCriticalSection(&mr->msg_crst);
540 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
542 int old_ring_buffer_size = mr->ring_buffer_size;
543 mr->ring_buffer_size += ARTS_RING_BUFFER_INCREMENT;
544 TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
545 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
546 /* Now we need to rearrange the ring buffer so that the new
547 buffers just allocated are in between mr->msg_tosave and
550 if (mr->msg_tosave < mr->msg_toget)
552 memmove(&(mr->messages[mr->msg_toget + ARTS_RING_BUFFER_INCREMENT]),
553 &(mr->messages[mr->msg_toget]),
554 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
556 mr->msg_toget += ARTS_RING_BUFFER_INCREMENT;
561 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
562 if (hEvent == INVALID_HANDLE_VALUE)
564 ERR("can't create event !?\n");
565 LeaveCriticalSection(&mr->msg_crst);
568 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
569 FIXME("two fast messages in the queue!!!!\n");
571 /* fast messages have to be added at the start of the queue */
572 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
574 mr->messages[mr->msg_toget].msg = msg;
575 mr->messages[mr->msg_toget].param = param;
576 mr->messages[mr->msg_toget].hEvent = hEvent;
580 mr->messages[mr->msg_tosave].msg = msg;
581 mr->messages[mr->msg_tosave].param = param;
582 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
583 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
586 LeaveCriticalSection(&mr->msg_crst);
588 SetEvent(mr->msg_event); /* signal a new message */
592 /* wait for playback/record thread to have processed the message */
593 WaitForSingleObject(hEvent, INFINITE);
600 /******************************************************************
601 * ARTS_RetrieveRingMessage
603 * Get a message from the ring. Should be called by the playback/record thread.
605 static int ARTS_RetrieveRingMessage(ARTS_MSG_RING* mr,
606 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
608 EnterCriticalSection(&mr->msg_crst);
610 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
612 LeaveCriticalSection(&mr->msg_crst);
616 *msg = mr->messages[mr->msg_toget].msg;
617 mr->messages[mr->msg_toget].msg = 0;
618 *param = mr->messages[mr->msg_toget].param;
619 *hEvent = mr->messages[mr->msg_toget].hEvent;
620 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
621 LeaveCriticalSection(&mr->msg_crst);
625 /*======================================================================*
626 * Low level WAVE OUT implementation *
627 *======================================================================*/
629 /**************************************************************************
630 * wodNotifyClient [internal]
632 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
634 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
640 if (wwo->wFlags != DCB_NULL &&
641 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
642 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
643 WARN("can't notify client !\n");
644 return MMSYSERR_ERROR;
648 FIXME("Unknown callback message %u\n", wMsg);
649 return MMSYSERR_INVALPARAM;
651 return MMSYSERR_NOERROR;
654 /**************************************************************************
655 * wodUpdatePlayedTotal [internal]
658 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
660 /* total played is the bytes written less the bytes to write ;-) */
661 wwo->dwPlayedTotal = wwo->dwWrittenTotal -
663 arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE));
668 /**************************************************************************
669 * wodPlayer_BeginWaveHdr [internal]
671 * Makes the specified lpWaveHdr the currently playing wave header.
672 * If the specified wave header is a begin loop and we're not already in
673 * a loop, setup the loop.
675 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
677 wwo->lpPlayPtr = lpWaveHdr;
679 if (!lpWaveHdr) return;
681 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
682 if (wwo->lpLoopPtr) {
683 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
684 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
686 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
687 wwo->lpLoopPtr = lpWaveHdr;
688 /* Windows does not touch WAVEHDR.dwLoops,
689 * so we need to make an internal copy */
690 wwo->dwLoops = lpWaveHdr->dwLoops;
693 wwo->dwPartialOffset = 0;
696 /**************************************************************************
697 * wodPlayer_PlayPtrNext [internal]
699 * Advance the play pointer to the next waveheader, looping if required.
701 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
703 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
705 wwo->dwPartialOffset = 0;
706 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
707 /* We're at the end of a loop, loop if required */
708 if (--wwo->dwLoops > 0) {
709 wwo->lpPlayPtr = wwo->lpLoopPtr;
711 /* Handle overlapping loops correctly */
712 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
713 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
714 /* shall we consider the END flag for the closing loop or for
715 * the opening one or for both ???
716 * code assumes for closing loop only
719 lpWaveHdr = lpWaveHdr->lpNext;
721 wwo->lpLoopPtr = NULL;
722 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
725 /* We're not in a loop. Advance to the next wave header */
726 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
732 /**************************************************************************
733 * wodPlayer_NotifyWait [internal]
734 * Returns the number of milliseconds to wait before attempting to notify
735 * completion of the specified wavehdr.
736 * This is based on the number of bytes remaining to be written in the
739 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
743 if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
749 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
750 if(!dwMillis) dwMillis = 1;
753 TRACE("dwMillis = %ld\n", dwMillis);
759 /**************************************************************************
760 * wodPlayer_WriteMaxFrags [internal]
761 * Writes the maximum number of bytes possible to the DSP and returns
762 * the number of bytes written.
764 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
766 /* Only attempt to write to free bytes */
767 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
768 int toWrite = min(dwLength, *bytes);
771 TRACE("Writing wavehdr %p.%lu[%lu]\n",
772 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);
774 /* see if our buffer isn't large enough for the data we are writing */
775 if(wwo->buffer_size < toWrite)
777 if(wwo->sound_buffer)
779 wwo->sound_buffer = HeapReAlloc(GetProcessHeap(), 0, wwo->sound_buffer, toWrite);
780 wwo->buffer_size = toWrite;
784 /* if we don't have a buffer then get one */
785 if(!wwo->sound_buffer)
787 /* allocate some memory for the buffer */
788 wwo->sound_buffer = HeapAlloc(GetProcessHeap(), 0, toWrite);
789 wwo->buffer_size = toWrite;
792 /* if we don't have a buffer then error out */
793 if(!wwo->sound_buffer)
795 ERR("error allocating sound_buffer memory\n");
799 TRACE("toWrite == %d\n", toWrite);
801 /* apply volume to the bits */
802 /* for single channel audio streams we only use the LEFT volume */
803 if(wwo->format.wBitsPerSample == 16)
805 /* apply volume to the buffer we are about to send */
806 /* divide toWrite(bytes) by 2 as volume processes by 16 bits */
807 volume_effect16(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
808 wwo->sound_buffer, toWrite>>1, wwo->volume_left,
809 wwo->volume_right, wwo->format.wf.nChannels);
810 } else if(wwo->format.wBitsPerSample == 8)
812 /* apply volume to the buffer we are about to send */
813 volume_effect8(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
814 wwo->sound_buffer, toWrite, wwo->volume_left,
815 wwo->volume_right, wwo->format.wf.nChannels);
818 FIXME("unsupported wwo->format.wBitsPerSample of %d\n",
819 wwo->format.wBitsPerSample);
822 /* send the audio data to arts for playing */
823 written = arts_write(wwo->play_stream, wwo->sound_buffer, toWrite);
825 TRACE("written = %d\n", written);
829 *bytes = 0; /* apparently arts is actually full */
830 return written; /* if we wrote nothing just return */
833 if (written >= dwLength)
834 wodPlayer_PlayPtrNext(wwo); /* If we wrote all current wavehdr, skip to the next one */
836 wwo->dwPartialOffset += written; /* Remove the amount written */
838 if (written < toWrite)
843 wwo->dwWrittenTotal += written; /* update stats on this wave device */
845 return written; /* return the number of bytes written */
849 /**************************************************************************
850 * wodPlayer_NotifyCompletions [internal]
852 * Notifies and remove from queue all wavehdrs which have been played to
853 * the speaker (ie. they have cleared the audio device). If force is true,
854 * we notify all wavehdrs and remove them all from the queue even if they
855 * are unplayed or part of a loop.
857 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
861 if (wwo->lpQueuePtr) {
862 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%ld), force=(%d)\n",
866 wwo->lpQueuePtr->reserved,
870 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), dwWrittenTotal=(%ld), force=(%d)\n",
878 /* Start from lpQueuePtr and keep notifying until:
879 * - we hit an unwritten wavehdr
880 * - we hit the beginning of a running loop
881 * - we hit a wavehdr which hasn't finished playing
883 while ((lpWaveHdr = wwo->lpQueuePtr) &&
885 (lpWaveHdr != wwo->lpPlayPtr &&
886 lpWaveHdr != wwo->lpLoopPtr &&
887 lpWaveHdr->reserved <= wwo->dwWrittenTotal))) {
889 wwo->lpQueuePtr = lpWaveHdr->lpNext;
891 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
892 lpWaveHdr->dwFlags |= WHDR_DONE;
894 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
896 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
897 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
900 /**************************************************************************
901 * wodPlayer_Reset [internal]
903 * wodPlayer helper. Resets current output stream.
905 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
907 wodUpdatePlayedTotal(wwo);
909 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
911 /* we aren't able to flush any data that has already been written */
912 /* to arts, otherwise we would do the flushing here */
915 enum win_wm_message msg;
919 /* remove any buffer */
920 wodPlayer_NotifyCompletions(wwo, TRUE);
922 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
923 wwo->state = WINE_WS_STOPPED;
924 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
926 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
928 /* remove any existing message in the ring */
929 EnterCriticalSection(&wwo->msgRing.msg_crst);
931 /* return all pending headers in queue */
932 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
934 TRACE("flushing msg\n");
935 if (msg != WINE_WM_HEADER)
937 FIXME("shouldn't have headers left\n");
941 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
942 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
944 wodNotifyClient(wwo, WOM_DONE, param, 0);
946 ResetEvent(wwo->msgRing.msg_event);
947 LeaveCriticalSection(&wwo->msgRing.msg_crst);
949 if (wwo->lpLoopPtr) {
950 /* complicated case, not handled yet (could imply modifying the loop counter */
951 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
952 wwo->lpPlayPtr = wwo->lpLoopPtr;
953 wwo->dwPartialOffset = 0;
954 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
956 /* the data already written is going to be played, so take */
957 /* this fact into account here */
958 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
960 wwo->state = WINE_WS_PAUSED;
964 /**************************************************************************
965 * wodPlayer_ProcessMessages [internal]
967 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
970 enum win_wm_message msg;
974 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
975 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
977 case WINE_WM_PAUSING:
978 wodPlayer_Reset(wwo, FALSE);
981 case WINE_WM_RESTARTING:
982 wwo->state = WINE_WS_PLAYING;
986 lpWaveHdr = (LPWAVEHDR)param;
988 /* insert buffer at the end of queue */
991 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
995 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
996 if (wwo->state == WINE_WS_STOPPED)
997 wwo->state = WINE_WS_PLAYING;
999 case WINE_WM_RESETTING:
1000 wodPlayer_Reset(wwo, TRUE);
1003 case WINE_WM_UPDATE:
1004 wodUpdatePlayedTotal(wwo);
1007 case WINE_WM_BREAKLOOP:
1008 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1009 /* ensure exit at end of current loop */
1014 case WINE_WM_CLOSING:
1015 /* sanity check: this should not happen since the device must have been reset before */
1016 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1018 wwo->state = WINE_WS_CLOSED;
1021 /* shouldn't go here */
1023 FIXME("unknown message %d\n", msg);
1029 /**************************************************************************
1030 * wodPlayer_FeedDSP [internal]
1031 * Feed as much sound data as we can into the DSP and return the number of
1032 * milliseconds before it will be necessary to feed the DSP again.
1034 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1038 wodUpdatePlayedTotal(wwo);
1039 availInQ = arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE);
1040 TRACE("availInQ = %ld\n", availInQ);
1042 /* input queue empty */
1043 if (!wwo->lpPlayPtr) {
1044 TRACE("Run out of wavehdr:s... flushing\n");
1048 /* no more room... no need to try to feed */
1051 TRACE("no more room, no need to try to feed\n");
1052 return wwo->dwSleepTime;
1055 /* Feed from partial wavehdr */
1056 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0)
1058 TRACE("feeding from partial wavehdr\n");
1059 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1062 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1063 if (!wwo->dwPartialOffset)
1065 while(wwo->lpPlayPtr && availInQ)
1067 TRACE("feeding waveheaders until we run out of space\n");
1068 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1069 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1070 TRACE("reserved=(%ld) dwWrittenTotal=(%ld) dwBufferLength=(%ld)\n",
1071 wwo->lpPlayPtr->reserved,
1072 wwo->dwWrittenTotal,
1073 wwo->lpPlayPtr->dwBufferLength
1075 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1079 if (!wwo->lpPlayPtr) {
1080 TRACE("Ran out of wavehdrs\n");
1084 return wwo->dwSleepTime;
1088 /**************************************************************************
1089 * wodPlayer [internal]
1091 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1093 WORD uDevID = (DWORD)pmt;
1094 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1095 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1096 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1099 wwo->state = WINE_WS_STOPPED;
1100 SetEvent(wwo->hStartUpEvent);
1103 /** Wait for the shortest time before an action is required. If there
1104 * are no pending actions, wait forever for a command.
1106 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1107 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1108 WaitForSingleObject(wwo->msgRing.msg_event, dwSleepTime);
1109 wodPlayer_ProcessMessages(wwo);
1110 if (wwo->state == WINE_WS_PLAYING) {
1111 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1112 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1114 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1119 /**************************************************************************
1120 * wodGetDevCaps [internal]
1122 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1124 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1126 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1128 if (wDevID >= MAX_WAVEOUTDRV) {
1129 TRACE("MAX_WAVOUTDRV reached !\n");
1130 return MMSYSERR_BADDEVICEID;
1133 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1134 return MMSYSERR_NOERROR;
1137 /**************************************************************************
1138 * wodOpen [internal]
1140 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1144 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1145 if (lpDesc == NULL) {
1146 WARN("Invalid Parameter !\n");
1147 return MMSYSERR_INVALPARAM;
1149 if (wDevID >= MAX_WAVEOUTDRV) {
1150 TRACE("MAX_WAVOUTDRV reached !\n");
1151 return MMSYSERR_BADDEVICEID;
1154 /* if this device is already open tell the app that it is allocated */
1155 if(WOutDev[wDevID].play_stream != (arts_stream_t*)-1)
1157 TRACE("device already allocated\n");
1158 return MMSYSERR_ALLOCATED;
1161 /* only PCM format is supported so far... */
1162 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1163 lpDesc->lpFormat->nChannels == 0 ||
1164 lpDesc->lpFormat->nSamplesPerSec < DSBFREQUENCY_MIN ||
1165 lpDesc->lpFormat->nSamplesPerSec > DSBFREQUENCY_MAX ||
1166 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1167 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1168 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1169 lpDesc->lpFormat->nSamplesPerSec);
1170 return WAVERR_BADFORMAT;
1173 if (dwFlags & WAVE_FORMAT_QUERY) {
1174 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1175 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1176 lpDesc->lpFormat->nSamplesPerSec);
1177 return MMSYSERR_NOERROR;
1180 wwo = &WOutDev[wDevID];
1182 /* direct sound not supported, ignore the flag */
1183 dwFlags &= ~WAVE_DIRECTSOUND;
1185 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1187 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1188 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1190 if (wwo->format.wBitsPerSample == 0) {
1191 WARN("Resetting zeroed wBitsPerSample\n");
1192 wwo->format.wBitsPerSample = 8 *
1193 (wwo->format.wf.nAvgBytesPerSec /
1194 wwo->format.wf.nSamplesPerSec) /
1195 wwo->format.wf.nChannels;
1198 wwo->play_stream = arts_play_stream(wwo->format.wf.nSamplesPerSec,
1199 wwo->format.wBitsPerSample, wwo->format.wf.nChannels, "winearts");
1201 /* clear these so we don't have any confusion ;-) */
1202 wwo->sound_buffer = 0;
1203 wwo->buffer_size = 0;
1205 arts_stream_set(wwo->play_stream, ARTS_P_BLOCKING, 0); /* disable blocking on this stream */
1207 if(!wwo->play_stream) return MMSYSERR_ALLOCATED;
1209 /* Try to set the packet settings from constant and store the value that it
1210 was actually set to for future use */
1211 wwo->packetSettings = arts_stream_set(wwo->play_stream, ARTS_P_PACKET_SETTINGS, WAVEOUT_PACKET_SETTINGS);
1212 TRACE("Tried to set ARTS_P_PACKET_SETTINGS to (%x), actually set to (%x)\n", WAVEOUT_PACKET_SETTINGS, wwo->packetSettings);
1214 wwo->dwBufferSize = arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SIZE);
1215 TRACE("Buffer size is now (%ld)\n",wwo->dwBufferSize);
1217 wwo->dwPlayedTotal = 0;
1218 wwo->dwWrittenTotal = 0;
1220 wwo->dwSleepTime = ((1 << (wwo->packetSettings & 0xFFFF)) * 1000 * BUFFER_REFILL_THRESHOLD) / wwo->format.wf.nAvgBytesPerSec;
1222 /* Initialize volume to full level */
1223 wwo->volume_left = 100;
1224 wwo->volume_right = 100;
1226 ARTS_InitRingMessage(&wwo->msgRing);
1228 /* create player thread */
1229 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1230 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1231 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1232 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1233 CloseHandle(wwo->hStartUpEvent);
1235 wwo->hThread = INVALID_HANDLE_VALUE;
1236 wwo->dwThreadID = 0;
1238 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1240 TRACE("stream=0x%lx, dwBufferSize=%ld\n",
1241 (long)wwo->play_stream, wwo->dwBufferSize);
1243 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1244 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1245 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1246 wwo->format.wf.nBlockAlign);
1248 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1251 /**************************************************************************
1252 * wodClose [internal]
1254 static DWORD wodClose(WORD wDevID)
1256 DWORD ret = MMSYSERR_NOERROR;
1259 TRACE("(%u);\n", wDevID);
1261 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1264 WARN("bad device ID !\n");
1265 return MMSYSERR_BADDEVICEID;
1268 wwo = &WOutDev[wDevID];
1269 if (wwo->lpQueuePtr) {
1270 WARN("buffers still playing !\n");
1271 ret = WAVERR_STILLPLAYING;
1273 TRACE("imhere[3-close]\n");
1274 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1275 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1278 ARTS_DestroyRingMessage(&wwo->msgRing);
1280 ARTS_CloseWaveOutDevice(wwo); /* close the stream and clean things up */
1282 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1287 /**************************************************************************
1288 * wodWrite [internal]
1291 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1293 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1295 /* first, do the sanity checks... */
1296 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1299 WARN("bad dev ID !\n");
1300 return MMSYSERR_BADDEVICEID;
1303 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1305 TRACE("unprepared\n");
1306 return WAVERR_UNPREPARED;
1309 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1311 TRACE("still playing\n");
1312 return WAVERR_STILLPLAYING;
1315 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1316 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1317 lpWaveHdr->lpNext = 0;
1319 TRACE("adding ring message\n");
1320 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1322 return MMSYSERR_NOERROR;
1325 /**************************************************************************
1326 * wodPrepare [internal]
1328 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1330 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1332 if (wDevID >= MAX_WAVEOUTDRV) {
1333 WARN("bad device ID !\n");
1334 return MMSYSERR_BADDEVICEID;
1337 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1338 return WAVERR_STILLPLAYING;
1340 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1341 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1342 return MMSYSERR_NOERROR;
1345 /**************************************************************************
1346 * wodUnprepare [internal]
1348 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1350 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1352 if (wDevID >= MAX_WAVEOUTDRV) {
1353 WARN("bad device ID !\n");
1354 return MMSYSERR_BADDEVICEID;
1357 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1358 return WAVERR_STILLPLAYING;
1360 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1361 lpWaveHdr->dwFlags |= WHDR_DONE;
1363 return MMSYSERR_NOERROR;
1366 /**************************************************************************
1367 * wodPause [internal]
1369 static DWORD wodPause(WORD wDevID)
1371 TRACE("(%u);!\n", wDevID);
1373 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1376 WARN("bad device ID !\n");
1377 return MMSYSERR_BADDEVICEID;
1380 TRACE("imhere[3-PAUSING]\n");
1381 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1383 return MMSYSERR_NOERROR;
1386 /**************************************************************************
1387 * wodRestart [internal]
1389 static DWORD wodRestart(WORD wDevID)
1391 TRACE("(%u);\n", wDevID);
1393 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1396 WARN("bad device ID !\n");
1397 return MMSYSERR_BADDEVICEID;
1400 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1401 TRACE("imhere[3-RESTARTING]\n");
1402 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1405 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1406 /* FIXME: Myst crashes with this ... hmm -MM
1407 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1410 return MMSYSERR_NOERROR;
1413 /**************************************************************************
1414 * wodReset [internal]
1416 static DWORD wodReset(WORD wDevID)
1418 TRACE("(%u);\n", wDevID);
1420 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1423 WARN("bad device ID !\n");
1424 return MMSYSERR_BADDEVICEID;
1427 TRACE("imhere[3-RESET]\n");
1428 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1430 return MMSYSERR_NOERROR;
1433 /**************************************************************************
1434 * wodGetPosition [internal]
1436 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1440 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1442 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1445 WARN("bad device ID !\n");
1446 return MMSYSERR_BADDEVICEID;
1449 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1451 wwo = &WOutDev[wDevID];
1452 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1454 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->format);
1457 /**************************************************************************
1458 * wodBreakLoop [internal]
1460 static DWORD wodBreakLoop(WORD wDevID)
1462 TRACE("(%u);\n", wDevID);
1464 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1467 WARN("bad device ID !\n");
1468 return MMSYSERR_BADDEVICEID;
1470 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1471 return MMSYSERR_NOERROR;
1474 /**************************************************************************
1475 * wodGetVolume [internal]
1477 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1481 left = WOutDev[wDevID].volume_left;
1482 right = WOutDev[wDevID].volume_right;
1484 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1486 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) <<
1489 return MMSYSERR_NOERROR;
1492 /**************************************************************************
1493 * wodSetVolume [internal]
1495 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1499 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1500 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1502 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1504 WOutDev[wDevID].volume_left = left;
1505 WOutDev[wDevID].volume_right = right;
1507 return MMSYSERR_NOERROR;
1510 /**************************************************************************
1511 * wodGetNumDevs [internal]
1513 static DWORD wodGetNumDevs(void)
1515 return MAX_WAVEOUTDRV;
1518 /**************************************************************************
1519 * wodDevInterfaceSize [internal]
1521 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1523 TRACE("(%u, %p)\n", wDevID, dwParam1);
1525 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1526 NULL, 0 ) * sizeof(WCHAR);
1527 return MMSYSERR_NOERROR;
1530 /**************************************************************************
1531 * wodDevInterface [internal]
1533 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1535 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1536 NULL, 0 ) * sizeof(WCHAR))
1538 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1539 dwParam1, dwParam2 / sizeof(WCHAR));
1540 return MMSYSERR_NOERROR;
1542 return MMSYSERR_INVALPARAM;
1545 /**************************************************************************
1546 * wodMessage (WINEARTS.@)
1548 DWORD WINAPI ARTS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1549 DWORD dwParam1, DWORD dwParam2)
1551 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1552 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1559 /* FIXME: Pretend this is supported */
1561 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1562 case WODM_CLOSE: return wodClose (wDevID);
1563 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1564 case WODM_PAUSE: return wodPause (wDevID);
1565 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1566 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1567 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1568 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1569 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1570 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1571 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1572 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1573 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1574 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1575 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1576 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1577 case WODM_RESTART: return wodRestart (wDevID);
1578 case WODM_RESET: return wodReset (wDevID);
1580 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1581 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1582 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1583 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1584 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1586 FIXME("unknown message %d!\n", wMsg);
1588 return MMSYSERR_NOTSUPPORTED;
1591 /*======================================================================*
1592 * Low level WAVE IN implementation *
1593 *======================================================================*/
1595 /**************************************************************************
1596 * widGetNumDevs [internal]
1598 static DWORD widGetNumDevs(void)
1600 TRACE("%d \n",MAX_WAVEINDRV);
1601 return MAX_WAVEINDRV;
1604 /**************************************************************************
1605 * widDevInterfaceSize [internal]
1607 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1609 TRACE("(%u, %p)\n", wDevID, dwParam1);
1612 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1613 NULL, 0 ) * sizeof(WCHAR);
1614 return MMSYSERR_NOERROR;
1617 /**************************************************************************
1618 * widDevInterface [internal]
1620 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1622 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1623 NULL, 0 ) * sizeof(WCHAR))
1625 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1626 dwParam1, dwParam2 / sizeof(WCHAR));
1627 return MMSYSERR_NOERROR;
1629 return MMSYSERR_INVALPARAM;
1632 /**************************************************************************
1633 * widNotifyClient [internal]
1635 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1637 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
1643 if (wwi->wFlags != DCB_NULL &&
1644 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1645 (HDRVR)wwi->waveDesc.hWave, wMsg,
1646 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
1647 WARN("can't notify client !\n");
1648 return MMSYSERR_ERROR;
1652 FIXME("Unknown callback message %u\n", wMsg);
1653 return MMSYSERR_INVALPARAM;
1655 return MMSYSERR_NOERROR;
1658 /**************************************************************************
1659 * widGetDevCaps [internal]
1661 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
1663 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1665 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1667 if (wDevID >= MAX_WAVEINDRV) {
1668 TRACE("MAX_WAVINDRV reached !\n");
1669 return MMSYSERR_BADDEVICEID;
1672 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1673 return MMSYSERR_NOERROR;
1676 /**************************************************************************
1677 * widRecorder [internal]
1679 static DWORD CALLBACK widRecorder(LPVOID pmt)
1681 WORD uDevID = (DWORD)pmt;
1682 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1687 enum win_wm_message msg;
1691 SetEvent(wwi->hStartUpEvent);
1693 /* make sleep time to be # of ms to record one packet */
1694 dwSleepTime = ((1 << (wwi->packetSettings & 0xFFFF)) * 1000) / wwi->format.wf.nAvgBytesPerSec;
1695 TRACE("sleeptime=%ld ms\n", dwSleepTime);
1698 /* Oddly enough, dwBufferSpace is sometimes negative....
1700 * NOTE: If you remove this call to arts_stream_get() and
1701 * remove the && (dwBufferSpace > 0) the code will still
1702 * function correctly. I don't know which way is
1705 dwBufferSpace = arts_stream_get(wwi->record_stream, ARTS_P_BUFFER_SPACE);
1706 TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d), dwBufferSpace=(%d)\n",wwi->lpQueuePtr,wwi->state,dwBufferSpace);
1708 /* read all data is arts input buffer. */
1709 if ((wwi->lpQueuePtr != NULL) && (wwi->state == WINE_WS_PLAYING) && (dwBufferSpace > 0))
1711 lpWaveHdr = wwi->lpQueuePtr;
1713 TRACE("read as much as we can\n");
1714 while(wwi->lpQueuePtr)
1716 TRACE("attempt to read %ld bytes\n",lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1717 bytesRead = arts_read(wwi->record_stream,
1718 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1719 lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1720 TRACE("bytesRead=%ld\n",bytesRead);
1721 if (bytesRead==0) break;
1723 lpWaveHdr->dwBytesRecorded += bytesRead;
1724 wwi->dwRecordedTotal += bytesRead;
1726 /* buffer full. notify client */
1727 if (lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength)
1729 /* must copy the value of next waveHdr, because we have no idea of what
1730 * will be done with the content of lpWaveHdr in callback
1732 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1734 TRACE("waveHdr full.\n");
1736 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1737 lpWaveHdr->dwFlags |= WHDR_DONE;
1739 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1740 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1745 /* wait for dwSleepTime or an event in thread's queue */
1746 WaitForSingleObject(wwi->msgRing.msg_event, dwSleepTime);
1748 while (ARTS_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
1750 TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString[msg - WM_USER - 1], param);
1752 case WINE_WM_PAUSING:
1753 wwi->state = WINE_WS_PAUSED;
1755 /* Put code here to "pause" arts recording
1760 case WINE_WM_STARTING:
1761 wwi->state = WINE_WS_PLAYING;
1763 /* Put code here to "start" arts recording
1768 case WINE_WM_HEADER:
1769 lpWaveHdr = (LPWAVEHDR)param;
1770 /* insert buffer at end of queue */
1773 int num_headers = 0;
1774 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1782 case WINE_WM_STOPPING:
1783 if (wwi->state != WINE_WS_STOPPED)
1786 /* Put code here to "stop" arts recording
1789 /* return current buffer to app */
1790 lpWaveHdr = wwi->lpQueuePtr;
1793 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1794 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1795 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1796 lpWaveHdr->dwFlags |= WHDR_DONE;
1797 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1798 wwi->lpQueuePtr = lpNext;
1801 wwi->state = WINE_WS_STOPPED;
1804 case WINE_WM_RESETTING:
1805 wwi->state = WINE_WS_STOPPED;
1806 wwi->dwRecordedTotal = 0;
1808 /* return all buffers to the app */
1809 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1810 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1811 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1812 lpWaveHdr->dwFlags |= WHDR_DONE;
1814 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1816 wwi->lpQueuePtr = NULL;
1819 case WINE_WM_CLOSING:
1821 wwi->state = WINE_WS_CLOSED;
1824 /* shouldn't go here */
1826 FIXME("unknown message %d\n", msg);
1832 /* just for not generating compilation warnings... should never be executed */
1836 /**************************************************************************
1837 * widOpen [internal]
1839 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1843 TRACE("(%u, %p %08lX);\n",wDevID, lpDesc, dwFlags);
1844 if (lpDesc == NULL) {
1845 WARN("Invalid Parametr (lpDesc == NULL)!\n");
1846 return MMSYSERR_INVALPARAM;
1849 if (wDevID >= MAX_WAVEINDRV) {
1850 TRACE ("MAX_WAVEINDRV reached !\n");
1851 return MMSYSERR_BADDEVICEID;
1854 /* if this device is already open tell the app that it is allocated */
1855 if(WInDev[wDevID].record_stream != (arts_stream_t*)-1)
1857 TRACE("device already allocated\n");
1858 return MMSYSERR_ALLOCATED;
1861 /* only PCM format is support so far... */
1862 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1863 lpDesc->lpFormat->nChannels == 0 ||
1864 lpDesc->lpFormat->nSamplesPerSec < DSBFREQUENCY_MIN ||
1865 lpDesc->lpFormat->nSamplesPerSec > DSBFREQUENCY_MAX ||
1866 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1867 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1868 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1869 lpDesc->lpFormat->nSamplesPerSec);
1870 return WAVERR_BADFORMAT;
1873 if (dwFlags & WAVE_FORMAT_QUERY) {
1874 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1875 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1876 lpDesc->lpFormat->nSamplesPerSec);
1877 return MMSYSERR_NOERROR;
1880 wwi = &WInDev[wDevID];
1882 /* direct sound not supported, ignore the flag */
1883 dwFlags &= ~WAVE_DIRECTSOUND;
1885 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1887 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1888 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1890 if (wwi->format.wBitsPerSample == 0) {
1891 WARN("Resetting zerod wBitsPerSample\n");
1892 wwi->format.wBitsPerSample = 8 *
1893 (wwi->format.wf.nAvgBytesPerSec /
1894 wwi->format.wf.nSamplesPerSec) /
1895 wwi->format.wf.nChannels;
1898 wwi->record_stream = arts_record_stream(wwi->format.wf.nSamplesPerSec,
1899 wwi->format.wBitsPerSample,
1900 wwi->format.wf.nChannels,
1902 TRACE("(wwi->record_stream=%p)\n",wwi->record_stream);
1903 wwi->state = WINE_WS_STOPPED;
1905 wwi->packetSettings = arts_stream_set(wwi->record_stream, ARTS_P_PACKET_SETTINGS, WAVEIN_PACKET_SETTINGS);
1906 TRACE("Tried to set ARTS_P_PACKET_SETTINGS to (%x), actually set to (%x)\n", WAVEIN_PACKET_SETTINGS, wwi->packetSettings);
1907 TRACE("Buffer size is now (%d)\n", arts_stream_get(wwi->record_stream, ARTS_P_BUFFER_SIZE));
1909 if (wwi->lpQueuePtr) {
1910 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1911 wwi->lpQueuePtr = NULL;
1913 arts_stream_set(wwi->record_stream, ARTS_P_BLOCKING, 0); /* disable blocking on this stream */
1915 if(!wwi->record_stream) return MMSYSERR_ALLOCATED;
1917 wwi->dwRecordedTotal = 0;
1918 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1920 ARTS_InitRingMessage(&wwi->msgRing);
1922 /* create recorder thread */
1923 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1924 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1925 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
1926 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
1927 CloseHandle(wwi->hStartUpEvent);
1929 wwi->hThread = INVALID_HANDLE_VALUE;
1930 wwi->dwThreadID = 0;
1932 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
1934 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1935 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
1936 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
1937 wwi->format.wf.nBlockAlign);
1938 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
1941 /**************************************************************************
1942 * widClose [internal]
1944 static DWORD widClose(WORD wDevID)
1948 TRACE("(%u);\n", wDevID);
1949 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1950 WARN("can't close !\n");
1951 return MMSYSERR_INVALHANDLE;
1954 wwi = &WInDev[wDevID];
1956 if (wwi->lpQueuePtr != NULL) {
1957 WARN("still buffers open !\n");
1958 return WAVERR_STILLPLAYING;
1961 ARTS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
1962 ARTS_CloseWaveInDevice(wwi);
1963 wwi->state = WINE_WS_CLOSED;
1964 ARTS_DestroyRingMessage(&wwi->msgRing);
1965 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
1968 /**************************************************************************
1969 * widAddBuffer [internal]
1971 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1973 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1975 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1976 WARN("can't do it !\n");
1977 return MMSYSERR_INVALHANDLE;
1979 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
1980 TRACE("never been prepared !\n");
1981 return WAVERR_UNPREPARED;
1983 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
1984 TRACE("header already in use !\n");
1985 return WAVERR_STILLPLAYING;
1988 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1989 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1990 lpWaveHdr->dwBytesRecorded = 0;
1991 lpWaveHdr->lpNext = NULL;
1993 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1994 return MMSYSERR_NOERROR;
1997 /**************************************************************************
1998 * widPrepare [internal]
2000 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2002 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2004 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
2006 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2007 return WAVERR_STILLPLAYING;
2009 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2010 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2011 lpWaveHdr->dwBytesRecorded = 0;
2013 return MMSYSERR_NOERROR;
2016 /**************************************************************************
2017 * widUnprepare [internal]
2019 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2021 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2022 if (wDevID >= MAX_WAVEINDRV) {
2023 WARN("bad device ID !\n");
2024 return MMSYSERR_INVALHANDLE;
2027 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2028 TRACE("Still playing...\n");
2029 return WAVERR_STILLPLAYING;
2032 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2033 lpWaveHdr->dwFlags |= WHDR_DONE;
2035 return MMSYSERR_NOERROR;
2038 /**************************************************************************
2039 * widStart [internal]
2041 static DWORD widStart(WORD wDevID)
2043 TRACE("(%u);\n", wDevID);
2044 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2045 WARN("can't start recording !\n");
2046 return MMSYSERR_INVALHANDLE;
2049 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
2050 return MMSYSERR_NOERROR;
2053 /**************************************************************************
2054 * widStop [internal]
2056 static DWORD widStop(WORD wDevID)
2058 TRACE("(%u);\n", wDevID);
2059 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2060 WARN("can't stop !\n");
2061 return MMSYSERR_INVALHANDLE;
2064 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
2066 return MMSYSERR_NOERROR;
2069 /**************************************************************************
2070 * widReset [internal]
2072 static DWORD widReset(WORD wDevID)
2074 TRACE("(%u);\n", wDevID);
2075 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2076 WARN("can't reset !\n");
2077 return MMSYSERR_INVALHANDLE;
2079 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2080 return MMSYSERR_NOERROR;
2083 /**************************************************************************
2084 * widMessage (WINEARTS.6)
2086 DWORD WINAPI ARTS_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2087 DWORD dwParam1, DWORD dwParam2)
2089 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2090 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2096 /* FIXME: Pretend this is supported */
2098 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2099 case WIDM_CLOSE: return widClose (wDevID);
2100 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2101 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2102 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2103 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2104 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2105 case WIDM_RESET: return widReset (wDevID);
2106 case WIDM_START: return widStart (wDevID);
2107 case WIDM_STOP: return widStop (wDevID);
2108 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2109 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2111 FIXME("unknown message %d!\n", wMsg);
2113 return MMSYSERR_NOTSUPPORTED;
2116 /*======================================================================*
2117 * Low level DSOUND implementation *
2118 *======================================================================*/
2119 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2121 /* we can't perform memory mapping as we don't have a file stream
2122 interface with arts like we do with oss */
2123 MESSAGE("This sound card's driver does not support direct access\n");
2124 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2125 return MMSYSERR_NOTSUPPORTED;
2128 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2130 memset(desc, 0, sizeof(*desc));
2131 strcpy(desc->szDesc, "Wine aRts DirectSound Driver");
2132 strcpy(desc->szDrvName, "winearts.drv");
2133 return MMSYSERR_NOERROR;
2136 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
2138 memcpy(pGuid, &DSDEVID_DefaultPlayback, sizeof(GUID));
2139 return MMSYSERR_NOERROR;
2142 #else /* !HAVE_ARTS */
2144 /**************************************************************************
2145 * wodMessage (WINEARTS.@)
2147 DWORD WINAPI ARTS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2148 DWORD dwParam1, DWORD dwParam2)
2150 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2151 return MMSYSERR_NOTENABLED;
2154 /**************************************************************************
2155 * widMessage (WINEARTS.6)
2157 DWORD WINAPI ARTS_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2158 DWORD dwParam1, DWORD dwParam2)
2160 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2161 return MMSYSERR_NOTENABLED;
2164 #endif /* HAVE_ARTS */