Added header errno.h.
[wine/gsoc_dplay.git] / dlls / winmm / wineesd / audio.c
blob62f6fd5e680a3c483e8df28a4aedc2fbe42e7900
1 /*
2 * Wine Driver for EsounD Sound Server
3 * http://www.tux.org/~ricdude/EsounD.html
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
8 * 2004 Zhangrong Huang (EsounD version of this file)
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* NOTE:
25 * with esd we cannot stop the audio that is already in
26 * the servers buffer.
28 * FIXME:
29 * pause in waveOut does not work correctly in loop mode
31 * does something need to be done in for WaveIn DirectSound?
35 #include "config.h"
37 #include <errno.h>
38 #include <math.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #include <fcntl.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "winerror.h"
51 #include "wine/winuser16.h"
52 #include "mmddk.h"
53 #include "dsound.h"
54 #include "dsdriver.h"
55 #include "esound.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(wave);
60 #ifdef HAVE_ESD
62 #include <esd.h>
64 /* define if you want to use esd_monitor_stream instead of
65 * esd_record_stream for waveIn stream
67 /*#define WID_USE_ESDMON*/
69 #define BUFFER_REFILL_THRESHOLD 4
71 #define MAX_WAVEOUTDRV (10)
72 #define MAX_WAVEINDRV (10)
74 /* state diagram for waveOut writing:
76 * +---------+-------------+---------------+---------------------------------+
77 * | state | function | event | new state |
78 * +---------+-------------+---------------+---------------------------------+
79 * | | open() | | STOPPED |
80 * | PAUSED | write() | | PAUSED |
81 * | STOPPED | write() | <thrd create> | PLAYING |
82 * | PLAYING | write() | HEADER | PLAYING |
83 * | (other) | write() | <error> | |
84 * | (any) | pause() | PAUSING | PAUSED |
85 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
86 * | (any) | reset() | RESETTING | STOPPED |
87 * | (any) | close() | CLOSING | CLOSED |
88 * +---------+-------------+---------------+---------------------------------+
91 /* states of the playing device */
92 #define WINE_WS_PLAYING 0
93 #define WINE_WS_PAUSED 1
94 #define WINE_WS_STOPPED 2
95 #define WINE_WS_CLOSED 3
97 /* events to be send to device */
98 enum win_wm_message {
99 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
100 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
103 typedef struct {
104 enum win_wm_message msg; /* message identifier */
105 DWORD param; /* parameter for this message */
106 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
107 } RING_MSG;
109 /* implement an in-process message ring for better performance
110 * (compared to passing thru the server)
111 * this ring will be used by the input (resp output) record (resp playback) routine
113 #define ESD_RING_BUFFER_INCREMENT 64
114 typedef struct {
115 RING_MSG * messages;
116 int ring_buffer_size;
117 int msg_tosave;
118 int msg_toget;
119 HANDLE msg_event;
120 CRITICAL_SECTION msg_crst;
121 } ESD_MSG_RING;
123 typedef struct {
124 volatile int state; /* one of the WINE_WS_ manifest constants */
125 WAVEOPENDESC waveDesc;
126 WORD wFlags;
127 PCMWAVEFORMAT format;
128 WAVEOUTCAPSW caps;
129 char interface_name[32];
131 DWORD dwSleepTime; /* Num of milliseconds to sleep between filling the dsp buffers */
133 /* esd information */
134 int esd_fd; /* the socket fd we get from esd when opening a stream for playing */
135 int bytes_per_frame;
136 DWORD dwBufferSize; /* size of whole buffer in bytes */
138 char* sound_buffer;
139 long buffer_size;
141 DWORD volume_left; /* volume control information */
142 DWORD volume_right;
144 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
145 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
146 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
148 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
149 DWORD dwLoops; /* private copy of loop counter */
151 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
152 DWORD dwWrittenTotal; /* number of bytes written to the audio device since opening */
154 /* synchronization stuff */
155 HANDLE hStartUpEvent;
156 HANDLE hThread;
157 DWORD dwThreadID;
158 ESD_MSG_RING msgRing;
159 } WINE_WAVEOUT;
161 typedef struct {
162 volatile int state; /* one of the WINE_WS_ manifest constants */
163 WAVEOPENDESC waveDesc;
164 WORD wFlags;
165 PCMWAVEFORMAT format;
166 WAVEINCAPSW caps;
167 char interface_name[32];
169 /* esd information */
170 int esd_fd; /* the socket fd we get from esd when opening a stream for recording */
171 int bytes_per_frame;
173 LPWAVEHDR lpQueuePtr;
174 DWORD dwRecordedTotal;
176 /* synchronization stuff */
177 HANDLE hStartUpEvent;
178 HANDLE hThread;
179 DWORD dwThreadID;
180 ESD_MSG_RING msgRing;
181 } WINE_WAVEIN;
183 static char* esd_host; /* the esd host */
185 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
186 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
188 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
189 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
191 /* These strings used only for tracing */
192 static const char *wodPlayerCmdString[] = {
193 "WINE_WM_PAUSING",
194 "WINE_WM_RESTARTING",
195 "WINE_WM_RESETTING",
196 "WINE_WM_HEADER",
197 "WINE_WM_UPDATE",
198 "WINE_WM_BREAKLOOP",
199 "WINE_WM_CLOSING",
200 "WINE_WM_STARTING",
201 "WINE_WM_STOPPING",
204 /*======================================================================*
205 * Low level WAVE implementation *
206 *======================================================================*/
208 /* Volume functions derived from Alsaplayer source */
209 /* length is the number of 16 bit samples */
210 void volume_effect16(void *bufin, void* bufout, int length, int left,
211 int right, int nChannels)
213 short *d_out = (short *)bufout;
214 short *d_in = (short *)bufin;
215 int i, v;
218 TRACE("length == %d, nChannels == %d\n", length, nChannels);
221 if (right == -1) right = left;
223 for(i = 0; i < length; i+=(nChannels))
225 v = (int) ((*(d_in++) * left) / 100);
226 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
227 if(nChannels == 2)
229 v = (int) ((*(d_in++) * right) / 100);
230 *(d_out++) = (v>32767) ? 32767 : ((v<-32768) ? -32768 : v);
235 /* length is the number of 8 bit samples */
236 void volume_effect8(void *bufin, void* bufout, int length, int left,
237 int right, int nChannels)
239 BYTE *d_out = (BYTE *)bufout;
240 BYTE *d_in = (BYTE *)bufin;
241 int i, v;
244 TRACE("length == %d, nChannels == %d\n", length, nChannels);
247 if (right == -1) right = left;
249 for(i = 0; i < length; i+=(nChannels))
251 v = (BYTE) ((*(d_in++) * left) / 100);
252 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
253 if(nChannels == 2)
255 v = (BYTE) ((*(d_in++) * right) / 100);
256 *(d_out++) = (v>255) ? 255 : ((v<0) ? 0 : v);
261 /******************************************************************
262 * ESD_CloseWaveOutDevice
265 void ESD_CloseWaveOutDevice(WINE_WAVEOUT* wwo)
267 esd_close(wwo->esd_fd); /* close the esd socket fd */
268 wwo->esd_fd = -1;
270 /* free up the buffer we use for volume and reset the size */
271 if(wwo->sound_buffer)
273 HeapFree(GetProcessHeap(), 0, wwo->sound_buffer);
274 wwo->sound_buffer = NULL;
277 wwo->buffer_size = 0;
280 /******************************************************************
281 * ESD_CloseWaveInDevice
284 void ESD_CloseWaveInDevice(WINE_WAVEIN* wwi)
286 esd_close(wwi->esd_fd); /* close the esd socket fd */
287 wwi->esd_fd = -1;
290 /******************************************************************
291 * ESD_WaveClose
293 LONG ESD_WaveClose(void)
295 int iDevice;
297 /* close all open devices */
298 for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
300 if(WOutDev[iDevice].esd_fd != -1)
302 ESD_CloseWaveOutDevice(&WOutDev[iDevice]);
306 for(iDevice = 0; iDevice < MAX_WAVEINDRV; iDevice++)
308 if(WInDev[iDevice].esd_fd != -1)
310 ESD_CloseWaveInDevice(&WInDev[iDevice]);
314 return 1;
317 /******************************************************************
318 * ESD_WaveInit
320 * Initialize internal structures from ESD server info
322 LONG ESD_WaveInit(void)
324 int i;
325 int fd;
327 TRACE("called\n");
329 /* FIXME: Maybe usefully to set the esd host. */
330 esd_host = NULL;
332 /* Testing whether the esd host is alive. */
333 if ((fd = esd_open_sound(esd_host)) < 0)
335 WARN("esd_open_sound() failed (%d)\n", errno);
336 return -1;
338 esd_close(fd);
340 /* initialize all device handles to -1 */
341 for (i = 0; i < MAX_WAVEOUTDRV; ++i)
343 static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','O','u','t','D','r','i','v','e','r',0};
345 WOutDev[i].esd_fd = -1;
346 memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
347 caps values */
348 WOutDev[i].caps.wMid = 0x00FF; /* Manufac ID */
349 WOutDev[i].caps.wPid = 0x0001; /* Product ID */
350 lstrcpyW(WOutDev[i].caps.szPname, ini);
351 snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "wineesd: %d", i);
353 WOutDev[i].caps.vDriverVersion = 0x0100;
354 WOutDev[i].caps.dwFormats = 0x00000000;
355 WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;
357 WOutDev[i].caps.wChannels = 2;
358 WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;
360 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
361 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
362 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
363 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
364 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
365 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
366 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
367 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
368 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
369 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
370 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
371 WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
374 for (i = 0; i < MAX_WAVEINDRV; ++i)
376 static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','I','n','D','r','i','v','e','r',0};
378 WInDev[i].esd_fd = -1;
379 memset(&WInDev[i].caps, 0, sizeof(WInDev[i].caps)); /* zero out
380 caps values */
381 WInDev[i].caps.wMid = 0x00FF;
382 WInDev[i].caps.wPid = 0x0001;
383 lstrcpyW(WInDev[i].caps.szPname, ini);
384 snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "wineesd: %d", i);
386 WInDev[i].caps.vDriverVersion = 0x0100;
387 WInDev[i].caps.dwFormats = 0x00000000;
389 WInDev[i].caps.wChannels = 2;
391 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
392 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
393 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
394 WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
395 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
396 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
397 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
398 WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
399 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
400 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
401 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
402 WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
404 WInDev[i].caps.wReserved1 = 0;
406 return 0;
409 /******************************************************************
410 * ESD_InitRingMessage
412 * Initialize the ring of messages for passing between driver's caller and playback/record
413 * thread
415 static int ESD_InitRingMessage(ESD_MSG_RING* mr)
417 mr->msg_toget = 0;
418 mr->msg_tosave = 0;
419 mr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
420 mr->ring_buffer_size = ESD_RING_BUFFER_INCREMENT;
421 mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
422 InitializeCriticalSection(&mr->msg_crst);
423 return 0;
426 /******************************************************************
427 * ESD_DestroyRingMessage
430 static int ESD_DestroyRingMessage(ESD_MSG_RING* mr)
432 CloseHandle(mr->msg_event);
433 HeapFree(GetProcessHeap(),0,mr->messages);
434 mr->messages=NULL;
435 DeleteCriticalSection(&mr->msg_crst);
436 return 0;
439 /******************************************************************
440 * ESD_AddRingMessage
442 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
444 static int ESD_AddRingMessage(ESD_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
446 HANDLE hEvent = INVALID_HANDLE_VALUE;
448 EnterCriticalSection(&mr->msg_crst);
449 if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
451 int old_ring_buffer_size = mr->ring_buffer_size;
452 mr->ring_buffer_size += ESD_RING_BUFFER_INCREMENT;
453 TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
454 mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
455 /* Now we need to rearrange the ring buffer so that the new
456 buffers just allocated are in between mr->msg_tosave and
457 mr->msg_toget.
459 if (mr->msg_tosave < mr->msg_toget)
461 memmove(&(mr->messages[mr->msg_toget + ESD_RING_BUFFER_INCREMENT]),
462 &(mr->messages[mr->msg_toget]),
463 sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
465 mr->msg_toget += ESD_RING_BUFFER_INCREMENT;
468 if (wait)
470 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
471 if (hEvent == INVALID_HANDLE_VALUE)
473 ERR("can't create event !?\n");
474 LeaveCriticalSection(&mr->msg_crst);
475 return 0;
477 if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
478 FIXME("two fast messages in the queue!!!!\n");
480 /* fast messages have to be added at the start of the queue */
481 mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;
483 mr->messages[mr->msg_toget].msg = msg;
484 mr->messages[mr->msg_toget].param = param;
485 mr->messages[mr->msg_toget].hEvent = hEvent;
487 else
489 mr->messages[mr->msg_tosave].msg = msg;
490 mr->messages[mr->msg_tosave].param = param;
491 mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
492 mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
495 LeaveCriticalSection(&mr->msg_crst);
497 SetEvent(mr->msg_event); /* signal a new message */
499 if (wait)
501 /* wait for playback/record thread to have processed the message */
502 WaitForSingleObject(hEvent, INFINITE);
503 CloseHandle(hEvent);
506 return 1;
509 /******************************************************************
510 * ESD_RetrieveRingMessage
512 * Get a message from the ring. Should be called by the playback/record thread.
514 static int ESD_RetrieveRingMessage(ESD_MSG_RING* mr,
515 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
517 EnterCriticalSection(&mr->msg_crst);
519 if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
521 LeaveCriticalSection(&mr->msg_crst);
522 return 0;
525 *msg = mr->messages[mr->msg_toget].msg;
526 mr->messages[mr->msg_toget].msg = 0;
527 *param = mr->messages[mr->msg_toget].param;
528 *hEvent = mr->messages[mr->msg_toget].hEvent;
529 mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
530 LeaveCriticalSection(&mr->msg_crst);
531 return 1;
534 /*======================================================================*
535 * Low level WAVE OUT implementation *
536 *======================================================================*/
538 /**************************************************************************
539 * wodNotifyClient [internal]
541 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
543 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
545 switch (wMsg) {
546 case WOM_OPEN:
547 case WOM_CLOSE:
548 case WOM_DONE:
549 if (wwo->wFlags != DCB_NULL &&
550 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
551 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
552 WARN("can't notify client !\n");
553 return MMSYSERR_ERROR;
555 break;
556 default:
557 FIXME("Unknown callback message %u\n", wMsg);
558 return MMSYSERR_INVALPARAM;
560 return MMSYSERR_NOERROR;
563 /**************************************************************************
564 * wodUpdatePlayedTotal [internal]
567 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
569 /* total played is the bytes written less the bytes to write ;-) */
570 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
572 return TRUE;
575 /**************************************************************************
576 * wodPlayer_BeginWaveHdr [internal]
578 * Makes the specified lpWaveHdr the currently playing wave header.
579 * If the specified wave header is a begin loop and we're not already in
580 * a loop, setup the loop.
582 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
584 wwo->lpPlayPtr = lpWaveHdr;
586 if (!lpWaveHdr) return;
588 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
589 if (wwo->lpLoopPtr) {
590 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
591 TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
592 } else {
593 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
594 wwo->lpLoopPtr = lpWaveHdr;
595 /* Windows does not touch WAVEHDR.dwLoops,
596 * so we need to make an internal copy */
597 wwo->dwLoops = lpWaveHdr->dwLoops;
600 wwo->dwPartialOffset = 0;
603 /**************************************************************************
604 * wodPlayer_PlayPtrNext [internal]
606 * Advance the play pointer to the next waveheader, looping if required.
608 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
610 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
612 wwo->dwPartialOffset = 0;
613 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
614 /* We're at the end of a loop, loop if required */
615 if (--wwo->dwLoops > 0) {
616 wwo->lpPlayPtr = wwo->lpLoopPtr;
617 } else {
618 /* Handle overlapping loops correctly */
619 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
620 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
621 /* shall we consider the END flag for the closing loop or for
622 * the opening one or for both ???
623 * code assumes for closing loop only
625 } else {
626 lpWaveHdr = lpWaveHdr->lpNext;
628 wwo->lpLoopPtr = NULL;
629 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
631 } else {
632 /* We're not in a loop. Advance to the next wave header */
633 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
636 return lpWaveHdr;
639 /**************************************************************************
640 * wodPlayer_NotifyWait [internal]
641 * Returns the number of milliseconds to wait before attempting to notify
642 * completion of the specified wavehdr.
643 * This is based on the number of bytes remaining to be written in the
644 * wave.
646 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
648 DWORD dwMillis;
650 if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
652 dwMillis = 1;
654 else
656 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
657 if(!dwMillis) dwMillis = 1;
660 TRACE("dwMillis = %ld\n", dwMillis);
662 return dwMillis;
666 /**************************************************************************
667 * wodPlayer_WriteMaxFrags [internal]
668 * Writes the maximum number of bytes possible to the DSP and returns
669 * the number of bytes written.
671 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* bytes)
673 /* Only attempt to write to free bytes */
674 DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
675 int toWrite = min(dwLength, *bytes);
676 int written;
678 TRACE("Writing wavehdr %p.%lu[%lu]\n",
679 wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);
681 /* see if our buffer isn't large enough for the data we are writing */
682 if(wwo->buffer_size < toWrite)
684 if(wwo->sound_buffer)
686 wwo->sound_buffer = HeapReAlloc(GetProcessHeap(), 0, wwo->sound_buffer, toWrite);
687 wwo->buffer_size = toWrite;
691 /* if we don't have a buffer then get one */
692 if(!wwo->sound_buffer)
694 /* allocate some memory for the buffer */
695 wwo->sound_buffer = HeapAlloc(GetProcessHeap(), 0, toWrite);
696 wwo->buffer_size = toWrite;
699 /* if we don't have a buffer then error out */
700 if(!wwo->sound_buffer)
702 ERR("error allocating sound_buffer memory\n");
703 return 0;
706 TRACE("toWrite == %d\n", toWrite);
708 /* apply volume to the bits */
709 /* for single channel audio streams we only use the LEFT volume */
710 if(wwo->format.wBitsPerSample == 16)
712 /* apply volume to the buffer we are about to send */
713 /* divide toWrite(bytes) by 2 as volume processes by 16 bits */
714 volume_effect16(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
715 wwo->sound_buffer, toWrite>>1, wwo->volume_left,
716 wwo->volume_right, wwo->format.wf.nChannels);
717 } else if(wwo->format.wBitsPerSample == 8)
719 /* apply volume to the buffer we are about to send */
720 volume_effect8(wwo->lpPlayPtr->lpData + wwo->dwPartialOffset,
721 wwo->sound_buffer, toWrite, wwo->volume_left,
722 wwo->volume_right, wwo->format.wf.nChannels);
723 } else
725 FIXME("unsupported wwo->format.wBitsPerSample of %d\n",
726 wwo->format.wBitsPerSample);
729 /* send the audio data to esd for playing */
730 written = write(wwo->esd_fd, wwo->sound_buffer, toWrite);
732 TRACE("written = %d\n", written);
734 if (written <= 0)
736 *bytes = 0; /* apparently esd is actually full */
737 return written; /* if we wrote nothing just return */
740 if (written >= dwLength)
741 wodPlayer_PlayPtrNext(wwo); /* If we wrote all current wavehdr, skip to the next one */
742 else
743 wwo->dwPartialOffset += written; /* Remove the amount written */
745 if (written < toWrite)
746 *bytes = 0;
747 else
748 *bytes -= written;
750 wwo->dwWrittenTotal += written; /* update stats on this wave device */
752 return written; /* return the number of bytes written */
756 /**************************************************************************
757 * wodPlayer_NotifyCompletions [internal]
759 * Notifies and remove from queue all wavehdrs which have been played to
760 * the speaker (ie. they have cleared the audio device). If force is true,
761 * we notify all wavehdrs and remove them all from the queue even if they
762 * are unplayed or part of a loop.
764 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
766 LPWAVEHDR lpWaveHdr;
768 if (wwo->lpQueuePtr) {
769 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%ld), force=(%d)\n",
770 wwo->lpQueuePtr,
771 wwo->lpPlayPtr,
772 wwo->lpLoopPtr,
773 wwo->lpQueuePtr->reserved,
774 wwo->dwWrittenTotal,
775 force);
776 } else {
777 TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), dwWrittenTotal=(%ld), force=(%d)\n",
778 wwo->lpQueuePtr,
779 wwo->lpPlayPtr,
780 wwo->lpLoopPtr,
781 wwo->dwWrittenTotal,
782 force);
785 /* Start from lpQueuePtr and keep notifying until:
786 * - we hit an unwritten wavehdr
787 * - we hit the beginning of a running loop
788 * - we hit a wavehdr which hasn't finished playing
790 while ((lpWaveHdr = wwo->lpQueuePtr) &&
791 (force ||
792 (lpWaveHdr != wwo->lpPlayPtr &&
793 lpWaveHdr != wwo->lpLoopPtr &&
794 lpWaveHdr->reserved <= wwo->dwWrittenTotal))) {
796 wwo->lpQueuePtr = lpWaveHdr->lpNext;
798 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
799 lpWaveHdr->dwFlags |= WHDR_DONE;
801 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
803 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
804 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
807 /**************************************************************************
808 * wodPlayer_Reset [internal]
810 * wodPlayer helper. Resets current output stream.
812 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
814 wodUpdatePlayedTotal(wwo);
816 wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */
818 /* we aren't able to flush any data that has already been written */
819 /* to esd, otherwise we would do the flushing here */
821 if (reset) {
822 enum win_wm_message msg;
823 DWORD param;
824 HANDLE ev;
826 /* remove any buffer */
827 wodPlayer_NotifyCompletions(wwo, TRUE);
829 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
830 wwo->state = WINE_WS_STOPPED;
831 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
833 wwo->dwPartialOffset = 0; /* Clear partial wavehdr */
835 /* remove any existing message in the ring */
836 EnterCriticalSection(&wwo->msgRing.msg_crst);
838 /* return all pending headers in queue */
839 while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
841 TRACE("flushing msg\n");
842 if (msg != WINE_WM_HEADER)
844 FIXME("shouldn't have headers left\n");
845 SetEvent(ev);
846 continue;
848 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
849 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
851 wodNotifyClient(wwo, WOM_DONE, param, 0);
853 ResetEvent(wwo->msgRing.msg_event);
854 LeaveCriticalSection(&wwo->msgRing.msg_crst);
855 } else {
856 if (wwo->lpLoopPtr) {
857 /* complicated case, not handled yet (could imply modifying the loop counter */
858 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
859 wwo->lpPlayPtr = wwo->lpLoopPtr;
860 wwo->dwPartialOffset = 0;
861 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
862 } else {
863 /* the data already written is going to be played, so take */
864 /* this fact into account here */
865 wwo->dwPlayedTotal = wwo->dwWrittenTotal;
867 wwo->state = WINE_WS_PAUSED;
871 /**************************************************************************
872 * wodPlayer_ProcessMessages [internal]
874 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
876 LPWAVEHDR lpWaveHdr;
877 enum win_wm_message msg;
878 DWORD param;
879 HANDLE ev;
881 while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
882 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
883 switch (msg) {
884 case WINE_WM_PAUSING:
885 wodPlayer_Reset(wwo, FALSE);
886 SetEvent(ev);
887 break;
888 case WINE_WM_RESTARTING:
889 wwo->state = WINE_WS_PLAYING;
890 SetEvent(ev);
891 break;
892 case WINE_WM_HEADER:
893 lpWaveHdr = (LPWAVEHDR)param;
895 /* insert buffer at the end of queue */
897 LPWAVEHDR* wh;
898 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
899 *wh = lpWaveHdr;
901 if (!wwo->lpPlayPtr)
902 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
903 if (wwo->state == WINE_WS_STOPPED)
904 wwo->state = WINE_WS_PLAYING;
905 break;
906 case WINE_WM_RESETTING:
907 wodPlayer_Reset(wwo, TRUE);
908 SetEvent(ev);
909 break;
910 case WINE_WM_UPDATE:
911 wodUpdatePlayedTotal(wwo);
912 SetEvent(ev);
913 break;
914 case WINE_WM_BREAKLOOP:
915 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
916 /* ensure exit at end of current loop */
917 wwo->dwLoops = 1;
919 SetEvent(ev);
920 break;
921 case WINE_WM_CLOSING:
922 /* sanity check: this should not happen since the device must have been reset before */
923 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
924 wwo->hThread = 0;
925 wwo->state = WINE_WS_CLOSED;
926 SetEvent(ev);
927 ExitThread(0);
928 /* shouldn't go here */
929 default:
930 FIXME("unknown message %d\n", msg);
931 break;
936 /**************************************************************************
937 * wodPlayer_FeedDSP [internal]
938 * Feed as much sound data as we can into the DSP and return the number of
939 * milliseconds before it will be necessary to feed the DSP again.
941 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
943 DWORD availInQ;
945 wodUpdatePlayedTotal(wwo);
946 /* better way to set availInQ? */
947 availInQ = ESD_BUF_SIZE;
948 TRACE("availInQ = %ld\n", availInQ);
950 /* input queue empty */
951 if (!wwo->lpPlayPtr) {
952 TRACE("Run out of wavehdr:s... flushing\n");
953 return INFINITE;
956 #if 0
957 /* no more room... no need to try to feed */
958 if(!availInQ)
960 TRACE("no more room, no need to try to feed\n");
961 return wwo->dwSleepTime;
963 #endif
965 /* Feed from partial wavehdr */
966 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0)
968 TRACE("feeding from partial wavehdr\n");
969 wodPlayer_WriteMaxFrags(wwo, &availInQ);
972 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
973 if (!wwo->dwPartialOffset)
975 while(wwo->lpPlayPtr && availInQ)
977 TRACE("feeding waveheaders until we run out of space\n");
978 /* note the value that dwPlayedTotal will return when this wave finishes playing */
979 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
980 TRACE("reserved=(%ld) dwWrittenTotal=(%ld) dwBufferLength=(%ld)\n",
981 wwo->lpPlayPtr->reserved,
982 wwo->dwWrittenTotal,
983 wwo->lpPlayPtr->dwBufferLength
985 wodPlayer_WriteMaxFrags(wwo, &availInQ);
989 if (!wwo->lpPlayPtr) {
990 TRACE("Ran out of wavehdrs\n");
991 return INFINITE;
994 return wwo->dwSleepTime;
998 /**************************************************************************
999 * wodPlayer [internal]
1001 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1003 WORD uDevID = (DWORD)pmt;
1004 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1005 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1006 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1007 DWORD dwSleepTime;
1009 wwo->state = WINE_WS_STOPPED;
1010 SetEvent(wwo->hStartUpEvent);
1012 for (;;) {
1013 /** Wait for the shortest time before an action is required. If there
1014 * are no pending actions, wait forever for a command.
1016 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1017 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1018 WaitForSingleObject(wwo->msgRing.msg_event, dwSleepTime);
1019 wodPlayer_ProcessMessages(wwo);
1020 if (wwo->state == WINE_WS_PLAYING) {
1021 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1022 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1023 } else {
1024 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1029 /**************************************************************************
1030 * wodGetDevCaps [internal]
1032 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1034 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1036 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1038 if (wDevID >= MAX_WAVEOUTDRV) {
1039 TRACE("MAX_WAVOUTDRV reached !\n");
1040 return MMSYSERR_BADDEVICEID;
1043 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1044 return MMSYSERR_NOERROR;
1047 /**************************************************************************
1048 * wodOpen [internal]
1050 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1052 WINE_WAVEOUT* wwo;
1053 /* output to esound... */
1054 int out_bits = ESD_BITS8, out_channels = ESD_MONO, out_rate;
1055 int out_mode = ESD_STREAM, out_func = ESD_PLAY;
1056 esd_format_t out_format;
1058 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1059 if (lpDesc == NULL) {
1060 WARN("Invalid Parameter !\n");
1061 return MMSYSERR_INVALPARAM;
1063 if (wDevID >= MAX_WAVEOUTDRV) {
1064 TRACE("MAX_WAVOUTDRV reached !\n");
1065 return MMSYSERR_BADDEVICEID;
1068 /* if this device is already open tell the app that it is allocated */
1069 if(WOutDev[wDevID].esd_fd != -1)
1071 TRACE("device already allocated\n");
1072 return MMSYSERR_ALLOCATED;
1075 /* only PCM format is supported so far... */
1076 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1077 lpDesc->lpFormat->nChannels == 0 ||
1078 lpDesc->lpFormat->nSamplesPerSec < DSBFREQUENCY_MIN ||
1079 lpDesc->lpFormat->nSamplesPerSec > DSBFREQUENCY_MAX ||
1080 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1081 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1082 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1083 lpDesc->lpFormat->nSamplesPerSec);
1084 return WAVERR_BADFORMAT;
1087 if (dwFlags & WAVE_FORMAT_QUERY) {
1088 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1089 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1090 lpDesc->lpFormat->nSamplesPerSec);
1091 return MMSYSERR_NOERROR;
1094 wwo = &WOutDev[wDevID];
1096 /* direct sound not supported, ignore the flag */
1097 dwFlags &= ~WAVE_DIRECTSOUND;
1099 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1101 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1102 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1104 if (wwo->format.wBitsPerSample == 0) {
1105 WARN("Resetting zeroed wBitsPerSample\n");
1106 wwo->format.wBitsPerSample = 8 *
1107 (wwo->format.wf.nAvgBytesPerSec /
1108 wwo->format.wf.nSamplesPerSec) /
1109 wwo->format.wf.nChannels;
1112 if (wwo->format.wBitsPerSample == 8)
1113 out_bits = ESD_BITS8;
1114 else if (wwo->format.wBitsPerSample == 16)
1115 out_bits = ESD_BITS16;
1117 wwo->bytes_per_frame = (wwo->format.wBitsPerSample * wwo->format.wf.nChannels) / 8;
1119 if (wwo->format.wf.nChannels == 1)
1120 out_channels = ESD_MONO;
1121 else if (wwo->format.wf.nChannels == 2)
1122 out_channels = ESD_STEREO;
1124 out_format = out_bits | out_channels | out_mode | out_func;
1125 out_rate = (int) wwo->format.wf.nSamplesPerSec;
1126 TRACE("esd output format = 0x%08x, rate = %d\n", out_format, out_rate);
1128 wwo->esd_fd = esd_play_stream(out_format, out_rate, esd_host, "wineesd");
1130 /* clear these so we don't have any confusion ;-) */
1131 wwo->sound_buffer = 0;
1132 wwo->buffer_size = 0;
1134 if(wwo->esd_fd < 0) return MMSYSERR_ALLOCATED;
1136 wwo->dwBufferSize = ESD_BUF_SIZE;
1137 TRACE("Buffer size is now (%ld)\n",wwo->dwBufferSize);
1139 wwo->dwPlayedTotal = 0;
1140 wwo->dwWrittenTotal = 0;
1142 wwo->dwSleepTime = (1024 * 1000 * BUFFER_REFILL_THRESHOLD) / wwo->format.wf.nAvgBytesPerSec;
1144 /* Initialize volume to full level */
1145 wwo->volume_left = 100;
1146 wwo->volume_right = 100;
1148 ESD_InitRingMessage(&wwo->msgRing);
1150 /* create player thread */
1151 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1152 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1153 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1154 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1155 CloseHandle(wwo->hStartUpEvent);
1156 } else {
1157 wwo->hThread = INVALID_HANDLE_VALUE;
1158 wwo->dwThreadID = 0;
1160 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1162 TRACE("esd=0x%lx, dwBufferSize=%ld\n",
1163 (long)wwo->esd_fd, wwo->dwBufferSize);
1165 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1166 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1167 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1168 wwo->format.wf.nBlockAlign);
1170 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1173 /**************************************************************************
1174 * wodClose [internal]
1176 static DWORD wodClose(WORD wDevID)
1178 DWORD ret = MMSYSERR_NOERROR;
1179 WINE_WAVEOUT* wwo;
1181 TRACE("(%u);\n", wDevID);
1183 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1184 WARN("bad device ID !\n");
1185 return MMSYSERR_BADDEVICEID;
1188 wwo = &WOutDev[wDevID];
1189 if (wwo->lpQueuePtr) {
1190 WARN("buffers still playing !\n");
1191 ret = WAVERR_STILLPLAYING;
1192 } else {
1193 TRACE("imhere[3-close]\n");
1194 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1195 ESD_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1198 ESD_DestroyRingMessage(&wwo->msgRing);
1200 ESD_CloseWaveOutDevice(wwo); /* close the stream and clean things up */
1202 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1204 return ret;
1207 /**************************************************************************
1208 * wodWrite [internal]
1211 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1213 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1215 /* first, do the sanity checks... */
1216 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1217 WARN("bad dev ID !\n");
1218 return MMSYSERR_BADDEVICEID;
1221 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1223 TRACE("unprepared\n");
1224 return WAVERR_UNPREPARED;
1227 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1229 TRACE("still playing\n");
1230 return WAVERR_STILLPLAYING;
1233 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1234 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1235 lpWaveHdr->lpNext = 0;
1237 TRACE("adding ring message\n");
1238 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1240 return MMSYSERR_NOERROR;
1243 /**************************************************************************
1244 * wodPrepare [internal]
1246 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1248 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1250 if (wDevID >= MAX_WAVEOUTDRV) {
1251 WARN("bad device ID !\n");
1252 return MMSYSERR_BADDEVICEID;
1255 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1256 return WAVERR_STILLPLAYING;
1258 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1259 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1260 return MMSYSERR_NOERROR;
1263 /**************************************************************************
1264 * wodUnprepare [internal]
1266 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1268 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1270 if (wDevID >= MAX_WAVEOUTDRV) {
1271 WARN("bad device ID !\n");
1272 return MMSYSERR_BADDEVICEID;
1275 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1276 return WAVERR_STILLPLAYING;
1278 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1279 lpWaveHdr->dwFlags |= WHDR_DONE;
1281 return MMSYSERR_NOERROR;
1284 /**************************************************************************
1285 * wodPause [internal]
1287 static DWORD wodPause(WORD wDevID)
1289 TRACE("(%u);!\n", wDevID);
1291 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1292 WARN("bad device ID !\n");
1293 return MMSYSERR_BADDEVICEID;
1296 TRACE("imhere[3-PAUSING]\n");
1297 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1299 return MMSYSERR_NOERROR;
1302 /**************************************************************************
1303 * wodRestart [internal]
1305 static DWORD wodRestart(WORD wDevID)
1307 TRACE("(%u);\n", wDevID);
1309 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1310 WARN("bad device ID !\n");
1311 return MMSYSERR_BADDEVICEID;
1314 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1315 TRACE("imhere[3-RESTARTING]\n");
1316 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1319 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1320 /* FIXME: Myst crashes with this ... hmm -MM
1321 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1324 return MMSYSERR_NOERROR;
1327 /**************************************************************************
1328 * wodReset [internal]
1330 static DWORD wodReset(WORD wDevID)
1332 TRACE("(%u);\n", wDevID);
1334 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1335 WARN("bad device ID !\n");
1336 return MMSYSERR_BADDEVICEID;
1339 TRACE("imhere[3-RESET]\n");
1340 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1342 return MMSYSERR_NOERROR;
1345 /**************************************************************************
1346 * wodGetPosition [internal]
1348 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1350 double time;
1351 DWORD val;
1352 WINE_WAVEOUT* wwo;
1354 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1356 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1357 WARN("bad device ID !\n");
1358 return MMSYSERR_BADDEVICEID;
1361 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1363 wwo = &WOutDev[wDevID];
1364 ESD_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1365 val = wwo->dwPlayedTotal;
1367 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1368 lpTime->wType, wwo->format.wBitsPerSample,
1369 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1370 wwo->format.wf.nAvgBytesPerSec);
1371 TRACE("dwPlayedTotal=%lu\n", val);
1373 switch (lpTime->wType) {
1374 case TIME_BYTES:
1375 lpTime->u.cb = val;
1376 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1377 break;
1378 case TIME_SAMPLES:
1379 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1380 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1381 break;
1382 case TIME_SMPTE:
1383 time = (double)val / (double)wwo->format.wf.nAvgBytesPerSec;
1384 lpTime->u.smpte.hour = time / (60 * 60);
1385 time -= lpTime->u.smpte.hour * (60 * 60);
1386 lpTime->u.smpte.min = time / 60;
1387 time -= lpTime->u.smpte.min * 60;
1388 lpTime->u.smpte.sec = time;
1389 time -= lpTime->u.smpte.sec;
1390 lpTime->u.smpte.frame = ceil(time * 30);
1391 lpTime->u.smpte.fps = 30;
1392 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1393 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1394 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1395 break;
1396 default:
1397 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1398 lpTime->wType = TIME_MS;
1399 case TIME_MS:
1400 lpTime->u.ms = val * 1000.0 / wwo->format.wf.nAvgBytesPerSec;
1401 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1402 break;
1404 return MMSYSERR_NOERROR;
1407 /**************************************************************************
1408 * wodBreakLoop [internal]
1410 static DWORD wodBreakLoop(WORD wDevID)
1412 TRACE("(%u);\n", wDevID);
1414 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].esd_fd == -1) {
1415 WARN("bad device ID !\n");
1416 return MMSYSERR_BADDEVICEID;
1418 ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1419 return MMSYSERR_NOERROR;
1422 /**************************************************************************
1423 * wodGetVolume [internal]
1425 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1427 DWORD left, right;
1429 left = WOutDev[wDevID].volume_left;
1430 right = WOutDev[wDevID].volume_right;
1432 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1434 *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) <<
1435 16);
1437 return MMSYSERR_NOERROR;
1440 /**************************************************************************
1441 * wodSetVolume [internal]
1443 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1445 DWORD left, right;
1447 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1448 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1450 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1452 WOutDev[wDevID].volume_left = left;
1453 WOutDev[wDevID].volume_right = right;
1455 return MMSYSERR_NOERROR;
1458 /**************************************************************************
1459 * wodGetNumDevs [internal]
1461 static DWORD wodGetNumDevs(void)
1463 return MAX_WAVEOUTDRV;
1466 /**************************************************************************
1467 * wodDevInterfaceSize [internal]
1469 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1471 TRACE("(%u, %p)\n", wDevID, dwParam1);
1473 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1474 NULL, 0 ) * sizeof(WCHAR);
1475 return MMSYSERR_NOERROR;
1478 /**************************************************************************
1479 * wodDevInterface [internal]
1481 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1483 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1484 NULL, 0 ) * sizeof(WCHAR))
1486 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
1487 dwParam1, dwParam2 / sizeof(WCHAR));
1488 return MMSYSERR_NOERROR;
1490 return MMSYSERR_INVALPARAM;
1493 /**************************************************************************
1494 * wodMessage (WINEESD.@)
1496 DWORD WINAPI ESD_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1497 DWORD dwParam1, DWORD dwParam2)
1499 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1500 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1502 switch (wMsg) {
1503 case DRVM_INIT:
1504 case DRVM_EXIT:
1505 case DRVM_ENABLE:
1506 case DRVM_DISABLE:
1507 /* FIXME: Pretend this is supported */
1508 return 0;
1509 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1510 case WODM_CLOSE: return wodClose (wDevID);
1511 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1512 case WODM_PAUSE: return wodPause (wDevID);
1513 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1514 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1515 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1516 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1517 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
1518 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1519 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1520 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1521 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1522 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1523 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1524 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1525 case WODM_RESTART: return wodRestart (wDevID);
1526 case WODM_RESET: return wodReset (wDevID);
1528 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1529 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1530 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1531 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1532 default:
1533 FIXME("unknown message %d!\n", wMsg);
1535 return MMSYSERR_NOTSUPPORTED;
1538 /*======================================================================*
1539 * Low level WAVE IN implementation *
1540 *======================================================================*/
1542 /**************************************************************************
1543 * widGetNumDevs [internal]
1545 static DWORD widGetNumDevs(void)
1547 TRACE("%d \n",MAX_WAVEINDRV);
1548 return MAX_WAVEINDRV;
1551 /**************************************************************************
1552 * widDevInterfaceSize [internal]
1554 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1556 TRACE("(%u, %p)\n", wDevID, dwParam1);
1559 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1560 NULL, 0 ) * sizeof(WCHAR);
1561 return MMSYSERR_NOERROR;
1564 /**************************************************************************
1565 * widDevInterface [internal]
1567 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1569 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1570 NULL, 0 ) * sizeof(WCHAR))
1572 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
1573 dwParam1, dwParam2 / sizeof(WCHAR));
1574 return MMSYSERR_NOERROR;
1576 return MMSYSERR_INVALPARAM;
1579 /**************************************************************************
1580 * widNotifyClient [internal]
1582 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1584 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
1586 switch (wMsg) {
1587 case WIM_OPEN:
1588 case WIM_CLOSE:
1589 case WIM_DATA:
1590 if (wwi->wFlags != DCB_NULL &&
1591 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
1592 (HDRVR)wwi->waveDesc.hWave, wMsg,
1593 wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
1594 WARN("can't notify client !\n");
1595 return MMSYSERR_ERROR;
1597 break;
1598 default:
1599 FIXME("Unknown callback message %u\n", wMsg);
1600 return MMSYSERR_INVALPARAM;
1602 return MMSYSERR_NOERROR;
1605 /**************************************************************************
1606 * widGetDevCaps [internal]
1608 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
1610 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1612 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1614 if (wDevID >= MAX_WAVEINDRV) {
1615 TRACE("MAX_WAVINDRV reached !\n");
1616 return MMSYSERR_BADDEVICEID;
1619 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1620 return MMSYSERR_NOERROR;
1623 /**************************************************************************
1624 * widRecorder [internal]
1626 static DWORD CALLBACK widRecorder(LPVOID pmt)
1628 WORD uDevID = (DWORD)pmt;
1629 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1630 WAVEHDR* lpWaveHdr;
1631 DWORD dwSleepTime;
1632 DWORD bytesRead;
1633 enum win_wm_message msg;
1634 DWORD param;
1635 HANDLE ev;
1637 SetEvent(wwi->hStartUpEvent);
1639 /* make sleep time to be # of ms to record one packet */
1640 dwSleepTime = (1024 * 1000) / wwi->format.wf.nAvgBytesPerSec;
1641 TRACE("sleeptime=%ld ms\n", dwSleepTime);
1643 for(;;) {
1644 TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d)\n",wwi->lpQueuePtr,wwi->state);
1646 /* read all data is esd input buffer. */
1647 if ((wwi->lpQueuePtr != NULL) && (wwi->state == WINE_WS_PLAYING))
1649 lpWaveHdr = wwi->lpQueuePtr;
1651 TRACE("read as much as we can\n");
1652 while(wwi->lpQueuePtr)
1654 TRACE("attempt to read %ld bytes\n",lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1655 bytesRead = read(wwi->esd_fd,
1656 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1657 lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1658 TRACE("bytesRead=%ld\n",bytesRead);
1659 if (bytesRead == -1 && errno == EAGAIN)
1660 bytesRead = 0;
1661 if (bytesRead==0) break; /* So we can stop recording smoothly */
1662 if (bytesRead < 0)
1663 bytesRead = 0;
1665 lpWaveHdr->dwBytesRecorded += bytesRead;
1666 wwi->dwRecordedTotal += bytesRead;
1668 /* buffer full. notify client */
1669 if (lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength)
1671 /* must copy the value of next waveHdr, because we have no idea of what
1672 * will be done with the content of lpWaveHdr in callback
1674 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1676 TRACE("waveHdr full.\n");
1678 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1679 lpWaveHdr->dwFlags |= WHDR_DONE;
1681 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1682 lpWaveHdr = wwi->lpQueuePtr = lpNext;
1687 /* wait for dwSleepTime or an event in thread's queue */
1688 WaitForSingleObject(wwi->msgRing.msg_event, dwSleepTime);
1690 while (ESD_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
1692 TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString[msg - WM_USER - 1], param);
1693 switch(msg) {
1694 case WINE_WM_PAUSING:
1695 wwi->state = WINE_WS_PAUSED;
1697 /* Put code here to "pause" esd recording
1700 SetEvent(ev);
1701 break;
1702 case WINE_WM_STARTING:
1703 wwi->state = WINE_WS_PLAYING;
1705 /* Put code here to "start" esd recording
1708 SetEvent(ev);
1709 break;
1710 case WINE_WM_HEADER:
1711 lpWaveHdr = (LPWAVEHDR)param;
1712 /* insert buffer at end of queue */
1714 LPWAVEHDR* wh;
1715 int num_headers = 0;
1716 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
1718 num_headers++;
1721 *wh=lpWaveHdr;
1723 break;
1724 case WINE_WM_STOPPING:
1725 if (wwi->state != WINE_WS_STOPPED)
1728 /* Put code here to "stop" esd recording
1731 /* return current buffer to app */
1732 lpWaveHdr = wwi->lpQueuePtr;
1733 if (lpWaveHdr)
1735 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
1736 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1737 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1738 lpWaveHdr->dwFlags |= WHDR_DONE;
1739 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1740 wwi->lpQueuePtr = lpNext;
1743 wwi->state = WINE_WS_STOPPED;
1744 SetEvent(ev);
1745 break;
1746 case WINE_WM_RESETTING:
1747 wwi->state = WINE_WS_STOPPED;
1748 wwi->dwRecordedTotal = 0;
1750 /* return all buffers to the app */
1751 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1752 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1753 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1754 lpWaveHdr->dwFlags |= WHDR_DONE;
1756 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
1758 wwi->lpQueuePtr = NULL;
1759 SetEvent(ev);
1760 break;
1761 case WINE_WM_CLOSING:
1762 wwi->hThread = 0;
1763 wwi->state = WINE_WS_CLOSED;
1764 SetEvent(ev);
1765 ExitThread(0);
1766 /* shouldn't go here */
1767 default:
1768 FIXME("unknown message %d\n", msg);
1769 break;
1773 ExitThread(0);
1774 /* just for not generating compilation warnings... should never be executed */
1775 return 0;
1778 /**************************************************************************
1779 * widOpen [internal]
1781 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1783 WINE_WAVEIN* wwi;
1784 /* input esound... */
1785 int in_bits = ESD_BITS16, in_channels = ESD_STEREO, in_rate;
1786 #ifdef WID_USE_ESDMON
1787 int in_mode = ESD_STREAM, in_func = ESD_PLAY;
1788 #else
1789 int in_mode = ESD_STREAM, in_func = ESD_RECORD;
1790 #endif
1791 esd_format_t in_format;
1792 int mode;
1794 TRACE("(%u, %p %08lX);\n",wDevID, lpDesc, dwFlags);
1795 if (lpDesc == NULL) {
1796 WARN("Invalid Parametr (lpDesc == NULL)!\n");
1797 return MMSYSERR_INVALPARAM;
1800 if (wDevID >= MAX_WAVEINDRV) {
1801 TRACE ("MAX_WAVEINDRV reached !\n");
1802 return MMSYSERR_BADDEVICEID;
1805 /* if this device is already open tell the app that it is allocated */
1806 if(WInDev[wDevID].esd_fd != -1)
1808 TRACE("device already allocated\n");
1809 return MMSYSERR_ALLOCATED;
1812 /* only PCM format is support so far... */
1813 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1814 lpDesc->lpFormat->nChannels == 0 ||
1815 lpDesc->lpFormat->nSamplesPerSec < DSBFREQUENCY_MIN ||
1816 lpDesc->lpFormat->nSamplesPerSec > DSBFREQUENCY_MAX ||
1817 (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1818 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1819 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1820 lpDesc->lpFormat->nSamplesPerSec);
1821 return WAVERR_BADFORMAT;
1824 if (dwFlags & WAVE_FORMAT_QUERY) {
1825 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1826 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1827 lpDesc->lpFormat->nSamplesPerSec);
1828 return MMSYSERR_NOERROR;
1831 wwi = &WInDev[wDevID];
1833 /* direct sound not supported, ignore the flag */
1834 dwFlags &= ~WAVE_DIRECTSOUND;
1836 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1838 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1839 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1841 if (wwi->format.wBitsPerSample == 0) {
1842 WARN("Resetting zerod wBitsPerSample\n");
1843 wwi->format.wBitsPerSample = 8 *
1844 (wwi->format.wf.nAvgBytesPerSec /
1845 wwi->format.wf.nSamplesPerSec) /
1846 wwi->format.wf.nChannels;
1849 if (wwi->format.wBitsPerSample == 8)
1850 in_bits = ESD_BITS8;
1851 else if (wwi->format.wBitsPerSample == 16)
1852 in_bits = ESD_BITS16;
1854 wwi->bytes_per_frame = (wwi->format.wBitsPerSample * wwi->format.wf.nChannels) / 8;
1856 if (wwi->format.wf.nChannels == 1)
1857 in_channels = ESD_MONO;
1858 else if (wwi->format.wf.nChannels == 2)
1859 in_channels = ESD_STEREO;
1861 in_format = in_bits | in_channels | in_mode | in_func;
1862 in_rate = (int) wwi->format.wf.nSamplesPerSec;
1863 TRACE("esd input format = 0x%08x, rate = %d\n", in_format, in_rate);
1865 #ifdef WID_USE_ESDMON
1866 wwi->esd_fd = esd_monitor_stream(in_format, in_rate, esd_host, "wineesd");
1867 #else
1868 wwi->esd_fd = esd_record_stream(in_format, in_rate, esd_host, "wineesd");
1869 #endif
1870 TRACE("(wwi->esd_fd=%d)\n",wwi->esd_fd);
1871 wwi->state = WINE_WS_STOPPED;
1873 if (wwi->lpQueuePtr) {
1874 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1875 wwi->lpQueuePtr = NULL;
1878 if(wwi->esd_fd < 0) return MMSYSERR_ALLOCATED;
1880 /* Set the esd socket O_NONBLOCK, so we can stop recording smoothly */
1881 mode = fcntl(wwi->esd_fd, F_GETFL);
1882 mode |= O_NONBLOCK;
1883 fcntl(wwi->esd_fd, F_SETFL, mode);
1885 wwi->dwRecordedTotal = 0;
1886 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1888 ESD_InitRingMessage(&wwi->msgRing);
1890 /* create recorder thread */
1891 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1892 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1893 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
1894 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
1895 CloseHandle(wwi->hStartUpEvent);
1896 } else {
1897 wwi->hThread = INVALID_HANDLE_VALUE;
1898 wwi->dwThreadID = 0;
1900 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
1902 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1903 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
1904 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
1905 wwi->format.wf.nBlockAlign);
1906 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
1909 /**************************************************************************
1910 * widClose [internal]
1912 static DWORD widClose(WORD wDevID)
1914 WINE_WAVEIN* wwi;
1916 TRACE("(%u);\n", wDevID);
1917 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1918 WARN("can't close !\n");
1919 return MMSYSERR_INVALHANDLE;
1922 wwi = &WInDev[wDevID];
1924 if (wwi->lpQueuePtr != NULL) {
1925 WARN("still buffers open !\n");
1926 return WAVERR_STILLPLAYING;
1929 ESD_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
1930 ESD_CloseWaveInDevice(wwi);
1931 wwi->state = WINE_WS_CLOSED;
1932 ESD_DestroyRingMessage(&wwi->msgRing);
1933 return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
1936 /**************************************************************************
1937 * widAddBuffer [internal]
1939 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1941 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1943 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
1944 WARN("can't do it !\n");
1945 return MMSYSERR_INVALHANDLE;
1947 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
1948 TRACE("never been prepared !\n");
1949 return WAVERR_UNPREPARED;
1951 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
1952 TRACE("header already in use !\n");
1953 return WAVERR_STILLPLAYING;
1956 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1957 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1958 lpWaveHdr->dwBytesRecorded = 0;
1959 lpWaveHdr->lpNext = NULL;
1961 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1962 return MMSYSERR_NOERROR;
1965 /**************************************************************************
1966 * widPrepare [internal]
1968 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1970 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1972 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
1974 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1975 return WAVERR_STILLPLAYING;
1977 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1978 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1979 lpWaveHdr->dwBytesRecorded = 0;
1981 return MMSYSERR_NOERROR;
1984 /**************************************************************************
1985 * widUnprepare [internal]
1987 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1989 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1990 if (wDevID >= MAX_WAVEINDRV) {
1991 WARN("bad device ID !\n");
1992 return MMSYSERR_INVALHANDLE;
1995 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
1996 TRACE("Still playing...\n");
1997 return WAVERR_STILLPLAYING;
2000 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
2001 lpWaveHdr->dwFlags |= WHDR_DONE;
2003 return MMSYSERR_NOERROR;
2006 /**************************************************************************
2007 * widStart [internal]
2009 static DWORD widStart(WORD wDevID)
2011 TRACE("(%u);\n", wDevID);
2012 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2013 WARN("can't start recording !\n");
2014 return MMSYSERR_INVALHANDLE;
2017 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
2018 return MMSYSERR_NOERROR;
2021 /**************************************************************************
2022 * widStop [internal]
2024 static DWORD widStop(WORD wDevID)
2026 TRACE("(%u);\n", wDevID);
2027 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2028 WARN("can't stop !\n");
2029 return MMSYSERR_INVALHANDLE;
2032 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
2034 return MMSYSERR_NOERROR;
2037 /**************************************************************************
2038 * widReset [internal]
2040 static DWORD widReset(WORD wDevID)
2042 TRACE("(%u);\n", wDevID);
2043 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
2044 WARN("can't reset !\n");
2045 return MMSYSERR_INVALHANDLE;
2047 ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2048 return MMSYSERR_NOERROR;
2051 /**************************************************************************
2052 * widMessage (WINEESD.6)
2054 DWORD WINAPI ESD_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2055 DWORD dwParam1, DWORD dwParam2)
2057 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2058 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2059 switch (wMsg) {
2060 case DRVM_INIT:
2061 case DRVM_EXIT:
2062 case DRVM_ENABLE:
2063 case DRVM_DISABLE:
2064 /* FIXME: Pretend this is supported */
2065 return 0;
2066 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2067 case WIDM_CLOSE: return widClose (wDevID);
2068 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2069 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2070 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2071 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
2072 case WIDM_GETNUMDEVS: return widGetNumDevs ();
2073 case WIDM_RESET: return widReset (wDevID);
2074 case WIDM_START: return widStart (wDevID);
2075 case WIDM_STOP: return widStop (wDevID);
2076 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
2077 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2078 default:
2079 FIXME("unknown message %d!\n", wMsg);
2081 return MMSYSERR_NOTSUPPORTED;
2084 /*======================================================================*
2085 * Low level DSOUND implementation *
2086 *======================================================================*/
2087 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2089 /* we can't perform memory mapping as we don't have a file stream
2090 interface with esd like we do with oss */
2091 MESSAGE("This sound card's driver does not support direct access\n");
2092 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2093 return MMSYSERR_NOTSUPPORTED;
2096 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2098 memset(desc, 0, sizeof(*desc));
2099 strcpy(desc->szDesc, "Wine EsounD DirectSound Driver");
2100 strcpy(desc->szDrvname, "wineesd.drv");
2101 return MMSYSERR_NOERROR;
2104 #else /* !HAVE_ESD */
2106 /**************************************************************************
2107 * wodMessage (WINEESD.@)
2109 DWORD WINAPI ESD_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2110 DWORD dwParam1, DWORD dwParam2)
2112 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2113 return MMSYSERR_NOTENABLED;
2116 /**************************************************************************
2117 * widMessage (WINEESD.6)
2119 DWORD WINAPI ESD_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2120 DWORD dwParam1, DWORD dwParam2)
2122 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2123 return MMSYSERR_NOTENABLED;
2126 #endif /* HAVE_ESD */