Fix calculation of SMPTE time (round up to full frame).
[wine/gsoc_dplay.git] / dlls / winmm / winearts / audio.c
blob5e9a3d8ad3e4d7fb5990a133b9f520ad2e3bc4f2
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 <math.h>
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <string.h>
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50 #include <fcntl.h>
51 #include "windef.h"
52 #include "winbase.h"
53 #include "wingdi.h"
54 #include "winerror.h"
55 #include "wine/winuser16.h"
56 #include "mmddk.h"
57 #include "dsound.h"
58 #include "dsdriver.h"
59 #include "arts.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wave);
64 #ifdef HAVE_ARTS
66 #include <artsc.h>
68 /* The following four #defines allow you to fine-tune the packet
69 * settings in arts for better low-latency support. You must also
70 * adjust the latency in the KDE arts control panel. I recommend 4
71 * fragments, 1024 bytes.
73 * The following is from the ARTS documentation and explains what CCCC
74 * and SSSS mean:
76 * @li ARTS_P_PACKET_SETTINGS (rw) This is a way to configure packet
77 * size & packet count at the same time. The format is 0xCCCCSSSS,
78 * where 2^SSSS is the packet size, and CCCC is the packet count. Note
79 * that when writing this, you don't necessarily get the settings you
80 * requested.
82 #define WAVEOUT_PACKET_CCCC 0x000C
83 #define WAVEOUT_PACKET_SSSS 0x0008
84 #define WAVEIN_PACKET_CCCC 0x000C
85 #define WAVEIN_PACKET_SSSS 0x0008
87 #define BUFFER_REFILL_THRESHOLD 4
89 #define WAVEOUT_PACKET_SETTINGS ((WAVEOUT_PACKET_CCCC << 16) | (WAVEOUT_PACKET_SSSS))
90 #define WAVEIN_PACKET_SETTINGS ((WAVEIN_PACKET_CCCC << 16) | (WAVEIN_PACKET_SSSS))
92 #define MAX_WAVEOUTDRV (10)
93 #define MAX_WAVEINDRV (10)
95 /* state diagram for waveOut writing:
97 * +---------+-------------+---------------+---------------------------------+
98 * | state | function | event | new state |
99 * +---------+-------------+---------------+---------------------------------+
100 * | | open() | | STOPPED |
101 * | PAUSED | write() | | PAUSED |
102 * | STOPPED | write() | <thrd create> | PLAYING |
103 * | PLAYING | write() | HEADER | PLAYING |
104 * | (other) | write() | <error> | |
105 * | (any) | pause() | PAUSING | PAUSED |
106 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
107 * | (any) | reset() | RESETTING | STOPPED |
108 * | (any) | close() | CLOSING | CLOSED |
109 * +---------+-------------+---------------+---------------------------------+
112 /* states of the playing device */
113 #define WINE_WS_PLAYING 0
114 #define WINE_WS_PAUSED 1
115 #define WINE_WS_STOPPED 2
116 #define WINE_WS_CLOSED 3
118 /* events to be send to device */
119 enum win_wm_message {
120 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
121 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
124 typedef struct {
125 enum win_wm_message msg; /* message identifier */
126 DWORD param; /* parameter for this message */
127 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
128 } RING_MSG;
130 /* implement an in-process message ring for better performance
131 * (compared to passing thru the server)
132 * this ring will be used by the input (resp output) record (resp playback) routine
134 #define ARTS_RING_BUFFER_INCREMENT 64
135 typedef struct {
136 RING_MSG * messages;
137 int ring_buffer_size;
138 int msg_tosave;
139 int msg_toget;
140 HANDLE msg_event;
141 CRITICAL_SECTION msg_crst;
142 } ARTS_MSG_RING;
144 typedef struct {
145 volatile int state; /* one of the WINE_WS_ manifest constants */
146 WAVEOPENDESC waveDesc;
147 WORD wFlags;
148 PCMWAVEFORMAT format;
149 WAVEOUTCAPSA caps;
150 char interface_name[32];
152 DWORD dwSleepTime; /* Num of milliseconds to sleep between filling the dsp buffers */
154 /* arts information */
155 arts_stream_t play_stream; /* the stream structure we get from arts when opening a stream for playing */
156 DWORD dwBufferSize; /* size of whole buffer in bytes */
157 int packetSettings;
159 char* sound_buffer;
160 long buffer_size;
162 DWORD volume_left; /* volume control information */
163 DWORD volume_right;
165 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
166 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
167 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
169 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
170 DWORD dwLoops; /* private copy of loop counter */
172 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
173 DWORD dwWrittenTotal; /* number of bytes written to the audio device since opening */
175 /* synchronization stuff */
176 HANDLE hStartUpEvent;
177 HANDLE hThread;
178 DWORD dwThreadID;
179 ARTS_MSG_RING msgRing;
180 } WINE_WAVEOUT;
182 typedef struct {
183 volatile int state; /* one of the WINE_WS_ manifest constants */
184 WAVEOPENDESC waveDesc;
185 WORD wFlags;
186 PCMWAVEFORMAT format;
187 WAVEINCAPSA caps;
188 char interface_name[32];
190 /* arts information */
191 arts_stream_t record_stream; /* the stream structure we get from arts when opening a stream for recording */
192 int packetSettings;
194 LPWAVEHDR lpQueuePtr;
195 DWORD dwRecordedTotal;
197 /* synchronization stuff */
198 HANDLE hStartUpEvent;
199 HANDLE hThread;
200 DWORD dwThreadID;
201 ARTS_MSG_RING msgRing;
202 } WINE_WAVEIN;
204 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
205 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
207 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
208 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
209 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
211 /* These strings used only for tracing */
212 static const char *wodPlayerCmdString[] = {
213 "WINE_WM_PAUSING",
214 "WINE_WM_RESTARTING",
215 "WINE_WM_RESETTING",
216 "WINE_WM_HEADER",
217 "WINE_WM_UPDATE",
218 "WINE_WM_BREAKLOOP",
219 "WINE_WM_CLOSING",
220 "WINE_WM_STARTING",
221 "WINE_WM_STOPPING",
224 /*======================================================================*
225 * Low level WAVE implementation *
226 *======================================================================*/
228 /* Volume functions derived from Alsaplayer source */
229 /* length is the number of 16 bit samples */
230 void volume_effect16(void *bufin, void* bufout, int length, int left,
231 int right, int nChannels)
233 short *d_out = (short *)bufout;
234 short *d_in = (short *)bufin;
235 int i, v;
238 TRACE("length == %d, nChannels == %d\n", length, nChannels);
241 if (right == -1) right = left;
243 for(i = 0; i < length; i+=(nChannels))
245 v = (int) ((*(d_in++) * left) / 100);
246 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
247 if(nChannels == 2)
249 v = (int) ((*(d_in++) * right) / 100);
250 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
255 /* length is the number of 8 bit samples */
256 void volume_effect8(void *bufin, void* bufout, int length, int left,
257 int right, int nChannels)
259 BYTE *d_out = (BYTE *)bufout;
260 BYTE *d_in = (BYTE *)bufin;
261 int i, v;
264 TRACE("length == %d, nChannels == %d\n", length, nChannels);
267 if (right == -1) right = left;
269 for(i = 0; i < length; i+=(nChannels))
271 v = (BYTE) ((*(d_in++) * left) / 100);
272 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
273 if(nChannels == 2)
275 v = (BYTE) ((*(d_in++) * right) / 100);
276 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
281 /******************************************************************
282 * ARTS_CloseWaveOutDevice
285 void ARTS_CloseWaveOutDevice(WINE_WAVEOUT* wwo)
287 arts_close_stream(wwo->play_stream); /* close the arts stream */
288 wwo->play_stream = (arts_stream_t*)-1;
290 /* free up the buffer we use for volume and reset the size */
291 if(wwo->sound_buffer)
293 HeapFree(GetProcessHeap(), 0, wwo->sound_buffer);
294 wwo->sound_buffer = NULL;
297 wwo->buffer_size = 0;
300 /******************************************************************
301 * ARTS_CloseWaveInDevice
304 void ARTS_CloseWaveInDevice(WINE_WAVEIN* wwi)
306 arts_close_stream(wwi->record_stream); /* close the arts stream */
307 wwi->record_stream = (arts_stream_t*)-1;
310 /******************************************************************
311 * ARTS_Init
313 static int ARTS_Init(void)
315 return arts_init(); /* initialize arts and return errorcode */
318 /******************************************************************
319 * ARTS_WaveClose
321 LONG ARTS_WaveClose(void)
323 int iDevice;
325 /* close all open devices */
326 for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
328 if(WOutDev[iDevice].play_stream != (arts_stream_t*)-1)
330 ARTS_CloseWaveOutDevice(&WOutDev[iDevice]);
334 for(iDevice = 0; iDevice < MAX_WAVEINDRV; iDevice++)
336 if(WInDev[iDevice].record_stream != (arts_stream_t*)-1)
338 ARTS_CloseWaveInDevice(&WInDev[iDevice]);
342 arts_free(); /* free up arts */
343 return 1;
346 /******************************************************************
347 * ARTS_WaveInit
349 * Initialize internal structures from ARTS server info
351 LONG ARTS_WaveInit(void)
353 int i;
354 int errorcode;
356 TRACE("called\n");
358 if ((errorcode = ARTS_Init()) < 0)
360 ERR("arts_init() failed (%d)\n", errorcode);
361 return -1;
364 /* initialize all device handles to -1 */
365 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
367 WOutDev[i].play_stream = (arts_stream_t*)-1;
368 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
369 caps values */
370 /* FIXME: some programs compare this string against the content of the registry
371 * for MM drivers. The names have to match in order for the program to work
372 * (e.g. MS win9x mplayer.exe)
374 #ifdef EMULATE_SB16
375 WOutDev[i].caps.wMid = 0x0002;
376 WOutDev[i].caps.wPid = 0x0104;
377 strcpy(WOutDev[i].caps.szPname, "SB16 Wave Out");
378 #else
379 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
380 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
381 /* strcpy(WOutDev[i].caps.szPname, "OpenSoundSystem WAVOUT Driver");*/
382 strcpy(WOutDev[i].caps.szPname, "CS4236/37/38");
383 #endif
384 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "winearts: %d", i);
386 WOutDev[i].caps.vDriverVersion = 0x0100;
387 WOutDev[i].caps.dwFormats = 0x00000000;
388 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
390 WOutDev[i].caps.wChannels = 2;
391 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
393 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
394 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
395 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
396 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
397 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
398 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
399 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
400 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
401 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
402 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
403 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
404 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
407 for (i = 0; i < MAX_WAVEINDRV; ++i)
409 WInDev[i].record_stream = (arts_stream_t*)-1;
410 memset(&WInDev[i].caps, 0, sizeof(WInDev[i].caps)); /* zero out
411 caps values */
412 /* FIXME: some programs compare this string against the content of the registry
413 * for MM drivers. The names have to match in order for the program to work
414 * (e.g. MS win9x mplayer.exe)
416 #ifdef EMULATE_SB16
417 WInDev[i].caps.wMid = 0x0002;
418 WInDev[i].caps.wPid = 0x0104;
419 strcpy(WInDev[i].caps.szPname, "SB16 Wave In");
420 #else
421 WInDev[i].caps.wMid = 0x00FF;
422 WInDev[i].caps.wPid = 0x0001;
423 strcpy(WInDev[i].caps.szPname,"CS4236/37/38");
424 #endif
425 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "winearts: %d", i);
427 WInDev[i].caps.vDriverVersion = 0x0100;
428 WInDev[i].caps.dwFormats = 0x00000000;
430 WInDev[i].caps.wChannels = 2;
432 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
433 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
434 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
435 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
436 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
437 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
438 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
439 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
440 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
441 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
442 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
443 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
445 WInDev[i].caps.wReserved1 = 0;
447 return 0;
450 /******************************************************************
451 * ARTS_InitRingMessage
453 * Initialize the ring of messages for passing between driver's caller and playback/record
454 * thread
456 static int ARTS_InitRingMessage(ARTS_MSG_RING* mr)
458 mr->msg_toget = 0;
459 mr->msg_tosave = 0;
460 mr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
461 mr->ring_buffer_size = ARTS_RING_BUFFER_INCREMENT;
462 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
463 InitializeCriticalSection(&mr->msg_crst);
464 return 0;
467 /******************************************************************
468 * ARTS_DestroyRingMessage
471 static int ARTS_DestroyRingMessage(ARTS_MSG_RING* mr)
473 CloseHandle(mr->msg_event);
474 HeapFree(GetProcessHeap(),0,mr->messages);
475 mr->messages=NULL;
476 DeleteCriticalSection(&mr->msg_crst);
477 return 0;
480 /******************************************************************
481 * ARTS_AddRingMessage
483 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
485 static int ARTS_AddRingMessage(ARTS_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
487 HANDLE hEvent = INVALID_HANDLE_VALUE;
489 EnterCriticalSection(&mr->msg_crst);
490 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
492 int old_ring_buffer_size = mr->ring_buffer_size;
493 mr->ring_buffer_size += ARTS_RING_BUFFER_INCREMENT;
494 TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
495 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
496 /* Now we need to rearrange the ring buffer so that the new
497 buffers just allocated are in between mr->msg_tosave and
498 mr->msg_toget.
500 if (mr->msg_tosave < mr->msg_toget)
502 memmove(&(mr->messages[mr->msg_toget + ARTS_RING_BUFFER_INCREMENT]),
503 &(mr->messages[mr->msg_toget]),
504 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
506 mr->msg_toget += ARTS_RING_BUFFER_INCREMENT;
509 if (wait)
511 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
512 if (hEvent == INVALID_HANDLE_VALUE)
514 ERR("can't create event !?\n");
515 LeaveCriticalSection(&mr->msg_crst);
516 return 0;
518 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
519 FIXME("two fast messages in the queue!!!!\n");
521 /* fast messages have to be added at the start of the queue */
522 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
524 mr->messages[mr->msg_toget].msg = msg;
525 mr->messages[mr->msg_toget].param = param;
526 mr->messages[mr->msg_toget].hEvent = hEvent;
528 else
530 mr->messages[mr->msg_tosave].msg = msg;
531 mr->messages[mr->msg_tosave].param = param;
532 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
533 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
536 LeaveCriticalSection(&mr->msg_crst);
538 SetEvent(mr->msg_event); /* signal a new message */
540 if (wait)
542 /* wait for playback/record thread to have processed the message */
543 WaitForSingleObject(hEvent, INFINITE);
544 CloseHandle(hEvent);
547 return 1;
550 /******************************************************************
551 * ARTS_RetrieveRingMessage
553 * Get a message from the ring. Should be called by the playback/record thread.
555 static int ARTS_RetrieveRingMessage(ARTS_MSG_RING* mr,
556 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
558 EnterCriticalSection(&mr->msg_crst);
560 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
562 LeaveCriticalSection(&mr->msg_crst);
563 return 0;
566 *msg = mr->messages[mr->msg_toget].msg;
567 mr->messages[mr->msg_toget].msg = 0;
568 *param = mr->messages[mr->msg_toget].param;
569 *hEvent = mr->messages[mr->msg_toget].hEvent;
570 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
571 LeaveCriticalSection(&mr->msg_crst);
572 return 1;
575 /*======================================================================*
576 * Low level WAVE OUT implementation *
577 *======================================================================*/
579 /**************************************************************************
580 * wodNotifyClient [internal]
582 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
584 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
586 switch (wMsg) {
587 case WOM_OPEN:
588 case WOM_CLOSE:
589 case WOM_DONE:
590 if (wwo->wFlags != DCB_NULL &&
591 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
592 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
593 WARN("can't notify client !\n");
594 return MMSYSERR_ERROR;
596 break;
597 default:
598 FIXME("Unknown callback message %u\n", wMsg);
599 return MMSYSERR_INVALPARAM;
601 return MMSYSERR_NOERROR;
604 /**************************************************************************
605 * wodUpdatePlayedTotal [internal]
608 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
610 /* total played is the bytes written less the bytes to write ;-) */
611 wwo->dwPlayedTotal = wwo->dwWrittenTotal -
612 (wwo->dwBufferSize -
613 arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE));
615 return TRUE;
618 /**************************************************************************
619 * wodPlayer_BeginWaveHdr [internal]
621 * Makes the specified lpWaveHdr the currently playing wave header.
622 * If the specified wave header is a begin loop and we're not already in
623 * a loop, setup the loop.
625 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
627 wwo->lpPlayPtr = lpWaveHdr;
629 if (!lpWaveHdr) return;
631 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
632 if (wwo->lpLoopPtr) {
633 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
634 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
635 } else {
636 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
637 wwo->lpLoopPtr = lpWaveHdr;
638 /* Windows does not touch WAVEHDR.dwLoops,
639 * so we need to make an internal copy */
640 wwo->dwLoops = lpWaveHdr->dwLoops;
643 wwo->dwPartialOffset = 0;
646 /**************************************************************************
647 * wodPlayer_PlayPtrNext [internal]
649 * Advance the play pointer to the next waveheader, looping if required.
651 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
653 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
655 wwo->dwPartialOffset = 0;
656 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
657 /* We're at the end of a loop, loop if required */
658 if (--wwo->dwLoops > 0) {
659 wwo->lpPlayPtr = wwo->lpLoopPtr;
660 } else {
661 /* Handle overlapping loops correctly */
662 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
663 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
664 /* shall we consider the END flag for the closing loop or for
665 * the opening one or for both ???
666 * code assumes for closing loop only
668 } else {
669 lpWaveHdr = lpWaveHdr->lpNext;
671 wwo->lpLoopPtr = NULL;
672 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
674 } else {
675 /* We're not in a loop. Advance to the next wave header */
676 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
679 return lpWaveHdr;
682 /**************************************************************************
683 * wodPlayer_NotifyWait [internal]
684 * Returns the number of milliseconds to wait before attempting to notify
685 * completion of the specified wavehdr.
686 * This is based on the number of bytes remaining to be written in the
687 * wave.
689 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
691 DWORD dwMillis;
693 if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
695 dwMillis = 1;
697 else
699 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
700 if(!dwMillis) dwMillis = 1;
703 TRACE("dwMillis = %ld\n", dwMillis);
705 return dwMillis;
709 /**************************************************************************
710 * wodPlayer_WriteMaxFrags [internal]
711 * Writes the maximum number of bytes possible to the DSP and returns
712 * the number of bytes written.
714 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
716 /* Only attempt to write to free bytes */
717 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
718 int toWrite = min(dwLength, *bytes);
719 int written;
721 TRACE("Writing wavehdr %p.%lu[%lu]\n",
722 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);
724 /* see if our buffer isn't large enough for the data we are writing */
725 if(wwo->buffer_size < toWrite)
727 if(wwo->sound_buffer)
729 wwo->sound_buffer = HeapReAlloc(GetProcessHeap(), 0, wwo->sound_buffer, toWrite);
730 wwo->buffer_size = toWrite;
734 /* if we don't have a buffer then get one */
735 if(!wwo->sound_buffer)
737 /* allocate some memory for the buffer */
738 wwo->sound_buffer = HeapAlloc(GetProcessHeap(), 0, toWrite);
739 wwo->buffer_size = toWrite;
742 /* if we don't have a buffer then error out */
743 if(!wwo->sound_buffer)
745 ERR("error allocating sound_buffer memory\n");
746 return 0;
749 TRACE("toWrite == %d\n", toWrite);
751 /* apply volume to the bits */
752 /* for single channel audio streams we only use the LEFT volume */
753 if(wwo->format.wBitsPerSample == 16)
755 /* apply volume to the buffer we are about to send */
756 /* divide toWrite(bytes) by 2 as volume processes by 16 bits */
757 volume_effect16(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
758 wwo->sound_buffer, toWrite>>1, wwo->volume_left,
759 wwo->volume_right, wwo->format.wf.nChannels);
760 } else if(wwo->format.wBitsPerSample == 8)
762 /* apply volume to the buffer we are about to send */
763 volume_effect8(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
764 wwo->sound_buffer, toWrite, wwo->volume_left,
765 wwo->volume_right, wwo->format.wf.nChannels);
766 } else
768 FIXME("unsupported wwo->format.wBitsPerSample of %d\n",
769 wwo->format.wBitsPerSample);
772 /* send the audio data to arts for playing */
773 written = arts_write(wwo->play_stream, wwo->sound_buffer, toWrite);
775 TRACE("written = %d\n", written);
777 if (written <= 0)
779 *bytes = 0; /* apparently arts is actually full */
780 return written; /* if we wrote nothing just return */
783 if (written >= dwLength)
784 wodPlayer_PlayPtrNext(wwo); /* If we wrote all current wavehdr, skip to the next one */
785 else
786 wwo->dwPartialOffset += written; /* Remove the amount written */
788 if (written < toWrite)
789 *bytes = 0;
790 else
791 *bytes -= written;
793 wwo->dwWrittenTotal += written; /* update stats on this wave device */
795 return written; /* return the number of bytes written */
799 /**************************************************************************
800 * wodPlayer_NotifyCompletions [internal]
802 * Notifies and remove from queue all wavehdrs which have been played to
803 * the speaker (ie. they have cleared the audio device). If force is true,
804 * we notify all wavehdrs and remove them all from the queue even if they
805 * are unplayed or part of a loop.
807 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
809 LPWAVEHDR lpWaveHdr;
811 if (wwo->lpQueuePtr) {
812 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%ld), force=(%d)\n",
813 wwo->lpQueuePtr,
814 wwo->lpPlayPtr,
815 wwo->lpLoopPtr,
816 wwo->lpQueuePtr->reserved,
817 wwo->dwWrittenTotal,
818 force);
819 } else {
820 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), dwWrittenTotal=(%ld), force=(%d)\n",
821 wwo->lpQueuePtr,
822 wwo->lpPlayPtr,
823 wwo->lpLoopPtr,
824 wwo->dwWrittenTotal,
825 force);
828 /* Start from lpQueuePtr and keep notifying until:
829 * - we hit an unwritten wavehdr
830 * - we hit the beginning of a running loop
831 * - we hit a wavehdr which hasn't finished playing
833 while ((lpWaveHdr = wwo->lpQueuePtr) &&
834 (force ||
835 (lpWaveHdr != wwo->lpPlayPtr &&
836 lpWaveHdr != wwo->lpLoopPtr &&
837 lpWaveHdr->reserved <= wwo->dwWrittenTotal))) {
839 wwo->lpQueuePtr = lpWaveHdr->lpNext;
841 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
842 lpWaveHdr->dwFlags |= WHDR_DONE;
844 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
846 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
847 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
850 /**************************************************************************
851 * wodPlayer_Reset [internal]
853 * wodPlayer helper. Resets current output stream.
855 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
857 wodUpdatePlayedTotal(wwo);
859 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
861 /* we aren't able to flush any data that has already been written */
862 /* to arts, otherwise we would do the flushing here */
864 if (reset) {
865 enum win_wm_message msg;
866 DWORD param;
867 HANDLE ev;
869 /* remove any buffer */
870 wodPlayer_NotifyCompletions(wwo, TRUE);
872 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
873 wwo->state = WINE_WS_STOPPED;
874 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
876 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
878 /* remove any existing message in the ring */
879 EnterCriticalSection(&wwo->msgRing.msg_crst);
881 /* return all pending headers in queue */
882 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
884 TRACE("flushing msg\n");
885 if (msg != WINE_WM_HEADER)
887 FIXME("shouldn't have headers left\n");
888 SetEvent(ev);
889 continue;
891 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
892 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
894 wodNotifyClient(wwo, WOM_DONE, param, 0);
896 ResetEvent(wwo->msgRing.msg_event);
897 LeaveCriticalSection(&wwo->msgRing.msg_crst);
898 } else {
899 if (wwo->lpLoopPtr) {
900 /* complicated case, not handled yet (could imply modifying the loop counter */
901 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
902 wwo->lpPlayPtr = wwo->lpLoopPtr;
903 wwo->dwPartialOffset = 0;
904 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
905 } else {
906 /* the data already written is going to be played, so take */
907 /* this fact into account here */
908 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
910 wwo->state = WINE_WS_PAUSED;
914 /**************************************************************************
915 * wodPlayer_ProcessMessages [internal]
917 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
919 LPWAVEHDR lpWaveHdr;
920 enum win_wm_message msg;
921 DWORD param;
922 HANDLE ev;
924 while (ARTS_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
925 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
926 switch (msg) {
927 case WINE_WM_PAUSING:
928 wodPlayer_Reset(wwo, FALSE);
929 SetEvent(ev);
930 break;
931 case WINE_WM_RESTARTING:
932 wwo->state = WINE_WS_PLAYING;
933 SetEvent(ev);
934 break;
935 case WINE_WM_HEADER:
936 lpWaveHdr = (LPWAVEHDR)param;
938 /* insert buffer at the end of queue */
940 LPWAVEHDR* wh;
941 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
942 *wh = lpWaveHdr;
944 if (!wwo->lpPlayPtr)
945 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
946 if (wwo->state == WINE_WS_STOPPED)
947 wwo->state = WINE_WS_PLAYING;
948 break;
949 case WINE_WM_RESETTING:
950 wodPlayer_Reset(wwo, TRUE);
951 SetEvent(ev);
952 break;
953 case WINE_WM_UPDATE:
954 wodUpdatePlayedTotal(wwo);
955 SetEvent(ev);
956 break;
957 case WINE_WM_BREAKLOOP:
958 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
959 /* ensure exit at end of current loop */
960 wwo->dwLoops = 1;
962 SetEvent(ev);
963 break;
964 case WINE_WM_CLOSING:
965 /* sanity check: this should not happen since the device must have been reset before */
966 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
967 wwo->hThread = 0;
968 wwo->state = WINE_WS_CLOSED;
969 SetEvent(ev);
970 ExitThread(0);
971 /* shouldn't go here */
972 default:
973 FIXME("unknown message %d\n", msg);
974 break;
979 /**************************************************************************
980 * wodPlayer_FeedDSP [internal]
981 * Feed as much sound data as we can into the DSP and return the number of
982 * milliseconds before it will be necessary to feed the DSP again.
984 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
986 DWORD availInQ;
988 wodUpdatePlayedTotal(wwo);
989 availInQ = arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SPACE);
990 TRACE("availInQ = %ld\n", availInQ);
992 /* input queue empty */
993 if (!wwo->lpPlayPtr) {
994 TRACE("Run out of wavehdr:s... flushing\n");
995 return INFINITE;
998 /* no more room... no need to try to feed */
999 if(!availInQ)
1001 TRACE("no more room, no need to try to feed\n");
1002 return wwo->dwSleepTime;
1005 /* Feed from partial wavehdr */
1006 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0)
1008 TRACE("feeding from partial wavehdr\n");
1009 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1012 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1013 if (!wwo->dwPartialOffset)
1015 while(wwo->lpPlayPtr && availInQ)
1017 TRACE("feeding waveheaders until we run out of space\n");
1018 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1019 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1020 TRACE("reserved=(%ld) dwWrittenTotal=(%ld) dwBufferLength=(%ld)\n",
1021 wwo->lpPlayPtr->reserved,
1022 wwo->dwWrittenTotal,
1023 wwo->lpPlayPtr->dwBufferLength
1025 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1029 if (!wwo->lpPlayPtr) {
1030 TRACE("Ran out of wavehdrs\n");
1031 return INFINITE;
1034 return wwo->dwSleepTime;
1038 /**************************************************************************
1039 * wodPlayer [internal]
1041 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1043 WORD uDevID = (DWORD)pmt;
1044 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1045 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1046 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1047 DWORD dwSleepTime;
1049 wwo->state = WINE_WS_STOPPED;
1050 SetEvent(wwo->hStartUpEvent);
1052 for (;;) {
1053 /** Wait for the shortest time before an action is required. If there
1054 * are no pending actions, wait forever for a command.
1056 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1057 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1058 WaitForSingleObject(wwo->msgRing.msg_event, dwSleepTime);
1059 wodPlayer_ProcessMessages(wwo);
1060 if (wwo->state == WINE_WS_PLAYING) {
1061 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1062 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1063 } else {
1064 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1069 /**************************************************************************
1070 * wodGetDevCaps [internal]
1072 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1074 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1076 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1078 if (wDevID >= MAX_WAVEOUTDRV) {
1079 TRACE("MAX_WAVOUTDRV reached !\n");
1080 return MMSYSERR_BADDEVICEID;
1083 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1084 return MMSYSERR_NOERROR;
1087 /**************************************************************************
1088 * wodOpen [internal]
1090 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1092 WINE_WAVEOUT* wwo;
1094 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1095 if (lpDesc == NULL) {
1096 WARN("Invalid Parameter !\n");
1097 return MMSYSERR_INVALPARAM;
1099 if (wDevID >= MAX_WAVEOUTDRV) {
1100 TRACE("MAX_WAVOUTDRV reached !\n");
1101 return MMSYSERR_BADDEVICEID;
1104 /* if this device is already open tell the app that it is allocated */
1105 if(WOutDev[wDevID].play_stream != (arts_stream_t*)-1)
1107 TRACE("device already allocated\n");
1108 return MMSYSERR_ALLOCATED;
1111 /* only PCM format is supported so far... */
1112 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1113 lpDesc->lpFormat->nChannels == 0 ||
1114 lpDesc->lpFormat->nSamplesPerSec < DSBFREQUENCY_MIN ||
1115 lpDesc->lpFormat->nSamplesPerSec > DSBFREQUENCY_MAX ||
1116 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1117 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1118 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1119 lpDesc->lpFormat->nSamplesPerSec);
1120 return WAVERR_BADFORMAT;
1123 if (dwFlags & WAVE_FORMAT_QUERY) {
1124 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1125 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1126 lpDesc->lpFormat->nSamplesPerSec);
1127 return MMSYSERR_NOERROR;
1130 wwo = &WOutDev[wDevID];
1132 /* direct sound not supported, ignore the flag */
1133 dwFlags &= ~WAVE_DIRECTSOUND;
1135 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1137 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1138 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1140 if (wwo->format.wBitsPerSample == 0) {
1141 WARN("Resetting zeroed wBitsPerSample\n");
1142 wwo->format.wBitsPerSample = 8 *
1143 (wwo->format.wf.nAvgBytesPerSec /
1144 wwo->format.wf.nSamplesPerSec) /
1145 wwo->format.wf.nChannels;
1148 wwo->play_stream = arts_play_stream(wwo->format.wf.nSamplesPerSec,
1149 wwo->format.wBitsPerSample, wwo->format.wf.nChannels, "winearts");
1151 /* clear these so we don't have any confusion ;-) */
1152 wwo->sound_buffer = 0;
1153 wwo->buffer_size = 0;
1155 arts_stream_set(wwo->play_stream, ARTS_P_BLOCKING, 0); /* disable blocking on this stream */
1157 if(!wwo->play_stream) return MMSYSERR_ALLOCATED;
1159 /* Try to set the packet settings from constant and store the value that it
1160 was actually set to for future use */
1161 wwo->packetSettings = arts_stream_set(wwo->play_stream, ARTS_P_PACKET_SETTINGS, WAVEOUT_PACKET_SETTINGS);
1162 TRACE("Tried to set ARTS_P_PACKET_SETTINGS to (%x), actually set to (%x)\n", WAVEOUT_PACKET_SETTINGS, wwo->packetSettings);
1164 wwo->dwBufferSize = arts_stream_get(wwo->play_stream, ARTS_P_BUFFER_SIZE);
1165 TRACE("Buffer size is now (%ld)\n",wwo->dwBufferSize);
1167 wwo->dwPlayedTotal = 0;
1168 wwo->dwWrittenTotal = 0;
1170 wwo->dwSleepTime = ((1 << (wwo->packetSettings & 0xFFFF)) * 1000 * BUFFER_REFILL_THRESHOLD) / wwo->format.wf.nAvgBytesPerSec;
1172 /* Initialize volume to full level */
1173 wwo->volume_left = 100;
1174 wwo->volume_right = 100;
1176 ARTS_InitRingMessage(&wwo->msgRing);
1178 /* create player thread */
1179 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1180 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1181 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1182 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1183 CloseHandle(wwo->hStartUpEvent);
1184 } else {
1185 wwo->hThread = INVALID_HANDLE_VALUE;
1186 wwo->dwThreadID = 0;
1188 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1190 TRACE("stream=0x%lx, dwBufferSize=%ld\n",
1191 (long)wwo->play_stream, wwo->dwBufferSize);
1193 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1194 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1195 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1196 wwo->format.wf.nBlockAlign);
1198 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1201 /**************************************************************************
1202 * wodClose [internal]
1204 static DWORD wodClose(WORD wDevID)
1206 DWORD ret = MMSYSERR_NOERROR;
1207 WINE_WAVEOUT* wwo;
1209 TRACE("(%u);\n", wDevID);
1211 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1212 (arts_stream_t*)-1)
1214 WARN("bad device ID !\n");
1215 return MMSYSERR_BADDEVICEID;
1218 wwo = &WOutDev[wDevID];
1219 if (wwo->lpQueuePtr) {
1220 WARN("buffers still playing !\n");
1221 ret = WAVERR_STILLPLAYING;
1222 } else {
1223 TRACE("imhere[3-close]\n");
1224 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1225 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1228 ARTS_DestroyRingMessage(&wwo->msgRing);
1230 ARTS_CloseWaveOutDevice(wwo); /* close the stream and clean things up */
1232 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1234 return ret;
1237 /**************************************************************************
1238 * wodWrite [internal]
1241 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1243 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1245 /* first, do the sanity checks... */
1246 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1247 (arts_stream_t*)-1)
1249 WARN("bad dev ID !\n");
1250 return MMSYSERR_BADDEVICEID;
1253 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1255 TRACE("unprepared\n");
1256 return WAVERR_UNPREPARED;
1259 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1261 TRACE("still playing\n");
1262 return WAVERR_STILLPLAYING;
1265 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1266 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1267 lpWaveHdr->lpNext = 0;
1269 TRACE("adding ring message\n");
1270 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1272 return MMSYSERR_NOERROR;
1275 /**************************************************************************
1276 * wodPrepare [internal]
1278 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1280 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1282 if (wDevID >= MAX_WAVEOUTDRV) {
1283 WARN("bad device ID !\n");
1284 return MMSYSERR_BADDEVICEID;
1287 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1288 return WAVERR_STILLPLAYING;
1290 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1291 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1292 return MMSYSERR_NOERROR;
1295 /**************************************************************************
1296 * wodUnprepare [internal]
1298 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1300 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1302 if (wDevID >= MAX_WAVEOUTDRV) {
1303 WARN("bad device ID !\n");
1304 return MMSYSERR_BADDEVICEID;
1307 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1308 return WAVERR_STILLPLAYING;
1310 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1311 lpWaveHdr->dwFlags |= WHDR_DONE;
1313 return MMSYSERR_NOERROR;
1316 /**************************************************************************
1317 * wodPause [internal]
1319 static DWORD wodPause(WORD wDevID)
1321 TRACE("(%u);!\n", wDevID);
1323 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1324 (arts_stream_t*)-1)
1326 WARN("bad device ID !\n");
1327 return MMSYSERR_BADDEVICEID;
1330 TRACE("imhere[3-PAUSING]\n");
1331 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1333 return MMSYSERR_NOERROR;
1336 /**************************************************************************
1337 * wodRestart [internal]
1339 static DWORD wodRestart(WORD wDevID)
1341 TRACE("(%u);\n", wDevID);
1343 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1344 (arts_stream_t*)-1)
1346 WARN("bad device ID !\n");
1347 return MMSYSERR_BADDEVICEID;
1350 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1351 TRACE("imhere[3-RESTARTING]\n");
1352 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1355 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1356 /* FIXME: Myst crashes with this ... hmm -MM
1357 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1360 return MMSYSERR_NOERROR;
1363 /**************************************************************************
1364 * wodReset [internal]
1366 static DWORD wodReset(WORD wDevID)
1368 TRACE("(%u);\n", wDevID);
1370 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1371 (arts_stream_t*)-1)
1373 WARN("bad device ID !\n");
1374 return MMSYSERR_BADDEVICEID;
1377 TRACE("imhere[3-RESET]\n");
1378 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1380 return MMSYSERR_NOERROR;
1383 /**************************************************************************
1384 * wodGetPosition [internal]
1386 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1388 double time;
1389 DWORD val;
1390 WINE_WAVEOUT* wwo;
1392 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1394 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1395 (arts_stream_t*)-1)
1397 WARN("bad device ID !\n");
1398 return MMSYSERR_BADDEVICEID;
1401 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1403 wwo = &WOutDev[wDevID];
1404 ARTS_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1405 val = wwo->dwPlayedTotal;
1407 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1408 lpTime->wType, wwo->format.wBitsPerSample,
1409 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1410 wwo->format.wf.nAvgBytesPerSec);
1411 TRACE("dwPlayedTotal=%lu\n", val);
1413 switch (lpTime->wType) {
1414 case TIME_BYTES:
1415 lpTime->u.cb = val;
1416 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1417 break;
1418 case TIME_SAMPLES:
1419 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1420 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1421 break;
1422 case TIME_SMPTE:
1423 time = (double)val / (double)wwo->format.wf.nAvgBytesPerSec;
1424 lpTime->u.smpte.hour = time / (60 * 60);
1425 time -= lpTime->u.smpte.hour * (60 * 60);
1426 lpTime->u.smpte.min = time / 60;
1427 time -= lpTime->u.smpte.min * 60;
1428 lpTime->u.smpte.sec = time;
1429 time -= lpTime->u.smpte.sec;
1430 lpTime->u.smpte.frame = ceil(time * 30);
1431 lpTime->u.smpte.fps = 30;
1432 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1433 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1434 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1435 break;
1436 default:
1437 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1438 lpTime->wType = TIME_MS;
1439 case TIME_MS:
1440 lpTime->u.ms = val * 1000.0 / wwo->format.wf.nAvgBytesPerSec;
1441 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1442 break;
1444 return MMSYSERR_NOERROR;
1447 /**************************************************************************
1448 * wodBreakLoop [internal]
1450 static DWORD wodBreakLoop(WORD wDevID)
1452 TRACE("(%u);\n", wDevID);
1454 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].play_stream ==
1455 (arts_stream_t*)-1)
1457 WARN("bad device ID !\n");
1458 return MMSYSERR_BADDEVICEID;
1460 ARTS_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1461 return MMSYSERR_NOERROR;
1464 /**************************************************************************
1465 * wodGetVolume [internal]
1467 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1469 DWORD left, right;
1471 left = WOutDev[wDevID].volume_left;
1472 right = WOutDev[wDevID].volume_right;
1474 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1476 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) <<
1477 16);
1479 return MMSYSERR_NOERROR;
1482 /**************************************************************************
1483 * wodSetVolume [internal]
1485 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1487 DWORD left, right;
1489 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1490 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1492 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1494 WOutDev[wDevID].volume_left = left;
1495 WOutDev[wDevID].volume_right = right;
1497 return MMSYSERR_NOERROR;
1500 /**************************************************************************
1501 * wodGetNumDevs [internal]
1503 static DWORD wodGetNumDevs(void)
1505 return MAX_WAVEOUTDRV;
1508 /**************************************************************************
1509 * wodDevInterfaceSize [internal]
1511 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1513 TRACE("(%u, %p)\n", wDevID, dwParam1);
1515 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1516 NULL, 0 ) * sizeof(WCHAR);
1517 return MMSYSERR_NOERROR;
1520 /**************************************************************************
1521 * wodDevInterface [internal]
1523 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1525 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1526 NULL, 0 ) * sizeof(WCHAR))
1528 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1529 dwParam1, dwParam2 / sizeof(WCHAR));
1530 return MMSYSERR_NOERROR;
1532 return MMSYSERR_INVALPARAM;
1535 /**************************************************************************
1536 * wodMessage (WINEARTS.@)
1538 DWORD WINAPI ARTS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1539 DWORD dwParam1, DWORD dwParam2)
1541 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1542 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1544 switch (wMsg) {
1545 case DRVM_INIT:
1546 case DRVM_EXIT:
1547 case DRVM_ENABLE:
1548 case DRVM_DISABLE:
1549 /* FIXME: Pretend this is supported */
1550 return 0;
1551 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1552 case WODM_CLOSE: return wodClose (wDevID);
1553 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1554 case WODM_PAUSE: return wodPause (wDevID);
1555 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1556 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1557 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1558 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1559 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1560 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1561 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1562 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1563 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1564 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1565 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1566 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1567 case WODM_RESTART: return wodRestart (wDevID);
1568 case WODM_RESET: return wodReset (wDevID);
1570 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1571 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1572 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1573 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1574 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
1575 default:
1576 FIXME("unknown message %d!\n", wMsg);
1578 return MMSYSERR_NOTSUPPORTED;
1581 /*======================================================================*
1582 * Low level WAVE IN implementation *
1583 *======================================================================*/
1585 /**************************************************************************
1586 * widGetNumDevs [internal]
1588 static DWORD widGetNumDevs(void)
1590 TRACE("%d \n",MAX_WAVEINDRV);
1591 return MAX_WAVEINDRV;
1594 /**************************************************************************
1595 * widDevInterfaceSize [internal]
1597 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1599 TRACE("(%u, %p)\n", wDevID, dwParam1);
1602 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1603 NULL, 0 ) * sizeof(WCHAR);
1604 return MMSYSERR_NOERROR;
1607 /**************************************************************************
1608 * widDevInterface [internal]
1610 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1612 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1613 NULL, 0 ) * sizeof(WCHAR))
1615 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1616 dwParam1, dwParam2 / sizeof(WCHAR));
1617 return MMSYSERR_NOERROR;
1619 return MMSYSERR_INVALPARAM;
1622 /**************************************************************************
1623 * widNotifyClient [internal]
1625 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1627 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
1629 switch (wMsg) {
1630 case WIM_OPEN:
1631 case WIM_CLOSE:
1632 case WIM_DATA:
1633 if (wwi->wFlags != DCB_NULL &&
1634 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1635 (HDRVR)wwi->waveDesc.hWave, wMsg,
1636 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
1637 WARN("can't notify client !\n");
1638 return MMSYSERR_ERROR;
1640 break;
1641 default:
1642 FIXME("Unknown callback message %u\n", wMsg);
1643 return MMSYSERR_INVALPARAM;
1645 return MMSYSERR_NOERROR;
1648 /**************************************************************************
1649 * widGetDevCaps [internal]
1651 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
1653 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1655 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1657 if (wDevID >= MAX_WAVEINDRV) {
1658 TRACE("MAX_WAVINDRV reached !\n");
1659 return MMSYSERR_BADDEVICEID;
1662 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1663 return MMSYSERR_NOERROR;
1666 /**************************************************************************
1667 * widRecorder [internal]
1669 static DWORD CALLBACK widRecorder(LPVOID pmt)
1671 WORD uDevID = (DWORD)pmt;
1672 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1673 WAVEHDR* lpWaveHdr;
1674 DWORD dwSleepTime;
1675 DWORD bytesRead;
1676 int dwBufferSpace;
1677 enum win_wm_message msg;
1678 DWORD param;
1679 HANDLE ev;
1681 SetEvent(wwi->hStartUpEvent);
1683 /* make sleep time to be # of ms to record one packet */
1684 dwSleepTime = ((1 << (wwi->packetSettings & 0xFFFF)) * 1000) / wwi->format.wf.nAvgBytesPerSec;
1685 TRACE("sleeptime=%ld ms\n", dwSleepTime);
1687 for(;;) {
1688 /* Oddly enough, dwBufferSpace is sometimes negative....
1690 * NOTE: If you remove this call to arts_stream_get() and
1691 * remove the && (dwBufferSpace > 0) the code will still
1692 * function correctly. I don't know which way is
1693 * faster/better.
1695 dwBufferSpace = arts_stream_get(wwi->record_stream, ARTS_P_BUFFER_SPACE);
1696 TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d), dwBufferSpace=(%d)\n",wwi->lpQueuePtr,wwi->state,dwBufferSpace);
1698 /* read all data is arts input buffer. */
1699 if ((wwi->lpQueuePtr != NULL) && (wwi->state == WINE_WS_PLAYING) && (dwBufferSpace > 0))
1701 lpWaveHdr = wwi->lpQueuePtr;
1703 TRACE("read as much as we can\n");
1704 while(wwi->lpQueuePtr)
1706 TRACE("attempt to read %ld bytes\n",lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1707 bytesRead = arts_read(wwi->record_stream,
1708 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1709 lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1710 TRACE("bytesRead=%ld\n",bytesRead);
1711 if (bytesRead==0) break;
1713 lpWaveHdr->dwBytesRecorded += bytesRead;
1714 wwi->dwRecordedTotal += bytesRead;
1716 /* buffer full. notify client */
1717 if (lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength)
1719 /* must copy the value of next waveHdr, because we have no idea of what
1720 * will be done with the content of lpWaveHdr in callback
1722 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1724 TRACE("waveHdr full.\n");
1726 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1727 lpWaveHdr->dwFlags |= WHDR_DONE;
1729 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1730 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1735 /* wait for dwSleepTime or an event in thread's queue */
1736 WaitForSingleObject(wwi->msgRing.msg_event, dwSleepTime);
1738 while (ARTS_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
1740 TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString[msg - WM_USER - 1], param);
1741 switch(msg) {
1742 case WINE_WM_PAUSING:
1743 wwi->state = WINE_WS_PAUSED;
1745 /* Put code here to "pause" arts recording
1748 SetEvent(ev);
1749 break;
1750 case WINE_WM_STARTING:
1751 wwi->state = WINE_WS_PLAYING;
1753 /* Put code here to "start" arts recording
1756 SetEvent(ev);
1757 break;
1758 case WINE_WM_HEADER:
1759 lpWaveHdr = (LPWAVEHDR)param;
1760 /* insert buffer at end of queue */
1762 LPWAVEHDR* wh;
1763 int num_headers = 0;
1764 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1766 num_headers++;
1769 *wh=lpWaveHdr;
1771 break;
1772 case WINE_WM_STOPPING:
1773 if (wwi->state != WINE_WS_STOPPED)
1776 /* Put code here to "stop" arts recording
1779 /* return current buffer to app */
1780 lpWaveHdr = wwi->lpQueuePtr;
1781 if (lpWaveHdr)
1783 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1784 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1785 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1786 lpWaveHdr->dwFlags |= WHDR_DONE;
1787 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1788 wwi->lpQueuePtr = lpNext;
1791 wwi->state = WINE_WS_STOPPED;
1792 SetEvent(ev);
1793 break;
1794 case WINE_WM_RESETTING:
1795 wwi->state = WINE_WS_STOPPED;
1796 wwi->dwRecordedTotal = 0;
1798 /* return all buffers to the app */
1799 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1800 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1801 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1802 lpWaveHdr->dwFlags |= WHDR_DONE;
1804 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1806 wwi->lpQueuePtr = NULL;
1807 SetEvent(ev);
1808 break;
1809 case WINE_WM_CLOSING:
1810 wwi->hThread = 0;
1811 wwi->state = WINE_WS_CLOSED;
1812 SetEvent(ev);
1813 ExitThread(0);
1814 /* shouldn't go here */
1815 default:
1816 FIXME("unknown message %d\n", msg);
1817 break;
1821 ExitThread(0);
1822 /* just for not generating compilation warnings... should never be executed */
1823 return 0;
1826 /**************************************************************************
1827 * widOpen [internal]
1829 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1831 WINE_WAVEIN* wwi;
1833 TRACE("(%u, %p %08lX);\n",wDevID, lpDesc, dwFlags);
1834 if (lpDesc == NULL) {
1835 WARN("Invalid Parametr (lpDesc == NULL)!\n");
1836 return MMSYSERR_INVALPARAM;
1839 if (wDevID >= MAX_WAVEINDRV) {
1840 TRACE ("MAX_WAVEINDRV reached !\n");
1841 return MMSYSERR_BADDEVICEID;
1844 /* if this device is already open tell the app that it is allocated */
1845 if(WInDev[wDevID].record_stream != (arts_stream_t*)-1)
1847 TRACE("device already allocated\n");
1848 return MMSYSERR_ALLOCATED;
1851 /* only PCM format is support so far... */
1852 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1853 lpDesc->lpFormat->nChannels == 0 ||
1854 lpDesc->lpFormat->nSamplesPerSec < DSBFREQUENCY_MIN ||
1855 lpDesc->lpFormat->nSamplesPerSec > DSBFREQUENCY_MAX ||
1856 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1857 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1858 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1859 lpDesc->lpFormat->nSamplesPerSec);
1860 return WAVERR_BADFORMAT;
1863 if (dwFlags & WAVE_FORMAT_QUERY) {
1864 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1865 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1866 lpDesc->lpFormat->nSamplesPerSec);
1867 return MMSYSERR_NOERROR;
1870 wwi = &WInDev[wDevID];
1872 /* direct sound not supported, ignore the flag */
1873 dwFlags &= ~WAVE_DIRECTSOUND;
1875 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1877 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1878 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1880 if (wwi->format.wBitsPerSample == 0) {
1881 WARN("Resetting zerod wBitsPerSample\n");
1882 wwi->format.wBitsPerSample = 8 *
1883 (wwi->format.wf.nAvgBytesPerSec /
1884 wwi->format.wf.nSamplesPerSec) /
1885 wwi->format.wf.nChannels;
1888 wwi->record_stream = arts_record_stream(wwi->format.wf.nSamplesPerSec,
1889 wwi->format.wBitsPerSample,
1890 wwi->format.wf.nChannels,
1891 "winearts");
1892 TRACE("(wwi->record_stream=%p)\n",wwi->record_stream);
1893 wwi->state = WINE_WS_STOPPED;
1895 wwi->packetSettings = arts_stream_set(wwi->record_stream, ARTS_P_PACKET_SETTINGS, WAVEIN_PACKET_SETTINGS);
1896 TRACE("Tried to set ARTS_P_PACKET_SETTINGS to (%x), actually set to (%x)\n", WAVEIN_PACKET_SETTINGS, wwi->packetSettings);
1897 TRACE("Buffer size is now (%d)\n", arts_stream_get(wwi->record_stream, ARTS_P_BUFFER_SIZE));
1899 if (wwi->lpQueuePtr) {
1900 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1901 wwi->lpQueuePtr = NULL;
1903 arts_stream_set(wwi->record_stream, ARTS_P_BLOCKING, 0); /* disable blocking on this stream */
1905 if(!wwi->record_stream) return MMSYSERR_ALLOCATED;
1907 wwi->dwRecordedTotal = 0;
1908 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1910 ARTS_InitRingMessage(&wwi->msgRing);
1912 /* create recorder thread */
1913 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1914 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1915 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
1916 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
1917 CloseHandle(wwi->hStartUpEvent);
1918 } else {
1919 wwi->hThread = INVALID_HANDLE_VALUE;
1920 wwi->dwThreadID = 0;
1922 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
1924 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1925 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
1926 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
1927 wwi->format.wf.nBlockAlign);
1928 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
1931 /**************************************************************************
1932 * widClose [internal]
1934 static DWORD widClose(WORD wDevID)
1936 WINE_WAVEIN* wwi;
1938 TRACE("(%u);\n", wDevID);
1939 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1940 WARN("can't close !\n");
1941 return MMSYSERR_INVALHANDLE;
1944 wwi = &WInDev[wDevID];
1946 if (wwi->lpQueuePtr != NULL) {
1947 WARN("still buffers open !\n");
1948 return WAVERR_STILLPLAYING;
1951 ARTS_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
1952 ARTS_CloseWaveInDevice(wwi);
1953 wwi->state = WINE_WS_CLOSED;
1954 ARTS_DestroyRingMessage(&wwi->msgRing);
1955 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
1958 /**************************************************************************
1959 * widAddBuffer [internal]
1961 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1963 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1965 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1966 WARN("can't do it !\n");
1967 return MMSYSERR_INVALHANDLE;
1969 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
1970 TRACE("never been prepared !\n");
1971 return WAVERR_UNPREPARED;
1973 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
1974 TRACE("header already in use !\n");
1975 return WAVERR_STILLPLAYING;
1978 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1979 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1980 lpWaveHdr->dwBytesRecorded = 0;
1981 lpWaveHdr->lpNext = NULL;
1983 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1984 return MMSYSERR_NOERROR;
1987 /**************************************************************************
1988 * widPrepare [internal]
1990 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1992 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1994 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
1996 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1997 return WAVERR_STILLPLAYING;
1999 lpWaveHdr->dwFlags |= WHDR_PREPARED;
2000 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2001 lpWaveHdr->dwBytesRecorded = 0;
2003 return MMSYSERR_NOERROR;
2006 /**************************************************************************
2007 * widUnprepare [internal]
2009 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2011 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2012 if (wDevID >= MAX_WAVEINDRV) {
2013 WARN("bad device ID !\n");
2014 return MMSYSERR_INVALHANDLE;
2017 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2018 TRACE("Still playing...\n");
2019 return WAVERR_STILLPLAYING;
2022 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2023 lpWaveHdr->dwFlags |= WHDR_DONE;
2025 return MMSYSERR_NOERROR;
2028 /**************************************************************************
2029 * widStart [internal]
2031 static DWORD widStart(WORD wDevID)
2033 TRACE("(%u);\n", wDevID);
2034 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2035 WARN("can't start recording !\n");
2036 return MMSYSERR_INVALHANDLE;
2039 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
2040 return MMSYSERR_NOERROR;
2043 /**************************************************************************
2044 * widStop [internal]
2046 static DWORD widStop(WORD wDevID)
2048 TRACE("(%u);\n", wDevID);
2049 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2050 WARN("can't stop !\n");
2051 return MMSYSERR_INVALHANDLE;
2054 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
2056 return MMSYSERR_NOERROR;
2059 /**************************************************************************
2060 * widReset [internal]
2062 static DWORD widReset(WORD wDevID)
2064 TRACE("(%u);\n", wDevID);
2065 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2066 WARN("can't reset !\n");
2067 return MMSYSERR_INVALHANDLE;
2069 ARTS_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2070 return MMSYSERR_NOERROR;
2073 /**************************************************************************
2074 * widMessage (WINEARTS.6)
2076 DWORD WINAPI ARTS_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2077 DWORD dwParam1, DWORD dwParam2)
2079 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2080 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2081 switch (wMsg) {
2082 case DRVM_INIT:
2083 case DRVM_EXIT:
2084 case DRVM_ENABLE:
2085 case DRVM_DISABLE:
2086 /* FIXME: Pretend this is supported */
2087 return 0;
2088 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2089 case WIDM_CLOSE: return widClose (wDevID);
2090 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2091 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2092 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2093 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSA)dwParam1, dwParam2);
2094 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2095 case WIDM_RESET: return widReset (wDevID);
2096 case WIDM_START: return widStart (wDevID);
2097 case WIDM_STOP: return widStop (wDevID);
2098 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2099 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2100 default:
2101 FIXME("unknown message %d!\n", wMsg);
2103 return MMSYSERR_NOTSUPPORTED;
2106 /*======================================================================*
2107 * Low level DSOUND implementation *
2108 *======================================================================*/
2109 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2111 /* we can't perform memory mapping as we don't have a file stream
2112 interface with arts like we do with oss */
2113 MESSAGE("This sound card's driver does not support direct access\n");
2114 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2115 return MMSYSERR_NOTSUPPORTED;
2118 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2120 memset(desc, 0, sizeof(*desc));
2121 strcpy(desc->szDesc, "Wine aRts DirectSound Driver");
2122 strcpy(desc->szDrvName, "winearts.drv");
2123 return MMSYSERR_NOERROR;
2126 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
2128 memcpy(pGuid, &DSDEVID_DefaultPlayback, sizeof(GUID));
2129 return MMSYSERR_NOERROR;
2132 #else /* !HAVE_ARTS */
2134 /**************************************************************************
2135 * wodMessage (WINEARTS.@)
2137 DWORD WINAPI ARTS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2138 DWORD dwParam1, DWORD dwParam2)
2140 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2141 return MMSYSERR_NOTENABLED;
2144 /**************************************************************************
2145 * widMessage (WINEARTS.6)
2147 DWORD WINAPI ARTS_widMessage(UINT wDevID, UINT 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 #endif /* HAVE_ARTS */