Add an interface name to wineoss, winealsa, winearts and winejack.
[wine/wine64.git] / dlls / winmm / winearts / audio.c
blob83711362edf17e92a4d1d693d122988c7d78947f
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 * does something need to be done in for WaveIn DirectSound?
38 /*#define EMULATE_SB16*/
40 #include "config.h"
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <string.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <fcntl.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "wingdi.h"
53 #include "winerror.h"
54 #include "wine/winuser16.h"
55 #include "mmddk.h"
56 #include "dsound.h"
57 #include "dsdriver.h"
58 #include "arts.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(wave);
63 #ifdef HAVE_ARTS
65 #include <artsc.h>
67 /* The following four #defines allow you to fine-tune the packet
68 * settings in arts for better low-latency support. You must also
69 * adjust the latency in the KDE arts control panel. I recommend 4
70 * fragments, 1024 bytes.
72 * The following is from the ARTS documentation and explains what CCCC
73 * and SSSS mean:
75 * @li ARTS_P_PACKET_SETTINGS (rw) This is a way to configure packet
76 * size & packet count at the same time. The format is 0xCCCCSSSS,
77 * where 2^SSSS is the packet size, and CCCC is the packet count. Note
78 * that when writing this, you don't necessarily get the settings you
79 * requested.
81 #define WAVEOUT_PACKET_CCCC 0x000C
82 #define WAVEOUT_PACKET_SSSS 0x0008
83 #define WAVEIN_PACKET_CCCC 0x000C
84 #define WAVEIN_PACKET_SSSS 0x0008
86 #define BUFFER_REFILL_THRESHOLD 4
88 #define WAVEOUT_PACKET_SETTINGS ((WAVEOUT_PACKET_CCCC << 16) | (WAVEOUT_PACKET_SSSS))
89 #define WAVEIN_PACKET_SETTINGS ((WAVEIN_PACKET_CCCC << 16) | (WAVEIN_PACKET_SSSS))
91 #define MAX_WAVEOUTDRV (10)
92 #define MAX_WAVEINDRV (10)
94 /* state diagram for waveOut writing:
96 * +---------+-------------+---------------+---------------------------------+
97 * | state | function | event | new state |
98 * +---------+-------------+---------------+---------------------------------+
99 * | | open() | | STOPPED |
100 * | PAUSED | write() | | PAUSED |
101 * | STOPPED | write() | <thrd create> | PLAYING |
102 * | PLAYING | write() | HEADER | PLAYING |
103 * | (other) | write() | <error> | |
104 * | (any) | pause() | PAUSING | PAUSED |
105 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
106 * | (any) | reset() | RESETTING | STOPPED |
107 * | (any) | close() | CLOSING | CLOSED |
108 * +---------+-------------+---------------+---------------------------------+
111 /* states of the playing device */
112 #define WINE_WS_PLAYING 0
113 #define WINE_WS_PAUSED 1
114 #define WINE_WS_STOPPED 2
115 #define WINE_WS_CLOSED 3
117 /* events to be send to device */
118 enum win_wm_message {
119 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
120 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
123 typedef struct {
124 enum win_wm_message msg; /* message identifier */
125 DWORD param; /* parameter for this message */
126 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
127 } RING_MSG;
129 /* implement an in-process message ring for better performance
130 * (compared to passing thru the server)
131 * this ring will be used by the input (resp output) record (resp playback) routine
133 #define ARTS_RING_BUFFER_INCREMENT 64
134 typedef struct {
135 RING_MSG * messages;
136 int ring_buffer_size;
137 int msg_tosave;
138 int msg_toget;
139 HANDLE msg_event;
140 CRITICAL_SECTION msg_crst;
141 } ARTS_MSG_RING;
143 typedef struct {
144 volatile int state; /* one of the WINE_WS_ manifest constants */
145 WAVEOPENDESC waveDesc;
146 WORD wFlags;
147 PCMWAVEFORMAT format;
148 WAVEOUTCAPSA caps;
149 char interface_name[32];
151 DWORD dwSleepTime; /* Num of milliseconds to sleep between filling the dsp buffers */
153 /* arts information */
154 arts_stream_t play_stream; /* the stream structure we get from arts when opening a stream for playing */
155 DWORD dwBufferSize; /* size of whole buffer in bytes */
156 int packetSettings;
158 char* sound_buffer;
159 long buffer_size;
161 DWORD volume_left; /* volume control information */
162 DWORD volume_right;
164 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
165 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
166 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
168 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
169 DWORD dwLoops; /* private copy of loop counter */
171 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
172 DWORD dwWrittenTotal; /* number of bytes written to the audio device since opening */
174 /* synchronization stuff */
175 HANDLE hStartUpEvent;
176 HANDLE hThread;
177 DWORD dwThreadID;
178 ARTS_MSG_RING msgRing;
179 } WINE_WAVEOUT;
181 typedef struct {
182 volatile int state; /* one of the WINE_WS_ manifest constants */
183 WAVEOPENDESC waveDesc;
184 WORD wFlags;
185 PCMWAVEFORMAT format;
186 WAVEINCAPSA caps;
187 char interface_name[32];
189 /* arts information */
190 arts_stream_t record_stream; /* the stream structure we get from arts when opening a stream for recording */
191 int packetSettings;
193 LPWAVEHDR lpQueuePtr;
194 DWORD dwRecordedTotal;
196 /* synchronization stuff */
197 HANDLE hStartUpEvent;
198 HANDLE hThread;
199 DWORD dwThreadID;
200 ARTS_MSG_RING msgRing;
201 } WINE_WAVEIN;
203 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
204 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
206 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
207 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
208 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
210 /* These strings used only for tracing */
211 static const char *wodPlayerCmdString[] = {
212 "WINE_WM_PAUSING",
213 "WINE_WM_RESTARTING",
214 "WINE_WM_RESETTING",
215 "WINE_WM_HEADER",
216 "WINE_WM_UPDATE",
217 "WINE_WM_BREAKLOOP",
218 "WINE_WM_CLOSING",
219 "WINE_WM_STARTING",
220 "WINE_WM_STOPPING",
223 /*======================================================================*
224 * Low level WAVE implementation *
225 *======================================================================*/
227 /* Volume functions derived from Alsaplayer source */
228 /* length is the number of 16 bit samples */
229 void volume_effect16(void *bufin, void* bufout, int length, int left,
230 int right, int nChannels)
232 short *d_out = (short *)bufout;
233 short *d_in = (short *)bufin;
234 int i, v;
237 TRACE("length == %d, nChannels == %d\n", length, nChannels);
240 if (right == -1) right = left;
242 for(i = 0; i < length; i+=(nChannels))
244 v = (int) ((*(d_in++) * left) / 100);
245 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
246 if(nChannels == 2)
248 v = (int) ((*(d_in++) * right) / 100);
249 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
254 /* length is the number of 8 bit samples */
255 void volume_effect8(void *bufin, void* bufout, int length, int left,
256 int right, int nChannels)
258 BYTE *d_out = (BYTE *)bufout;
259 BYTE *d_in = (BYTE *)bufin;
260 int i, v;
263 TRACE("length == %d, nChannels == %d\n", length, nChannels);
266 if (right == -1) right = left;
268 for(i = 0; i < length; i+=(nChannels))
270 v = (BYTE) ((*(d_in++) * left) / 100);
271 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
272 if(nChannels == 2)
274 v = (BYTE) ((*(d_in++) * right) / 100);
275 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
280 /******************************************************************
281 * ARTS_CloseWaveOutDevice
284 void ARTS_CloseWaveOutDevice(WINE_WAVEOUT* wwo)
286 arts_close_stream(wwo->play_stream); /* close the arts stream */
287 wwo->play_stream = (arts_stream_t*)-1;
289 /* free up the buffer we use for volume and reset the size */
290 if(wwo->sound_buffer)
292 HeapFree(GetProcessHeap(), 0, wwo->sound_buffer);
293 wwo->sound_buffer = NULL;
296 wwo->buffer_size = 0;
299 /******************************************************************
300 * ARTS_CloseWaveInDevice
303 void ARTS_CloseWaveInDevice(WINE_WAVEIN* wwi)
305 arts_close_stream(wwi->record_stream); /* close the arts stream */
306 wwi->record_stream = (arts_stream_t*)-1;
309 /******************************************************************
310 * ARTS_Init
312 static int ARTS_Init(void)
314 return arts_init(); /* initialize arts and return errorcode */
317 /******************************************************************
318 * ARTS_WaveClose
320 LONG ARTS_WaveClose(void)
322 int iDevice;
324 /* close all open devices */
325 for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
327 if(WOutDev[iDevice].play_stream != (arts_stream_t*)-1)
329 ARTS_CloseWaveOutDevice(&WOutDev[iDevice]);
333 for(iDevice = 0; iDevice < MAX_WAVEINDRV; iDevice++)
335 if(WInDev[iDevice].record_stream != (arts_stream_t*)-1)
337 ARTS_CloseWaveInDevice(&WInDev[iDevice]);
341 arts_free(); /* free up arts */
342 return 1;
345 /******************************************************************
346 * ARTS_WaveInit
348 * Initialize internal structures from ARTS server info
350 LONG ARTS_WaveInit(void)
352 int i;
353 int errorcode;
355 TRACE("called\n");
357 if ((errorcode = ARTS_Init()) < 0)
359 ERR("arts_init() failed (%d)\n", errorcode);
360 return -1;
363 /* initialize all device handles to -1 */
364 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
366 WOutDev[i].play_stream = (arts_stream_t*)-1;
367 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
368 caps values */
369 /* FIXME: some programs compare this string against the content of the registry
370 * for MM drivers. The names have to match in order for the program to work
371 * (e.g. MS win9x mplayer.exe)
373 #ifdef EMULATE_SB16
374 WOutDev[i].caps.wMid = 0x0002;
375 WOutDev[i].caps.wPid = 0x0104;
376 strcpy(WOutDev[i].caps.szPname, "SB16 Wave Out");
377 #else
378 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
379 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
380 /* strcpy(WOutDev[i].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
381 strcpy(WOutDev[i].caps.szPname, "CS4236/37/38");
382 #endif
383 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "winearts: %d", i);
385 WOutDev[i].caps.vDriverVersion = 0x0100;
386 WOutDev[i].caps.dwFormats = 0x00000000;
387 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
389 WOutDev[i].caps.wChannels = 2;
390 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
392 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
393 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
394 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
395 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
396 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
397 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
398 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
399 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
400 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
401 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
402 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
403 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
406 for (i = 0; i < MAX_WAVEINDRV; ++i)
408 WInDev[i].record_stream = (arts_stream_t*)-1;
409 memset(&WInDev[i].caps, 0, sizeof(WInDev[i].caps)); /* zero out
410 caps values */
411 /* FIXME: some programs compare this string against the content of the registry
412 * for MM drivers. The names have to match in order for the program to work
413 * (e.g. MS win9x mplayer.exe)
415 #ifdef EMULATE_SB16
416 WInDev[i].caps.wMid = 0x0002;
417 WInDev[i].caps.wPid = 0x0104;
418 strcpy(WInDev[i].caps.szPname, "SB16 Wave In");
419 #else
420 WInDev[i].caps.wMid = 0x00FF;
421 WInDev[i].caps.wPid = 0x0001;
422 strcpy(WInDev[i].caps.szPname,"CS4236/37/38");
423 #endif
424 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "winearts: %d", i);
426 WInDev[i].caps.vDriverVersion = 0x0100;
427 WInDev[i].caps.dwFormats = 0x00000000;
429 WInDev[i].caps.wChannels = 2;
431 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
432 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
433 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
434 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
435 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
436 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
437 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
438 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
439 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
440 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
441 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
442 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
444 WInDev[i].caps.wReserved1 = 0;
446 return 0;
449 /******************************************************************
450 * ARTS_InitRingMessage
452 * Initialize the ring of messages for passing between driver's caller and playback/record
453 * thread
455 static int ARTS_InitRingMessage(ARTS_MSG_RING* mr)
457 mr->msg_toget = 0;
458 mr->msg_tosave = 0;
459 mr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
460 mr->ring_buffer_size = ARTS_RING_BUFFER_INCREMENT;
461 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
462 InitializeCriticalSection(&mr->msg_crst);
463 return 0;
466 /******************************************************************
467 * ARTS_DestroyRingMessage
470 static int ARTS_DestroyRingMessage(ARTS_MSG_RING* mr)
472 CloseHandle(mr->msg_event);
473 HeapFree(GetProcessHeap(),0,mr->messages);
474 mr->messages=NULL;
475 DeleteCriticalSection(&mr->msg_crst);
476 return 0;
479 /******************************************************************
480 * ARTS_AddRingMessage
482 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
484 static int ARTS_AddRingMessage(ARTS_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
486 HANDLE hEvent = INVALID_HANDLE_VALUE;
488 EnterCriticalSection(&mr->msg_crst);
489 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
491 int old_ring_buffer_size = mr->ring_buffer_size;
492 mr->ring_buffer_size += ARTS_RING_BUFFER_INCREMENT;
493 TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
494 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
495 /* Now we need to rearrange the ring buffer so that the new
496 buffers just allocated are in between mr->msg_tosave and
497 mr->msg_toget.
499 if (mr->msg_tosave < mr->msg_toget)
501 memmove(&(mr->messages[mr->msg_toget + ARTS_RING_BUFFER_INCREMENT]),
502 &(mr->messages[mr->msg_toget]),
503 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
505 mr->msg_toget += ARTS_RING_BUFFER_INCREMENT;
508 if (wait)
510 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
511 if (hEvent == INVALID_HANDLE_VALUE)
513 ERR("can't create event !?\n");
514 LeaveCriticalSection(&mr->msg_crst);
515 return 0;
517 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
518 FIXME("two fast messages in the queue!!!!\n");
520 /* fast messages have to be added at the start of the queue */
521 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
523 mr->messages[mr->msg_toget].msg = msg;
524 mr->messages[mr->msg_toget].param = param;
525 mr->messages[mr->msg_toget].hEvent = hEvent;
527 else
529 mr->messages[mr->msg_tosave].msg = msg;
530 mr->messages[mr->msg_tosave].param = param;
531 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
532 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
535 LeaveCriticalSection(&mr->msg_crst);
537 SetEvent(mr->msg_event); /* signal a new message */
539 if (wait)
541 /* wait for playback/record thread to have processed the message */
542 WaitForSingleObject(hEvent, INFINITE);
543 CloseHandle(hEvent);
546 return 1;
549 /******************************************************************
550 * ARTS_RetrieveRingMessage
552 * Get a message from the ring. Should be called by the playback/record thread.
554 static int ARTS_RetrieveRingMessage(ARTS_MSG_RING* mr,
555 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
557 EnterCriticalSection(&mr->msg_crst);
559 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
561 LeaveCriticalSection(&mr->msg_crst);
562 return 0;
565 *msg = mr->messages[mr->msg_toget].msg;
566 mr->messages[mr->msg_toget].msg = 0;
567 *param = mr->messages[mr->msg_toget].param;
568 *hEvent = mr->messages[mr->msg_toget].hEvent;
569 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
570 LeaveCriticalSection(&mr->msg_crst);
571 return 1;
574 /*======================================================================*
575 * Low level WAVE OUT implementation *
576 *======================================================================*/
578 /**************************************************************************
579 * wodNotifyClient [internal]
581 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
583 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
585 switch (wMsg) {
586 case WOM_OPEN:
587 case WOM_CLOSE:
588 case WOM_DONE:
589 if (wwo->wFlags != DCB_NULL &&
590 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
591 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
592 WARN("can't notify client !\n");
593 return MMSYSERR_ERROR;
595 break;
596 default:
597 FIXME("Unknown callback message %u\n", wMsg);
598 return MMSYSERR_INVALPARAM;
600 return MMSYSERR_NOERROR;
603 /**************************************************************************
604 * wodUpdatePlayedTotal [internal]
607 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
609 /* total played is the bytes written less the bytes to write ;-) */
610 wwo->dwPlayedTotal = wwo->dwWrittenTotal -
611 (wwo->dwBufferSize -
612 arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE));
614 return TRUE;
617 /**************************************************************************
618 * wodPlayer_BeginWaveHdr [internal]
620 * Makes the specified lpWaveHdr the currently playing wave header.
621 * If the specified wave header is a begin loop and we're not already in
622 * a loop, setup the loop.
624 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
626 wwo->lpPlayPtr = lpWaveHdr;
628 if (!lpWaveHdr) return;
630 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
631 if (wwo->lpLoopPtr) {
632 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
633 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
634 } else {
635 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
636 wwo->lpLoopPtr = lpWaveHdr;
637 /* Windows does not touch WAVEHDR.dwLoops,
638 * so we need to make an internal copy */
639 wwo->dwLoops = lpWaveHdr->dwLoops;
642 wwo->dwPartialOffset = 0;
645 /**************************************************************************
646 * wodPlayer_PlayPtrNext [internal]
648 * Advance the play pointer to the next waveheader, looping if required.
650 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
652 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
654 wwo->dwPartialOffset = 0;
655 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
656 /* We're at the end of a loop, loop if required */
657 if (--wwo->dwLoops > 0) {
658 wwo->lpPlayPtr = wwo->lpLoopPtr;
659 } else {
660 /* Handle overlapping loops correctly */
661 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
662 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
663 /* shall we consider the END flag for the closing loop or for
664 * the opening one or for both ???
665 * code assumes for closing loop only
667 } else {
668 lpWaveHdr = lpWaveHdr->lpNext;
670 wwo->lpLoopPtr = NULL;
671 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
673 } else {
674 /* We're not in a loop. Advance to the next wave header */
675 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
678 return lpWaveHdr;
681 /**************************************************************************
682 * wodPlayer_NotifyWait [internal]
683 * Returns the number of milliseconds to wait before attempting to notify
684 * completion of the specified wavehdr.
685 * This is based on the number of bytes remaining to be written in the
686 * wave.
688 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
690 DWORD dwMillis;
692 if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
694 dwMillis = 1;
696 else
698 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
699 if(!dwMillis) dwMillis = 1;
702 TRACE("dwMillis = %ld\n", dwMillis);
704 return dwMillis;
708 /**************************************************************************
709 * wodPlayer_WriteMaxFrags [internal]
710 * Writes the maximum number of bytes possible to the DSP and returns
711 * the number of bytes written.
713 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
715 /* Only attempt to write to free bytes */
716 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
717 int toWrite = min(dwLength, *bytes);
718 int written;
720 TRACE("Writing wavehdr %p.%lu[%lu]\n",
721 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);
723 /* see if our buffer isn't large enough for the data we are writing */
724 if(wwo->buffer_size < toWrite)
726 if(wwo->sound_buffer)
728 wwo->sound_buffer = HeapReAlloc(GetProcessHeap(), 0, wwo->sound_buffer, toWrite);
729 wwo->buffer_size = toWrite;
733 /* if we don't have a buffer then get one */
734 if(!wwo->sound_buffer)
736 /* allocate some memory for the buffer */
737 wwo->sound_buffer = HeapAlloc(GetProcessHeap(), 0, toWrite);
738 wwo->buffer_size = toWrite;
741 /* if we don't have a buffer then error out */
742 if(!wwo->sound_buffer)
744 ERR("error allocating sound_buffer memory\n");
745 return 0;
748 TRACE("toWrite == %d\n", toWrite);
750 /* apply volume to the bits */
751 /* for single channel audio streams we only use the LEFT volume */
752 if(wwo->format.wBitsPerSample == 16)
754 /* apply volume to the buffer we are about to send */
755 /* divide toWrite(bytes) by 2 as volume processes by 16 bits */
756 volume_effect16(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
757 wwo->sound_buffer, toWrite>>1, wwo->volume_left,
758 wwo->volume_right, wwo->format.wf.nChannels);
759 } else if(wwo->format.wBitsPerSample == 8)
761 /* apply volume to the buffer we are about to send */
762 volume_effect8(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
763 wwo->sound_buffer, toWrite, wwo->volume_left,
764 wwo->volume_right, wwo->format.wf.nChannels);
765 } else
767 FIXME("unsupported wwo->format.wBitsPerSample of %d\n",
768 wwo->format.wBitsPerSample);
771 /* send the audio data to arts for playing */
772 written = arts_write(wwo->play_stream, wwo->sound_buffer, toWrite);
774 TRACE("written = %d\n", written);
776 if (written <= 0)
778 *bytes = 0; /* apparently arts is actually full */
779 return written; /* if we wrote nothing just return */
782 if (written >= dwLength)
783 wodPlayer_PlayPtrNext(wwo); /* If we wrote all current wavehdr, skip to the next one */
784 else
785 wwo->dwPartialOffset += written; /* Remove the amount written */
787 if (written < toWrite)
788 *bytes = 0;
789 else
790 *bytes -= written;
792 wwo->dwWrittenTotal += written; /* update stats on this wave device */
794 return written; /* return the number of bytes written */
798 /**************************************************************************
799 * wodPlayer_NotifyCompletions [internal]
801 * Notifies and remove from queue all wavehdrs which have been played to
802 * the speaker (ie. they have cleared the audio device). If force is true,
803 * we notify all wavehdrs and remove them all from the queue even if they
804 * are unplayed or part of a loop.
806 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
808 LPWAVEHDR lpWaveHdr;
810 if (wwo->lpQueuePtr) {
811 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%ld), force=(%d)\n",
812 wwo->lpQueuePtr,
813 wwo->lpPlayPtr,
814 wwo->lpLoopPtr,
815 wwo->lpQueuePtr->reserved,
816 wwo->dwWrittenTotal,
817 force);
818 } else {
819 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), dwWrittenTotal=(%ld), force=(%d)\n",
820 wwo->lpQueuePtr,
821 wwo->lpPlayPtr,
822 wwo->lpLoopPtr,
823 wwo->dwWrittenTotal,
824 force);
827 /* Start from lpQueuePtr and keep notifying until:
828 * - we hit an unwritten wavehdr
829 * - we hit the beginning of a running loop
830 * - we hit a wavehdr which hasn't finished playing
832 while ((lpWaveHdr = wwo->lpQueuePtr) &&
833 (force ||
834 (lpWaveHdr != wwo->lpPlayPtr &&
835 lpWaveHdr != wwo->lpLoopPtr &&
836 lpWaveHdr->reserved <= wwo->dwWrittenTotal))) {
838 wwo->lpQueuePtr = lpWaveHdr->lpNext;
840 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
841 lpWaveHdr->dwFlags |= WHDR_DONE;
843 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
845 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
846 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
849 /**************************************************************************
850 * wodPlayer_Reset [internal]
852 * wodPlayer helper. Resets current output stream.
854 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
856 wodUpdatePlayedTotal(wwo);
858 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
860 /* we aren't able to flush any data that has already been written */
861 /* to arts, otherwise we would do the flushing here */
863 if (reset) {
864 enum win_wm_message msg;
865 DWORD param;
866 HANDLE ev;
868 /* remove any buffer */
869 wodPlayer_NotifyCompletions(wwo, TRUE);
871 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
872 wwo->state = WINE_WS_STOPPED;
873 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
875 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
877 /* remove any existing message in the ring */
878 EnterCriticalSection(&wwo->msgRing.msg_crst);
880 /* return all pending headers in queue */
881 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
883 TRACE("flushing msg\n");
884 if (msg != WINE_WM_HEADER)
886 FIXME("shouldn't have headers left\n");
887 SetEvent(ev);
888 continue;
890 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
891 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
893 wodNotifyClient(wwo, WOM_DONE, param, 0);
895 ResetEvent(wwo->msgRing.msg_event);
896 LeaveCriticalSection(&wwo->msgRing.msg_crst);
897 } else {
898 if (wwo->lpLoopPtr) {
899 /* complicated case, not handled yet (could imply modifying the loop counter */
900 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
901 wwo->lpPlayPtr = wwo->lpLoopPtr;
902 wwo->dwPartialOffset = 0;
903 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
904 } else {
905 /* the data already written is going to be played, so take */
906 /* this fact into account here */
907 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
909 wwo->state = WINE_WS_PAUSED;
913 /**************************************************************************
914 * wodPlayer_ProcessMessages [internal]
916 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
918 LPWAVEHDR lpWaveHdr;
919 enum win_wm_message msg;
920 DWORD param;
921 HANDLE ev;
923 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
924 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
925 switch (msg) {
926 case WINE_WM_PAUSING:
927 wodPlayer_Reset(wwo, FALSE);
928 SetEvent(ev);
929 break;
930 case WINE_WM_RESTARTING:
931 wwo->state = WINE_WS_PLAYING;
932 SetEvent(ev);
933 break;
934 case WINE_WM_HEADER:
935 lpWaveHdr = (LPWAVEHDR)param;
937 /* insert buffer at the end of queue */
939 LPWAVEHDR* wh;
940 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
941 *wh = lpWaveHdr;
943 if (!wwo->lpPlayPtr)
944 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
945 if (wwo->state == WINE_WS_STOPPED)
946 wwo->state = WINE_WS_PLAYING;
947 break;
948 case WINE_WM_RESETTING:
949 wodPlayer_Reset(wwo, TRUE);
950 SetEvent(ev);
951 break;
952 case WINE_WM_UPDATE:
953 wodUpdatePlayedTotal(wwo);
954 SetEvent(ev);
955 break;
956 case WINE_WM_BREAKLOOP:
957 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
958 /* ensure exit at end of current loop */
959 wwo->dwLoops = 1;
961 SetEvent(ev);
962 break;
963 case WINE_WM_CLOSING:
964 /* sanity check: this should not happen since the device must have been reset before */
965 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
966 wwo->hThread = 0;
967 wwo->state = WINE_WS_CLOSED;
968 SetEvent(ev);
969 ExitThread(0);
970 /* shouldn't go here */
971 default:
972 FIXME("unknown message %d\n", msg);
973 break;
978 /**************************************************************************
979 * wodPlayer_FeedDSP [internal]
980 * Feed as much sound data as we can into the DSP and return the number of
981 * milliseconds before it will be necessary to feed the DSP again.
983 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
985 DWORD availInQ;
987 wodUpdatePlayedTotal(wwo);
988 availInQ = arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE);
989 TRACE("availInQ = %ld\n", availInQ);
991 /* input queue empty */
992 if (!wwo->lpPlayPtr) {
993 TRACE("Run out of wavehdr:s... flushing\n");
994 return INFINITE;
997 /* no more room... no need to try to feed */
998 if(!availInQ)
1000 TRACE("no more room, no need to try to feed\n");
1001 return wwo->dwSleepTime;
1004 /* Feed from partial wavehdr */
1005 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0)
1007 TRACE("feeding from partial wavehdr\n");
1008 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1011 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1012 if (!wwo->dwPartialOffset)
1014 while(wwo->lpPlayPtr && availInQ)
1016 TRACE("feeding waveheaders until we run out of space\n");
1017 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1018 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1019 TRACE("reserved=(%ld) dwWrittenTotal=(%ld) dwBufferLength=(%ld)\n",
1020 wwo->lpPlayPtr->reserved,
1021 wwo->dwWrittenTotal,
1022 wwo->lpPlayPtr->dwBufferLength
1024 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1028 if (!wwo->lpPlayPtr) {
1029 TRACE("Ran out of wavehdrs\n");
1030 return INFINITE;
1033 return wwo->dwSleepTime;
1037 /**************************************************************************
1038 * wodPlayer [internal]
1040 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1042 WORD uDevID = (DWORD)pmt;
1043 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1044 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1045 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1046 DWORD dwSleepTime;
1048 wwo->state = WINE_WS_STOPPED;
1049 SetEvent(wwo->hStartUpEvent);
1051 for (;;) {
1052 /** Wait for the shortest time before an action is required. If there
1053 * are no pending actions, wait forever for a command.
1055 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1056 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1057 WaitForSingleObject(wwo->msgRing.msg_event, dwSleepTime);
1058 wodPlayer_ProcessMessages(wwo);
1059 if (wwo->state == WINE_WS_PLAYING) {
1060 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1061 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1062 } else {
1063 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1068 /**************************************************************************
1069 * wodGetDevCaps [internal]
1071 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1073 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1075 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1077 if (wDevID >= MAX_WAVEOUTDRV) {
1078 TRACE("MAX_WAVOUTDRV reached !\n");
1079 return MMSYSERR_BADDEVICEID;
1082 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1083 return MMSYSERR_NOERROR;
1086 /**************************************************************************
1087 * wodOpen [internal]
1089 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1091 WINE_WAVEOUT* wwo;
1093 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1094 if (lpDesc == NULL) {
1095 WARN("Invalid Parameter !\n");
1096 return MMSYSERR_INVALPARAM;
1098 if (wDevID >= MAX_WAVEOUTDRV) {
1099 TRACE("MAX_WAVOUTDRV reached !\n");
1100 return MMSYSERR_BADDEVICEID;
1103 /* if this device is already open tell the app that it is allocated */
1104 if(WOutDev[wDevID].play_stream != (arts_stream_t*)-1)
1106 TRACE("device already allocated\n");
1107 return MMSYSERR_ALLOCATED;
1110 /* only PCM format is supported so far... */
1111 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1112 lpDesc->lpFormat->nChannels == 0 ||
1113 lpDesc->lpFormat->nSamplesPerSec == 0 ||
1114 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1115 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1116 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1117 lpDesc->lpFormat->nSamplesPerSec);
1118 return WAVERR_BADFORMAT;
1121 if (dwFlags & WAVE_FORMAT_QUERY) {
1122 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1123 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1124 lpDesc->lpFormat->nSamplesPerSec);
1125 return MMSYSERR_NOERROR;
1128 wwo = &WOutDev[wDevID];
1130 /* direct sound not supported, ignore the flag */
1131 dwFlags &= ~WAVE_DIRECTSOUND;
1133 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1135 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1136 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1138 if (wwo->format.wBitsPerSample == 0) {
1139 WARN("Resetting zeroed wBitsPerSample\n");
1140 wwo->format.wBitsPerSample = 8 *
1141 (wwo->format.wf.nAvgBytesPerSec /
1142 wwo->format.wf.nSamplesPerSec) /
1143 wwo->format.wf.nChannels;
1146 wwo->play_stream = arts_play_stream(wwo->format.wf.nSamplesPerSec,
1147 wwo->format.wBitsPerSample, wwo->format.wf.nChannels, "winearts");
1149 /* clear these so we don't have any confusion ;-) */
1150 wwo->sound_buffer = 0;
1151 wwo->buffer_size = 0;
1153 arts_stream_set(wwo->play_stream, ARTS_P_BLOCKING, 0); /* disable blocking on this stream */
1155 if(!wwo->play_stream) return MMSYSERR_ALLOCATED;
1157 /* Try to set the packet settings from constant and store the value that it
1158 was actually set to for future use */
1159 wwo->packetSettings = arts_stream_set(wwo->play_stream, ARTS_P_PACKET_SETTINGS, WAVEOUT_PACKET_SETTINGS);
1160 TRACE("Tried to set ARTS_P_PACKET_SETTINGS to (%x), actually set to (%x)\n", WAVEOUT_PACKET_SETTINGS, wwo->packetSettings);
1162 wwo->dwBufferSize = arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SIZE);
1163 TRACE("Buffer size is now (%ld)\n",wwo->dwBufferSize);
1165 wwo->dwPlayedTotal = 0;
1166 wwo->dwWrittenTotal = 0;
1168 wwo->dwSleepTime = ((1 << (wwo->packetSettings & 0xFFFF)) * 1000 * BUFFER_REFILL_THRESHOLD) / wwo->format.wf.nAvgBytesPerSec;
1170 /* Initialize volume to full level */
1171 wwo->volume_left = 100;
1172 wwo->volume_right = 100;
1174 ARTS_InitRingMessage(&wwo->msgRing);
1176 /* create player thread */
1177 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1178 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1179 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1180 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1181 CloseHandle(wwo->hStartUpEvent);
1182 } else {
1183 wwo->hThread = INVALID_HANDLE_VALUE;
1184 wwo->dwThreadID = 0;
1186 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1188 TRACE("stream=0x%lx, dwBufferSize=%ld\n",
1189 (long)wwo->play_stream, wwo->dwBufferSize);
1191 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1192 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1193 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1194 wwo->format.wf.nBlockAlign);
1196 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1199 /**************************************************************************
1200 * wodClose [internal]
1202 static DWORD wodClose(WORD wDevID)
1204 DWORD ret = MMSYSERR_NOERROR;
1205 WINE_WAVEOUT* wwo;
1207 TRACE("(%u);\n", wDevID);
1209 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1210 (arts_stream_t*)-1)
1212 WARN("bad device ID !\n");
1213 return MMSYSERR_BADDEVICEID;
1216 wwo = &WOutDev[wDevID];
1217 if (wwo->lpQueuePtr) {
1218 WARN("buffers still playing !\n");
1219 ret = WAVERR_STILLPLAYING;
1220 } else {
1221 TRACE("imhere[3-close]\n");
1222 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1223 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1226 ARTS_DestroyRingMessage(&wwo->msgRing);
1228 ARTS_CloseWaveOutDevice(wwo); /* close the stream and clean things up */
1230 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1232 return ret;
1235 /**************************************************************************
1236 * wodWrite [internal]
1239 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1241 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1243 /* first, do the sanity checks... */
1244 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1245 (arts_stream_t*)-1)
1247 WARN("bad dev ID !\n");
1248 return MMSYSERR_BADDEVICEID;
1251 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1253 TRACE("unprepared\n");
1254 return WAVERR_UNPREPARED;
1257 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1259 TRACE("still playing\n");
1260 return WAVERR_STILLPLAYING;
1263 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1264 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1265 lpWaveHdr->lpNext = 0;
1267 TRACE("adding ring message\n");
1268 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1270 return MMSYSERR_NOERROR;
1273 /**************************************************************************
1274 * wodPrepare [internal]
1276 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1278 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1280 if (wDevID >= MAX_WAVEOUTDRV) {
1281 WARN("bad device ID !\n");
1282 return MMSYSERR_BADDEVICEID;
1285 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1286 return WAVERR_STILLPLAYING;
1288 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1289 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1290 return MMSYSERR_NOERROR;
1293 /**************************************************************************
1294 * wodUnprepare [internal]
1296 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1298 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1300 if (wDevID >= MAX_WAVEOUTDRV) {
1301 WARN("bad device ID !\n");
1302 return MMSYSERR_BADDEVICEID;
1305 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1306 return WAVERR_STILLPLAYING;
1308 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1309 lpWaveHdr->dwFlags |= WHDR_DONE;
1311 return MMSYSERR_NOERROR;
1314 /**************************************************************************
1315 * wodPause [internal]
1317 static DWORD wodPause(WORD wDevID)
1319 TRACE("(%u);!\n", wDevID);
1321 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1322 (arts_stream_t*)-1)
1324 WARN("bad device ID !\n");
1325 return MMSYSERR_BADDEVICEID;
1328 TRACE("imhere[3-PAUSING]\n");
1329 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1331 return MMSYSERR_NOERROR;
1334 /**************************************************************************
1335 * wodRestart [internal]
1337 static DWORD wodRestart(WORD wDevID)
1339 TRACE("(%u);\n", wDevID);
1341 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1342 (arts_stream_t*)-1)
1344 WARN("bad device ID !\n");
1345 return MMSYSERR_BADDEVICEID;
1348 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1349 TRACE("imhere[3-RESTARTING]\n");
1350 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1353 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1354 /* FIXME: Myst crashes with this ... hmm -MM
1355 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1358 return MMSYSERR_NOERROR;
1361 /**************************************************************************
1362 * wodReset [internal]
1364 static DWORD wodReset(WORD wDevID)
1366 TRACE("(%u);\n", wDevID);
1368 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1369 (arts_stream_t*)-1)
1371 WARN("bad device ID !\n");
1372 return MMSYSERR_BADDEVICEID;
1375 TRACE("imhere[3-RESET]\n");
1376 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1378 return MMSYSERR_NOERROR;
1381 /**************************************************************************
1382 * wodGetPosition [internal]
1384 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1386 int time;
1387 DWORD val;
1388 WINE_WAVEOUT* wwo;
1390 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1392 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1393 (arts_stream_t*)-1)
1395 WARN("bad device ID !\n");
1396 return MMSYSERR_BADDEVICEID;
1399 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1401 wwo = &WOutDev[wDevID];
1402 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1403 val = wwo->dwPlayedTotal;
1405 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1406 lpTime->wType, wwo->format.wBitsPerSample,
1407 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1408 wwo->format.wf.nAvgBytesPerSec);
1409 TRACE("dwPlayedTotal=%lu\n", val);
1411 switch (lpTime->wType) {
1412 case TIME_BYTES:
1413 lpTime->u.cb = val;
1414 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1415 break;
1416 case TIME_SAMPLES:
1417 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1418 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1419 break;
1420 case TIME_SMPTE:
1421 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1422 lpTime->u.smpte.hour = time / 108000;
1423 time -= lpTime->u.smpte.hour * 108000;
1424 lpTime->u.smpte.min = time / 1800;
1425 time -= lpTime->u.smpte.min * 1800;
1426 lpTime->u.smpte.sec = time / 30;
1427 time -= lpTime->u.smpte.sec * 30;
1428 lpTime->u.smpte.frame = time;
1429 lpTime->u.smpte.fps = 30;
1430 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1431 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1432 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1433 break;
1434 default:
1435 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1436 lpTime->wType = TIME_MS;
1437 case TIME_MS:
1438 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1439 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1440 break;
1442 return MMSYSERR_NOERROR;
1445 /**************************************************************************
1446 * wodBreakLoop [internal]
1448 static DWORD wodBreakLoop(WORD wDevID)
1450 TRACE("(%u);\n", wDevID);
1452 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1453 (arts_stream_t*)-1)
1455 WARN("bad device ID !\n");
1456 return MMSYSERR_BADDEVICEID;
1458 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1459 return MMSYSERR_NOERROR;
1462 /**************************************************************************
1463 * wodGetVolume [internal]
1465 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1467 DWORD left, right;
1469 left = WOutDev[wDevID].volume_left;
1470 right = WOutDev[wDevID].volume_right;
1472 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1474 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) <<
1475 16);
1477 return MMSYSERR_NOERROR;
1480 /**************************************************************************
1481 * wodSetVolume [internal]
1483 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1485 DWORD left, right;
1487 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1488 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1490 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1492 WOutDev[wDevID].volume_left = left;
1493 WOutDev[wDevID].volume_right = right;
1495 return MMSYSERR_NOERROR;
1498 /**************************************************************************
1499 * wodGetNumDevs [internal]
1501 static DWORD wodGetNumDevs(void)
1503 return MAX_WAVEOUTDRV;
1506 /**************************************************************************
1507 * wodDevInterfaceSize [internal]
1509 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1511 TRACE("(%u, %p)\n", wDevID, dwParam1);
1513 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1514 NULL, 0 ) * sizeof(WCHAR);
1515 return MMSYSERR_NOERROR;
1518 /**************************************************************************
1519 * wodDevInterface [internal]
1521 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1523 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1524 NULL, 0 ) * sizeof(WCHAR))
1526 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1527 dwParam1, dwParam2 / sizeof(WCHAR));
1528 return MMSYSERR_NOERROR;
1530 return MMSYSERR_INVALPARAM;
1533 /**************************************************************************
1534 * wodMessage (WINEARTS.@)
1536 DWORD WINAPI ARTS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1537 DWORD dwParam1, DWORD dwParam2)
1539 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1540 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1542 switch (wMsg) {
1543 case DRVM_INIT:
1544 case DRVM_EXIT:
1545 case DRVM_ENABLE:
1546 case DRVM_DISABLE:
1547 /* FIXME: Pretend this is supported */
1548 return 0;
1549 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1550 case WODM_CLOSE: return wodClose (wDevID);
1551 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1552 case WODM_PAUSE: return wodPause (wDevID);
1553 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1554 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1555 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1556 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1557 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1558 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1559 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1560 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1561 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1562 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1563 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1564 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1565 case WODM_RESTART: return wodRestart (wDevID);
1566 case WODM_RESET: return wodReset (wDevID);
1568 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1569 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1570 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1571 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1572 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1573 default:
1574 FIXME("unknown message %d!\n", wMsg);
1576 return MMSYSERR_NOTSUPPORTED;
1579 /*======================================================================*
1580 * Low level WAVE IN implementation *
1581 *======================================================================*/
1583 /**************************************************************************
1584 * widGetNumDevs [internal]
1586 static DWORD widGetNumDevs(void)
1588 TRACE("%d \n",MAX_WAVEINDRV);
1589 return MAX_WAVEINDRV;
1592 /**************************************************************************
1593 * widDevInterfaceSize [internal]
1595 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1597 TRACE("(%u, %p)\n", wDevID, dwParam1);
1600 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1601 NULL, 0 ) * sizeof(WCHAR);
1602 return MMSYSERR_NOERROR;
1605 /**************************************************************************
1606 * widDevInterface [internal]
1608 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1610 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1611 NULL, 0 ) * sizeof(WCHAR))
1613 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1614 dwParam1, dwParam2 / sizeof(WCHAR));
1615 return MMSYSERR_NOERROR;
1617 return MMSYSERR_INVALPARAM;
1620 /**************************************************************************
1621 * widNotifyClient [internal]
1623 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1625 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
1627 switch (wMsg) {
1628 case WIM_OPEN:
1629 case WIM_CLOSE:
1630 case WIM_DATA:
1631 if (wwi->wFlags != DCB_NULL &&
1632 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1633 (HDRVR)wwi->waveDesc.hWave, wMsg,
1634 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
1635 WARN("can't notify client !\n");
1636 return MMSYSERR_ERROR;
1638 break;
1639 default:
1640 FIXME("Unknown callback message %u\n", wMsg);
1641 return MMSYSERR_INVALPARAM;
1643 return MMSYSERR_NOERROR;
1646 /**************************************************************************
1647 * widGetDevCaps [internal]
1649 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
1651 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1653 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1655 if (wDevID >= MAX_WAVEINDRV) {
1656 TRACE("MAX_WAVINDRV reached !\n");
1657 return MMSYSERR_BADDEVICEID;
1660 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1661 return MMSYSERR_NOERROR;
1664 /**************************************************************************
1665 * widRecorder [internal]
1667 static DWORD CALLBACK widRecorder(LPVOID pmt)
1669 WORD uDevID = (DWORD)pmt;
1670 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1671 WAVEHDR* lpWaveHdr;
1672 DWORD dwSleepTime;
1673 DWORD bytesRead;
1674 int dwBufferSpace;
1675 enum win_wm_message msg;
1676 DWORD param;
1677 HANDLE ev;
1679 SetEvent(wwi->hStartUpEvent);
1681 /* make sleep time to be # of ms to record one packet */
1682 dwSleepTime = ((1 << (wwi->packetSettings & 0xFFFF)) * 1000) / wwi->format.wf.nAvgBytesPerSec;
1683 TRACE("sleeptime=%ld ms\n", dwSleepTime);
1685 for(;;) {
1686 /* Oddly enough, dwBufferSpace is sometimes negative....
1688 * NOTE: If you remove this call to arts_stream_get() and
1689 * remove the && (dwBufferSpace > 0) the code will still
1690 * function correctly. I don't know which way is
1691 * faster/better.
1693 dwBufferSpace = arts_stream_get(wwi->record_stream, ARTS_P_BUFFER_SPACE);
1694 TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d), dwBufferSpace=(%d)\n",wwi->lpQueuePtr,wwi->state,dwBufferSpace);
1696 /* read all data is arts input buffer. */
1697 if ((wwi->lpQueuePtr != NULL) && (wwi->state == WINE_WS_PLAYING) && (dwBufferSpace > 0))
1699 lpWaveHdr = wwi->lpQueuePtr;
1701 TRACE("read as much as we can\n");
1702 while(wwi->lpQueuePtr)
1704 TRACE("attempt to read %ld bytes\n",lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1705 bytesRead = arts_read(wwi->record_stream,
1706 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1707 lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1708 TRACE("bytesRead=%ld\n",bytesRead);
1709 if (bytesRead==0) break;
1711 lpWaveHdr->dwBytesRecorded += bytesRead;
1712 wwi->dwRecordedTotal += bytesRead;
1714 /* buffer full. notify client */
1715 if (lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength)
1717 /* must copy the value of next waveHdr, because we have no idea of what
1718 * will be done with the content of lpWaveHdr in callback
1720 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1722 TRACE("waveHdr full.\n");
1724 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1725 lpWaveHdr->dwFlags |= WHDR_DONE;
1727 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1728 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1733 /* wait for dwSleepTime or an event in thread's queue */
1734 WaitForSingleObject(wwi->msgRing.msg_event, dwSleepTime);
1736 while (ARTS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
1738 TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString[msg - WM_USER - 1], param);
1739 switch(msg) {
1740 case WINE_WM_PAUSING:
1741 wwi->state = WINE_WS_PAUSED;
1743 /* Put code here to "pause" arts recording
1746 SetEvent(ev);
1747 break;
1748 case WINE_WM_STARTING:
1749 wwi->state = WINE_WS_PLAYING;
1751 /* Put code here to "start" arts recording
1754 SetEvent(ev);
1755 break;
1756 case WINE_WM_HEADER:
1757 lpWaveHdr = (LPWAVEHDR)param;
1758 /* insert buffer at end of queue */
1760 LPWAVEHDR* wh;
1761 int num_headers = 0;
1762 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1764 num_headers++;
1767 *wh=lpWaveHdr;
1769 break;
1770 case WINE_WM_STOPPING:
1771 if (wwi->state != WINE_WS_STOPPED)
1774 /* Put code here to "stop" arts recording
1777 /* return current buffer to app */
1778 lpWaveHdr = wwi->lpQueuePtr;
1779 if (lpWaveHdr)
1781 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1782 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1783 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1784 lpWaveHdr->dwFlags |= WHDR_DONE;
1785 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1786 wwi->lpQueuePtr = lpNext;
1789 wwi->state = WINE_WS_STOPPED;
1790 SetEvent(ev);
1791 break;
1792 case WINE_WM_RESETTING:
1793 wwi->state = WINE_WS_STOPPED;
1794 wwi->dwRecordedTotal = 0;
1796 /* return all buffers to the app */
1797 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1798 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1799 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1800 lpWaveHdr->dwFlags |= WHDR_DONE;
1802 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1804 wwi->lpQueuePtr = NULL;
1805 SetEvent(ev);
1806 break;
1807 case WINE_WM_CLOSING:
1808 wwi->hThread = 0;
1809 wwi->state = WINE_WS_CLOSED;
1810 SetEvent(ev);
1811 ExitThread(0);
1812 /* shouldn't go here */
1813 default:
1814 FIXME("unknown message %d\n", msg);
1815 break;
1819 ExitThread(0);
1820 /* just for not generating compilation warnings... should never be executed */
1821 return 0;
1824 /**************************************************************************
1825 * widOpen [internal]
1827 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1829 WINE_WAVEIN* wwi;
1831 TRACE("(%u, %p %08lX);\n",wDevID, lpDesc, dwFlags);
1832 if (lpDesc == NULL) {
1833 WARN("Invalid Parametr (lpDesc == NULL)!\n");
1834 return MMSYSERR_INVALPARAM;
1837 if (wDevID >= MAX_WAVEINDRV) {
1838 TRACE ("MAX_WAVEINDRV reached !\n");
1839 return MMSYSERR_BADDEVICEID;
1842 /* if this device is already open tell the app that it is allocated */
1843 if(WInDev[wDevID].record_stream != (arts_stream_t*)-1)
1845 TRACE("device already allocated\n");
1846 return MMSYSERR_ALLOCATED;
1849 /* only PCM format is support so far... */
1850 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1851 lpDesc->lpFormat->nChannels == 0 ||
1852 lpDesc->lpFormat->nSamplesPerSec == 0 ||
1853 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1854 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1855 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1856 lpDesc->lpFormat->nSamplesPerSec);
1857 return WAVERR_BADFORMAT;
1860 if (dwFlags & WAVE_FORMAT_QUERY) {
1861 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1862 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1863 lpDesc->lpFormat->nSamplesPerSec);
1864 return MMSYSERR_NOERROR;
1867 wwi = &WInDev[wDevID];
1869 /* direct sound not supported, ignore the flag */
1870 dwFlags &= ~WAVE_DIRECTSOUND;
1872 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1874 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1875 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1877 if (wwi->format.wBitsPerSample == 0) {
1878 WARN("Resetting zerod wBitsPerSample\n");
1879 wwi->format.wBitsPerSample = 8 *
1880 (wwi->format.wf.nAvgBytesPerSec /
1881 wwi->format.wf.nSamplesPerSec) /
1882 wwi->format.wf.nChannels;
1885 wwi->record_stream = arts_record_stream(wwi->format.wf.nSamplesPerSec,
1886 wwi->format.wBitsPerSample,
1887 wwi->format.wf.nChannels,
1888 "winearts");
1889 TRACE("(wwi->record_stream=%p)\n",wwi->record_stream);
1890 wwi->state = WINE_WS_STOPPED;
1892 wwi->packetSettings = arts_stream_set(wwi->record_stream, ARTS_P_PACKET_SETTINGS, WAVEIN_PACKET_SETTINGS);
1893 TRACE("Tried to set ARTS_P_PACKET_SETTINGS to (%x), actually set to (%x)\n", WAVEIN_PACKET_SETTINGS, wwi->packetSettings);
1894 TRACE("Buffer size is now (%d)\n", arts_stream_get(wwi->record_stream, ARTS_P_BUFFER_SIZE));
1896 if (wwi->lpQueuePtr) {
1897 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1898 wwi->lpQueuePtr = NULL;
1900 arts_stream_set(wwi->record_stream, ARTS_P_BLOCKING, 0); /* disable blocking on this stream */
1902 if(!wwi->record_stream) return MMSYSERR_ALLOCATED;
1904 wwi->dwRecordedTotal = 0;
1905 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1907 ARTS_InitRingMessage(&wwi->msgRing);
1909 /* create recorder thread */
1910 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1911 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1912 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
1913 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
1914 CloseHandle(wwi->hStartUpEvent);
1915 } else {
1916 wwi->hThread = INVALID_HANDLE_VALUE;
1917 wwi->dwThreadID = 0;
1919 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
1921 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1922 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
1923 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
1924 wwi->format.wf.nBlockAlign);
1925 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
1928 /**************************************************************************
1929 * widClose [internal]
1931 static DWORD widClose(WORD wDevID)
1933 WINE_WAVEIN* wwi;
1935 TRACE("(%u);\n", wDevID);
1936 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1937 WARN("can't close !\n");
1938 return MMSYSERR_INVALHANDLE;
1941 wwi = &WInDev[wDevID];
1943 if (wwi->lpQueuePtr != NULL) {
1944 WARN("still buffers open !\n");
1945 return WAVERR_STILLPLAYING;
1948 ARTS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
1949 ARTS_CloseWaveInDevice(wwi);
1950 wwi->state = WINE_WS_CLOSED;
1951 ARTS_DestroyRingMessage(&wwi->msgRing);
1952 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
1955 /**************************************************************************
1956 * widAddBuffer [internal]
1958 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1960 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1962 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1963 WARN("can't do it !\n");
1964 return MMSYSERR_INVALHANDLE;
1966 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
1967 TRACE("never been prepared !\n");
1968 return WAVERR_UNPREPARED;
1970 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
1971 TRACE("header already in use !\n");
1972 return WAVERR_STILLPLAYING;
1975 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1976 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1977 lpWaveHdr->dwBytesRecorded = 0;
1978 lpWaveHdr->lpNext = NULL;
1980 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1981 return MMSYSERR_NOERROR;
1984 /**************************************************************************
1985 * widPrepare [internal]
1987 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1989 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1991 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
1993 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1994 return WAVERR_STILLPLAYING;
1996 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1997 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1998 lpWaveHdr->dwBytesRecorded = 0;
2000 return MMSYSERR_NOERROR;
2003 /**************************************************************************
2004 * widUnprepare [internal]
2006 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2008 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2009 if (wDevID >= MAX_WAVEINDRV) {
2010 WARN("bad device ID !\n");
2011 return MMSYSERR_INVALHANDLE;
2014 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2015 TRACE("Still playing...\n");
2016 return WAVERR_STILLPLAYING;
2019 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2020 lpWaveHdr->dwFlags |= WHDR_DONE;
2022 return MMSYSERR_NOERROR;
2025 /**************************************************************************
2026 * widStart [internal]
2028 static DWORD widStart(WORD wDevID)
2030 TRACE("(%u);\n", wDevID);
2031 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2032 WARN("can't start recording !\n");
2033 return MMSYSERR_INVALHANDLE;
2036 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
2037 return MMSYSERR_NOERROR;
2040 /**************************************************************************
2041 * widStop [internal]
2043 static DWORD widStop(WORD wDevID)
2045 TRACE("(%u);\n", wDevID);
2046 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2047 WARN("can't stop !\n");
2048 return MMSYSERR_INVALHANDLE;
2051 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
2053 return MMSYSERR_NOERROR;
2056 /**************************************************************************
2057 * widReset [internal]
2059 static DWORD widReset(WORD wDevID)
2061 TRACE("(%u);\n", wDevID);
2062 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2063 WARN("can't reset !\n");
2064 return MMSYSERR_INVALHANDLE;
2066 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2067 return MMSYSERR_NOERROR;
2070 /**************************************************************************
2071 * widMessage (WINEARTS.6)
2073 DWORD WINAPI ARTS_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2074 DWORD dwParam1, DWORD dwParam2)
2076 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2077 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2078 switch (wMsg) {
2079 case DRVM_INIT:
2080 case DRVM_EXIT:
2081 case DRVM_ENABLE:
2082 case DRVM_DISABLE:
2083 /* FIXME: Pretend this is supported */
2084 return 0;
2085 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2086 case WIDM_CLOSE: return widClose (wDevID);
2087 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2088 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2089 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2090 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2091 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2092 case WIDM_RESET: return widReset (wDevID);
2093 case WIDM_START: return widStart (wDevID);
2094 case WIDM_STOP: return widStop (wDevID);
2095 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2096 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2097 default:
2098 FIXME("unknown message %d!\n", wMsg);
2100 return MMSYSERR_NOTSUPPORTED;
2103 /*======================================================================*
2104 * Low level DSOUND implementation *
2105 *======================================================================*/
2106 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2108 /* we can't perform memory mapping as we don't have a file stream
2109 interface with arts like we do with oss */
2110 MESSAGE("This sound card's driver does not support direct access\n");
2111 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2112 return MMSYSERR_NOTSUPPORTED;
2115 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2117 memset(desc, 0, sizeof(*desc));
2118 strcpy(desc->szDesc, "Wine aRts DirectSound Driver");
2119 strcpy(desc->szDrvName, "winearts.drv");
2120 return MMSYSERR_NOERROR;
2123 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
2125 memcpy(pGuid, &DSDEVID_DefaultPlayback, sizeof(GUID));
2126 return MMSYSERR_NOERROR;
2129 #else /* !HAVE_ARTS */
2131 /**************************************************************************
2132 * wodMessage (WINEARTS.@)
2134 DWORD WINAPI ARTS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2135 DWORD dwParam1, DWORD dwParam2)
2137 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2138 return MMSYSERR_NOTENABLED;
2141 /**************************************************************************
2142 * widMessage (WINEARTS.6)
2144 DWORD WINAPI ARTS_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2145 DWORD dwParam1, DWORD dwParam2)
2147 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2148 return MMSYSERR_NOTENABLED;
2151 #endif /* HAVE_ARTS */