Added detection of 48kHz and 96kHz support. Fixed a thinko, and some
[wine.git] / dlls / winmm / winealsa / audio.c
blob674bc5f64115ab8ef82fe89c152297da882541d8
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
4 * Based on version <final> of the ALSA API
6 * Copyright 2002 Eric Pouech
7 * 2002 Marco Pietrobono
8 * 2003 Christian Costa : WaveIn support
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
25 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
26 #define USE_PIPE_SYNC
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <errno.h>
39 #include <limits.h>
40 #include <fcntl.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
43 #endif
44 #ifdef HAVE_SYS_MMAN_H
45 # include <sys/mman.h>
46 #endif
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wingdi.h"
50 #include "winerror.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "winreg.h"
54 #include "mmddk.h"
55 #include "dsound.h"
56 #include "dsdriver.h"
57 #define ALSA_PCM_OLD_HW_PARAMS_API
58 #define ALSA_PCM_OLD_SW_PARAMS_API
59 #include "alsa.h"
60 #include "wine/library.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wave);
66 #if defined(HAVE_ALSA) && ((SND_LIB_MAJOR == 0 && SND_LIB_MINOR >= 9) || SND_LIB_MAJOR >= 1)
68 /* internal ALSALIB functions */
69 snd_pcm_uframes_t _snd_pcm_mmap_hw_ptr(snd_pcm_t *pcm);
72 #define MAX_WAVEOUTDRV (1)
73 #define MAX_WAVEINDRV (1)
75 /* state diagram for waveOut writing:
77 * +---------+-------------+---------------+---------------------------------+
78 * | state | function | event | new state |
79 * +---------+-------------+---------------+---------------------------------+
80 * | | open() | | STOPPED |
81 * | PAUSED | write() | | PAUSED |
82 * | STOPPED | write() | <thrd create> | PLAYING |
83 * | PLAYING | write() | HEADER | PLAYING |
84 * | (other) | write() | <error> | |
85 * | (any) | pause() | PAUSING | PAUSED |
86 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
87 * | (any) | reset() | RESETTING | STOPPED |
88 * | (any) | close() | CLOSING | CLOSED |
89 * +---------+-------------+---------------+---------------------------------+
92 /* states of the playing device */
93 #define WINE_WS_PLAYING 0
94 #define WINE_WS_PAUSED 1
95 #define WINE_WS_STOPPED 2
96 #define WINE_WS_CLOSED 3
98 /* events to be send to device */
99 enum win_wm_message {
100 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
101 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
104 #ifdef USE_PIPE_SYNC
105 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
106 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
107 #define RESET_OMR(omr) do { } while (0)
108 #define WAIT_OMR(omr, sleep) \
109 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
110 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
111 #else
112 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
113 #define CLEAR_OMR(omr) do { } while (0)
114 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
115 #define WAIT_OMR(omr, sleep) \
116 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
117 #endif
119 typedef struct {
120 enum win_wm_message msg; /* message identifier */
121 DWORD param; /* parameter for this message */
122 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
123 } ALSA_MSG;
125 /* implement an in-process message ring for better performance
126 * (compared to passing thru the server)
127 * this ring will be used by the input (resp output) record (resp playback) routine
129 #define ALSA_RING_BUFFER_INCREMENT 64
130 typedef struct {
131 ALSA_MSG * messages;
132 int ring_buffer_size;
133 int msg_tosave;
134 int msg_toget;
135 #ifdef USE_PIPE_SYNC
136 int msg_pipe[2];
137 #else
138 HANDLE msg_event;
139 #endif
140 CRITICAL_SECTION msg_crst;
141 } ALSA_MSG_RING;
143 typedef struct {
144 /* Windows information */
145 volatile int state; /* one of the WINE_WS_ manifest constants */
146 WAVEOPENDESC waveDesc;
147 WORD wFlags;
148 PCMWAVEFORMAT format;
149 WAVEOUTCAPSA caps;
151 /* ALSA information (ALSA 0.9/1.x uses two different devices for playback/capture) */
152 char* device;
153 snd_pcm_t* p_handle; /* handle to ALSA playback device */
154 snd_pcm_t* c_handle; /* handle to ALSA capture device */
155 snd_pcm_hw_params_t * hw_params; /* ALSA Hw params */
157 snd_ctl_t * ctl; /* control handle for the playback volume */
158 snd_ctl_elem_id_t * playback_eid; /* element id of the playback volume control */
159 snd_ctl_elem_value_t * playback_evalue; /* element value of the playback volume control */
160 snd_ctl_elem_info_t * playback_einfo; /* element info of the playback volume control */
162 snd_pcm_sframes_t (*write)(snd_pcm_t *, const void *, snd_pcm_uframes_t );
164 struct pollfd *ufds;
165 int count;
167 DWORD dwBufferSize; /* size of whole ALSA buffer in bytes */
168 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
169 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
170 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
172 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
173 DWORD dwLoops; /* private copy of loop counter */
175 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
176 DWORD dwWrittenTotal; /* number of bytes written to ALSA buffer since opening */
178 /* synchronization stuff */
179 HANDLE hStartUpEvent;
180 HANDLE hThread;
181 DWORD dwThreadID;
182 ALSA_MSG_RING msgRing;
184 /* DirectSound stuff */
185 DSDRIVERDESC ds_desc;
186 GUID ds_guid;
187 } WINE_WAVEOUT;
189 typedef struct {
190 /* Windows information */
191 volatile int state; /* one of the WINE_WS_ manifest constants */
192 WAVEOPENDESC waveDesc;
193 WORD wFlags;
194 PCMWAVEFORMAT format;
195 WAVEOUTCAPSA caps;
197 /* ALSA information (ALSA 0.9/1.x uses two different devices for playback/capture) */
198 char* device;
199 snd_pcm_t* p_handle; /* handle to ALSA playback device */
200 snd_pcm_t* c_handle; /* handle to ALSA capture device */
201 snd_pcm_hw_params_t * hw_params; /* ALSA Hw params */
203 snd_ctl_t * ctl; /* control handle for the playback volume */
204 snd_ctl_elem_id_t * playback_eid; /* element id of the playback volume control */
205 snd_ctl_elem_value_t * playback_evalue; /* element value of the playback volume control */
206 snd_ctl_elem_info_t * playback_einfo; /* element info of the playback volume control */
208 snd_pcm_sframes_t (*read)(snd_pcm_t *, void *, snd_pcm_uframes_t );
210 struct pollfd *ufds;
211 int count;
213 DWORD dwPeriodSize; /* size of OSS buffer period */
214 DWORD dwBufferSize; /* size of whole ALSA buffer in bytes */
215 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
216 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
218 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
219 DWORD dwLoops; /* private copy of loop counter */
221 /*DWORD dwPlayedTotal; */
222 DWORD dwTotalRecorded;
224 /* synchronization stuff */
225 HANDLE hStartUpEvent;
226 HANDLE hThread;
227 DWORD dwThreadID;
228 ALSA_MSG_RING msgRing;
230 /* DirectSound stuff */
231 DSDRIVERDESC ds_desc;
232 GUID ds_guid;
233 } WINE_WAVEIN;
235 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
236 static DWORD ALSA_WodNumDevs;
237 static WINE_WAVEIN WInDev [MAX_WAVEINDRV];
238 static DWORD ALSA_WidNumDevs;
240 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
241 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
242 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid);
244 /* These strings used only for tracing */
245 #if 1
246 static const char *wodPlayerCmdString[] = {
247 "WINE_WM_PAUSING",
248 "WINE_WM_RESTARTING",
249 "WINE_WM_RESETTING",
250 "WINE_WM_HEADER",
251 "WINE_WM_UPDATE",
252 "WINE_WM_BREAKLOOP",
253 "WINE_WM_CLOSING",
254 "WINE_WM_STARTING",
255 "WINE_WM_STOPPING",
257 #endif
259 /*======================================================================*
260 * Low level WAVE implementation *
261 *======================================================================*/
263 /**************************************************************************
264 * ALSA_InitializeVolumeCtl [internal]
266 * used to initialize the PCM Volume Control
268 static int ALSA_InitializeVolumeCtl(WINE_WAVEOUT * wwo)
270 snd_ctl_t * ctl = NULL;
271 snd_ctl_card_info_t * cardinfo;
272 snd_ctl_elem_list_t * elemlist;
273 snd_ctl_elem_id_t * e_id;
274 snd_ctl_elem_info_t * einfo;
275 snd_hctl_t * hctl = NULL;
276 snd_hctl_elem_t * elem;
277 int nCtrls;
278 int i;
280 snd_ctl_card_info_alloca(&cardinfo);
281 memset(cardinfo,0,snd_ctl_card_info_sizeof());
283 snd_ctl_elem_list_alloca(&elemlist);
284 memset(elemlist,0,snd_ctl_elem_list_sizeof());
286 snd_ctl_elem_id_alloca(&e_id);
287 memset(e_id,0,snd_ctl_elem_id_sizeof());
289 snd_ctl_elem_info_alloca(&einfo);
290 memset(einfo,0,snd_ctl_elem_info_sizeof());
292 #define EXIT_ON_ERROR(f,txt) do \
294 int err; \
295 if ( (err = (f) ) < 0) \
297 ERR(txt ": %s\n", snd_strerror(err)); \
298 if (hctl) \
299 snd_hctl_close(hctl); \
300 if (ctl) \
301 snd_ctl_close(ctl); \
302 return -1; \
304 } while(0)
306 EXIT_ON_ERROR( snd_ctl_open(&ctl,"hw",0) , "ctl open failed" );
307 EXIT_ON_ERROR( snd_ctl_card_info(ctl, cardinfo), "card info failed");
308 EXIT_ON_ERROR( snd_ctl_elem_list(ctl, elemlist), "elem list failed");
310 nCtrls = snd_ctl_elem_list_get_count(elemlist);
312 EXIT_ON_ERROR( snd_hctl_open(&hctl,"hw",0), "hctl open failed");
313 EXIT_ON_ERROR( snd_hctl_load(hctl), "hctl load failed" );
315 elem=snd_hctl_first_elem(hctl);
316 for ( i= 0; i<nCtrls; i++) {
318 memset(e_id,0,snd_ctl_elem_id_sizeof());
320 snd_hctl_elem_get_id(elem,e_id);
322 TRACE("ctl: #%d '%s'%d\n",
323 snd_ctl_elem_id_get_numid(e_id),
324 snd_ctl_elem_id_get_name(e_id),
325 snd_ctl_elem_id_get_index(e_id));
327 if ( !strcmp("PCM Playback Volume", snd_ctl_elem_id_get_name(e_id)) )
329 EXIT_ON_ERROR( snd_hctl_elem_info(elem,einfo), "hctl elem info failed" );
331 /* few sanity checks... you'll never know... */
332 if ( snd_ctl_elem_info_get_type(einfo) != SND_CTL_ELEM_TYPE_INTEGER )
333 WARN("playback volume control is not an integer\n");
334 if ( !snd_ctl_elem_info_is_readable(einfo) )
335 WARN("playback volume control is readable\n");
336 if ( !snd_ctl_elem_info_is_writable(einfo) )
337 WARN("playback volume control is readable\n");
339 TRACE(" ctrl range: min=%ld max=%ld step=%ld\n",
340 snd_ctl_elem_info_get_min(einfo),
341 snd_ctl_elem_info_get_max(einfo),
342 snd_ctl_elem_info_get_step(einfo));
344 EXIT_ON_ERROR( snd_ctl_elem_id_malloc(&wwo->playback_eid), "elem id malloc failed" );
345 EXIT_ON_ERROR( snd_ctl_elem_info_malloc(&wwo->playback_einfo), "elem info malloc failed" );
346 EXIT_ON_ERROR( snd_ctl_elem_value_malloc(&wwo->playback_evalue), "elem value malloc failed" );
348 /* ok, now we can safely save these objects for later */
349 snd_ctl_elem_id_copy(wwo->playback_eid, e_id);
350 snd_ctl_elem_info_copy(wwo->playback_einfo, einfo);
351 snd_ctl_elem_value_set_id(wwo->playback_evalue, wwo->playback_eid);
352 wwo->ctl = ctl;
355 elem=snd_hctl_elem_next(elem);
357 snd_hctl_close(hctl);
358 #undef EXIT_ON_ERROR
359 return 0;
362 /**************************************************************************
363 * ALSA_XRUNRecovery [internal]
365 * used to recovery from XRUN errors (buffer underflow/overflow)
367 static int ALSA_XRUNRecovery(WINE_WAVEOUT * wwo, int err)
369 if (err == -EPIPE) { /* under-run */
370 err = snd_pcm_prepare(wwo->p_handle);
371 if (err < 0)
372 ERR( "underrun recovery failed. prepare failed: %s\n", snd_strerror(err));
373 return 0;
374 } else if (err == -ESTRPIPE) {
375 while ((err = snd_pcm_resume(wwo->p_handle)) == -EAGAIN)
376 sleep(1); /* wait until the suspend flag is released */
377 if (err < 0) {
378 err = snd_pcm_prepare(wwo->p_handle);
379 if (err < 0)
380 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
382 return 0;
384 return err;
387 /**************************************************************************
388 * ALSA_TraceParameters [internal]
390 * used to trace format changes, hw and sw parameters
392 static void ALSA_TraceParameters(snd_pcm_hw_params_t * hw_params, snd_pcm_sw_params_t * sw, int full)
394 snd_pcm_format_t format = snd_pcm_hw_params_get_format(hw_params);
395 snd_pcm_access_t access = snd_pcm_hw_params_get_access(hw_params);
397 #define X(x) ((x)? "true" : "false")
398 if (full)
399 TRACE("FLAGS: sampleres=%s overrng=%s pause=%s resume=%s syncstart=%s batch=%s block=%s double=%s "
400 "halfd=%s joint=%s \n",
401 X(snd_pcm_hw_params_can_mmap_sample_resolution(hw_params)),
402 X(snd_pcm_hw_params_can_overrange(hw_params)),
403 X(snd_pcm_hw_params_can_pause(hw_params)),
404 X(snd_pcm_hw_params_can_resume(hw_params)),
405 X(snd_pcm_hw_params_can_sync_start(hw_params)),
406 X(snd_pcm_hw_params_is_batch(hw_params)),
407 X(snd_pcm_hw_params_is_block_transfer(hw_params)),
408 X(snd_pcm_hw_params_is_double(hw_params)),
409 X(snd_pcm_hw_params_is_half_duplex(hw_params)),
410 X(snd_pcm_hw_params_is_joint_duplex(hw_params)));
411 #undef X
413 if (access >= 0)
414 TRACE("access=%s\n", snd_pcm_access_name(access));
415 else
417 snd_pcm_access_mask_t * acmask;
418 snd_pcm_access_mask_alloca(&acmask);
419 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
420 for ( access = SND_PCM_ACCESS_MMAP_INTERLEAVED; access <= SND_PCM_ACCESS_LAST; access++)
421 if (snd_pcm_access_mask_test(acmask, access))
422 TRACE("access=%s\n", snd_pcm_access_name(access));
425 if (format >= 0)
427 TRACE("format=%s\n", snd_pcm_format_name(format));
430 else
432 snd_pcm_format_mask_t * fmask;
434 snd_pcm_format_mask_alloca(&fmask);
435 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
436 for ( format = SND_PCM_FORMAT_S8; format <= SND_PCM_FORMAT_LAST ; format++)
437 if ( snd_pcm_format_mask_test(fmask, format) )
438 TRACE("format=%s\n", snd_pcm_format_name(format));
441 #define X(x) do { \
442 int n = snd_pcm_hw_params_get_##x(hw_params); \
443 if (n<0) \
444 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
445 (long int)snd_pcm_hw_params_get_##x##_min(hw_params), \
446 (long int)snd_pcm_hw_params_get_##x##_max(hw_params)); \
447 else \
448 TRACE(#x "=%d\n", n); \
449 } while(0)
450 X(channels);
451 X(buffer_size);
452 #undef X
454 #define X(x) do { \
455 int n = snd_pcm_hw_params_get_##x(hw_params,0); \
456 if (n<0) \
457 TRACE(#x "_min=%ld " #x "_max=%ld\n", \
458 (long int)snd_pcm_hw_params_get_##x##_min(hw_params,0), \
459 (long int)snd_pcm_hw_params_get_##x##_max(hw_params,0)); \
460 else \
461 TRACE(#x "=%d\n", n); \
462 } while(0)
463 X(rate);
464 X(buffer_time);
465 X(periods);
466 X(period_size);
467 X(period_time);
468 X(tick_time);
469 #undef X
471 if (!sw)
472 return;
477 /* return a string duplicated on the win32 process heap, free with HeapFree */
478 static char* ALSA_strdup(char *s) {
479 char *result = HeapAlloc(GetProcessHeap(), 0, strlen(s)+1);
480 strcpy(result, s);
481 return result;
484 /******************************************************************
485 * ALSA_GetDeviceFromReg
487 * Returns either "default" or reads the registry so the user can
488 * override the playback/record device used.
490 char *ALSA_GetDeviceFromReg(char *value)
492 DWORD res;
493 DWORD type;
494 HKEY playbackKey = 0;
495 char *result = NULL;
496 DWORD resultSize;
498 res = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ALSA", 0, KEY_QUERY_VALUE, &playbackKey);
499 if (res != ERROR_SUCCESS) goto end;
501 res = RegQueryValueExA(playbackKey, value, NULL, &type, NULL, &resultSize);
502 if (res != ERROR_SUCCESS) goto end;
504 if (type != REG_SZ) {
505 ERR("Registry key [HKEY_LOCAL_MACHINE\\Software\\Wine\\Wine\\ALSA\\%s] must be a string\n", value);
506 goto end;
509 result = HeapAlloc(GetProcessHeap(), 0, resultSize);
510 res = RegQueryValueExA(playbackKey, value, NULL, NULL, result, &resultSize);
512 end:
513 if (!result) result = ALSA_strdup("default");
514 if (playbackKey) RegCloseKey(playbackKey);
515 return result;
518 /******************************************************************
519 * ALSA_WaveInit
521 * Initialize internal structures from ALSA information
523 LONG ALSA_WaveInit(void)
525 snd_pcm_t* h = NULL;
526 snd_pcm_info_t * info;
527 snd_pcm_hw_params_t * hw_params;
528 WINE_WAVEOUT* wwo;
529 WINE_WAVEIN* wwi;
531 wwo = &WOutDev[0];
533 /* FIXME: use better values */
534 wwo->device = ALSA_GetDeviceFromReg("PlaybackDevice");
535 TRACE("using waveout device \"%s\"\n", wwo->device);
537 wwo->caps.wMid = 0x0002;
538 wwo->caps.wPid = 0x0104;
539 strcpy(wwo->caps.szPname, "SB16 Wave Out");
540 wwo->caps.vDriverVersion = 0x0100;
541 wwo->caps.dwFormats = 0x00000000;
542 wwo->caps.dwSupport = WAVECAPS_VOLUME;
543 strcpy(wwo->ds_desc.szDesc, "WineALSA DirectSound Driver");
544 strcpy(wwo->ds_desc.szDrvName, "winealsa.drv");
545 wwo->ds_guid = DSDEVID_DefaultPlayback;
547 if (!wine_dlopen("libasound.so.2", RTLD_LAZY|RTLD_GLOBAL, NULL, 0))
549 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
550 return -1;
553 snd_pcm_info_alloca(&info);
554 snd_pcm_hw_params_alloca(&hw_params);
556 #define EXIT_ON_ERROR(f,txt) do { int err; if ( (err = (f) ) < 0) { ERR(txt ": %s\n", snd_strerror(err)); if (h) snd_pcm_close(h); return -1; } } while(0)
558 ALSA_WodNumDevs = 0;
559 EXIT_ON_ERROR( snd_pcm_open(&h, wwo->device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) , "open pcm" );
560 if (!h) return -1;
561 ALSA_WodNumDevs++;
563 EXIT_ON_ERROR( snd_pcm_info(h, info) , "pcm info" );
565 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
566 snd_pcm_info_get_device(info),
567 snd_pcm_info_get_id(info),
568 snd_pcm_info_get_name(info),
569 snd_pcm_info_get_subdevice(info),
570 snd_pcm_info_get_subdevice_name(info),
571 snd_pcm_info_get_subdevices_avail(info),
572 snd_pcm_info_get_subdevices_count(info),
573 snd_pcm_stream_name(snd_pcm_info_get_stream(info)),
574 (snd_pcm_info_get_subclass(info) == SND_PCM_SUBCLASS_GENERIC_MIX ? "GENERIC MIX": "MULTI MIX"));
576 EXIT_ON_ERROR( snd_pcm_hw_params_any(h, hw_params) , "pcm hw params" );
577 #undef EXIT_ON_ERROR
579 if (TRACE_ON(wave))
580 ALSA_TraceParameters(hw_params, NULL, TRUE);
583 snd_pcm_format_mask_t * fmask;
584 int ratemin = snd_pcm_hw_params_get_rate_min(hw_params, 0);
585 int ratemax = snd_pcm_hw_params_get_rate_max(hw_params, 0);
586 int chmin = snd_pcm_hw_params_get_channels_min(hw_params); \
587 int chmax = snd_pcm_hw_params_get_channels_max(hw_params); \
589 snd_pcm_format_mask_alloca(&fmask);
590 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
592 #define X(r,v) \
593 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
595 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
597 if (chmin <= 1 && 1 <= chmax) \
598 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
599 if (chmin <= 2 && 2 <= chmax) \
600 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
602 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
604 if (chmin <= 1 && 1 <= chmax) \
605 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
606 if (chmin <= 2 && 2 <= chmax) \
607 wwo->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
610 X(11025,1);
611 X(22050,2);
612 X(44100,4);
613 X(48000,48);
614 X(96000,96);
615 #undef X
618 if ( snd_pcm_hw_params_get_channels_min(hw_params) > 1) FIXME("-\n");
619 wwo->caps.wChannels = (snd_pcm_hw_params_get_channels_max(hw_params) >= 2) ? 2 : 1;
620 if (snd_pcm_hw_params_get_channels_min(hw_params) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params))
621 wwo->caps.dwSupport |= WAVECAPS_LRVOLUME;
623 /* FIXME: always true ? */
624 wwo->caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
627 snd_pcm_access_mask_t * acmask;
628 snd_pcm_access_mask_alloca(&acmask);
629 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
631 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
632 if ( snd_pcm_access_mask_test( acmask, SND_PCM_ACCESS_MMAP_INTERLEAVED ) )
633 wwo->caps.dwSupport |= WAVECAPS_DIRECTSOUND;
636 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
637 wwo->caps.dwFormats, wwo->caps.dwSupport);
639 snd_pcm_close(h);
641 ALSA_InitializeVolumeCtl(wwo);
643 wwi = &WInDev[0];
645 /* FIXME: use better values */
646 wwi->device = ALSA_GetDeviceFromReg("RecordDevice");
647 TRACE("using wavein device \"%s\"\n", wwi->device);
649 wwi->caps.wMid = 0x0002;
650 wwi->caps.wPid = 0x0104;
651 strcpy(wwi->caps.szPname, "SB16 Wave In");
652 wwi->caps.vDriverVersion = 0x0100;
653 wwi->caps.dwFormats = 0x00000000;
654 wwi->caps.dwSupport = WAVECAPS_VOLUME;
655 strcpy(wwi->ds_desc.szDesc, "WineALSA DirectSound Driver");
656 strcpy(wwi->ds_desc.szDrvName, "winealsa.drv");
657 wwi->ds_guid = DSDEVID_DefaultPlayback;
659 snd_pcm_info_alloca(&info);
660 snd_pcm_hw_params_alloca(&hw_params);
662 #define EXIT_ON_ERROR(f,txt) do { int err; if ( (err = (f) ) < 0) { ERR(txt ": %s\n", snd_strerror(err)); if (h) snd_pcm_close(h); return -1; } } while(0)
664 ALSA_WidNumDevs = 0;
665 EXIT_ON_ERROR( snd_pcm_open(&h, wwi->device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) , "open pcm" );
666 if (!h) return -1;
667 ALSA_WidNumDevs++;
669 EXIT_ON_ERROR( snd_pcm_info(h, info) , "pcm info" );
671 TRACE("dev=%d id=%s name=%s subdev=%d subdev_name=%s subdev_avail=%d subdev_num=%d stream=%s subclass=%s \n",
672 snd_pcm_info_get_device(info),
673 snd_pcm_info_get_id(info),
674 snd_pcm_info_get_name(info),
675 snd_pcm_info_get_subdevice(info),
676 snd_pcm_info_get_subdevice_name(info),
677 snd_pcm_info_get_subdevices_avail(info),
678 snd_pcm_info_get_subdevices_count(info),
679 snd_pcm_stream_name(snd_pcm_info_get_stream(info)),
680 (snd_pcm_info_get_subclass(info) == SND_PCM_SUBCLASS_GENERIC_MIX ? "GENERIC MIX": "MULTI MIX"));
682 EXIT_ON_ERROR( snd_pcm_hw_params_any(h, hw_params) , "pcm hw params" );
683 #undef EXIT_ON_ERROR
685 if (TRACE_ON(wave))
686 ALSA_TraceParameters(hw_params, NULL, TRUE);
689 snd_pcm_format_mask_t * fmask;
690 int ratemin = snd_pcm_hw_params_get_rate_min(hw_params, 0);
691 int ratemax = snd_pcm_hw_params_get_rate_max(hw_params, 0);
692 int chmin = snd_pcm_hw_params_get_channels_min(hw_params); \
693 int chmax = snd_pcm_hw_params_get_channels_max(hw_params); \
695 snd_pcm_format_mask_alloca(&fmask);
696 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
698 #define X(r,v) \
699 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
701 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
703 if (chmin <= 1 && 1 <= chmax) \
704 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
705 if (chmin <= 2 && 2 <= chmax) \
706 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S08; \
708 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
710 if (chmin <= 1 && 1 <= chmax) \
711 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
712 if (chmin <= 2 && 2 <= chmax) \
713 wwi->caps.dwFormats |= WAVE_FORMAT_##v##S16; \
716 X(11025,1);
717 X(22050,2);
718 X(44100,4);
719 X(48000,48);
720 X(96000,96);
721 #undef X
724 if ( snd_pcm_hw_params_get_channels_min(hw_params) > 1) FIXME("-\n");
725 wwi->caps.wChannels = (snd_pcm_hw_params_get_channels_max(hw_params) >= 2) ? 2 : 1;
726 if (snd_pcm_hw_params_get_channels_min(hw_params) <= 2 && 2 <= snd_pcm_hw_params_get_channels_max(hw_params))
727 wwi->caps.dwSupport |= WAVECAPS_LRVOLUME;
729 /* FIXME: always true ? */
730 wwi->caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
733 snd_pcm_access_mask_t * acmask;
734 snd_pcm_access_mask_alloca(&acmask);
735 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
737 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
738 if ( snd_pcm_access_mask_test( acmask, SND_PCM_ACCESS_MMAP_INTERLEAVED ) )
739 wwi->caps.dwSupport |= WAVECAPS_DIRECTSOUND;
742 TRACE("Configured with dwFmts=%08lx dwSupport=%08lx\n",
743 wwi->caps.dwFormats, wwi->caps.dwSupport);
745 snd_pcm_close(h);
747 return 0;
750 /******************************************************************
751 * ALSA_InitRingMessage
753 * Initialize the ring of messages for passing between driver's caller and playback/record
754 * thread
756 static int ALSA_InitRingMessage(ALSA_MSG_RING* omr)
758 omr->msg_toget = 0;
759 omr->msg_tosave = 0;
760 #ifdef USE_PIPE_SYNC
761 if (pipe(omr->msg_pipe) < 0) {
762 omr->msg_pipe[0] = -1;
763 omr->msg_pipe[1] = -1;
764 ERR("could not create pipe, error=%s\n", strerror(errno));
766 #else
767 omr->msg_event = CreateEventA(NULL, FALSE, FALSE, NULL);
768 #endif
769 omr->ring_buffer_size = ALSA_RING_BUFFER_INCREMENT;
770 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(ALSA_MSG));
772 InitializeCriticalSection(&omr->msg_crst);
773 return 0;
776 /******************************************************************
777 * ALSA_DestroyRingMessage
780 static int ALSA_DestroyRingMessage(ALSA_MSG_RING* omr)
782 #ifdef USE_PIPE_SYNC
783 close(omr->msg_pipe[0]);
784 close(omr->msg_pipe[1]);
785 #else
786 CloseHandle(omr->msg_event);
787 #endif
788 HeapFree(GetProcessHeap(),0,omr->messages);
789 DeleteCriticalSection(&omr->msg_crst);
790 return 0;
793 /******************************************************************
794 * ALSA_AddRingMessage
796 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
798 static int ALSA_AddRingMessage(ALSA_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
800 HANDLE hEvent = INVALID_HANDLE_VALUE;
802 EnterCriticalSection(&omr->msg_crst);
803 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
805 int old_ring_buffer_size = omr->ring_buffer_size;
806 omr->ring_buffer_size += ALSA_RING_BUFFER_INCREMENT;
807 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
808 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(ALSA_MSG));
809 /* Now we need to rearrange the ring buffer so that the new
810 buffers just allocated are in between omr->msg_tosave and
811 omr->msg_toget.
813 if (omr->msg_tosave < omr->msg_toget)
815 memmove(&(omr->messages[omr->msg_toget + ALSA_RING_BUFFER_INCREMENT]),
816 &(omr->messages[omr->msg_toget]),
817 sizeof(ALSA_MSG)*(old_ring_buffer_size - omr->msg_toget)
819 omr->msg_toget += ALSA_RING_BUFFER_INCREMENT;
822 if (wait)
824 hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
825 if (hEvent == INVALID_HANDLE_VALUE)
827 ERR("can't create event !?\n");
828 LeaveCriticalSection(&omr->msg_crst);
829 return 0;
831 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
832 FIXME("two fast messages in the queue!!!!\n");
834 /* fast messages have to be added at the start of the queue */
835 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
837 omr->messages[omr->msg_toget].msg = msg;
838 omr->messages[omr->msg_toget].param = param;
839 omr->messages[omr->msg_toget].hEvent = hEvent;
841 else
843 omr->messages[omr->msg_tosave].msg = msg;
844 omr->messages[omr->msg_tosave].param = param;
845 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
846 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
848 LeaveCriticalSection(&omr->msg_crst);
849 /* signal a new message */
850 SIGNAL_OMR(omr);
851 if (wait)
853 /* wait for playback/record thread to have processed the message */
854 WaitForSingleObject(hEvent, INFINITE);
855 CloseHandle(hEvent);
857 return 1;
860 /******************************************************************
861 * ALSA_RetrieveRingMessage
863 * Get a message from the ring. Should be called by the playback/record thread.
865 static int ALSA_RetrieveRingMessage(ALSA_MSG_RING* omr,
866 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
868 EnterCriticalSection(&omr->msg_crst);
870 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
872 LeaveCriticalSection(&omr->msg_crst);
873 return 0;
876 *msg = omr->messages[omr->msg_toget].msg;
877 omr->messages[omr->msg_toget].msg = 0;
878 *param = omr->messages[omr->msg_toget].param;
879 *hEvent = omr->messages[omr->msg_toget].hEvent;
880 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
881 CLEAR_OMR(omr);
882 LeaveCriticalSection(&omr->msg_crst);
883 return 1;
886 /******************************************************************
887 * ALSA_PeekRingMessage
889 * Peek at a message from the ring but do not remove it.
890 * Should be called by the playback/record thread.
892 static int ALSA_PeekRingMessage(ALSA_MSG_RING* omr,
893 enum win_wm_message *msg,
894 DWORD *param, HANDLE *hEvent)
896 EnterCriticalSection(&omr->msg_crst);
898 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
900 LeaveCriticalSection(&omr->msg_crst);
901 return 0;
904 *msg = omr->messages[omr->msg_toget].msg;
905 *param = omr->messages[omr->msg_toget].param;
906 *hEvent = omr->messages[omr->msg_toget].hEvent;
907 LeaveCriticalSection(&omr->msg_crst);
908 return 1;
911 /*======================================================================*
912 * Low level WAVE OUT implementation *
913 *======================================================================*/
915 /**************************************************************************
916 * wodNotifyClient [internal]
918 static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
920 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
922 switch (wMsg) {
923 case WOM_OPEN:
924 case WOM_CLOSE:
925 case WOM_DONE:
926 if (wwo->wFlags != DCB_NULL &&
927 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
928 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
929 WARN("can't notify client !\n");
930 return MMSYSERR_ERROR;
932 break;
933 default:
934 FIXME("Unknown callback message %u\n", wMsg);
935 return MMSYSERR_INVALPARAM;
937 return MMSYSERR_NOERROR;
940 /**************************************************************************
941 * wodUpdatePlayedTotal [internal]
944 static BOOL wodUpdatePlayedTotal(WINE_WAVEOUT* wwo, snd_pcm_status_t* ps)
946 snd_pcm_sframes_t delay = 0;
947 snd_pcm_delay(wwo->p_handle, &delay);
948 wwo->dwPlayedTotal = wwo->dwWrittenTotal - delay;
949 return TRUE;
952 /**************************************************************************
953 * wodPlayer_BeginWaveHdr [internal]
955 * Makes the specified lpWaveHdr the currently playing wave header.
956 * If the specified wave header is a begin loop and we're not already in
957 * a loop, setup the loop.
959 static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
961 wwo->lpPlayPtr = lpWaveHdr;
963 if (!lpWaveHdr) return;
965 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
966 if (wwo->lpLoopPtr) {
967 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
968 } else {
969 TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
970 wwo->lpLoopPtr = lpWaveHdr;
971 /* Windows does not touch WAVEHDR.dwLoops,
972 * so we need to make an internal copy */
973 wwo->dwLoops = lpWaveHdr->dwLoops;
976 wwo->dwPartialOffset = 0;
979 /**************************************************************************
980 * wodPlayer_PlayPtrNext [internal]
982 * Advance the play pointer to the next waveheader, looping if required.
984 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
986 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
988 wwo->dwPartialOffset = 0;
989 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
990 /* We're at the end of a loop, loop if required */
991 if (--wwo->dwLoops > 0) {
992 wwo->lpPlayPtr = wwo->lpLoopPtr;
993 } else {
994 /* Handle overlapping loops correctly */
995 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
996 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
997 /* shall we consider the END flag for the closing loop or for
998 * the opening one or for both ???
999 * code assumes for closing loop only
1001 } else {
1002 lpWaveHdr = lpWaveHdr->lpNext;
1004 wwo->lpLoopPtr = NULL;
1005 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1007 } else {
1008 /* We're not in a loop. Advance to the next wave header */
1009 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1012 return lpWaveHdr;
1015 /**************************************************************************
1016 * wodPlayer_DSPWait [internal]
1017 * Returns the number of milliseconds to wait for the DSP buffer to play a
1018 * period
1020 static DWORD wodPlayer_DSPWait(const WINE_WAVEOUT *wwo)
1022 /* time for one period to be played */
1023 return snd_pcm_hw_params_get_period_time(wwo->hw_params, 0) / 1000;
1026 /**************************************************************************
1027 * wodPlayer_NotifyWait [internal]
1028 * Returns the number of milliseconds to wait before attempting to notify
1029 * completion of the specified wavehdr.
1030 * This is based on the number of bytes remaining to be written in the
1031 * wave.
1033 static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
1035 DWORD dwMillis;
1037 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1038 dwMillis = 1;
1039 } else {
1040 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.wf.nAvgBytesPerSec;
1041 if (!dwMillis) dwMillis = 1;
1044 return dwMillis;
1048 /**************************************************************************
1049 * wodPlayer_WriteMaxFrags [internal]
1050 * Writes the maximum number of frames possible to the DSP and returns
1051 * the number of frames written.
1053 static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo, DWORD* frames)
1055 /* Only attempt to write to free frames */
1056 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1057 DWORD dwLength = snd_pcm_bytes_to_frames(wwo->p_handle, lpWaveHdr->dwBufferLength - wwo->dwPartialOffset);
1058 int toWrite = min(dwLength, *frames);
1059 int written;
1061 TRACE("Writing wavehdr %p.%lu[%lu]\n", lpWaveHdr, wwo->dwPartialOffset, lpWaveHdr->dwBufferLength);
1063 written = (wwo->write)(wwo->p_handle, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
1064 if ( written < 0)
1066 /* XRUN occurred. let's try to recover */
1067 ALSA_XRUNRecovery(wwo, written);
1068 written = (wwo->write)(wwo->p_handle, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
1070 if (written <= 0)
1072 /* still in error */
1073 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written));
1074 return written;
1077 wwo->dwPartialOffset += snd_pcm_frames_to_bytes(wwo->p_handle, written);
1078 if ( wwo->dwPartialOffset >= lpWaveHdr->dwBufferLength) {
1079 /* this will be used to check if the given wave header has been fully played or not... */
1080 wwo->dwPartialOffset = lpWaveHdr->dwBufferLength;
1081 /* If we wrote all current wavehdr, skip to the next one */
1082 wodPlayer_PlayPtrNext(wwo);
1084 *frames -= written;
1085 wwo->dwWrittenTotal += snd_pcm_frames_to_bytes(wwo->p_handle, written);
1087 return written;
1091 /**************************************************************************
1092 * wodPlayer_NotifyCompletions [internal]
1094 * Notifies and remove from queue all wavehdrs which have been played to
1095 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
1096 * we notify all wavehdrs and remove them all from the queue even if they
1097 * are unplayed or part of a loop.
1099 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
1101 LPWAVEHDR lpWaveHdr;
1103 /* Start from lpQueuePtr and keep notifying until:
1104 * - we hit an unwritten wavehdr
1105 * - we hit the beginning of a running loop
1106 * - we hit a wavehdr which hasn't finished playing
1108 #if 0
1109 while ((lpWaveHdr = wwo->lpQueuePtr) &&
1110 (force ||
1111 (lpWaveHdr != wwo->lpPlayPtr &&
1112 lpWaveHdr != wwo->lpLoopPtr &&
1113 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
1115 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1117 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1118 lpWaveHdr->dwFlags |= WHDR_DONE;
1120 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1122 #else
1123 for (;;)
1125 lpWaveHdr = wwo->lpQueuePtr;
1126 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
1127 if (!force)
1129 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
1130 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
1131 if (lpWaveHdr->reserved > wwo->dwPlayedTotal){TRACE("still playing %p (%lu/%lu)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
1133 wwo->lpQueuePtr = lpWaveHdr->lpNext;
1135 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1136 lpWaveHdr->dwFlags |= WHDR_DONE;
1138 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
1140 #endif
1141 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
1142 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
1146 void wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count)
1148 unsigned short revents;
1150 if (snd_pcm_state(handle) != SND_PCM_STATE_RUNNING)
1151 return;
1153 while (1) {
1154 poll(ufds, count, -1);
1155 snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);
1157 if (revents & POLLERR)
1158 return;
1160 /*if (revents & POLLOUT)
1161 return 0;*/
1166 /**************************************************************************
1167 * wodPlayer_Reset [internal]
1169 * wodPlayer helper. Resets current output stream.
1171 static void wodPlayer_Reset(WINE_WAVEOUT* wwo)
1173 enum win_wm_message msg;
1174 DWORD param;
1175 HANDLE ev;
1176 int err;
1178 /* flush all possible output */
1179 wait_for_poll(wwo->p_handle, wwo->ufds, wwo->count);
1181 wodUpdatePlayedTotal(wwo, NULL);
1182 /* updates current notify list */
1183 wodPlayer_NotifyCompletions(wwo, FALSE);
1185 if ( (err = snd_pcm_drop(wwo->p_handle)) < 0) {
1186 FIXME("flush: %s\n", snd_strerror(err));
1187 wwo->hThread = 0;
1188 wwo->state = WINE_WS_STOPPED;
1189 ExitThread(-1);
1191 if ( (err = snd_pcm_prepare(wwo->p_handle)) < 0 )
1192 ERR("pcm prepare failed: %s\n", snd_strerror(err));
1194 /* remove any buffer */
1195 wodPlayer_NotifyCompletions(wwo, TRUE);
1197 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
1198 wwo->state = WINE_WS_STOPPED;
1199 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
1200 /* Clear partial wavehdr */
1201 wwo->dwPartialOffset = 0;
1203 /* remove any existing message in the ring */
1204 EnterCriticalSection(&wwo->msgRing.msg_crst);
1205 /* return all pending headers in queue */
1206 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
1208 if (msg != WINE_WM_HEADER)
1210 FIXME("shouldn't have headers left\n");
1211 SetEvent(ev);
1212 continue;
1214 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
1215 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
1217 wodNotifyClient(wwo, WOM_DONE, param, 0);
1219 RESET_OMR(&wwo->msgRing);
1220 LeaveCriticalSection(&wwo->msgRing.msg_crst);
1223 /**************************************************************************
1224 * wodPlayer_ProcessMessages [internal]
1226 static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
1228 LPWAVEHDR lpWaveHdr;
1229 enum win_wm_message msg;
1230 DWORD param;
1231 HANDLE ev;
1232 int err;
1234 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
1235 TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
1237 switch (msg) {
1238 case WINE_WM_PAUSING:
1239 if ( snd_pcm_state(wwo->p_handle) == SND_PCM_STATE_RUNNING )
1241 err = snd_pcm_pause(wwo->p_handle, 1);
1242 if ( err < 0 )
1243 ERR("pcm_pause failed: %s\n", snd_strerror(err));
1245 wwo->state = WINE_WS_PAUSED;
1246 SetEvent(ev);
1247 break;
1248 case WINE_WM_RESTARTING:
1249 if (wwo->state == WINE_WS_PAUSED)
1251 if ( snd_pcm_state(wwo->p_handle) == SND_PCM_STATE_PAUSED )
1253 err = snd_pcm_pause(wwo->p_handle, 0);
1254 if ( err < 0 )
1255 ERR("pcm_pause failed: %s\n", snd_strerror(err));
1257 wwo->state = WINE_WS_PLAYING;
1259 SetEvent(ev);
1260 break;
1261 case WINE_WM_HEADER:
1262 lpWaveHdr = (LPWAVEHDR)param;
1264 /* insert buffer at the end of queue */
1266 LPWAVEHDR* wh;
1267 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1268 *wh = lpWaveHdr;
1270 if (!wwo->lpPlayPtr)
1271 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
1272 if (wwo->state == WINE_WS_STOPPED)
1273 wwo->state = WINE_WS_PLAYING;
1274 break;
1275 case WINE_WM_RESETTING:
1276 wodPlayer_Reset(wwo);
1277 SetEvent(ev);
1278 break;
1279 case WINE_WM_UPDATE:
1280 wodUpdatePlayedTotal(wwo, NULL);
1281 SetEvent(ev);
1282 break;
1283 case WINE_WM_BREAKLOOP:
1284 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
1285 /* ensure exit at end of current loop */
1286 wwo->dwLoops = 1;
1288 SetEvent(ev);
1289 break;
1290 case WINE_WM_CLOSING:
1291 /* sanity check: this should not happen since the device must have been reset before */
1292 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
1293 wwo->hThread = 0;
1294 wwo->state = WINE_WS_CLOSED;
1295 SetEvent(ev);
1296 ExitThread(0);
1297 /* shouldn't go here */
1298 default:
1299 FIXME("unknown message %d\n", msg);
1300 break;
1305 /**************************************************************************
1306 * wodPlayer_FeedDSP [internal]
1307 * Feed as much sound data as we can into the DSP and return the number of
1308 * milliseconds before it will be necessary to feed the DSP again.
1310 static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
1312 DWORD availInQ;
1314 wodUpdatePlayedTotal(wwo, NULL);
1315 availInQ = snd_pcm_avail_update(wwo->p_handle);
1317 #if 0
1318 /* input queue empty and output buffer with less than one fragment to play */
1319 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + wwo->dwFragmentSize) {
1320 TRACE("Run out of wavehdr:s...\n");
1321 return INFINITE;
1323 #endif
1325 /* no more room... no need to try to feed */
1326 if (availInQ > 0) {
1327 /* Feed from partial wavehdr */
1328 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
1329 wodPlayer_WriteMaxFrags(wwo, &availInQ);
1332 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1333 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
1334 do {
1335 TRACE("Setting time to elapse for %p to %lu\n",
1336 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
1337 /* note the value that dwPlayedTotal will return when this wave finishes playing */
1338 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
1339 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
1343 return wodPlayer_DSPWait(wwo);
1346 /**************************************************************************
1347 * wodPlayer [internal]
1349 static DWORD CALLBACK wodPlayer(LPVOID pmt)
1351 WORD uDevID = (DWORD)pmt;
1352 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
1353 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
1354 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
1355 DWORD dwSleepTime;
1357 wwo->state = WINE_WS_STOPPED;
1358 SetEvent(wwo->hStartUpEvent);
1360 for (;;) {
1361 /** Wait for the shortest time before an action is required. If there
1362 * are no pending actions, wait forever for a command.
1364 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1365 TRACE("waiting %lums (%lu,%lu)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1366 WAIT_OMR(&wwo->msgRing, dwSleepTime);
1367 wodPlayer_ProcessMessages(wwo);
1368 if (wwo->state == WINE_WS_PLAYING) {
1369 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1370 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
1371 if (dwNextFeedTime == INFINITE) {
1372 /* FeedDSP ran out of data, but before giving up, */
1373 /* check that a notification didn't give us more */
1374 wodPlayer_ProcessMessages(wwo);
1375 if (wwo->lpPlayPtr) {
1376 TRACE("recovering\n");
1377 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
1380 } else {
1381 dwNextFeedTime = dwNextNotifyTime = INFINITE;
1386 /**************************************************************************
1387 * wodGetDevCaps [internal]
1389 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
1391 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1393 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1395 if (wDevID >= MAX_WAVEOUTDRV) {
1396 TRACE("MAX_WAVOUTDRV reached !\n");
1397 return MMSYSERR_BADDEVICEID;
1400 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1401 return MMSYSERR_NOERROR;
1404 /**************************************************************************
1405 * wodOpen [internal]
1407 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1409 WINE_WAVEOUT* wwo;
1410 snd_pcm_hw_params_t * hw_params;
1411 snd_pcm_sw_params_t * sw_params;
1412 snd_pcm_access_t access;
1413 snd_pcm_format_t format;
1414 int rate;
1415 unsigned int buffer_time = 500000;
1416 unsigned int period_time = 10000;
1417 int buffer_size;
1418 snd_pcm_uframes_t period_size;
1419 int flags;
1420 snd_pcm_t * pcm;
1421 int err;
1423 snd_pcm_hw_params_alloca(&hw_params);
1424 snd_pcm_sw_params_alloca(&sw_params);
1426 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1427 if (lpDesc == NULL) {
1428 WARN("Invalid Parameter !\n");
1429 return MMSYSERR_INVALPARAM;
1431 if (wDevID >= MAX_WAVEOUTDRV) {
1432 TRACE("MAX_WAVOUTDRV reached !\n");
1433 return MMSYSERR_BADDEVICEID;
1436 /* only PCM format is supported so far... */
1437 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1438 lpDesc->lpFormat->nChannels == 0 ||
1439 lpDesc->lpFormat->nSamplesPerSec == 0) {
1440 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1441 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1442 lpDesc->lpFormat->nSamplesPerSec);
1443 return WAVERR_BADFORMAT;
1446 if (dwFlags & WAVE_FORMAT_QUERY) {
1447 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1448 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1449 lpDesc->lpFormat->nSamplesPerSec);
1450 return MMSYSERR_NOERROR;
1453 wwo = &WOutDev[wDevID];
1455 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->caps.dwSupport & WAVECAPS_DIRECTSOUND))
1456 /* not supported, ignore it */
1457 dwFlags &= ~WAVE_DIRECTSOUND;
1459 wwo->p_handle = 0;
1460 flags = SND_PCM_NONBLOCK;
1461 if ( dwFlags & WAVE_DIRECTSOUND )
1462 flags |= SND_PCM_ASYNC;
1464 if ( (err = snd_pcm_open(&pcm, wwo->device, SND_PCM_STREAM_PLAYBACK, flags)) < 0)
1466 ERR("Error open: %s\n", snd_strerror(err));
1467 return MMSYSERR_NOTENABLED;
1470 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1472 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1473 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1475 if (wwo->format.wBitsPerSample == 0) {
1476 WARN("Resetting zeroed wBitsPerSample\n");
1477 wwo->format.wBitsPerSample = 8 *
1478 (wwo->format.wf.nAvgBytesPerSec /
1479 wwo->format.wf.nSamplesPerSec) /
1480 wwo->format.wf.nChannels;
1483 snd_pcm_hw_params_any(pcm, hw_params);
1485 #define EXIT_ON_ERROR(f,e,txt) do \
1487 int err; \
1488 if ( (err = (f) ) < 0) \
1490 ERR(txt ": %s\n", snd_strerror(err)); \
1491 snd_pcm_close(pcm); \
1492 return e; \
1494 } while(0)
1496 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
1497 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
1498 WARN("mmap not available. switching to standard write.\n");
1499 access = SND_PCM_ACCESS_RW_INTERLEAVED;
1500 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
1501 wwo->write = snd_pcm_writei;
1503 else
1504 wwo->write = snd_pcm_mmap_writei;
1506 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.wf.nChannels), MMSYSERR_INVALPARAM, "unable to set required channels");
1508 format = (wwo->format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_U8;
1509 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), MMSYSERR_INVALPARAM, "unable to set required format");
1511 rate = snd_pcm_hw_params_set_rate_near(pcm, hw_params, wwo->format.wf.nSamplesPerSec, 0);
1512 if (rate < 0) {
1513 ERR("Rate %ld Hz not available for playback: %s\n", wwo->format.wf.nSamplesPerSec, snd_strerror(rate));
1514 snd_pcm_close(pcm);
1515 return WAVERR_BADFORMAT;
1517 if (rate != wwo->format.wf.nSamplesPerSec) {
1518 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwo->format.wf.nSamplesPerSec, rate);
1519 snd_pcm_close(pcm);
1520 return WAVERR_BADFORMAT;
1523 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, buffer_time, 0), MMSYSERR_INVALPARAM, "unable to set buffer time");
1524 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, period_time, 0), MMSYSERR_INVALPARAM, "unable to set period time");
1526 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
1528 period_size = snd_pcm_hw_params_get_period_size(hw_params, 0);
1529 buffer_size = snd_pcm_hw_params_get_buffer_size(hw_params);
1531 snd_pcm_sw_params_current(pcm, sw_params);
1532 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, dwFlags & WAVE_DIRECTSOUND ? INT_MAX : 1 ), MMSYSERR_ERROR, "unable to set start threshold");
1533 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
1534 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
1535 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
1536 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
1537 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
1538 #undef EXIT_ON_ERROR
1540 snd_pcm_prepare(pcm);
1542 if (TRACE_ON(wave))
1543 ALSA_TraceParameters(hw_params, sw_params, FALSE);
1545 /* now, we can save all required data for later use... */
1546 if ( wwo->hw_params )
1547 snd_pcm_hw_params_free(wwo->hw_params);
1548 snd_pcm_hw_params_malloc(&(wwo->hw_params));
1549 snd_pcm_hw_params_copy(wwo->hw_params, hw_params);
1551 wwo->dwBufferSize = buffer_size;
1552 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
1553 wwo->p_handle = pcm;
1555 ALSA_InitRingMessage(&wwo->msgRing);
1557 wwo->count = snd_pcm_poll_descriptors_count (wwo->p_handle);
1558 if (wwo->count <= 0) {
1559 ERR("Invalid poll descriptors count\n");
1560 return MMSYSERR_ERROR;
1563 wwo->ufds = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct pollfd) * wwo->count);
1564 if (wwo->ufds == NULL) {
1565 ERR("No enough memory\n");
1566 return MMSYSERR_NOMEM;
1568 if ((err = snd_pcm_poll_descriptors(wwo->p_handle, wwo->ufds, wwo->count)) < 0) {
1569 ERR("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err));
1570 return MMSYSERR_ERROR;
1573 if (!(dwFlags & WAVE_DIRECTSOUND)) {
1574 wwo->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
1575 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
1576 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
1577 CloseHandle(wwo->hStartUpEvent);
1578 } else {
1579 wwo->hThread = INVALID_HANDLE_VALUE;
1580 wwo->dwThreadID = 0;
1582 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
1584 TRACE("handle=%08lx \n", (DWORD)wwo->p_handle);
1585 /* if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
1586 ERR("Fragment doesn't contain an integral number of data blocks\n");
1588 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1589 wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
1590 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1591 wwo->format.wf.nBlockAlign);
1593 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
1597 /**************************************************************************
1598 * wodClose [internal]
1600 static DWORD wodClose(WORD wDevID)
1602 DWORD ret = MMSYSERR_NOERROR;
1603 WINE_WAVEOUT* wwo;
1605 TRACE("(%u);\n", wDevID);
1607 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1608 WARN("bad device ID !\n");
1609 return MMSYSERR_BADDEVICEID;
1612 wwo = &WOutDev[wDevID];
1613 if (wwo->lpQueuePtr) {
1614 WARN("buffers still playing !\n");
1615 ret = WAVERR_STILLPLAYING;
1616 } else {
1617 if (wwo->hThread != INVALID_HANDLE_VALUE) {
1618 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
1620 ALSA_DestroyRingMessage(&wwo->msgRing);
1622 snd_pcm_hw_params_free(wwo->hw_params);
1623 wwo->hw_params = NULL;
1625 snd_pcm_close(wwo->p_handle);
1626 wwo->p_handle = NULL;
1628 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
1631 HeapFree(GetProcessHeap(), 0, wwo->ufds);
1632 return ret;
1636 /**************************************************************************
1637 * wodWrite [internal]
1640 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1642 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1644 /* first, do the sanity checks... */
1645 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1646 WARN("bad dev ID !\n");
1647 return MMSYSERR_BADDEVICEID;
1650 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
1651 return WAVERR_UNPREPARED;
1653 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1654 return WAVERR_STILLPLAYING;
1656 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1657 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
1658 lpWaveHdr->lpNext = 0;
1660 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
1662 return MMSYSERR_NOERROR;
1665 /**************************************************************************
1666 * wodPrepare [internal]
1668 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1670 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1672 if (wDevID >= MAX_WAVEOUTDRV) {
1673 WARN("bad device ID !\n");
1674 return MMSYSERR_BADDEVICEID;
1677 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1678 return WAVERR_STILLPLAYING;
1680 lpWaveHdr->dwFlags |= WHDR_PREPARED;
1681 lpWaveHdr->dwFlags &= ~WHDR_DONE;
1682 return MMSYSERR_NOERROR;
1685 /**************************************************************************
1686 * wodUnprepare [internal]
1688 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1690 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1692 if (wDevID >= MAX_WAVEOUTDRV) {
1693 WARN("bad device ID !\n");
1694 return MMSYSERR_BADDEVICEID;
1697 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1698 return WAVERR_STILLPLAYING;
1700 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
1701 lpWaveHdr->dwFlags |= WHDR_DONE;
1703 return MMSYSERR_NOERROR;
1706 /**************************************************************************
1707 * wodPause [internal]
1709 static DWORD wodPause(WORD wDevID)
1711 TRACE("(%u);!\n", wDevID);
1713 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1714 WARN("bad device ID !\n");
1715 return MMSYSERR_BADDEVICEID;
1718 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
1720 return MMSYSERR_NOERROR;
1723 /**************************************************************************
1724 * wodRestart [internal]
1726 static DWORD wodRestart(WORD wDevID)
1728 TRACE("(%u);\n", wDevID);
1730 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1731 WARN("bad device ID !\n");
1732 return MMSYSERR_BADDEVICEID;
1735 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1736 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
1739 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1740 /* FIXME: Myst crashes with this ... hmm -MM
1741 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
1744 return MMSYSERR_NOERROR;
1747 /**************************************************************************
1748 * wodReset [internal]
1750 static DWORD wodReset(WORD wDevID)
1752 TRACE("(%u);\n", wDevID);
1754 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1755 WARN("bad device ID !\n");
1756 return MMSYSERR_BADDEVICEID;
1759 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1761 return MMSYSERR_NOERROR;
1764 /**************************************************************************
1765 * wodGetPosition [internal]
1767 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1769 int time;
1770 DWORD val;
1771 WINE_WAVEOUT* wwo;
1773 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1775 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1776 WARN("bad device ID !\n");
1777 return MMSYSERR_BADDEVICEID;
1780 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1782 wwo = &WOutDev[wDevID];
1783 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
1784 val = wwo->dwPlayedTotal;
1786 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
1787 lpTime->wType, wwo->format.wBitsPerSample,
1788 wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
1789 wwo->format.wf.nAvgBytesPerSec);
1790 TRACE("dwPlayedTotal=%lu\n", val);
1792 switch (lpTime->wType) {
1793 case TIME_BYTES:
1794 lpTime->u.cb = val;
1795 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1796 break;
1797 case TIME_SAMPLES:
1798 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample /wwo->format.wf.nChannels;
1799 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
1800 break;
1801 case TIME_SMPTE:
1802 time = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1803 lpTime->u.smpte.hour = time / (60 * 60 * 1000);
1804 time -= lpTime->u.smpte.hour * (60 * 60 * 1000);
1805 lpTime->u.smpte.min = time / (60 * 1000);
1806 time -= lpTime->u.smpte.min * (60 * 1000);
1807 lpTime->u.smpte.sec = time / 1000;
1808 time -= lpTime->u.smpte.sec * 1000;
1809 lpTime->u.smpte.frame = time * 30 / 1000;
1810 lpTime->u.smpte.fps = 30;
1811 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
1812 lpTime->u.smpte.hour, lpTime->u.smpte.min,
1813 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
1814 break;
1815 default:
1816 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1817 lpTime->wType = TIME_MS;
1818 case TIME_MS:
1819 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1820 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1821 break;
1823 return MMSYSERR_NOERROR;
1826 /**************************************************************************
1827 * wodBreakLoop [internal]
1829 static DWORD wodBreakLoop(WORD wDevID)
1831 TRACE("(%u);\n", wDevID);
1833 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1834 WARN("bad device ID !\n");
1835 return MMSYSERR_BADDEVICEID;
1837 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1838 return MMSYSERR_NOERROR;
1841 /**************************************************************************
1842 * wodGetVolume [internal]
1844 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1846 WORD left, right;
1847 WINE_WAVEOUT* wwo;
1848 int count;
1849 long min, max;
1851 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1852 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1853 WARN("bad device ID !\n");
1854 return MMSYSERR_BADDEVICEID;
1856 wwo = &WOutDev[wDevID];
1857 count = snd_ctl_elem_info_get_count(wwo->playback_einfo);
1858 min = snd_ctl_elem_info_get_min(wwo->playback_einfo);
1859 max = snd_ctl_elem_info_get_max(wwo->playback_einfo);
1861 #define VOLUME_ALSA_TO_WIN(x) (((x)-min) * 65536 /(max-min))
1862 if (lpdwVol == NULL)
1863 return MMSYSERR_NOTENABLED;
1865 switch (count)
1867 case 2:
1868 left = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 0));
1869 right = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 1));
1870 break;
1871 case 1:
1872 left = right = VOLUME_ALSA_TO_WIN(snd_ctl_elem_value_get_integer(wwo->playback_evalue, 0));
1873 break;
1874 default:
1875 WARN("%d channels mixer not supported\n", count);
1876 return MMSYSERR_NOERROR;
1878 #undef VOLUME_ALSA_TO_WIN
1880 TRACE("left=%d right=%d !\n", left, right);
1881 *lpdwVol = MAKELONG( left, right );
1882 return MMSYSERR_NOERROR;
1885 /**************************************************************************
1886 * wodSetVolume [internal]
1888 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1890 WORD left, right;
1891 WINE_WAVEOUT* wwo;
1892 int count, err;
1893 long min, max;
1895 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1896 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].p_handle == NULL) {
1897 WARN("bad device ID !\n");
1898 return MMSYSERR_BADDEVICEID;
1900 wwo = &WOutDev[wDevID];
1901 count=snd_ctl_elem_info_get_count(wwo->playback_einfo);
1902 min = snd_ctl_elem_info_get_min(wwo->playback_einfo);
1903 max = snd_ctl_elem_info_get_max(wwo->playback_einfo);
1905 left = LOWORD(dwParam);
1906 right = HIWORD(dwParam);
1908 #define VOLUME_WIN_TO_ALSA(x) ( (((x) * (max-min)) / 65536) + min )
1909 switch (count)
1911 case 2:
1912 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 0, VOLUME_WIN_TO_ALSA(left));
1913 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 1, VOLUME_WIN_TO_ALSA(right));
1914 break;
1915 case 1:
1916 snd_ctl_elem_value_set_integer(wwo->playback_evalue, 0, VOLUME_WIN_TO_ALSA(left));
1917 break;
1918 default:
1919 WARN("%d channels mixer not supported\n", count);
1921 #undef VOLUME_WIN_TO_ALSA
1922 if ( (err = snd_ctl_elem_write(wwo->ctl, wwo->playback_evalue)) < 0)
1924 ERR("error writing snd_ctl_elem_value: %s\n", snd_strerror(err));
1926 return MMSYSERR_NOERROR;
1929 /**************************************************************************
1930 * wodGetNumDevs [internal]
1932 static DWORD wodGetNumDevs(void)
1934 return ALSA_WodNumDevs;
1937 /**************************************************************************
1938 * wodDevInterfaceSize [internal]
1940 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1942 TRACE("(%u, %p)\n", wDevID, dwParam1);
1944 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].device, -1,
1945 NULL, 0 ) * sizeof(WCHAR);
1946 return MMSYSERR_NOERROR;
1949 /**************************************************************************
1950 * wodDevInterface [internal]
1952 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1954 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].device, -1,
1955 NULL, 0 ) * sizeof(WCHAR))
1957 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].device, -1,
1958 dwParam1, dwParam2 / sizeof(WCHAR));
1959 return MMSYSERR_NOERROR;
1961 return MMSYSERR_INVALPARAM;
1964 /**************************************************************************
1965 * wodMessage (WINEALSA.@)
1967 DWORD WINAPI ALSA_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1968 DWORD dwParam1, DWORD dwParam2)
1970 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1971 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1973 switch (wMsg) {
1974 case DRVM_INIT:
1975 case DRVM_EXIT:
1976 case DRVM_ENABLE:
1977 case DRVM_DISABLE:
1978 /* FIXME: Pretend this is supported */
1979 return 0;
1980 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1981 case WODM_CLOSE: return wodClose (wDevID);
1982 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
1983 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1984 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1985 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1986 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1987 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1988 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1989 case WODM_PAUSE: return wodPause (wDevID);
1990 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1991 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1992 case WODM_PREPARE: return wodPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1993 case WODM_UNPREPARE: return wodUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1994 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1995 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1996 case WODM_RESTART: return wodRestart (wDevID);
1997 case WODM_RESET: return wodReset (wDevID);
1998 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1999 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
2000 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
2001 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
2002 case DRV_QUERYDSOUNDGUID: return wodDsGuid (wDevID, (LPGUID)dwParam1);
2004 default:
2005 FIXME("unknown message %d!\n", wMsg);
2007 return MMSYSERR_NOTSUPPORTED;
2010 /*======================================================================*
2011 * Low level DSOUND implementation *
2012 *======================================================================*/
2014 typedef struct IDsDriverImpl IDsDriverImpl;
2015 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
2017 struct IDsDriverImpl
2019 /* IUnknown fields */
2020 ICOM_VFIELD(IDsDriver);
2021 DWORD ref;
2022 /* IDsDriverImpl fields */
2023 UINT wDevID;
2024 IDsDriverBufferImpl*primary;
2027 struct IDsDriverBufferImpl
2029 /* IUnknown fields */
2030 ICOM_VFIELD(IDsDriverBuffer);
2031 DWORD ref;
2032 /* IDsDriverBufferImpl fields */
2033 IDsDriverImpl* drv;
2035 CRITICAL_SECTION mmap_crst;
2036 LPVOID mmap_buffer;
2037 DWORD mmap_buflen_bytes;
2038 snd_pcm_uframes_t mmap_buflen_frames;
2039 snd_pcm_channel_area_t * mmap_areas;
2040 snd_async_handler_t * mmap_async_handler;
2043 static void DSDB_CheckXRUN(IDsDriverBufferImpl* pdbi)
2045 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
2046 snd_pcm_state_t state = snd_pcm_state(wwo->p_handle);
2048 if ( state == SND_PCM_STATE_XRUN )
2050 int err = snd_pcm_prepare(wwo->p_handle);
2051 TRACE("xrun occurred\n");
2052 if ( err < 0 )
2053 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
2055 else if ( state == SND_PCM_STATE_SUSPENDED )
2057 int err = snd_pcm_resume(wwo->p_handle);
2058 TRACE("recovery from suspension occurred\n");
2059 if (err < 0 && err != -EAGAIN){
2060 err = snd_pcm_prepare(wwo->p_handle);
2061 if (err < 0)
2062 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
2067 static void DSDB_MMAPCopy(IDsDriverBufferImpl* pdbi)
2069 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
2070 int channels;
2071 snd_pcm_format_t format;
2072 snd_pcm_uframes_t period_size;
2073 snd_pcm_sframes_t avail;
2075 if ( !pdbi->mmap_buffer || !wwo->hw_params || !wwo->p_handle)
2076 return;
2078 channels = snd_pcm_hw_params_get_channels(wwo->hw_params);
2079 format = snd_pcm_hw_params_get_format(wwo->hw_params);
2080 period_size = snd_pcm_hw_params_get_period_size(wwo->hw_params, 0);
2081 avail = snd_pcm_avail_update(wwo->p_handle);
2083 DSDB_CheckXRUN(pdbi);
2085 TRACE("avail=%d format=%s channels=%d\n", (int)avail, snd_pcm_format_name(format), channels );
2087 while (avail >= period_size)
2089 const snd_pcm_channel_area_t *areas;
2090 snd_pcm_uframes_t ofs;
2091 snd_pcm_uframes_t frames;
2092 int err;
2094 frames = avail / period_size * period_size; /* round down to a multiple of period_size */
2096 EnterCriticalSection(&pdbi->mmap_crst);
2098 snd_pcm_mmap_begin(wwo->p_handle, &areas, &ofs, &frames);
2099 snd_pcm_areas_copy(areas, ofs, pdbi->mmap_areas, ofs, channels, frames, format);
2100 err = snd_pcm_mmap_commit(wwo->p_handle, ofs, frames);
2102 LeaveCriticalSection(&pdbi->mmap_crst);
2104 if ( err != (snd_pcm_sframes_t) frames)
2105 ERR("mmap partially failed.\n");
2107 avail = snd_pcm_avail_update(wwo->p_handle);
2111 static void DSDB_PCMCallback(snd_async_handler_t *ahandler)
2113 /* snd_pcm_t * handle = snd_async_handler_get_pcm(ahandler); */
2114 IDsDriverBufferImpl* pdbi = snd_async_handler_get_callback_private(ahandler);
2115 TRACE("callback called\n");
2116 DSDB_MMAPCopy(pdbi);
2119 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
2121 WINE_WAVEOUT * wwo = &(WOutDev[pdbi->drv->wDevID]);
2122 snd_pcm_format_t format = snd_pcm_hw_params_get_format(wwo->hw_params);
2123 snd_pcm_uframes_t frames = snd_pcm_hw_params_get_buffer_size(wwo->hw_params);
2124 int channels = snd_pcm_hw_params_get_channels(wwo->hw_params);
2125 unsigned int bits_per_sample = snd_pcm_format_physical_width(format);
2126 unsigned int bits_per_frame = bits_per_sample * channels;
2127 snd_pcm_channel_area_t * a;
2128 unsigned int c;
2129 int err;
2131 if (TRACE_ON(wave))
2132 ALSA_TraceParameters(wwo->hw_params, NULL, FALSE);
2134 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
2135 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
2137 pdbi->mmap_buflen_frames = frames;
2138 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( wwo->p_handle, frames );
2139 pdbi->mmap_buffer = HeapAlloc(GetProcessHeap(),0,pdbi->mmap_buflen_bytes);
2140 if (!pdbi->mmap_buffer)
2141 return DSERR_OUTOFMEMORY;
2143 snd_pcm_format_set_silence(format, pdbi->mmap_buffer, frames );
2145 TRACE("created mmap buffer of %ld frames (%ld bytes) at %p\n",
2146 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
2148 pdbi->mmap_areas = HeapAlloc(GetProcessHeap(),0,channels*sizeof(snd_pcm_channel_area_t));
2149 if (!pdbi->mmap_areas)
2150 return DSERR_OUTOFMEMORY;
2152 a = pdbi->mmap_areas;
2153 for (c = 0; c < channels; c++, a++)
2155 a->addr = pdbi->mmap_buffer;
2156 a->first = bits_per_sample * c;
2157 a->step = bits_per_frame;
2158 TRACE("Area %d: addr=%p first=%d step=%d\n", c, a->addr, a->first, a->step);
2161 InitializeCriticalSection(&pdbi->mmap_crst);
2163 err = snd_async_add_pcm_handler(&pdbi->mmap_async_handler, wwo->p_handle, DSDB_PCMCallback, pdbi);
2164 if ( err < 0 )
2166 ERR("add_pcm_handler failed. reason: %s\n", snd_strerror(err));
2167 return DSERR_GENERIC;
2170 return DS_OK;
2173 static void DSDB_DestroyMMAP(IDsDriverBufferImpl* pdbi)
2175 TRACE("mmap buffer %p destroyed\n", pdbi->mmap_buffer);
2176 HeapFree(GetProcessHeap(), 0, pdbi->mmap_areas);
2177 HeapFree(GetProcessHeap(), 0, pdbi->mmap_buffer);
2178 pdbi->mmap_areas = NULL;
2179 pdbi->mmap_buffer = NULL;
2180 DeleteCriticalSection(&pdbi->mmap_crst);
2184 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
2186 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2187 FIXME("(): stub!\n");
2188 return DSERR_UNSUPPORTED;
2191 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
2193 ICOM_THIS(IDsDriverBufferImpl,iface);
2194 TRACE("(%p)\n",iface);
2195 return ++This->ref;
2198 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
2200 ICOM_THIS(IDsDriverBufferImpl,iface);
2201 TRACE("(%p)\n",iface);
2202 if (--This->ref)
2203 return This->ref;
2204 if (This == This->drv->primary)
2205 This->drv->primary = NULL;
2206 DSDB_DestroyMMAP(This);
2207 HeapFree(GetProcessHeap(), 0, This);
2208 return 0;
2211 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
2212 LPVOID*ppvAudio1,LPDWORD pdwLen1,
2213 LPVOID*ppvAudio2,LPDWORD pdwLen2,
2214 DWORD dwWritePosition,DWORD dwWriteLen,
2215 DWORD dwFlags)
2217 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2218 TRACE("(%p)\n",iface);
2219 return DSERR_UNSUPPORTED;
2222 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
2223 LPVOID pvAudio1,DWORD dwLen1,
2224 LPVOID pvAudio2,DWORD dwLen2)
2226 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2227 TRACE("(%p)\n",iface);
2228 return DSERR_UNSUPPORTED;
2231 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
2232 LPWAVEFORMATEX pwfx)
2234 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2235 TRACE("(%p,%p)\n",iface,pwfx);
2236 return DSERR_BUFFERLOST;
2239 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
2241 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
2242 TRACE("(%p,%ld): stub\n",iface,dwFreq);
2243 return DSERR_UNSUPPORTED;
2246 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
2248 DWORD vol;
2249 ICOM_THIS(IDsDriverBufferImpl,iface);
2250 TRACE("(%p,%p)\n",iface,pVolPan);
2251 vol = pVolPan->dwTotalLeftAmpFactor | (pVolPan->dwTotalRightAmpFactor << 16);
2253 if (wodSetVolume(This->drv->wDevID, vol) != MMSYSERR_NOERROR) {
2254 WARN("wodSetVolume failed\n");
2255 return DSERR_INVALIDPARAM;
2258 return DS_OK;
2261 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
2263 /* ICOM_THIS(IDsDriverImpl,iface); */
2264 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
2265 return DSERR_UNSUPPORTED;
2268 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
2269 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
2271 ICOM_THIS(IDsDriverBufferImpl,iface);
2272 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
2273 snd_pcm_uframes_t hw_ptr;
2274 snd_pcm_uframes_t period_size;
2276 if (wwo->hw_params == NULL) return DSERR_GENERIC;
2278 period_size = snd_pcm_hw_params_get_period_size(wwo->hw_params, 0);
2280 if (wwo->p_handle == NULL) return DSERR_GENERIC;
2281 /** we need to track down buffer underruns */
2282 DSDB_CheckXRUN(This);
2284 EnterCriticalSection(&This->mmap_crst);
2285 hw_ptr = _snd_pcm_mmap_hw_ptr(wwo->p_handle);
2286 if (lpdwPlay)
2287 *lpdwPlay = snd_pcm_frames_to_bytes(wwo->p_handle, hw_ptr/ period_size * period_size) % This->mmap_buflen_bytes;
2288 if (lpdwWrite)
2289 *lpdwWrite = snd_pcm_frames_to_bytes(wwo->p_handle, (hw_ptr / period_size + 1) * period_size ) % This->mmap_buflen_bytes;
2290 LeaveCriticalSection(&This->mmap_crst);
2292 TRACE("hw_ptr=0x%08x, playpos=%ld, writepos=%ld\n", (unsigned int)hw_ptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
2293 return DS_OK;
2296 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
2298 ICOM_THIS(IDsDriverBufferImpl,iface);
2299 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
2300 snd_pcm_state_t state;
2301 int err;
2303 TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
2305 if (wwo->p_handle == NULL) return DSERR_GENERIC;
2307 state = snd_pcm_state(wwo->p_handle);
2308 if ( state == SND_PCM_STATE_SETUP )
2310 err = snd_pcm_prepare(wwo->p_handle);
2311 state = snd_pcm_state(wwo->p_handle);
2313 if ( state == SND_PCM_STATE_PREPARED )
2315 DSDB_MMAPCopy(This);
2316 err = snd_pcm_start(wwo->p_handle);
2318 return DS_OK;
2321 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
2323 ICOM_THIS(IDsDriverBufferImpl,iface);
2324 WINE_WAVEOUT * wwo = &(WOutDev[This->drv->wDevID]);
2325 int err;
2326 DWORD play;
2327 DWORD write;
2329 TRACE("(%p)\n",iface);
2331 if (wwo->p_handle == NULL) return DSERR_GENERIC;
2333 /* ring buffer wrap up detection */
2334 IDsDriverBufferImpl_GetPosition(iface, &play, &write);
2335 if ( play > write)
2337 TRACE("writepos wrapper up\n");
2338 return DS_OK;
2341 if ( ( err = snd_pcm_drop(wwo->p_handle)) < 0 )
2343 ERR("error while stopping pcm: %s\n", snd_strerror(err));
2344 return DSERR_GENERIC;
2346 return DS_OK;
2349 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
2351 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2352 IDsDriverBufferImpl_QueryInterface,
2353 IDsDriverBufferImpl_AddRef,
2354 IDsDriverBufferImpl_Release,
2355 IDsDriverBufferImpl_Lock,
2356 IDsDriverBufferImpl_Unlock,
2357 IDsDriverBufferImpl_SetFormat,
2358 IDsDriverBufferImpl_SetFrequency,
2359 IDsDriverBufferImpl_SetVolumePan,
2360 IDsDriverBufferImpl_SetPosition,
2361 IDsDriverBufferImpl_GetPosition,
2362 IDsDriverBufferImpl_Play,
2363 IDsDriverBufferImpl_Stop
2366 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
2368 /* ICOM_THIS(IDsDriverImpl,iface); */
2369 FIXME("(%p): stub!\n",iface);
2370 return DSERR_UNSUPPORTED;
2373 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
2375 ICOM_THIS(IDsDriverImpl,iface);
2376 TRACE("(%p)\n",iface);
2377 This->ref++;
2378 return This->ref;
2381 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
2383 ICOM_THIS(IDsDriverImpl,iface);
2384 TRACE("(%p)\n",iface);
2385 if (--This->ref)
2386 return This->ref;
2387 HeapFree(GetProcessHeap(),0,This);
2388 return 0;
2391 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
2393 ICOM_THIS(IDsDriverImpl,iface);
2394 TRACE("(%p,%p)\n",iface,pDesc);
2395 memcpy(pDesc, &(WOutDev[This->wDevID].ds_desc), sizeof(DSDRIVERDESC));
2396 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
2397 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
2398 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
2399 pDesc->wVxdId = 0;
2400 pDesc->wReserved = 0;
2401 pDesc->ulDeviceNum = This->wDevID;
2402 pDesc->dwHeapType = DSDHEAP_NOHEAP;
2403 pDesc->pvDirectDrawHeap = NULL;
2404 pDesc->dwMemStartAddress = 0;
2405 pDesc->dwMemEndAddress = 0;
2406 pDesc->dwMemAllocExtra = 0;
2407 pDesc->pvReserved1 = NULL;
2408 pDesc->pvReserved2 = NULL;
2409 return DS_OK;
2412 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
2414 /* ICOM_THIS(IDsDriverImpl,iface); */
2415 TRACE("(%p)\n",iface);
2416 return DS_OK;
2419 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
2421 /* ICOM_THIS(IDsDriverImpl,iface); */
2422 TRACE("(%p)\n",iface);
2423 return DS_OK;
2426 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
2428 ICOM_THIS(IDsDriverImpl,iface);
2429 TRACE("(%p,%p)\n",iface,pCaps);
2430 memset(pCaps, 0, sizeof(*pCaps));
2432 pCaps->dwFlags = DSCAPS_PRIMARYMONO;
2433 if ( WOutDev[This->wDevID].caps.wChannels == 2 )
2434 pCaps->dwFlags |= DSCAPS_PRIMARYSTEREO;
2436 if ( WOutDev[This->wDevID].caps.dwFormats & (WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 ) )
2437 pCaps->dwFlags |= DSCAPS_PRIMARY8BIT;
2439 if ( WOutDev[This->wDevID].caps.dwFormats & (WAVE_FORMAT_1S16 | WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16))
2440 pCaps->dwFlags |= DSCAPS_PRIMARY16BIT;
2442 pCaps->dwPrimaryBuffers = 1;
2443 TRACE("caps=0x%X\n",(unsigned int)pCaps->dwFlags);
2444 pCaps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
2445 pCaps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
2447 /* the other fields only apply to secondary buffers, which we don't support
2448 * (unless we want to mess with wavetable synthesizers and MIDI) */
2449 return DS_OK;
2452 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
2453 LPWAVEFORMATEX pwfx,
2454 DWORD dwFlags, DWORD dwCardAddress,
2455 LPDWORD pdwcbBufferSize,
2456 LPBYTE *ppbBuffer,
2457 LPVOID *ppvObj)
2459 ICOM_THIS(IDsDriverImpl,iface);
2460 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
2461 int err;
2463 TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
2464 /* we only support primary buffers */
2465 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
2466 return DSERR_UNSUPPORTED;
2467 if (This->primary)
2468 return DSERR_ALLOCATED;
2469 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
2470 return DSERR_CONTROLUNAVAIL;
2472 *ippdsdb = (IDsDriverBufferImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
2473 if (*ippdsdb == NULL)
2474 return DSERR_OUTOFMEMORY;
2475 (*ippdsdb)->lpVtbl = &dsdbvt;
2476 (*ippdsdb)->ref = 1;
2477 (*ippdsdb)->drv = This;
2479 err = DSDB_CreateMMAP((*ippdsdb));
2480 if ( err != DS_OK )
2482 HeapFree(GetProcessHeap(), 0, *ippdsdb);
2483 *ippdsdb = NULL;
2484 return err;
2486 *ppbBuffer = (*ippdsdb)->mmap_buffer;
2487 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
2489 This->primary = *ippdsdb;
2491 /* buffer is ready to go */
2492 TRACE("buffer created at %p\n", *ippdsdb);
2493 return DS_OK;
2496 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
2497 PIDSDRIVERBUFFER pBuffer,
2498 LPVOID *ppvObj)
2500 /* ICOM_THIS(IDsDriverImpl,iface); */
2501 TRACE("(%p,%p): stub\n",iface,pBuffer);
2502 return DSERR_INVALIDCALL;
2505 static ICOM_VTABLE(IDsDriver) dsdvt =
2507 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
2508 IDsDriverImpl_QueryInterface,
2509 IDsDriverImpl_AddRef,
2510 IDsDriverImpl_Release,
2511 IDsDriverImpl_GetDriverDesc,
2512 IDsDriverImpl_Open,
2513 IDsDriverImpl_Close,
2514 IDsDriverImpl_GetCaps,
2515 IDsDriverImpl_CreateSoundBuffer,
2516 IDsDriverImpl_DuplicateSoundBuffer
2519 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
2521 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
2523 TRACE("driver created\n");
2525 /* the HAL isn't much better than the HEL if we can't do mmap() */
2526 if (!(WOutDev[wDevID].caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
2527 ERR("DirectSound flag not set\n");
2528 MESSAGE("This sound card's driver does not support direct access\n");
2529 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2530 return MMSYSERR_NOTSUPPORTED;
2533 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
2534 if (!*idrv)
2535 return MMSYSERR_NOMEM;
2536 (*idrv)->lpVtbl = &dsdvt;
2537 (*idrv)->ref = 1;
2539 (*idrv)->wDevID = wDevID;
2540 (*idrv)->primary = NULL;
2541 return MMSYSERR_NOERROR;
2544 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2546 memcpy(desc, &(WOutDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
2547 return MMSYSERR_NOERROR;
2550 static DWORD wodDsGuid(UINT wDevID, LPGUID pGuid)
2552 TRACE("(%d,%p)\n",wDevID,pGuid);
2554 memcpy(pGuid, &(WOutDev[wDevID].ds_guid), sizeof(GUID));
2556 return MMSYSERR_NOERROR;
2559 /*======================================================================*
2560 * Low level WAVE IN implementation *
2561 *======================================================================*/
2563 /**************************************************************************
2564 * widNotifyClient [internal]
2566 static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
2568 TRACE("wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n", wMsg, dwParam1, dwParam2);
2570 switch (wMsg) {
2571 case WIM_OPEN:
2572 case WIM_CLOSE:
2573 case WIM_DATA:
2574 if (wwi->wFlags != DCB_NULL &&
2575 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags, (HDRVR)wwi->waveDesc.hWave,
2576 wMsg, wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
2577 WARN("can't notify client !\n");
2578 return MMSYSERR_ERROR;
2580 break;
2581 default:
2582 FIXME("Unknown callback message %u\n", wMsg);
2583 return MMSYSERR_INVALPARAM;
2585 return MMSYSERR_NOERROR;
2588 /**************************************************************************
2589 * widGetDevCaps [internal]
2591 static DWORD widGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
2593 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
2595 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2597 if (wDevID >= MAX_WAVEINDRV) {
2598 TRACE("MAX_WAVOUTDRV reached !\n");
2599 return MMSYSERR_BADDEVICEID;
2602 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
2603 return MMSYSERR_NOERROR;
2606 /**************************************************************************
2607 * widRecorder_ReadHeaders [internal]
2609 static void widRecorder_ReadHeaders(WINE_WAVEIN * wwi)
2611 enum win_wm_message tmp_msg;
2612 DWORD tmp_param;
2613 HANDLE tmp_ev;
2614 WAVEHDR* lpWaveHdr;
2616 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
2617 if (tmp_msg == WINE_WM_HEADER) {
2618 LPWAVEHDR* wh;
2619 lpWaveHdr = (LPWAVEHDR)tmp_param;
2620 lpWaveHdr->lpNext = 0;
2622 if (wwi->lpQueuePtr == 0)
2623 wwi->lpQueuePtr = lpWaveHdr;
2624 else {
2625 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2626 *wh = lpWaveHdr;
2628 } else {
2629 ERR("should only have headers left\n");
2634 /**************************************************************************
2635 * widRecorder [internal]
2637 static DWORD CALLBACK widRecorder(LPVOID pmt)
2639 WORD uDevID = (DWORD)pmt;
2640 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
2641 WAVEHDR* lpWaveHdr;
2642 DWORD dwSleepTime;
2643 DWORD bytesRead;
2644 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwPeriodSize);
2645 char *pOffset = buffer;
2646 enum win_wm_message msg;
2647 DWORD param;
2648 HANDLE ev;
2649 DWORD frames_per_period;
2651 wwi->state = WINE_WS_STOPPED;
2652 wwi->dwTotalRecorded = 0;
2653 wwi->lpQueuePtr = NULL;
2655 SetEvent(wwi->hStartUpEvent);
2657 /* make sleep time to be # of ms to output a period */
2658 dwSleepTime = (1024/*wwi-dwPeriodSize => overrun!*/ * 1000) / wwi->format.wf.nAvgBytesPerSec;
2659 frames_per_period = snd_pcm_bytes_to_frames(wwi->p_handle, wwi->dwPeriodSize);
2660 TRACE("sleeptime=%ld ms\n", dwSleepTime);
2662 for (;;) {
2663 /* wait for dwSleepTime or an event in thread's queue */
2664 /* FIXME: could improve wait time depending on queue state,
2665 * ie, number of queued fragments
2667 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
2669 int periods;
2670 DWORD frames;
2671 DWORD bytes;
2672 DWORD read;
2674 lpWaveHdr = wwi->lpQueuePtr;
2675 /* read all the fragments accumulated so far */
2676 frames = snd_pcm_avail_update(wwi->p_handle);
2677 bytes = snd_pcm_frames_to_bytes(wwi->p_handle, frames);
2678 TRACE("frames = %ld bytes = %ld\n", frames, bytes);
2679 periods = bytes / wwi->dwPeriodSize;
2680 while ((periods > 0) && (wwi->lpQueuePtr))
2682 periods--;
2683 bytes = wwi->dwPeriodSize;
2684 TRACE("bytes = %ld\n",bytes);
2685 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwPeriodSize)
2687 /* directly read fragment in wavehdr */
2688 read = wwi->read(wwi->p_handle, lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded, frames_per_period);
2689 bytesRead = snd_pcm_frames_to_bytes(wwi->p_handle, read);
2691 TRACE("bytesRead=%ld (direct)\n", bytesRead);
2692 if (bytesRead != (DWORD) -1)
2694 /* update number of bytes recorded in current buffer and by this device */
2695 lpWaveHdr->dwBytesRecorded += bytesRead;
2696 wwi->dwTotalRecorded += bytesRead;
2698 /* buffer is full. notify client */
2699 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2701 /* must copy the value of next waveHdr, because we have no idea of what
2702 * will be done with the content of lpWaveHdr in callback
2704 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2706 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2707 lpWaveHdr->dwFlags |= WHDR_DONE;
2709 wwi->lpQueuePtr = lpNext;
2710 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2711 lpWaveHdr = lpNext;
2715 else
2717 /* read the fragment in a local buffer */
2718 read = wwi->read(wwi->p_handle, buffer, frames_per_period);
2719 bytesRead = snd_pcm_frames_to_bytes(wwi->p_handle, read);
2720 pOffset = buffer;
2722 TRACE("bytesRead=%ld (local)\n", bytesRead);
2724 /* copy data in client buffers */
2725 while (bytesRead != (DWORD) -1 && bytesRead > 0)
2727 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
2729 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
2730 pOffset,
2731 dwToCopy);
2733 /* update number of bytes recorded in current buffer and by this device */
2734 lpWaveHdr->dwBytesRecorded += dwToCopy;
2735 wwi->dwTotalRecorded += dwToCopy;
2736 bytesRead -= dwToCopy;
2737 pOffset += dwToCopy;
2739 /* client buffer is full. notify client */
2740 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
2742 /* must copy the value of next waveHdr, because we have no idea of what
2743 * will be done with the content of lpWaveHdr in callback
2745 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2746 TRACE("lpNext=%p\n", lpNext);
2748 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2749 lpWaveHdr->dwFlags |= WHDR_DONE;
2751 wwi->lpQueuePtr = lpNext;
2752 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2754 lpWaveHdr = lpNext;
2755 if (!lpNext && bytesRead) {
2756 /* before we give up, check for more header messages */
2757 while (ALSA_PeekRingMessage(&wwi->msgRing, &msg, &param, &ev))
2759 if (msg == WINE_WM_HEADER) {
2760 LPWAVEHDR hdr;
2761 ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev);
2762 hdr = ((LPWAVEHDR)param);
2763 TRACE("msg = %s, hdr = %p, ev = %p\n", wodPlayerCmdString[msg - WM_USER - 1], hdr, ev);
2764 hdr->lpNext = 0;
2765 if (lpWaveHdr == 0) {
2766 /* new head of queue */
2767 wwi->lpQueuePtr = lpWaveHdr = hdr;
2768 } else {
2769 /* insert buffer at the end of queue */
2770 LPWAVEHDR* wh;
2771 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2772 *wh = hdr;
2774 } else
2775 break;
2778 if (lpWaveHdr == 0) {
2779 /* no more buffer to copy data to, but we did read more.
2780 * what hasn't been copied will be dropped
2782 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
2783 wwi->lpQueuePtr = NULL;
2784 break;
2793 WAIT_OMR(&wwi->msgRing, dwSleepTime);
2795 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
2797 TRACE("msg=%s param=0x%lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
2798 switch (msg) {
2799 case WINE_WM_PAUSING:
2800 wwi->state = WINE_WS_PAUSED;
2801 /*FIXME("Device should stop recording\n");*/
2802 SetEvent(ev);
2803 break;
2804 case WINE_WM_STARTING:
2805 wwi->state = WINE_WS_PLAYING;
2806 snd_pcm_start(wwi->p_handle);
2807 SetEvent(ev);
2808 break;
2809 case WINE_WM_HEADER:
2810 lpWaveHdr = (LPWAVEHDR)param;
2811 lpWaveHdr->lpNext = 0;
2813 /* insert buffer at the end of queue */
2815 LPWAVEHDR* wh;
2816 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2817 *wh = lpWaveHdr;
2819 break;
2820 case WINE_WM_STOPPING:
2821 if (wwi->state != WINE_WS_STOPPED)
2823 snd_pcm_drain(wwi->p_handle);
2825 /* read any headers in queue */
2826 widRecorder_ReadHeaders(wwi);
2828 /* return current buffer to app */
2829 lpWaveHdr = wwi->lpQueuePtr;
2830 if (lpWaveHdr)
2832 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
2833 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2834 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2835 lpWaveHdr->dwFlags |= WHDR_DONE;
2836 wwi->lpQueuePtr = lpNext;
2837 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2840 wwi->state = WINE_WS_STOPPED;
2841 SetEvent(ev);
2842 break;
2843 case WINE_WM_RESETTING:
2844 if (wwi->state != WINE_WS_STOPPED)
2846 snd_pcm_drain(wwi->p_handle);
2848 wwi->state = WINE_WS_STOPPED;
2849 wwi->dwTotalRecorded = 0;
2851 /* read any headers in queue */
2852 widRecorder_ReadHeaders(wwi);
2854 /* return all buffers to the app */
2855 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
2856 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
2857 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2858 lpWaveHdr->dwFlags |= WHDR_DONE;
2859 wwi->lpQueuePtr = lpWaveHdr->lpNext;
2860 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
2863 wwi->lpQueuePtr = NULL;
2864 SetEvent(ev);
2865 break;
2866 case WINE_WM_CLOSING:
2867 wwi->hThread = 0;
2868 wwi->state = WINE_WS_CLOSED;
2869 SetEvent(ev);
2870 HeapFree(GetProcessHeap(), 0, buffer);
2871 ExitThread(0);
2872 /* shouldn't go here */
2873 case WINE_WM_UPDATE:
2874 SetEvent(ev);
2875 break;
2877 default:
2878 FIXME("unknown message %d\n", msg);
2879 break;
2883 ExitThread(0);
2884 /* just for not generating compilation warnings... should never be executed */
2885 return 0;
2888 /**************************************************************************
2889 * widOpen [internal]
2891 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2893 WINE_WAVEIN* wwi;
2894 snd_pcm_hw_params_t * hw_params;
2895 snd_pcm_sw_params_t * sw_params;
2896 snd_pcm_access_t access;
2897 snd_pcm_format_t format;
2898 int rate;
2899 unsigned int buffer_time = 500000;
2900 unsigned int period_time = 10000;
2901 int buffer_size;
2902 snd_pcm_uframes_t period_size;
2903 int flags;
2904 snd_pcm_t * pcm;
2905 int err;
2907 snd_pcm_hw_params_alloca(&hw_params);
2908 snd_pcm_sw_params_alloca(&sw_params);
2910 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
2911 if (lpDesc == NULL) {
2912 WARN("Invalid Parameter !\n");
2913 return MMSYSERR_INVALPARAM;
2915 if (wDevID >= MAX_WAVEOUTDRV) {
2916 TRACE("MAX_WAVOUTDRV reached !\n");
2917 return MMSYSERR_BADDEVICEID;
2920 /* only PCM format is supported so far... */
2921 if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
2922 lpDesc->lpFormat->nChannels == 0 ||
2923 lpDesc->lpFormat->nSamplesPerSec == 0) {
2924 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2925 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2926 lpDesc->lpFormat->nSamplesPerSec);
2927 return WAVERR_BADFORMAT;
2930 if (dwFlags & WAVE_FORMAT_QUERY) {
2931 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
2932 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2933 lpDesc->lpFormat->nSamplesPerSec);
2934 return MMSYSERR_NOERROR;
2937 wwi = &WInDev[wDevID];
2939 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwi->caps.dwSupport & WAVECAPS_DIRECTSOUND))
2940 /* not supported, ignore it */
2941 dwFlags &= ~WAVE_DIRECTSOUND;
2943 wwi->p_handle = 0;
2944 flags = SND_PCM_NONBLOCK;
2945 if ( dwFlags & WAVE_DIRECTSOUND )
2946 flags |= SND_PCM_ASYNC;
2948 if ( (err=snd_pcm_open(&pcm, wwi->device, SND_PCM_STREAM_CAPTURE, dwFlags)) < 0 )
2950 ERR("Error open: %s\n", snd_strerror(err));
2951 return MMSYSERR_NOTENABLED;
2954 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2956 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2957 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
2959 if (wwi->format.wBitsPerSample == 0) {
2960 WARN("Resetting zeroed wBitsPerSample\n");
2961 wwi->format.wBitsPerSample = 8 *
2962 (wwi->format.wf.nAvgBytesPerSec /
2963 wwi->format.wf.nSamplesPerSec) /
2964 wwi->format.wf.nChannels;
2967 snd_pcm_hw_params_any(pcm, hw_params);
2969 #define EXIT_ON_ERROR(f,e,txt) do \
2971 int err; \
2972 if ( (err = (f) ) < 0) \
2974 ERR(txt ": %s\n", snd_strerror(err)); \
2975 snd_pcm_close(pcm); \
2976 return e; \
2978 } while(0)
2980 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
2981 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
2982 WARN("mmap not available. switching to standard write.\n");
2983 access = SND_PCM_ACCESS_RW_INTERLEAVED;
2984 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
2985 wwi->read = snd_pcm_readi;
2987 else
2988 wwi->read = snd_pcm_mmap_readi;
2990 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwi->format.wf.nChannels), MMSYSERR_INVALPARAM, "unable to set required channels");
2992 format = (wwi->format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_U8;
2993 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), MMSYSERR_INVALPARAM, "unable to set required format");
2995 rate = snd_pcm_hw_params_set_rate_near(pcm, hw_params, wwi->format.wf.nSamplesPerSec, 0);
2996 if (rate < 0) {
2997 ERR("Rate %ld Hz not available for playback: %s\n", wwi->format.wf.nSamplesPerSec, snd_strerror(rate));
2998 snd_pcm_close(pcm);
2999 return WAVERR_BADFORMAT;
3001 if (rate != wwi->format.wf.nSamplesPerSec) {
3002 ERR("Rate doesn't match (requested %ld Hz, got %d Hz)\n", wwi->format.wf.nSamplesPerSec, rate);
3003 snd_pcm_close(pcm);
3004 return WAVERR_BADFORMAT;
3007 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, buffer_time, 0), MMSYSERR_INVALPARAM, "unable to set buffer time");
3008 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, period_time, 0), MMSYSERR_INVALPARAM, "unable to set period time");
3010 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
3012 period_size = snd_pcm_hw_params_get_period_size(hw_params, 0);
3013 buffer_size = snd_pcm_hw_params_get_buffer_size(hw_params);
3015 snd_pcm_sw_params_current(pcm, sw_params);
3016 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, dwFlags & WAVE_DIRECTSOUND ? INT_MAX : 1 ), MMSYSERR_ERROR, "unable to set start threshold");
3017 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
3018 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
3019 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
3020 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
3021 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
3022 #undef EXIT_ON_ERROR
3024 snd_pcm_prepare(pcm);
3026 if (TRACE_ON(wave))
3027 ALSA_TraceParameters(hw_params, sw_params, FALSE);
3029 /* now, we can save all required data for later use... */
3030 if ( wwi->hw_params )
3031 snd_pcm_hw_params_free(wwi->hw_params);
3032 snd_pcm_hw_params_malloc(&(wwi->hw_params));
3033 snd_pcm_hw_params_copy(wwi->hw_params, hw_params);
3035 wwi->dwBufferSize = buffer_size;
3036 wwi->lpQueuePtr = wwi->lpPlayPtr = wwi->lpLoopPtr = NULL;
3037 wwi->p_handle = pcm;
3039 ALSA_InitRingMessage(&wwi->msgRing);
3041 wwi->count = snd_pcm_poll_descriptors_count (wwi->p_handle);
3042 if (wwi->count <= 0) {
3043 ERR("Invalid poll descriptors count\n");
3044 return MMSYSERR_ERROR;
3047 wwi->ufds = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof(struct pollfd) * wwi->count);
3048 if (wwi->ufds == NULL) {
3049 ERR("No enough memory\n");
3050 return MMSYSERR_NOMEM;
3052 if ((err = snd_pcm_poll_descriptors(wwi->p_handle, wwi->ufds, wwi->count)) < 0) {
3053 ERR("Unable to obtain poll descriptors for playback: %s\n", snd_strerror(err));
3054 return MMSYSERR_ERROR;
3057 wwi->dwPeriodSize = period_size;
3058 /*if (wwi->dwFragmentSize % wwi->format.wf.nBlockAlign)
3059 ERR("Fragment doesn't contain an integral number of data blocks\n");
3061 TRACE("dwPeriodSize=%lu\n", wwi->dwPeriodSize);
3062 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
3063 wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
3064 wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
3065 wwi->format.wf.nBlockAlign);
3067 if (!(dwFlags & WAVE_DIRECTSOUND)) {
3068 wwi->hStartUpEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
3069 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
3070 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
3071 CloseHandle(wwi->hStartUpEvent);
3072 } else {
3073 wwi->hThread = INVALID_HANDLE_VALUE;
3074 wwi->dwThreadID = 0;
3076 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
3078 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
3082 /**************************************************************************
3083 * widClose [internal]
3085 static DWORD widClose(WORD wDevID)
3087 DWORD ret = MMSYSERR_NOERROR;
3088 WINE_WAVEIN* wwi;
3090 TRACE("(%u);\n", wDevID);
3092 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3093 WARN("bad device ID !\n");
3094 return MMSYSERR_BADDEVICEID;
3097 wwi = &WInDev[wDevID];
3098 if (wwi->lpQueuePtr) {
3099 WARN("buffers still playing !\n");
3100 ret = WAVERR_STILLPLAYING;
3101 } else {
3102 if (wwi->hThread != INVALID_HANDLE_VALUE) {
3103 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
3105 ALSA_DestroyRingMessage(&wwi->msgRing);
3107 snd_pcm_hw_params_free(wwi->hw_params);
3108 wwi->hw_params = NULL;
3110 snd_pcm_close(wwi->p_handle);
3111 wwi->p_handle = NULL;
3113 ret = widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
3116 HeapFree(GetProcessHeap(), 0, wwi->ufds);
3117 return ret;
3120 /**************************************************************************
3121 * widAddBuffer [internal]
3124 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3126 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3128 /* first, do the sanity checks... */
3129 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3130 WARN("bad dev ID !\n");
3131 return MMSYSERR_BADDEVICEID;
3134 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
3135 return WAVERR_UNPREPARED;
3137 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3138 return WAVERR_STILLPLAYING;
3140 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3141 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
3142 lpWaveHdr->lpNext = 0;
3144 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
3146 return MMSYSERR_NOERROR;
3149 /**************************************************************************
3150 * widPrepare [internal]
3152 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3154 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3156 if (wDevID >= MAX_WAVEINDRV) {
3157 WARN("bad device ID !\n");
3158 return MMSYSERR_BADDEVICEID;
3161 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3162 return WAVERR_STILLPLAYING;
3164 lpWaveHdr->dwFlags |= WHDR_PREPARED;
3165 lpWaveHdr->dwFlags &= ~WHDR_DONE;
3166 return MMSYSERR_NOERROR;
3169 /**************************************************************************
3170 * widUnprepare [internal]
3172 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3174 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3176 if (wDevID >= MAX_WAVEINDRV) {
3177 WARN("bad device ID !\n");
3178 return MMSYSERR_BADDEVICEID;
3181 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
3182 return WAVERR_STILLPLAYING;
3184 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
3185 lpWaveHdr->dwFlags |= WHDR_DONE;
3187 return MMSYSERR_NOERROR;
3190 /**************************************************************************
3191 * widStart [internal]
3194 static DWORD widStart(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3196 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3198 /* first, do the sanity checks... */
3199 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3200 WARN("bad dev ID !\n");
3201 return MMSYSERR_BADDEVICEID;
3204 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
3206 Sleep(500);
3208 return MMSYSERR_NOERROR;
3211 /**************************************************************************
3212 * widStop [internal]
3215 static DWORD widStop(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
3217 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
3219 /* first, do the sanity checks... */
3220 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].p_handle == NULL) {
3221 WARN("bad dev ID !\n");
3222 return MMSYSERR_BADDEVICEID;
3225 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
3227 return MMSYSERR_NOERROR;
3230 /**************************************************************************
3231 * widReset [internal]
3233 static DWORD widReset(WORD wDevID)
3235 TRACE("(%u);\n", wDevID);
3236 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
3237 WARN("can't reset !\n");
3238 return MMSYSERR_INVALHANDLE;
3240 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
3241 return MMSYSERR_NOERROR;
3244 /**************************************************************************
3245 * widGetPosition [internal]
3247 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
3249 int time;
3250 WINE_WAVEIN* wwi;
3252 FIXME("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
3254 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
3255 WARN("can't get pos !\n");
3256 return MMSYSERR_INVALHANDLE;
3258 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
3260 wwi = &WInDev[wDevID];
3261 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_UPDATE, 0, TRUE);
3263 TRACE("wType=%04X !\n", lpTime->wType);
3264 TRACE("wBitsPerSample=%u\n", wwi->format.wBitsPerSample);
3265 TRACE("nSamplesPerSec=%lu\n", wwi->format.wf.nSamplesPerSec);
3266 TRACE("nChannels=%u\n", wwi->format.wf.nChannels);
3267 TRACE("nAvgBytesPerSec=%lu\n", wwi->format.wf.nAvgBytesPerSec);
3268 FIXME("dwTotalRecorded=%lu\n",wwi->dwTotalRecorded);
3269 switch (lpTime->wType) {
3270 case TIME_BYTES:
3271 lpTime->u.cb = wwi->dwTotalRecorded;
3272 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
3273 break;
3274 case TIME_SAMPLES:
3275 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
3276 wwi->format.wBitsPerSample / wwi->format.wf.nChannels;
3277 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
3278 break;
3279 case TIME_SMPTE:
3280 time = wwi->dwTotalRecorded /
3281 (wwi->format.wf.nAvgBytesPerSec / 1000);
3282 lpTime->u.smpte.hour = time / (60 * 60 * 1000);
3283 time -= lpTime->u.smpte.hour * (60 * 60 * 1000);
3284 lpTime->u.smpte.min = time / (60 * 1000);
3285 time -= lpTime->u.smpte.min * (60 * 1000);
3286 lpTime->u.smpte.sec = time / 1000;
3287 time -= lpTime->u.smpte.sec * 1000;
3288 lpTime->u.smpte.frame = time * 30 / 1000;
3289 lpTime->u.smpte.fps = 30;
3290 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
3291 lpTime->u.smpte.hour, lpTime->u.smpte.min,
3292 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
3293 break;
3294 default:
3295 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
3296 lpTime->wType = TIME_MS;
3297 case TIME_MS:
3298 lpTime->u.ms = wwi->dwTotalRecorded /
3299 (wwi->format.wf.nAvgBytesPerSec / 1000);
3300 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
3301 break;
3303 return MMSYSERR_NOERROR;
3306 /**************************************************************************
3307 * widGetNumDevs [internal]
3309 static DWORD widGetNumDevs(void)
3311 return ALSA_WidNumDevs;
3314 /**************************************************************************
3315 * widMessage (WINEALSA.@)
3317 DWORD WINAPI ALSA_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
3318 DWORD dwParam1, DWORD dwParam2)
3320 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
3321 wDevID, wMsg, dwUser, dwParam1, dwParam2);
3323 switch (wMsg) {
3324 case DRVM_INIT:
3325 case DRVM_EXIT:
3326 case DRVM_ENABLE:
3327 case DRVM_DISABLE:
3328 /* FIXME: Pretend this is supported */
3329 return 0;
3330 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
3331 case WIDM_CLOSE: return widClose (wDevID);
3332 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3333 case WIDM_PREPARE: return widPrepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3334 case WIDM_UNPREPARE: return widUnprepare (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3335 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEOUTCAPSA)dwParam1, dwParam2);
3336 case WIDM_GETNUMDEVS: return widGetNumDevs ();
3337 case WIDM_GETPOS: return widGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
3338 case WIDM_RESET: return widReset (wDevID);
3339 case WIDM_START: return widStart (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3340 case WIDM_STOP: return widStop (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
3341 /*case DRV_QUERYDEVICEINTERFACESIZE: return wdDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3342 case DRV_QUERYDEVICEINTERFACE: return wdDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3343 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
3344 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3345 case DRV_QUERYDSOUNDGUID: return widDsGuid (wDevID, (LPGUID)dwParam1);*/
3346 default:
3347 FIXME("unknown message %d!\n", wMsg);
3349 return MMSYSERR_NOTSUPPORTED;
3352 #endif
3354 #if !(defined(HAVE_ALSA) && ((SND_LIB_MAJOR == 0 && SND_LIB_MINOR >= 9) || SND_LIB_MAJOR >= 1))
3356 /**************************************************************************
3357 * widMessage (WINEALSA.@)
3359 DWORD WINAPI ALSA_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3360 DWORD dwParam1, DWORD dwParam2)
3362 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3363 return MMSYSERR_NOTENABLED;
3366 #endif
3368 #ifndef HAVE_ALSA
3370 /**************************************************************************
3371 * wodMessage (WINEALSA.@)
3373 DWORD WINAPI ALSA_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
3374 DWORD dwParam1, DWORD dwParam2)
3376 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
3377 return MMSYSERR_NOTENABLED;
3380 #endif /* HAVE_ALSA */