1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
29 #include "wine/port.h"
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
44 #ifdef HAVE_SYS_MMAN_H
45 # include <sys/mman.h>
61 #define ALSA_PCM_NEW_HW_PARAMS_API
62 #define ALSA_PCM_NEW_SW_PARAMS_API
64 #include "wine/library.h"
65 #include "wine/unicode.h"
66 #include "wine/debug.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(wave);
73 /* internal ALSALIB functions */
74 /* FIXME: we shouldn't be using internal functions... */
75 snd_pcm_uframes_t _snd_pcm_mmap_hw_ptr(snd_pcm_t *pcm);
78 /* state diagram for waveOut writing:
80 * +---------+-------------+---------------+---------------------------------+
81 * | state | function | event | new state |
82 * +---------+-------------+---------------+---------------------------------+
83 * | | open() | | STOPPED |
84 * | PAUSED | write() | | PAUSED |
85 * | STOPPED | write() | <thrd create> | PLAYING |
86 * | PLAYING | write() | HEADER | PLAYING |
87 * | (other) | write() | <error> | |
88 * | (any) | pause() | PAUSING | PAUSED |
89 * | PAUSED | restart() | RESTARTING | PLAYING (if no thrd => STOPPED) |
90 * | (any) | reset() | RESETTING | STOPPED |
91 * | (any) | close() | CLOSING | CLOSED |
92 * +---------+-------------+---------------+---------------------------------+
95 /* states of the playing device */
96 #define WINE_WS_PLAYING 0
97 #define WINE_WS_PAUSED 1
98 #define WINE_WS_STOPPED 2
99 #define WINE_WS_CLOSED 3
101 /* events to be send to device */
102 enum win_wm_message {
103 WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
104 WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
108 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
109 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
110 #define RESET_OMR(omr) do { } while (0)
111 #define WAIT_OMR(omr, sleep) \
112 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
113 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
115 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
116 #define CLEAR_OMR(omr) do { } while (0)
117 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
118 #define WAIT_OMR(omr, sleep) \
119 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
123 enum win_wm_message msg; /* message identifier */
124 DWORD param; /* parameter for this message */
125 HANDLE hEvent; /* if message is synchronous, handle of event for synchro */
128 /* implement an in-process message ring for better performance
129 * (compared to passing thru the server)
130 * this ring will be used by the input (resp output) record (resp playback) routine
132 #define ALSA_RING_BUFFER_INCREMENT 64
135 int ring_buffer_size;
143 CRITICAL_SECTION msg_crst;
147 volatile int state; /* one of the WINE_WS_ manifest constants */
148 WAVEOPENDESC waveDesc;
150 WAVEFORMATPCMEX format;
152 char* pcmname; /* string name of alsa PCM device */
153 char* ctlname; /* string name of alsa control device */
154 char interface_name[MAXPNAMELEN * 2];
156 snd_pcm_t* pcm; /* handle to ALSA playback device */
158 snd_pcm_hw_params_t * hw_params;
160 DWORD dwBufferSize; /* size of whole ALSA buffer in bytes */
161 LPWAVEHDR lpQueuePtr; /* start of queued WAVEHDRs (waiting to be notified) */
162 LPWAVEHDR lpPlayPtr; /* start of not yet fully played buffers */
164 LPWAVEHDR lpLoopPtr; /* pointer of first buffer in loop, if any */
165 DWORD dwLoops; /* private copy of loop counter */
167 DWORD dwPlayedTotal; /* number of bytes actually played since opening */
168 DWORD dwWrittenTotal; /* number of bytes written to ALSA buffer since opening */
170 /* synchronization stuff */
171 HANDLE hStartUpEvent;
174 ALSA_MSG_RING msgRing;
176 /* DirectSound stuff */
177 DSDRIVERDESC ds_desc;
178 DSDRIVERCAPS ds_caps;
180 /* Waveout only fields */
181 WAVEOUTCAPSW outcaps;
183 snd_hctl_t * hctl; /* control handle for the playback volume */
185 snd_pcm_sframes_t (*write)(snd_pcm_t *, const void *, snd_pcm_uframes_t );
187 DWORD dwPartialOffset; /* Offset of not yet written bytes in lpPlayPtr */
189 /* Wavein only fields */
194 snd_pcm_sframes_t (*read)(snd_pcm_t *, void *, snd_pcm_uframes_t );
196 DWORD dwPeriodSize; /* size of OSS buffer period */
197 DWORD dwTotalRecorded;
202 /*----------------------------------------------------------------------------
203 ** Global array of output and input devices, initialized via ALSA_WaveInit
205 #define WAVEDEV_ALLOC_EXTENT_SIZE 10
206 static WINE_WAVEDEV *WOutDev;
207 static DWORD ALSA_WodNumMallocedDevs;
208 static DWORD ALSA_WodNumDevs;
210 static WINE_WAVEDEV *WInDev;
211 static DWORD ALSA_WidNumMallocedDevs;
212 static DWORD ALSA_WidNumDevs;
214 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
215 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
218 /*======================================================================*
219 * Utility functions *
220 *======================================================================*/
222 /* These strings used only for tracing */
223 static const char * getCmdString(enum win_wm_message msg)
225 static char unknown[32];
226 #define MSG_TO_STR(x) case x: return #x
228 MSG_TO_STR(WINE_WM_PAUSING);
229 MSG_TO_STR(WINE_WM_RESTARTING);
230 MSG_TO_STR(WINE_WM_RESETTING);
231 MSG_TO_STR(WINE_WM_HEADER);
232 MSG_TO_STR(WINE_WM_UPDATE);
233 MSG_TO_STR(WINE_WM_BREAKLOOP);
234 MSG_TO_STR(WINE_WM_CLOSING);
235 MSG_TO_STR(WINE_WM_STARTING);
236 MSG_TO_STR(WINE_WM_STOPPING);
239 sprintf(unknown, "UNKNOWN(0x%08x)", msg);
243 static const char * getMessage(UINT msg)
245 static char unknown[32];
246 #define MSG_TO_STR(x) case x: return #x
248 MSG_TO_STR(DRVM_INIT);
249 MSG_TO_STR(DRVM_EXIT);
250 MSG_TO_STR(DRVM_ENABLE);
251 MSG_TO_STR(DRVM_DISABLE);
252 MSG_TO_STR(WIDM_OPEN);
253 MSG_TO_STR(WIDM_CLOSE);
254 MSG_TO_STR(WIDM_ADDBUFFER);
255 MSG_TO_STR(WIDM_PREPARE);
256 MSG_TO_STR(WIDM_UNPREPARE);
257 MSG_TO_STR(WIDM_GETDEVCAPS);
258 MSG_TO_STR(WIDM_GETNUMDEVS);
259 MSG_TO_STR(WIDM_GETPOS);
260 MSG_TO_STR(WIDM_RESET);
261 MSG_TO_STR(WIDM_START);
262 MSG_TO_STR(WIDM_STOP);
263 MSG_TO_STR(WODM_OPEN);
264 MSG_TO_STR(WODM_CLOSE);
265 MSG_TO_STR(WODM_WRITE);
266 MSG_TO_STR(WODM_PAUSE);
267 MSG_TO_STR(WODM_GETPOS);
268 MSG_TO_STR(WODM_BREAKLOOP);
269 MSG_TO_STR(WODM_PREPARE);
270 MSG_TO_STR(WODM_UNPREPARE);
271 MSG_TO_STR(WODM_GETDEVCAPS);
272 MSG_TO_STR(WODM_GETNUMDEVS);
273 MSG_TO_STR(WODM_GETPITCH);
274 MSG_TO_STR(WODM_SETPITCH);
275 MSG_TO_STR(WODM_GETPLAYBACKRATE);
276 MSG_TO_STR(WODM_SETPLAYBACKRATE);
277 MSG_TO_STR(WODM_GETVOLUME);
278 MSG_TO_STR(WODM_SETVOLUME);
279 MSG_TO_STR(WODM_RESTART);
280 MSG_TO_STR(WODM_RESET);
281 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
282 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
283 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
284 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
287 sprintf(unknown, "UNKNOWN(0x%04x)", msg);
291 static const char * getFormat(WORD wFormatTag)
293 static char unknown[32];
294 #define FMT_TO_STR(x) case x: return #x
296 FMT_TO_STR(WAVE_FORMAT_PCM);
297 FMT_TO_STR(WAVE_FORMAT_EXTENSIBLE);
298 FMT_TO_STR(WAVE_FORMAT_MULAW);
299 FMT_TO_STR(WAVE_FORMAT_ALAW);
300 FMT_TO_STR(WAVE_FORMAT_ADPCM);
303 sprintf(unknown, "UNKNOWN(0x%04x)", wFormatTag);
307 /* Allow 1% deviation for sample rates (some ES137x cards) */
308 static BOOL NearMatch(int rate1, int rate2)
310 return (((100 * (rate1 - rate2)) / rate1) == 0);
313 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
314 WAVEFORMATPCMEX* format)
316 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
317 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
318 format->Format.nChannels, format->Format.nAvgBytesPerSec);
319 TRACE("Position in bytes=%u\n", position);
321 switch (lpTime->wType) {
323 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
324 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
327 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
328 TRACE("TIME_MS=%u\n", lpTime->u.ms);
331 lpTime->u.smpte.fps = 30;
332 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
333 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
334 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
335 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
336 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
337 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
338 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
339 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
340 lpTime->u.smpte.fps = 30;
341 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
342 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
343 lpTime->u.smpte.hour, lpTime->u.smpte.min,
344 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
347 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
348 lpTime->wType = TIME_BYTES;
351 lpTime->u.cb = position;
352 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
355 return MMSYSERR_NOERROR;
358 static BOOL supportedFormat(LPWAVEFORMATEX wf)
362 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
365 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
366 if (wf->nChannels==1||wf->nChannels==2) {
367 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
370 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
371 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
373 if (wf->cbSize == 22 &&
374 (IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM) ||
375 IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))) {
376 if (wf->nChannels>=1 && wf->nChannels<=6) {
377 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
378 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16||
379 wf->wBitsPerSample==24||wf->wBitsPerSample==32) {
383 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
386 WARN("only KSDATAFORMAT_SUBTYPE_PCM and KSDATAFORMAT_SUBTYPE_IEEE_FLOAT "
388 } else if (wf->wFormatTag == WAVE_FORMAT_MULAW || wf->wFormatTag == WAVE_FORMAT_ALAW) {
389 if (wf->wBitsPerSample==8)
392 ERR("WAVE_FORMAT_MULAW and WAVE_FORMAT_ALAW wBitsPerSample must = 8\n");
394 } else if (wf->wFormatTag == WAVE_FORMAT_ADPCM) {
395 if (wf->wBitsPerSample==4)
398 ERR("WAVE_FORMAT_ADPCM wBitsPerSample must = 4\n");
400 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
405 static void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
407 unsigned int iLength;
409 ZeroMemory(wf2, sizeof(wf2));
410 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
411 iLength = sizeof(PCMWAVEFORMAT);
412 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
413 iLength = sizeof(WAVEFORMATPCMEX);
415 iLength = sizeof(WAVEFORMATEX) + wf1->cbSize;
416 if (iLength > sizeof(WAVEFORMATPCMEX)) {
417 ERR("calculated %u bytes, capping to %u bytes\n", iLength, sizeof(WAVEFORMATPCMEX));
418 iLength = sizeof(WAVEFORMATPCMEX);
420 memcpy(wf2, wf1, iLength);
423 /*----------------------------------------------------------------------------
425 ** Retrieve a string from a registry key
427 static int ALSA_RegGetString(HKEY key, const char *value, char **bufp)
434 rc = RegQueryValueExA(key, value, NULL, &type, NULL, &bufsize);
435 if (rc != ERROR_SUCCESS)
441 *bufp = HeapAlloc(GetProcessHeap(), 0, bufsize);
445 rc = RegQueryValueExA(key, value, NULL, NULL, (LPBYTE)*bufp, &bufsize);
449 /*----------------------------------------------------------------------------
450 ** ALSA_RegGetBoolean
451 ** Get a string and interpret it as a boolean
453 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
454 static int ALSA_RegGetBoolean(HKEY key, const char *value, BOOL *answer)
459 rc = ALSA_RegGetString(key, value, &buf);
463 if (IS_OPTION_TRUE(*buf))
466 HeapFree(GetProcessHeap(), 0, buf);
472 /*----------------------------------------------------------------------------
473 ** ALSA_RegGetBoolean
474 ** Get a string and interpret it as a DWORD
476 static int ALSA_RegGetInt(HKEY key, const char *value, DWORD *answer)
481 rc = ALSA_RegGetString(key, value, &buf);
485 HeapFree(GetProcessHeap(), 0, buf);
491 /*======================================================================*
492 * Low level WAVE implementation *
493 *======================================================================*/
495 /*----------------------------------------------------------------------------
496 ** ALSA_TestDeviceForWine
498 ** Test to see if a given device is sufficient for Wine.
500 static int ALSA_TestDeviceForWine(int card, int device, snd_pcm_stream_t streamtype)
502 snd_pcm_t *pcm = NULL;
505 snd_pcm_hw_params_t *hwparams;
506 const char *reason = NULL;
509 /* Note that the plug: device masks out a lot of info, we want to avoid that */
510 sprintf(pcmname, "hw:%d,%d", card, device);
511 retcode = snd_pcm_open(&pcm, pcmname, streamtype, SND_PCM_NONBLOCK);
514 /* Note that a busy device isn't automatically disqualified */
515 if (retcode == (-1 * EBUSY))
520 snd_pcm_hw_params_alloca(&hwparams);
522 retcode = snd_pcm_hw_params_any(pcm, hwparams);
525 reason = "Could not retrieve hw_params";
529 /* set the count of channels */
530 retcode = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
533 reason = "Could not set channels";
538 retcode = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rrate, 0);
541 reason = "Could not set rate";
547 reason = "Rate came back as 0";
551 /* write the parameters to device */
552 retcode = snd_pcm_hw_params(pcm, hwparams);
555 reason = "Could not set hwparams";
565 if (retcode != 0 && retcode != (-1 * ENOENT))
566 TRACE("Discarding card %d/device %d: %s [%d(%s)]\n", card, device, reason, retcode, snd_strerror(retcode));
571 /**************************************************************************
572 * ALSA_CheckSetVolume [internal]
574 * Helper function for Alsa volume queries. This tries to simplify
575 * the process of managing the volume. All parameters are optional
576 * (pass NULL to ignore or not use).
577 * Return values are MMSYSERR_NOERROR on success, or !0 on failure;
578 * error codes are normalized into the possible documented return
579 * values from waveOutGetVolume.
581 static int ALSA_CheckSetVolume(snd_hctl_t *hctl, int *out_left, int *out_right,
582 int *out_min, int *out_max, int *out_step,
583 int *new_left, int *new_right)
585 int rc = MMSYSERR_NOERROR;
587 snd_hctl_elem_t * elem = NULL;
588 snd_ctl_elem_info_t * eleminfop = NULL;
589 snd_ctl_elem_value_t * elemvaluep = NULL;
590 snd_ctl_elem_id_t * elemidp = NULL;
593 #define EXIT_ON_ERROR(f,txt,exitcode) do \
596 if ( (err = (f) ) < 0) \
598 ERR(txt " failed: %s\n", snd_strerror(err)); \
605 return MMSYSERR_NOTSUPPORTED;
607 /* Allocate areas to return information about the volume */
608 EXIT_ON_ERROR(snd_ctl_elem_id_malloc(&elemidp), "snd_ctl_elem_id_malloc", MMSYSERR_NOMEM);
609 EXIT_ON_ERROR(snd_ctl_elem_value_malloc (&elemvaluep), "snd_ctl_elem_value_malloc", MMSYSERR_NOMEM);
610 EXIT_ON_ERROR(snd_ctl_elem_info_malloc (&eleminfop), "snd_ctl_elem_info_malloc", MMSYSERR_NOMEM);
611 snd_ctl_elem_id_clear(elemidp);
612 snd_ctl_elem_value_clear(elemvaluep);
613 snd_ctl_elem_info_clear(eleminfop);
615 /* Setup and find an element id that exactly matches the characteristic we want
616 ** FIXME: It is probably short sighted to hard code and fixate on PCM Playback Volume */
617 snd_ctl_elem_id_set_name(elemidp, "PCM Playback Volume");
618 snd_ctl_elem_id_set_interface(elemidp, SND_CTL_ELEM_IFACE_MIXER);
619 elem = snd_hctl_find_elem(hctl, elemidp);
622 /* Read and return volume information */
623 EXIT_ON_ERROR(snd_hctl_elem_info(elem, eleminfop), "snd_hctl_elem_info", MMSYSERR_NOTSUPPORTED);
624 value_count = snd_ctl_elem_info_get_count(eleminfop);
625 if (out_min || out_max || out_step)
627 if (!snd_ctl_elem_info_is_readable(eleminfop))
629 ERR("snd_ctl_elem_info_is_readable returned false; cannot return info\n");
630 rc = MMSYSERR_NOTSUPPORTED;
635 *out_min = snd_ctl_elem_info_get_min(eleminfop);
638 *out_max = snd_ctl_elem_info_get_max(eleminfop);
641 *out_step = snd_ctl_elem_info_get_step(eleminfop);
644 if (out_left || out_right)
646 EXIT_ON_ERROR(snd_hctl_elem_read(elem, elemvaluep), "snd_hctl_elem_read", MMSYSERR_NOTSUPPORTED);
649 *out_left = snd_ctl_elem_value_get_integer(elemvaluep, 0);
653 if (value_count == 1)
654 *out_right = snd_ctl_elem_value_get_integer(elemvaluep, 0);
655 else if (value_count == 2)
656 *out_right = snd_ctl_elem_value_get_integer(elemvaluep, 1);
659 ERR("Unexpected value count %d from snd_ctl_elem_info_get_count while getting volume info\n", value_count);
667 if (new_left || new_right)
669 EXIT_ON_ERROR(snd_hctl_elem_read(elem, elemvaluep), "snd_hctl_elem_read", MMSYSERR_NOTSUPPORTED);
671 snd_ctl_elem_value_set_integer(elemvaluep, 0, *new_left);
674 if (value_count == 1)
675 snd_ctl_elem_value_set_integer(elemvaluep, 0, *new_right);
676 else if (value_count == 2)
677 snd_ctl_elem_value_set_integer(elemvaluep, 1, *new_right);
680 ERR("Unexpected value count %d from snd_ctl_elem_info_get_count while setting volume info\n", value_count);
686 EXIT_ON_ERROR(snd_hctl_elem_write(elem, elemvaluep), "snd_hctl_elem_write", MMSYSERR_NOTSUPPORTED);
691 ERR("Could not find 'PCM Playback Volume' element\n");
692 rc = MMSYSERR_NOTSUPPORTED;
701 snd_ctl_elem_value_free(elemvaluep);
703 snd_ctl_elem_info_free(eleminfop);
705 snd_ctl_elem_id_free(elemidp);
711 /**************************************************************************
712 * ALSA_XRUNRecovery [internal]
714 * used to recovery from XRUN errors (buffer underflow/overflow)
716 static int ALSA_XRUNRecovery(WINE_WAVEDEV * wwo, int err)
718 if (err == -EPIPE) { /* under-run */
719 err = snd_pcm_prepare(wwo->pcm);
721 ERR( "underrun recovery failed. prepare failed: %s\n", snd_strerror(err));
723 } else if (err == -ESTRPIPE) {
724 while ((err = snd_pcm_resume(wwo->pcm)) == -EAGAIN)
725 sleep(1); /* wait until the suspend flag is released */
727 err = snd_pcm_prepare(wwo->pcm);
729 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
736 /**************************************************************************
737 * ALSA_TraceParameters [internal]
739 * used to trace format changes, hw and sw parameters
741 static void ALSA_TraceParameters(snd_pcm_hw_params_t * hw_params, snd_pcm_sw_params_t * sw, int full)
744 snd_pcm_format_t format;
745 snd_pcm_access_t access;
746 err = snd_pcm_hw_params_get_access(hw_params, &access);
747 err = snd_pcm_hw_params_get_format(hw_params, &format);
749 #define X(x) ((x)? "true" : "false")
751 TRACE("FLAGS: sampleres=%s overrng=%s pause=%s resume=%s syncstart=%s batch=%s block=%s double=%s "
752 "halfd=%s joint=%s\n",
753 X(snd_pcm_hw_params_can_mmap_sample_resolution(hw_params)),
754 X(snd_pcm_hw_params_can_overrange(hw_params)),
755 X(snd_pcm_hw_params_can_pause(hw_params)),
756 X(snd_pcm_hw_params_can_resume(hw_params)),
757 X(snd_pcm_hw_params_can_sync_start(hw_params)),
758 X(snd_pcm_hw_params_is_batch(hw_params)),
759 X(snd_pcm_hw_params_is_block_transfer(hw_params)),
760 X(snd_pcm_hw_params_is_double(hw_params)),
761 X(snd_pcm_hw_params_is_half_duplex(hw_params)),
762 X(snd_pcm_hw_params_is_joint_duplex(hw_params)));
766 TRACE("access=%s\n", snd_pcm_access_name(access));
769 snd_pcm_access_mask_t * acmask;
770 snd_pcm_access_mask_alloca(&acmask);
771 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
772 for ( access = SND_PCM_ACCESS_MMAP_INTERLEAVED; access <= SND_PCM_ACCESS_LAST; access++)
773 if (snd_pcm_access_mask_test(acmask, access))
774 TRACE("access=%s\n", snd_pcm_access_name(access));
779 TRACE("format=%s\n", snd_pcm_format_name(format));
784 snd_pcm_format_mask_t * fmask;
786 snd_pcm_format_mask_alloca(&fmask);
787 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
788 for ( format = SND_PCM_FORMAT_S8; format <= SND_PCM_FORMAT_LAST ; format++)
789 if ( snd_pcm_format_mask_test(fmask, format) )
790 TRACE("format=%s\n", snd_pcm_format_name(format));
796 err = snd_pcm_hw_params_get_channels(hw_params, &val);
798 unsigned int min = 0;
799 unsigned int max = 0;
800 err = snd_pcm_hw_params_get_channels_min(hw_params, &min),
801 err = snd_pcm_hw_params_get_channels_max(hw_params, &max);
802 TRACE("channels_min=%u, channels_min_max=%u\n", min, max);
804 TRACE("channels=%d\n", val);
809 snd_pcm_uframes_t val=0;
810 err = snd_pcm_hw_params_get_buffer_size(hw_params, &val);
812 snd_pcm_uframes_t min = 0;
813 snd_pcm_uframes_t max = 0;
814 err = snd_pcm_hw_params_get_buffer_size_min(hw_params, &min),
815 err = snd_pcm_hw_params_get_buffer_size_max(hw_params, &max);
816 TRACE("buffer_size_min=%lu, buffer_size_min_max=%lu\n", min, max);
818 TRACE("buffer_size=%lu\n", val);
825 unsigned int val=0; \
826 err = snd_pcm_hw_params_get_##x(hw_params,&val, &dir); \
828 unsigned int min = 0; \
829 unsigned int max = 0; \
830 err = snd_pcm_hw_params_get_##x##_min(hw_params, &min, &dir); \
831 err = snd_pcm_hw_params_get_##x##_max(hw_params, &max, &dir); \
832 TRACE(#x "_min=%u " #x "_max=%u\n", min, max); \
834 TRACE(#x "=%d\n", val); \
843 snd_pcm_uframes_t val=0;
844 err = snd_pcm_hw_params_get_period_size(hw_params, &val, &dir);
846 snd_pcm_uframes_t min = 0;
847 snd_pcm_uframes_t max = 0;
848 err = snd_pcm_hw_params_get_period_size_min(hw_params, &min, &dir),
849 err = snd_pcm_hw_params_get_period_size_max(hw_params, &max, &dir);
850 TRACE("period_size_min=%lu, period_size_min_max=%lu\n", min, max);
852 TRACE("period_size=%lu\n", val);
864 /* return a string duplicated on the win32 process heap, free with HeapFree */
865 static char* ALSA_strdup(const char *s) {
866 char *result = HeapAlloc(GetProcessHeap(), 0, strlen(s)+1);
873 #define ALSA_RETURN_ONFAIL(mycall) \
879 ERR("%s failed: %s(%d)\n", #mycall, snd_strerror(rc), rc); \
884 /*----------------------------------------------------------------------------
887 ** Given an ALSA PCM, figure out our HW CAPS structure info.
888 ** ctl can be null, pcm is required, as is all output parms.
891 static int ALSA_ComputeCaps(snd_ctl_t *ctl, snd_pcm_t *pcm,
892 WORD *channels, DWORD *flags, DWORD *formats, DWORD *supports)
894 snd_pcm_hw_params_t *hw_params;
895 snd_pcm_format_mask_t *fmask;
896 snd_pcm_access_mask_t *acmask;
897 unsigned int ratemin = 0;
898 unsigned int ratemax = 0;
899 unsigned int chmin = 0;
900 unsigned int chmax = 0;
903 snd_pcm_hw_params_alloca(&hw_params);
904 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_any(pcm, hw_params));
906 snd_pcm_format_mask_alloca(&fmask);
907 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
909 snd_pcm_access_mask_alloca(&acmask);
910 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_access_mask(hw_params, acmask));
912 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_rate_min(hw_params, &ratemin, &dir));
913 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_rate_max(hw_params, &ratemax, &dir));
914 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_channels_min(hw_params, &chmin));
915 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_channels_max(hw_params, &chmax));
918 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
920 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
922 if (chmin <= 1 && 1 <= chmax) \
923 *formats |= WAVE_FORMAT_##v##M08; \
924 if (chmin <= 2 && 2 <= chmax) \
925 *formats |= WAVE_FORMAT_##v##S08; \
927 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
929 if (chmin <= 1 && 1 <= chmax) \
930 *formats |= WAVE_FORMAT_##v##M16; \
931 if (chmin <= 2 && 2 <= chmax) \
932 *formats |= WAVE_FORMAT_##v##S16; \
943 FIXME("Device has a minimum of %d channels\n", chmin);
946 /* FIXME: is sample accurate always true ?
947 ** Can we do WAVECAPS_PITCH, WAVECAPS_SYNC, or WAVECAPS_PLAYBACKRATE? */
948 *supports |= WAVECAPS_SAMPLEACCURATE;
950 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
951 if ( snd_pcm_access_mask_test( acmask, SND_PCM_ACCESS_MMAP_INTERLEAVED ) )
952 *supports |= WAVECAPS_DIRECTSOUND;
954 /* check for volume control support */
956 *supports |= WAVECAPS_VOLUME;
958 if (chmin <= 2 && 2 <= chmax)
959 *supports |= WAVECAPS_LRVOLUME;
962 if (*formats & (WAVE_FORMAT_1M08 | WAVE_FORMAT_2M08 |
963 WAVE_FORMAT_4M08 | WAVE_FORMAT_48M08 |
964 WAVE_FORMAT_96M08 | WAVE_FORMAT_1M16 |
965 WAVE_FORMAT_2M16 | WAVE_FORMAT_4M16 |
966 WAVE_FORMAT_48M16 | WAVE_FORMAT_96M16) )
967 *flags |= DSCAPS_PRIMARYMONO;
969 if (*formats & (WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 |
970 WAVE_FORMAT_4S08 | WAVE_FORMAT_48S08 |
971 WAVE_FORMAT_96S08 | WAVE_FORMAT_1S16 |
972 WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16 |
973 WAVE_FORMAT_48S16 | WAVE_FORMAT_96S16) )
974 *flags |= DSCAPS_PRIMARYSTEREO;
976 if (*formats & (WAVE_FORMAT_1M08 | WAVE_FORMAT_2M08 |
977 WAVE_FORMAT_4M08 | WAVE_FORMAT_48M08 |
978 WAVE_FORMAT_96M08 | WAVE_FORMAT_1S08 |
979 WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 |
980 WAVE_FORMAT_48S08 | WAVE_FORMAT_96S08) )
981 *flags |= DSCAPS_PRIMARY8BIT;
983 if (*formats & (WAVE_FORMAT_1M16 | WAVE_FORMAT_2M16 |
984 WAVE_FORMAT_4M16 | WAVE_FORMAT_48M16 |
985 WAVE_FORMAT_96M16 | WAVE_FORMAT_1S16 |
986 WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16 |
987 WAVE_FORMAT_48S16 | WAVE_FORMAT_96S16) )
988 *flags |= DSCAPS_PRIMARY16BIT;
993 /*----------------------------------------------------------------------------
994 ** ALSA_AddCommonDevice
996 ** Perform Alsa initialization common to both capture and playback
998 ** Side Effect: ww->pcname and ww->ctlname may need to be freed.
1000 ** Note: this was originally coded by using snd_pcm_name(pcm), until
1001 ** I discovered that with at least one version of alsa lib,
1002 ** the use of a pcm named default:0 would cause snd_pcm_name() to fail.
1003 ** So passing the name in is logically extraneous. Sigh.
1005 static int ALSA_AddCommonDevice(snd_ctl_t *ctl, snd_pcm_t *pcm, const char *pcmname, WINE_WAVEDEV *ww)
1007 snd_pcm_info_t *infop;
1009 snd_pcm_info_alloca(&infop);
1010 ALSA_RETURN_ONFAIL(snd_pcm_info(pcm, infop));
1013 ww->pcmname = ALSA_strdup(pcmname);
1017 if (ctl && snd_ctl_name(ctl))
1018 ww->ctlname = ALSA_strdup(snd_ctl_name(ctl));
1020 strcpy(ww->interface_name, "winealsa: ");
1021 memcpy(ww->interface_name + strlen(ww->interface_name),
1023 min(strlen(ww->pcmname), sizeof(ww->interface_name) - strlen("winealsa: ")));
1025 strcpy(ww->ds_desc.szDrvname, "winealsa.drv");
1027 memcpy(ww->ds_desc.szDesc, snd_pcm_info_get_name(infop),
1028 min( (sizeof(ww->ds_desc.szDesc) - 1), strlen(snd_pcm_info_get_name(infop))) );
1030 ww->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
1031 ww->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
1032 ww->ds_caps.dwPrimaryBuffers = 1;
1037 /*----------------------------------------------------------------------------
1040 static void ALSA_FreeDevice(WINE_WAVEDEV *ww)
1042 HeapFree(GetProcessHeap(), 0, ww->pcmname);
1045 HeapFree(GetProcessHeap(), 0, ww->ctlname);
1049 /*----------------------------------------------------------------------------
1050 ** ALSA_AddDeviceToArray
1052 ** Dynamically size one of the wavein or waveout arrays of devices,
1053 ** and add a fully configured device node to the array.
1056 static int ALSA_AddDeviceToArray(WINE_WAVEDEV *ww, WINE_WAVEDEV **array,
1057 DWORD *count, DWORD *alloced, int isdefault)
1061 if (*count >= *alloced)
1063 (*alloced) += WAVEDEV_ALLOC_EXTENT_SIZE;
1065 *array = HeapAlloc(GetProcessHeap(), 0, sizeof(*ww) * (*alloced));
1067 *array = HeapReAlloc(GetProcessHeap(), 0, *array, sizeof(*ww) * (*alloced));
1075 /* If this is the default, arrange for it to be the first element */
1076 if (isdefault && i > 0)
1078 (*array)[*count] = (*array)[0];
1088 /*----------------------------------------------------------------------------
1089 ** ALSA_AddPlaybackDevice
1091 ** Add a given Alsa device to Wine's internal list of Playback
1094 static int ALSA_AddPlaybackDevice(snd_ctl_t *ctl, snd_pcm_t *pcm, const char *pcmname, int isdefault)
1099 memset(&wwo, '\0', sizeof(wwo));
1101 rc = ALSA_AddCommonDevice(ctl, pcm, pcmname, &wwo);
1105 MultiByteToWideChar(CP_ACP, 0, wwo.ds_desc.szDesc, -1,
1106 wwo.outcaps.szPname, sizeof(wwo.outcaps.szPname)/sizeof(WCHAR));
1107 wwo.outcaps.szPname[sizeof(wwo.outcaps.szPname)/sizeof(WCHAR) - 1] = '\0';
1109 wwo.outcaps.wMid = MM_CREATIVE;
1110 wwo.outcaps.wPid = MM_CREATIVE_SBP16_WAVEOUT;
1111 wwo.outcaps.vDriverVersion = 0x0100;
1113 rc = ALSA_ComputeCaps(ctl, pcm, &wwo.outcaps.wChannels, &wwo.ds_caps.dwFlags,
1114 &wwo.outcaps.dwFormats, &wwo.outcaps.dwSupport);
1117 WARN("Error calculating device caps for pcm [%s]\n", wwo.pcmname);
1118 ALSA_FreeDevice(&wwo);
1122 rc = ALSA_AddDeviceToArray(&wwo, &WOutDev, &ALSA_WodNumDevs, &ALSA_WodNumMallocedDevs, isdefault);
1124 ALSA_FreeDevice(&wwo);
1128 /*----------------------------------------------------------------------------
1129 ** ALSA_AddCaptureDevice
1131 ** Add a given Alsa device to Wine's internal list of Capture
1134 static int ALSA_AddCaptureDevice(snd_ctl_t *ctl, snd_pcm_t *pcm, const char *pcmname, int isdefault)
1139 memset(&wwi, '\0', sizeof(wwi));
1141 rc = ALSA_AddCommonDevice(ctl, pcm, pcmname, &wwi);
1145 MultiByteToWideChar(CP_ACP, 0, wwi.ds_desc.szDesc, -1,
1146 wwi.incaps.szPname, sizeof(wwi.incaps.szPname) / sizeof(WCHAR));
1147 wwi.incaps.szPname[sizeof(wwi.incaps.szPname)/sizeof(WCHAR) - 1] = '\0';
1149 wwi.incaps.wMid = MM_CREATIVE;
1150 wwi.incaps.wPid = MM_CREATIVE_SBP16_WAVEOUT;
1151 wwi.incaps.vDriverVersion = 0x0100;
1153 rc = ALSA_ComputeCaps(ctl, pcm, &wwi.incaps.wChannels, &wwi.ds_caps.dwFlags,
1154 &wwi.incaps.dwFormats, &wwi.dwSupport);
1157 WARN("Error calculating device caps for pcm [%s]\n", wwi.pcmname);
1158 ALSA_FreeDevice(&wwi);
1162 rc = ALSA_AddDeviceToArray(&wwi, &WInDev, &ALSA_WidNumDevs, &ALSA_WidNumMallocedDevs, isdefault);
1164 ALSA_FreeDevice(&wwi);
1168 /*----------------------------------------------------------------------------
1169 ** ALSA_CheckEnvironment
1171 ** Given an Alsa style configuration node, scan its subitems
1172 ** for environment variable names, and use them to find an override,
1174 ** This is essentially a long and convolunted way of doing:
1175 ** getenv("ALSA_CARD")
1176 ** getenv("ALSA_CTL_CARD")
1177 ** getenv("ALSA_PCM_CARD")
1178 ** getenv("ALSA_PCM_DEVICE")
1180 ** The output value is set with the atoi() of the first environment
1181 ** variable found to be set, if any; otherwise, it is left alone
1183 static void ALSA_CheckEnvironment(snd_config_t *node, int *outvalue)
1185 snd_config_iterator_t iter;
1187 for (iter = snd_config_iterator_first(node);
1188 iter != snd_config_iterator_end(node);
1189 iter = snd_config_iterator_next(iter))
1191 snd_config_t *leaf = snd_config_iterator_entry(iter);
1192 if (snd_config_get_type(leaf) == SND_CONFIG_TYPE_STRING)
1195 if (snd_config_get_string(leaf, &value) >= 0)
1197 char *p = getenv(value);
1200 *outvalue = atoi(p);
1208 /*----------------------------------------------------------------------------
1209 ** ALSA_DefaultDevices
1211 ** Jump through Alsa style hoops to (hopefully) properly determine
1212 ** Alsa defaults for CTL Card #, as well as for PCM Card + Device #.
1213 ** We'll also find out if the user has set any of the environment
1214 ** variables that specify we're to use a specific card or device.
1217 ** directhw Whether to use a direct hardware device or not;
1218 ** essentially switches the pcm device name from
1219 ** one of 'default:X' or 'plughw:X' to "hw:X"
1220 ** defctlcard If !NULL, will hold the ctl card number given
1221 ** by the ALSA config as the default
1222 ** defpcmcard If !NULL, default pcm card #
1223 ** defpcmdev If !NULL, default pcm device #
1224 ** fixedctlcard If !NULL, and the user set the appropriate
1225 ** environment variable, we'll set to the
1226 ** card the user specified.
1227 ** fixedpcmcard If !NULL, and the user set the appropriate
1228 ** environment variable, we'll set to the
1229 ** card the user specified.
1230 ** fixedpcmdev If !NULL, and the user set the appropriate
1231 ** environment variable, we'll set to the
1232 ** device the user specified.
1234 ** Returns: 0 on success, < 0 on failiure
1236 static int ALSA_DefaultDevices(int directhw,
1238 long *defpcmcard, long *defpcmdev,
1240 int *fixedpcmcard, int *fixedpcmdev)
1242 snd_config_t *configp;
1243 char pcmsearch[256];
1245 ALSA_RETURN_ONFAIL(snd_config_update());
1248 if (snd_config_search(snd_config, "defaults.ctl.card", &configp) >= 0)
1249 snd_config_get_integer(configp, defctlcard);
1252 if (snd_config_search(snd_config, "defaults.pcm.card", &configp) >= 0)
1253 snd_config_get_integer(configp, defpcmcard);
1256 if (snd_config_search(snd_config, "defaults.pcm.device", &configp) >= 0)
1257 snd_config_get_integer(configp, defpcmdev);
1262 if (snd_config_search(snd_config, "ctl.hw.@args.CARD.default.vars", &configp) >= 0)
1263 ALSA_CheckEnvironment(configp, fixedctlcard);
1268 sprintf(pcmsearch, "pcm.%s.@args.CARD.default.vars", directhw ? "hw" : "plughw");
1269 if (snd_config_search(snd_config, pcmsearch, &configp) >= 0)
1270 ALSA_CheckEnvironment(configp, fixedpcmcard);
1275 sprintf(pcmsearch, "pcm.%s.@args.DEV.default.vars", directhw ? "hw" : "plughw");
1276 if (snd_config_search(snd_config, pcmsearch, &configp) >= 0)
1277 ALSA_CheckEnvironment(configp, fixedpcmdev);
1284 /*----------------------------------------------------------------------------
1287 ** Iterate through all discoverable ALSA cards, searching
1288 ** for usable PCM devices.
1291 ** directhw Whether to use a direct hardware device or not;
1292 ** essentially switches the pcm device name from
1293 ** one of 'default:X' or 'plughw:X' to "hw:X"
1294 ** defctlcard Alsa's notion of the default ctl card.
1295 ** defpcmcard . pcm card
1296 ** defpcmdev . pcm device
1297 ** fixedctlcard If not -1, then gives the value of ALSA_CTL_CARD
1298 ** or equivalent environment variable
1299 ** fixedpcmcard If not -1, then gives the value of ALSA_PCM_CARD
1300 ** or equivalent environment variable
1301 ** fixedpcmdev If not -1, then gives the value of ALSA_PCM_DEVICE
1302 ** or equivalent environment variable
1304 ** Returns: 0 on success, < 0 on failiure
1306 static int ALSA_ScanDevices(int directhw,
1307 long defctlcard, long defpcmcard, long defpcmdev,
1308 int fixedctlcard, int fixedpcmcard, int fixedpcmdev)
1310 int card = fixedpcmcard;
1311 int scan_devices = (fixedpcmdev == -1);
1313 /*------------------------------------------------------------------------
1314 ** Loop through all available cards
1315 **----------------------------------------------------------------------*/
1317 snd_card_next(&card);
1319 for (; card != -1; snd_card_next(&card))
1326 /*--------------------------------------------------------------------
1327 ** Try to open a ctl handle; Wine doesn't absolutely require one,
1328 ** but it does allow for volume control and for device scanning
1329 **------------------------------------------------------------------*/
1330 sprintf(ctlname, "default:%d", fixedctlcard == -1 ? card : fixedctlcard);
1331 rc = snd_ctl_open(&ctl, ctlname, SND_CTL_NONBLOCK);
1334 sprintf(ctlname, "hw:%d", fixedctlcard == -1 ? card : fixedctlcard);
1335 rc = snd_ctl_open(&ctl, ctlname, SND_CTL_NONBLOCK);
1340 WARN("Unable to open an alsa ctl for [%s] (pcm card %d): %s; not scanning devices\n",
1341 ctlname, card, snd_strerror(rc));
1342 if (fixedpcmdev == -1)
1346 /*--------------------------------------------------------------------
1347 ** Loop through all available devices on this card
1348 **------------------------------------------------------------------*/
1349 device = fixedpcmdev;
1351 snd_ctl_pcm_next_device(ctl, &device);
1353 for (; device != -1; snd_ctl_pcm_next_device(ctl, &device))
1355 char defaultpcmname[256];
1356 char plugpcmname[256];
1357 char hwpcmname[256];
1358 char *pcmname = NULL;
1361 sprintf(defaultpcmname, "default:%d", card);
1362 sprintf(plugpcmname, "plughw:%d,%d", card, device);
1363 sprintf(hwpcmname, "hw:%d,%d", card, device);
1365 /*----------------------------------------------------------------
1366 ** See if it's a valid playback device
1367 **--------------------------------------------------------------*/
1368 if (ALSA_TestDeviceForWine(card, device, SND_PCM_STREAM_PLAYBACK) == 0)
1370 /* If we can, try the default:X device name first */
1371 if (! scan_devices && ! directhw)
1373 pcmname = defaultpcmname;
1374 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
1381 pcmname = directhw ? hwpcmname : plugpcmname;
1382 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
1387 if (defctlcard == card && defpcmcard == card && defpcmdev == device)
1388 ALSA_AddPlaybackDevice(ctl, pcm, pcmname, TRUE);
1390 ALSA_AddPlaybackDevice(ctl, pcm, pcmname, FALSE);
1395 TRACE("Device [%s/%s] failed to open for playback: %s\n",
1396 directhw || scan_devices ? "(N/A)" : defaultpcmname,
1397 directhw ? hwpcmname : plugpcmname,
1402 /*----------------------------------------------------------------
1403 ** See if it's a valid capture device
1404 **--------------------------------------------------------------*/
1405 if (ALSA_TestDeviceForWine(card, device, SND_PCM_STREAM_CAPTURE) == 0)
1407 /* If we can, try the default:X device name first */
1408 if (! scan_devices && ! directhw)
1410 pcmname = defaultpcmname;
1411 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
1418 pcmname = directhw ? hwpcmname : plugpcmname;
1419 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
1424 if (defctlcard == card && defpcmcard == card && defpcmdev == device)
1425 ALSA_AddCaptureDevice(ctl, pcm, pcmname, TRUE);
1427 ALSA_AddCaptureDevice(ctl, pcm, pcmname, FALSE);
1433 TRACE("Device [%s/%s] failed to open for capture: %s\n",
1434 directhw || scan_devices ? "(N/A)" : defaultpcmname,
1435 directhw ? hwpcmname : plugpcmname,
1447 /*--------------------------------------------------------------------
1448 ** If the user has set env variables such that we're pegged to
1449 ** a specific card, then break after we've examined it
1450 **------------------------------------------------------------------*/
1451 if (fixedpcmcard != -1)
1459 /*----------------------------------------------------------------------------
1460 ** ALSA_PerformDefaultScan
1461 ** Perform the basic default scanning for devices within ALSA.
1462 ** The hope is that this routine implements a 'correct'
1463 ** scanning algorithm from the Alsalib point of view.
1465 ** Note that Wine, overall, has other mechanisms to
1466 ** override and specify exact CTL and PCM device names,
1467 ** but this routine is imagined as the default that
1468 ** 99% of users will use.
1470 ** The basic algorithm is simple:
1471 ** Use snd_card_next to iterate cards; within cards, use
1472 ** snd_ctl_pcm_next_device to iterate through devices.
1474 ** We add a little complexity by taking into consideration
1475 ** environment variables such as ALSA_CARD (et all), and by
1476 ** detecting when a given device matches the default specified
1480 ** directhw If !0, indicates we should use the hw:X
1481 ** PCM interface, rather than first try
1482 ** the 'default' device followed by the plughw
1483 ** device. (default and plughw do fancy mixing
1484 ** and audio scaling, if they are available).
1485 ** devscan If TRUE, we should scan all devices, not
1486 ** juse use device 0 on each card
1492 ** Invokes the ALSA_AddXXXDevice functions on valid
1495 static int ALSA_PerformDefaultScan(int directhw, BOOL devscan)
1497 long defctlcard = -1, defpcmcard = -1, defpcmdev = -1;
1498 int fixedctlcard = -1, fixedpcmcard = -1, fixedpcmdev = -1;
1501 /* FIXME: We should dlsym the new snd_names_list/snd_names_list_free 1.0.9 apis,
1502 ** and use them instead of this scan mechanism if they are present */
1504 rc = ALSA_DefaultDevices(directhw, &defctlcard, &defpcmcard, &defpcmdev,
1505 &fixedctlcard, &fixedpcmcard, &fixedpcmdev);
1509 if (fixedpcmdev == -1 && ! devscan)
1512 return(ALSA_ScanDevices(directhw, defctlcard, defpcmcard, defpcmdev, fixedctlcard, fixedpcmcard, fixedpcmdev));
1516 /*----------------------------------------------------------------------------
1517 ** ALSA_AddUserSpecifiedDevice
1518 ** Add a device given from the registry
1520 static int ALSA_AddUserSpecifiedDevice(const char *ctlname, const char *pcmname)
1524 snd_ctl_t *ctl = NULL;
1525 snd_pcm_t *pcm = NULL;
1529 rc = snd_ctl_open(&ctl, ctlname, SND_CTL_NONBLOCK);
1534 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
1537 ALSA_AddPlaybackDevice(ctl, pcm, pcmname, FALSE);
1542 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
1545 ALSA_AddCaptureDevice(ctl, pcm, pcmname, FALSE);
1557 /*----------------------------------------------------------------------------
1559 ** Initialize the Wine Alsa sub system.
1560 ** The main task is to probe for and store a list of all appropriate playback
1561 ** and capture devices.
1562 ** Key control points are from the registry key:
1563 ** [Software\Wine\Alsa Driver]
1564 ** AutoScanCards Whether or not to scan all known sound cards
1565 ** and add them to Wine's list (default yes)
1566 ** AutoScanDevices Whether or not to scan all known PCM devices
1567 ** on each card (default no)
1568 ** UseDirectHW Whether or not to use the hw:X device,
1569 ** instead of the fancy default:X or plughw:X device.
1570 ** The hw:X device goes straight to the hardware
1571 ** without any fancy mixing or audio scaling in between.
1572 ** DeviceCount If present, specifies the number of hard coded
1573 ** Alsa devices to add to Wine's list; default 0
1574 ** DevicePCMn Specifies the Alsa PCM devices to open for
1575 ** Device n (where n goes from 1 to DeviceCount)
1576 ** DeviceCTLn Specifies the Alsa control devices to open for
1577 ** Device n (where n goes from 1 to DeviceCount)
1579 ** Using AutoScanCards no, and then Devicexxx info
1580 ** is a way to exactly specify the devices used by Wine.
1583 LONG ALSA_WaveInit(void)
1586 BOOL AutoScanCards = TRUE;
1587 BOOL AutoScanDevices = FALSE;
1588 BOOL UseDirectHW = FALSE;
1589 DWORD DeviceCount = 0;
1593 if (!wine_dlopen("libasound.so.2", RTLD_LAZY|RTLD_GLOBAL, NULL, 0))
1595 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
1599 /* @@ Wine registry key: HKCU\Software\Wine\Alsa Driver */
1600 rc = RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Wine\\Alsa Driver", 0, KEY_QUERY_VALUE, &key);
1601 if (rc == ERROR_SUCCESS)
1603 ALSA_RegGetBoolean(key, "AutoScanCards", &AutoScanCards);
1604 ALSA_RegGetBoolean(key, "AutoScanDevices", &AutoScanDevices);
1605 ALSA_RegGetBoolean(key, "UseDirectHW", &UseDirectHW);
1606 ALSA_RegGetInt(key, "DeviceCount", &DeviceCount);
1610 rc = ALSA_PerformDefaultScan(UseDirectHW, AutoScanDevices);
1612 for (i = 0; i < DeviceCount; i++)
1614 char *ctl_name = NULL;
1615 char *pcm_name = NULL;
1618 sprintf(value, "DevicePCM%d", i + 1);
1619 if (ALSA_RegGetString(key, value, &pcm_name) == ERROR_SUCCESS)
1621 sprintf(value, "DeviceCTL%d", i + 1);
1622 ALSA_RegGetString(key, value, &ctl_name);
1623 ALSA_AddUserSpecifiedDevice(ctl_name, pcm_name);
1626 HeapFree(GetProcessHeap(), 0, ctl_name);
1627 HeapFree(GetProcessHeap(), 0, pcm_name);
1636 /******************************************************************
1637 * ALSA_InitRingMessage
1639 * Initialize the ring of messages for passing between driver's caller and playback/record
1642 static int ALSA_InitRingMessage(ALSA_MSG_RING* omr)
1645 omr->msg_tosave = 0;
1646 #ifdef USE_PIPE_SYNC
1647 if (pipe(omr->msg_pipe) < 0) {
1648 omr->msg_pipe[0] = -1;
1649 omr->msg_pipe[1] = -1;
1650 ERR("could not create pipe, error=%s\n", strerror(errno));
1653 omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1655 omr->ring_buffer_size = ALSA_RING_BUFFER_INCREMENT;
1656 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(ALSA_MSG));
1658 InitializeCriticalSection(&omr->msg_crst);
1659 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)"WINEALSA_msg_crst";
1663 /******************************************************************
1664 * ALSA_DestroyRingMessage
1667 static int ALSA_DestroyRingMessage(ALSA_MSG_RING* omr)
1669 #ifdef USE_PIPE_SYNC
1670 close(omr->msg_pipe[0]);
1671 close(omr->msg_pipe[1]);
1673 CloseHandle(omr->msg_event);
1675 HeapFree(GetProcessHeap(),0,omr->messages);
1676 omr->ring_buffer_size = 0;
1677 omr->msg_crst.DebugInfo->Spare[0] = 0;
1678 DeleteCriticalSection(&omr->msg_crst);
1682 /******************************************************************
1683 * ALSA_AddRingMessage
1685 * Inserts a new message into the ring (should be called from DriverProc derivated routines)
1687 static int ALSA_AddRingMessage(ALSA_MSG_RING* omr, enum win_wm_message msg, DWORD param, BOOL wait)
1689 HANDLE hEvent = INVALID_HANDLE_VALUE;
1691 EnterCriticalSection(&omr->msg_crst);
1692 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
1694 int old_ring_buffer_size = omr->ring_buffer_size;
1695 omr->ring_buffer_size += ALSA_RING_BUFFER_INCREMENT;
1696 TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
1697 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(ALSA_MSG));
1698 /* Now we need to rearrange the ring buffer so that the new
1699 buffers just allocated are in between omr->msg_tosave and
1702 if (omr->msg_tosave < omr->msg_toget)
1704 memmove(&(omr->messages[omr->msg_toget + ALSA_RING_BUFFER_INCREMENT]),
1705 &(omr->messages[omr->msg_toget]),
1706 sizeof(ALSA_MSG)*(old_ring_buffer_size - omr->msg_toget)
1708 omr->msg_toget += ALSA_RING_BUFFER_INCREMENT;
1713 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1714 if (hEvent == INVALID_HANDLE_VALUE)
1716 ERR("can't create event !?\n");
1717 LeaveCriticalSection(&omr->msg_crst);
1720 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
1721 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
1722 omr->msg_toget,getCmdString(omr->messages[omr->msg_toget].msg),
1723 omr->msg_tosave,getCmdString(omr->messages[omr->msg_tosave].msg));
1725 /* fast messages have to be added at the start of the queue */
1726 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
1728 omr->messages[omr->msg_toget].msg = msg;
1729 omr->messages[omr->msg_toget].param = param;
1730 omr->messages[omr->msg_toget].hEvent = hEvent;
1734 omr->messages[omr->msg_tosave].msg = msg;
1735 omr->messages[omr->msg_tosave].param = param;
1736 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
1737 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
1739 LeaveCriticalSection(&omr->msg_crst);
1740 /* signal a new message */
1744 /* wait for playback/record thread to have processed the message */
1745 WaitForSingleObject(hEvent, INFINITE);
1746 CloseHandle(hEvent);
1751 /******************************************************************
1752 * ALSA_RetrieveRingMessage
1754 * Get a message from the ring. Should be called by the playback/record thread.
1756 static int ALSA_RetrieveRingMessage(ALSA_MSG_RING* omr,
1757 enum win_wm_message *msg, DWORD *param, HANDLE *hEvent)
1759 EnterCriticalSection(&omr->msg_crst);
1761 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1763 LeaveCriticalSection(&omr->msg_crst);
1767 *msg = omr->messages[omr->msg_toget].msg;
1768 omr->messages[omr->msg_toget].msg = 0;
1769 *param = omr->messages[omr->msg_toget].param;
1770 *hEvent = omr->messages[omr->msg_toget].hEvent;
1771 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
1773 LeaveCriticalSection(&omr->msg_crst);
1777 /******************************************************************
1778 * ALSA_PeekRingMessage
1780 * Peek at a message from the ring but do not remove it.
1781 * Should be called by the playback/record thread.
1783 static int ALSA_PeekRingMessage(ALSA_MSG_RING* omr,
1784 enum win_wm_message *msg,
1785 DWORD *param, HANDLE *hEvent)
1787 EnterCriticalSection(&omr->msg_crst);
1789 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
1791 LeaveCriticalSection(&omr->msg_crst);
1795 *msg = omr->messages[omr->msg_toget].msg;
1796 *param = omr->messages[omr->msg_toget].param;
1797 *hEvent = omr->messages[omr->msg_toget].hEvent;
1798 LeaveCriticalSection(&omr->msg_crst);
1802 /*======================================================================*
1803 * Low level WAVE OUT implementation *
1804 *======================================================================*/
1806 /**************************************************************************
1807 * wodNotifyClient [internal]
1809 static DWORD wodNotifyClient(WINE_WAVEDEV* wwo, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
1811 TRACE("wMsg = 0x%04x dwParm1 = %04X dwParam2 = %04X\n", wMsg, dwParam1, dwParam2);
1817 if (wwo->wFlags != DCB_NULL &&
1818 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
1819 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
1820 WARN("can't notify client !\n");
1821 return MMSYSERR_ERROR;
1825 FIXME("Unknown callback message %u\n", wMsg);
1826 return MMSYSERR_INVALPARAM;
1828 return MMSYSERR_NOERROR;
1831 /**************************************************************************
1832 * wodUpdatePlayedTotal [internal]
1835 static BOOL wodUpdatePlayedTotal(WINE_WAVEDEV* wwo, snd_pcm_status_t* ps)
1837 snd_pcm_sframes_t delay = 0;
1838 snd_pcm_state_t state;
1840 state = snd_pcm_state(wwo->pcm);
1841 snd_pcm_delay(wwo->pcm, &delay);
1843 /* A delay < 0 indicates an underrun; for our purposes that's 0. */
1844 if ( (state != SND_PCM_STATE_RUNNING && state != SND_PCM_STATE_PREPARED) || (delay < 0))
1846 WARN("Unexpected state (%d) or delay (%ld) while updating Total Played, resetting\n", state, delay);
1849 wwo->dwPlayedTotal = wwo->dwWrittenTotal - snd_pcm_frames_to_bytes(wwo->pcm, delay);
1853 /**************************************************************************
1854 * wodPlayer_BeginWaveHdr [internal]
1856 * Makes the specified lpWaveHdr the currently playing wave header.
1857 * If the specified wave header is a begin loop and we're not already in
1858 * a loop, setup the loop.
1860 static void wodPlayer_BeginWaveHdr(WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
1862 wwo->lpPlayPtr = lpWaveHdr;
1864 if (!lpWaveHdr) return;
1866 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
1867 if (wwo->lpLoopPtr) {
1868 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
1870 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
1871 wwo->lpLoopPtr = lpWaveHdr;
1872 /* Windows does not touch WAVEHDR.dwLoops,
1873 * so we need to make an internal copy */
1874 wwo->dwLoops = lpWaveHdr->dwLoops;
1877 wwo->dwPartialOffset = 0;
1880 /**************************************************************************
1881 * wodPlayer_PlayPtrNext [internal]
1883 * Advance the play pointer to the next waveheader, looping if required.
1885 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEDEV* wwo)
1887 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1889 wwo->dwPartialOffset = 0;
1890 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
1891 /* We're at the end of a loop, loop if required */
1892 if (--wwo->dwLoops > 0) {
1893 wwo->lpPlayPtr = wwo->lpLoopPtr;
1895 /* Handle overlapping loops correctly */
1896 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
1897 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
1898 /* shall we consider the END flag for the closing loop or for
1899 * the opening one or for both ???
1900 * code assumes for closing loop only
1903 lpWaveHdr = lpWaveHdr->lpNext;
1905 wwo->lpLoopPtr = NULL;
1906 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
1909 /* We're not in a loop. Advance to the next wave header */
1910 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
1916 /**************************************************************************
1917 * wodPlayer_DSPWait [internal]
1918 * Returns the number of milliseconds to wait for the DSP buffer to play a
1921 static DWORD wodPlayer_DSPWait(const WINE_WAVEDEV *wwo)
1923 /* time for one period to be played */
1927 err = snd_pcm_hw_params_get_period_time(wwo->hw_params, &val, &dir);
1931 /**************************************************************************
1932 * wodPlayer_NotifyWait [internal]
1933 * Returns the number of milliseconds to wait before attempting to notify
1934 * completion of the specified wavehdr.
1935 * This is based on the number of bytes remaining to be written in the
1938 static DWORD wodPlayer_NotifyWait(const WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
1942 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
1945 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.Format.nAvgBytesPerSec;
1946 if (!dwMillis) dwMillis = 1;
1953 /**************************************************************************
1954 * wodPlayer_WriteMaxFrags [internal]
1955 * Writes the maximum number of frames possible to the DSP and returns
1956 * the number of frames written.
1958 static int wodPlayer_WriteMaxFrags(WINE_WAVEDEV* wwo, DWORD* frames)
1960 /* Only attempt to write to free frames */
1961 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
1962 DWORD dwLength = snd_pcm_bytes_to_frames(wwo->pcm, lpWaveHdr->dwBufferLength - wwo->dwPartialOffset);
1963 int toWrite = min(dwLength, *frames);
1966 TRACE("Writing wavehdr %p.%u[%u]\n", lpWaveHdr, wwo->dwPartialOffset, lpWaveHdr->dwBufferLength);
1969 written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
1971 /* XRUN occurred. let's try to recover */
1972 ALSA_XRUNRecovery(wwo, written);
1973 written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
1976 /* still in error */
1977 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written));
1983 wwo->dwPartialOffset += snd_pcm_frames_to_bytes(wwo->pcm, written);
1984 if ( wwo->dwPartialOffset >= lpWaveHdr->dwBufferLength) {
1985 /* this will be used to check if the given wave header has been fully played or not... */
1986 wwo->dwPartialOffset = lpWaveHdr->dwBufferLength;
1987 /* If we wrote all current wavehdr, skip to the next one */
1988 wodPlayer_PlayPtrNext(wwo);
1991 wwo->dwWrittenTotal += snd_pcm_frames_to_bytes(wwo->pcm, written);
1992 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
1998 /**************************************************************************
1999 * wodPlayer_NotifyCompletions [internal]
2001 * Notifies and remove from queue all wavehdrs which have been played to
2002 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
2003 * we notify all wavehdrs and remove them all from the queue even if they
2004 * are unplayed or part of a loop.
2006 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEDEV* wwo, BOOL force)
2008 LPWAVEHDR lpWaveHdr;
2010 /* Start from lpQueuePtr and keep notifying until:
2011 * - we hit an unwritten wavehdr
2012 * - we hit the beginning of a running loop
2013 * - we hit a wavehdr which hasn't finished playing
2016 while ((lpWaveHdr = wwo->lpQueuePtr) &&
2018 (lpWaveHdr != wwo->lpPlayPtr &&
2019 lpWaveHdr != wwo->lpLoopPtr &&
2020 lpWaveHdr->reserved <= wwo->dwPlayedTotal))) {
2022 wwo->lpQueuePtr = lpWaveHdr->lpNext;
2024 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2025 lpWaveHdr->dwFlags |= WHDR_DONE;
2027 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
2032 lpWaveHdr = wwo->lpQueuePtr;
2033 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
2036 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
2037 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
2038 if (lpWaveHdr->reserved > wwo->dwPlayedTotal) {TRACE("still playing %p (%u/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
2040 wwo->lpQueuePtr = lpWaveHdr->lpNext;
2042 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
2043 lpWaveHdr->dwFlags |= WHDR_DONE;
2045 wodNotifyClient(wwo, WOM_DONE, (DWORD)lpWaveHdr, 0);
2048 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
2049 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
2053 /**************************************************************************
2054 * wodPlayer_Reset [internal]
2056 * wodPlayer helper. Resets current output stream.
2058 static void wodPlayer_Reset(WINE_WAVEDEV* wwo, BOOL reset)
2061 TRACE("(%p)\n", wwo);
2063 /* flush all possible output */
2064 snd_pcm_drain(wwo->pcm);
2066 wodUpdatePlayedTotal(wwo, NULL);
2067 /* updates current notify list */
2068 wodPlayer_NotifyCompletions(wwo, FALSE);
2070 if ( (err = snd_pcm_drop(wwo->pcm)) < 0) {
2071 FIXME("flush: %s\n", snd_strerror(err));
2073 wwo->state = WINE_WS_STOPPED;
2076 if ( (err = snd_pcm_prepare(wwo->pcm)) < 0 )
2077 ERR("pcm prepare failed: %s\n", snd_strerror(err));
2080 enum win_wm_message msg;
2084 /* remove any buffer */
2085 wodPlayer_NotifyCompletions(wwo, TRUE);
2087 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
2088 wwo->state = WINE_WS_STOPPED;
2089 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
2090 /* Clear partial wavehdr */
2091 wwo->dwPartialOffset = 0;
2093 /* remove any existing message in the ring */
2094 EnterCriticalSection(&wwo->msgRing.msg_crst);
2095 /* return all pending headers in queue */
2096 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
2098 if (msg != WINE_WM_HEADER)
2100 FIXME("shouldn't have headers left\n");
2104 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
2105 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
2107 wodNotifyClient(wwo, WOM_DONE, param, 0);
2109 RESET_OMR(&wwo->msgRing);
2110 LeaveCriticalSection(&wwo->msgRing.msg_crst);
2112 if (wwo->lpLoopPtr) {
2113 /* complicated case, not handled yet (could imply modifying the loop counter */
2114 FIXME("Pausing while in loop isn't correctly handled yet, except strange results\n");
2115 wwo->lpPlayPtr = wwo->lpLoopPtr;
2116 wwo->dwPartialOffset = 0;
2117 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
2120 DWORD sz = wwo->dwPartialOffset;
2122 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
2123 /* compute the max size playable from lpQueuePtr */
2124 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
2125 sz += ptr->dwBufferLength;
2127 /* because the reset lpPlayPtr will be lpQueuePtr */
2128 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
2129 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
2130 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
2131 wwo->lpPlayPtr = wwo->lpQueuePtr;
2133 wwo->state = WINE_WS_PAUSED;
2137 /**************************************************************************
2138 * wodPlayer_ProcessMessages [internal]
2140 static void wodPlayer_ProcessMessages(WINE_WAVEDEV* wwo)
2142 LPWAVEHDR lpWaveHdr;
2143 enum win_wm_message msg;
2148 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
2149 TRACE("Received %s %x\n", getCmdString(msg), param);
2152 case WINE_WM_PAUSING:
2153 if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_RUNNING )
2155 if ( snd_pcm_hw_params_can_pause(wwo->hw_params) )
2157 err = snd_pcm_pause(wwo->pcm, 1);
2159 ERR("pcm_pause failed: %s\n", snd_strerror(err));
2160 wwo->state = WINE_WS_PAUSED;
2164 wodPlayer_Reset(wwo,FALSE);
2169 case WINE_WM_RESTARTING:
2170 if (wwo->state == WINE_WS_PAUSED)
2172 if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_PAUSED )
2174 err = snd_pcm_pause(wwo->pcm, 0);
2176 ERR("pcm_pause failed: %s\n", snd_strerror(err));
2178 wwo->state = WINE_WS_PLAYING;
2182 case WINE_WM_HEADER:
2183 lpWaveHdr = (LPWAVEHDR)param;
2185 /* insert buffer at the end of queue */
2188 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
2191 if (!wwo->lpPlayPtr)
2192 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
2193 if (wwo->state == WINE_WS_STOPPED)
2194 wwo->state = WINE_WS_PLAYING;
2196 case WINE_WM_RESETTING:
2197 wodPlayer_Reset(wwo,TRUE);
2200 case WINE_WM_UPDATE:
2201 wodUpdatePlayedTotal(wwo, NULL);
2204 case WINE_WM_BREAKLOOP:
2205 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
2206 /* ensure exit at end of current loop */
2211 case WINE_WM_CLOSING:
2212 /* sanity check: this should not happen since the device must have been reset before */
2213 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
2215 wwo->state = WINE_WS_CLOSED;
2218 /* shouldn't go here */
2220 FIXME("unknown message %d\n", msg);
2226 /**************************************************************************
2227 * wodPlayer_FeedDSP [internal]
2228 * Feed as much sound data as we can into the DSP and return the number of
2229 * milliseconds before it will be necessary to feed the DSP again.
2231 static DWORD wodPlayer_FeedDSP(WINE_WAVEDEV* wwo)
2235 wodUpdatePlayedTotal(wwo, NULL);
2236 availInQ = snd_pcm_avail_update(wwo->pcm);
2239 /* input queue empty and output buffer with less than one fragment to play */
2240 if (!wwo->lpPlayPtr && wwo->dwBufferSize < availInQ + wwo->dwFragmentSize) {
2241 TRACE("Run out of wavehdr:s...\n");
2245 /* no more room... no need to try to feed */
2247 /* Feed from partial wavehdr */
2248 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
2249 wodPlayer_WriteMaxFrags(wwo, &availInQ);
2252 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
2253 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
2255 TRACE("Setting time to elapse for %p to %u\n",
2256 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
2257 /* note the value that dwPlayedTotal will return when this wave finishes playing */
2258 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
2259 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
2263 return wodPlayer_DSPWait(wwo);
2266 /**************************************************************************
2267 * wodPlayer [internal]
2269 static DWORD CALLBACK wodPlayer(LPVOID pmt)
2271 WORD uDevID = (DWORD)pmt;
2272 WINE_WAVEDEV* wwo = (WINE_WAVEDEV*)&WOutDev[uDevID];
2273 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
2274 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
2277 wwo->state = WINE_WS_STOPPED;
2278 SetEvent(wwo->hStartUpEvent);
2281 /** Wait for the shortest time before an action is required. If there
2282 * are no pending actions, wait forever for a command.
2284 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
2285 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
2286 WAIT_OMR(&wwo->msgRing, dwSleepTime);
2287 wodPlayer_ProcessMessages(wwo);
2288 if (wwo->state == WINE_WS_PLAYING) {
2289 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
2290 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
2291 if (dwNextFeedTime == INFINITE) {
2292 /* FeedDSP ran out of data, but before giving up, */
2293 /* check that a notification didn't give us more */
2294 wodPlayer_ProcessMessages(wwo);
2295 if (wwo->lpPlayPtr) {
2296 TRACE("recovering\n");
2297 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
2301 dwNextFeedTime = dwNextNotifyTime = INFINITE;
2306 /**************************************************************************
2307 * wodGetDevCaps [internal]
2309 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
2311 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
2313 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
2315 if (wDevID >= ALSA_WodNumDevs) {
2316 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2317 return MMSYSERR_BADDEVICEID;
2320 memcpy(lpCaps, &WOutDev[wDevID].outcaps, min(dwSize, sizeof(*lpCaps)));
2321 return MMSYSERR_NOERROR;
2324 /**************************************************************************
2325 * wodOpen [internal]
2327 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
2330 snd_pcm_t * pcm = NULL;
2331 snd_hctl_t * hctl = NULL;
2332 snd_pcm_hw_params_t * hw_params = NULL;
2333 snd_pcm_sw_params_t * sw_params;
2334 snd_pcm_access_t access;
2335 snd_pcm_format_t format = -1;
2337 unsigned int buffer_time = 500000;
2338 unsigned int period_time = 10000;
2339 snd_pcm_uframes_t buffer_size;
2340 snd_pcm_uframes_t period_size;
2346 snd_pcm_sw_params_alloca(&sw_params);
2348 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
2349 if (lpDesc == NULL) {
2350 WARN("Invalid Parameter !\n");
2351 return MMSYSERR_INVALPARAM;
2353 if (wDevID >= ALSA_WodNumDevs) {
2354 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2355 return MMSYSERR_BADDEVICEID;
2358 /* only PCM format is supported so far... */
2359 if (!supportedFormat(lpDesc->lpFormat)) {
2360 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2361 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2362 lpDesc->lpFormat->nSamplesPerSec);
2363 return WAVERR_BADFORMAT;
2366 if (dwFlags & WAVE_FORMAT_QUERY) {
2367 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
2368 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
2369 lpDesc->lpFormat->nSamplesPerSec);
2370 return MMSYSERR_NOERROR;
2373 wwo = &WOutDev[wDevID];
2375 if (wwo->pcm != NULL) {
2376 WARN("%d already allocated\n", wDevID);
2377 return MMSYSERR_ALLOCATED;
2380 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->outcaps.dwSupport & WAVECAPS_DIRECTSOUND))
2381 /* not supported, ignore it */
2382 dwFlags &= ~WAVE_DIRECTSOUND;
2384 flags = SND_PCM_NONBLOCK;
2386 /* FIXME - why is this ifdefed? */
2388 if ( dwFlags & WAVE_DIRECTSOUND )
2389 flags |= SND_PCM_ASYNC;
2392 if ( (err = snd_pcm_open(&pcm, wwo->pcmname, SND_PCM_STREAM_PLAYBACK, flags)) < 0)
2394 ERR("Error open: %s\n", snd_strerror(err));
2395 return MMSYSERR_NOTENABLED;
2400 err = snd_hctl_open(&hctl, wwo->ctlname, 0);
2403 snd_hctl_load(hctl);
2407 WARN("Could not open hctl for [%s]: %s\n", wwo->ctlname, snd_strerror(err));
2412 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
2414 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
2415 copy_format(lpDesc->lpFormat, &wwo->format);
2417 TRACE("Requested this format: %dx%dx%d %s\n",
2418 wwo->format.Format.nSamplesPerSec,
2419 wwo->format.Format.wBitsPerSample,
2420 wwo->format.Format.nChannels,
2421 getFormat(wwo->format.Format.wFormatTag));
2423 if (wwo->format.Format.wBitsPerSample == 0) {
2424 WARN("Resetting zeroed wBitsPerSample\n");
2425 wwo->format.Format.wBitsPerSample = 8 *
2426 (wwo->format.Format.nAvgBytesPerSec /
2427 wwo->format.Format.nSamplesPerSec) /
2428 wwo->format.Format.nChannels;
2431 #define EXIT_ON_ERROR(f,e,txt) do \
2434 if ( (err = (f) ) < 0) \
2436 WARN(txt ": %s\n", snd_strerror(err)); \
2442 snd_pcm_hw_params_malloc(&hw_params);
2445 retcode = MMSYSERR_NOMEM;
2448 snd_pcm_hw_params_any(pcm, hw_params);
2450 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
2451 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
2452 WARN("mmap not available. switching to standard write.\n");
2453 access = SND_PCM_ACCESS_RW_INTERLEAVED;
2454 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
2455 wwo->write = snd_pcm_writei;
2458 wwo->write = snd_pcm_mmap_writei;
2460 if ((err = snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels)) < 0) {
2461 WARN("unable to set required channels: %d\n", wwo->format.Format.nChannels);
2462 if (dwFlags & WAVE_DIRECTSOUND) {
2463 if (wwo->format.Format.nChannels > 2)
2464 wwo->format.Format.nChannels = 2;
2465 else if (wwo->format.Format.nChannels == 2)
2466 wwo->format.Format.nChannels = 1;
2467 else if (wwo->format.Format.nChannels == 1)
2468 wwo->format.Format.nChannels = 2;
2469 /* recalculate block align and bytes per second */
2470 wwo->format.Format.nBlockAlign = (wwo->format.Format.wBitsPerSample * wwo->format.Format.nChannels) / 8;
2471 wwo->format.Format.nAvgBytesPerSec = wwo->format.Format.nSamplesPerSec * wwo->format.Format.nBlockAlign;
2472 WARN("changed number of channels from %d to %d\n", lpDesc->lpFormat->nChannels, wwo->format.Format.nChannels);
2474 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels ), WAVERR_BADFORMAT, "unable to set required channels" );
2477 if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
2478 ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
2479 IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) {
2480 format = (wwo->format.Format.wBitsPerSample == 8) ? SND_PCM_FORMAT_U8 :
2481 (wwo->format.Format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE :
2482 (wwo->format.Format.wBitsPerSample == 24) ? SND_PCM_FORMAT_S24_LE :
2483 (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_S32_LE : -1;
2484 } else if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
2485 IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
2486 format = (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_FLOAT_LE : -1;
2487 } else if (wwo->format.Format.wFormatTag == WAVE_FORMAT_MULAW) {
2488 FIXME("unimplemented format: WAVE_FORMAT_MULAW\n");
2489 retcode = WAVERR_BADFORMAT;
2491 } else if (wwo->format.Format.wFormatTag == WAVE_FORMAT_ALAW) {
2492 FIXME("unimplemented format: WAVE_FORMAT_ALAW\n");
2493 retcode = WAVERR_BADFORMAT;
2495 } else if (wwo->format.Format.wFormatTag == WAVE_FORMAT_ADPCM) {
2496 FIXME("unimplemented format: WAVE_FORMAT_ADPCM\n");
2497 retcode = WAVERR_BADFORMAT;
2500 ERR("invalid format: %0x04x\n", wwo->format.Format.wFormatTag);
2501 retcode = WAVERR_BADFORMAT;
2505 if ((err = snd_pcm_hw_params_set_format(pcm, hw_params, format)) < 0) {
2506 WARN("unable to set required format: %s\n", snd_pcm_format_name(format));
2507 if (dwFlags & WAVE_DIRECTSOUND) {
2508 if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
2509 ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
2510 IsEqualGUID(&wwo->format.SubFormat, & KSDATAFORMAT_SUBTYPE_PCM))) {
2511 if (wwo->format.Format.wBitsPerSample != 16) {
2512 wwo->format.Format.wBitsPerSample = 16;
2513 format = SND_PCM_FORMAT_S16_LE;
2515 wwo->format.Format.wBitsPerSample = 8;
2516 format = SND_PCM_FORMAT_U8;
2518 /* recalculate block align and bytes per second */
2519 wwo->format.Format.nBlockAlign = (wwo->format.Format.wBitsPerSample * wwo->format.Format.nChannels) / 8;
2520 wwo->format.Format.nAvgBytesPerSec = wwo->format.Format.nSamplesPerSec * wwo->format.Format.nBlockAlign;
2521 WARN("changed bits per sample from %d to %d\n", lpDesc->lpFormat->wBitsPerSample, wwo->format.Format.wBitsPerSample);
2524 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), WAVERR_BADFORMAT, "unable to set required format" );
2527 rate = wwo->format.Format.nSamplesPerSec;
2529 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
2531 WARN("Rate %d Hz not available for playback: %s\n", wwo->format.Format.nSamplesPerSec, snd_strerror(rate));
2532 retcode = WAVERR_BADFORMAT;
2535 if (!NearMatch(rate, wwo->format.Format.nSamplesPerSec)) {
2536 if (dwFlags & WAVE_DIRECTSOUND) {
2537 WARN("changed sample rate from %d Hz to %d Hz\n", wwo->format.Format.nSamplesPerSec, rate);
2538 wwo->format.Format.nSamplesPerSec = rate;
2539 /* recalculate bytes per second */
2540 wwo->format.Format.nAvgBytesPerSec = wwo->format.Format.nSamplesPerSec * wwo->format.Format.nBlockAlign;
2542 WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwo->format.Format.nSamplesPerSec, rate);
2543 retcode = WAVERR_BADFORMAT;
2548 /* give the new format back to direct sound */
2549 if (dwFlags & WAVE_DIRECTSOUND) {
2550 lpDesc->lpFormat->wFormatTag = wwo->format.Format.wFormatTag;
2551 lpDesc->lpFormat->nChannels = wwo->format.Format.nChannels;
2552 lpDesc->lpFormat->nSamplesPerSec = wwo->format.Format.nSamplesPerSec;
2553 lpDesc->lpFormat->wBitsPerSample = wwo->format.Format.wBitsPerSample;
2554 lpDesc->lpFormat->nBlockAlign = wwo->format.Format.nBlockAlign;
2555 lpDesc->lpFormat->nAvgBytesPerSec = wwo->format.Format.nAvgBytesPerSec;
2558 TRACE("Got this format: %dx%dx%d %s\n",
2559 wwo->format.Format.nSamplesPerSec,
2560 wwo->format.Format.wBitsPerSample,
2561 wwo->format.Format.nChannels,
2562 getFormat(wwo->format.Format.wFormatTag));
2565 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
2567 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
2569 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
2571 err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
2572 err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
2574 snd_pcm_sw_params_current(pcm, sw_params);
2575 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");
2576 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
2577 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
2578 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
2579 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
2580 EXIT_ON_ERROR( snd_pcm_sw_params_set_xrun_mode(pcm, sw_params, SND_PCM_XRUN_NONE), MMSYSERR_ERROR, "unable to set xrun mode");
2581 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
2582 #undef EXIT_ON_ERROR
2584 snd_pcm_prepare(pcm);
2587 ALSA_TraceParameters(hw_params, sw_params, FALSE);
2589 /* now, we can save all required data for later use... */
2591 wwo->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
2592 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
2593 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
2594 wwo->dwPartialOffset = 0;
2596 ALSA_InitRingMessage(&wwo->msgRing);
2598 if (!(dwFlags & WAVE_DIRECTSOUND)) {
2599 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2600 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
2602 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
2605 ERR("Thread creation for the wodPlayer failed!\n");
2606 CloseHandle(wwo->hStartUpEvent);
2607 retcode = MMSYSERR_NOMEM;
2610 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
2611 CloseHandle(wwo->hStartUpEvent);
2613 wwo->hThread = INVALID_HANDLE_VALUE;
2614 wwo->dwThreadID = 0;
2616 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
2618 TRACE("handle=%p\n", pcm);
2619 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
2620 wwo->format.Format.wBitsPerSample, wwo->format.Format.nAvgBytesPerSec,
2621 wwo->format.Format.nSamplesPerSec, wwo->format.Format.nChannels,
2622 wwo->format.Format.nBlockAlign);
2626 if ( wwo->hw_params )
2627 snd_pcm_hw_params_free(wwo->hw_params);
2628 wwo->hw_params = hw_params;
2631 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
2639 snd_hctl_free(hctl);
2640 snd_hctl_close(hctl);
2644 snd_pcm_hw_params_free(hw_params);
2646 if (wwo->msgRing.ring_buffer_size > 0)
2647 ALSA_DestroyRingMessage(&wwo->msgRing);
2653 /**************************************************************************
2654 * wodClose [internal]
2656 static DWORD wodClose(WORD wDevID)
2658 DWORD ret = MMSYSERR_NOERROR;
2661 TRACE("(%u);\n", wDevID);
2663 if (wDevID >= ALSA_WodNumDevs) {
2664 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2665 return MMSYSERR_BADDEVICEID;
2668 if (WOutDev[wDevID].pcm == NULL) {
2669 WARN("Requested to close already closed device %d!\n", wDevID);
2670 return MMSYSERR_BADDEVICEID;
2673 wwo = &WOutDev[wDevID];
2674 if (wwo->lpQueuePtr) {
2675 WARN("buffers still playing !\n");
2676 ret = WAVERR_STILLPLAYING;
2678 if (wwo->hThread != INVALID_HANDLE_VALUE) {
2679 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
2681 ALSA_DestroyRingMessage(&wwo->msgRing);
2684 snd_pcm_hw_params_free(wwo->hw_params);
2685 wwo->hw_params = NULL;
2688 snd_pcm_close(wwo->pcm);
2693 snd_hctl_free(wwo->hctl);
2694 snd_hctl_close(wwo->hctl);
2698 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
2705 /**************************************************************************
2706 * wodWrite [internal]
2709 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2711 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
2713 if (wDevID >= ALSA_WodNumDevs) {
2714 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2715 return MMSYSERR_BADDEVICEID;
2718 if (WOutDev[wDevID].pcm == NULL) {
2719 WARN("Requested to write to closed device %d!\n", wDevID);
2720 return MMSYSERR_BADDEVICEID;
2723 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
2724 return WAVERR_UNPREPARED;
2726 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
2727 return WAVERR_STILLPLAYING;
2729 lpWaveHdr->dwFlags &= ~WHDR_DONE;
2730 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2731 lpWaveHdr->lpNext = 0;
2733 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
2735 return MMSYSERR_NOERROR;
2738 /**************************************************************************
2739 * wodPause [internal]
2741 static DWORD wodPause(WORD wDevID)
2743 TRACE("(%u);!\n", wDevID);
2745 if (wDevID >= ALSA_WodNumDevs) {
2746 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2747 return MMSYSERR_BADDEVICEID;
2750 if (WOutDev[wDevID].pcm == NULL) {
2751 WARN("Requested to pause closed device %d!\n", wDevID);
2752 return MMSYSERR_BADDEVICEID;
2755 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
2757 return MMSYSERR_NOERROR;
2760 /**************************************************************************
2761 * wodRestart [internal]
2763 static DWORD wodRestart(WORD wDevID)
2765 TRACE("(%u);\n", wDevID);
2767 if (wDevID >= ALSA_WodNumDevs) {
2768 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2769 return MMSYSERR_BADDEVICEID;
2772 if (WOutDev[wDevID].pcm == NULL) {
2773 WARN("Requested to restart closed device %d!\n", wDevID);
2774 return MMSYSERR_BADDEVICEID;
2777 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
2778 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
2781 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
2782 /* FIXME: Myst crashes with this ... hmm -MM
2783 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
2786 return MMSYSERR_NOERROR;
2789 /**************************************************************************
2790 * wodReset [internal]
2792 static DWORD wodReset(WORD wDevID)
2794 TRACE("(%u);\n", wDevID);
2796 if (wDevID >= ALSA_WodNumDevs) {
2797 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2798 return MMSYSERR_BADDEVICEID;
2801 if (WOutDev[wDevID].pcm == NULL) {
2802 WARN("Requested to reset closed device %d!\n", wDevID);
2803 return MMSYSERR_BADDEVICEID;
2806 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
2808 return MMSYSERR_NOERROR;
2811 /**************************************************************************
2812 * wodGetPosition [internal]
2814 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2818 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
2820 if (wDevID >= ALSA_WodNumDevs) {
2821 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2822 return MMSYSERR_BADDEVICEID;
2825 if (WOutDev[wDevID].pcm == NULL) {
2826 WARN("Requested to get position of closed device %d!\n", wDevID);
2827 return MMSYSERR_BADDEVICEID;
2830 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
2832 wwo = &WOutDev[wDevID];
2833 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);
2835 return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->format);
2838 /**************************************************************************
2839 * wodBreakLoop [internal]
2841 static DWORD wodBreakLoop(WORD wDevID)
2843 TRACE("(%u);\n", wDevID);
2845 if (wDevID >= ALSA_WodNumDevs) {
2846 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2847 return MMSYSERR_BADDEVICEID;
2850 if (WOutDev[wDevID].pcm == NULL) {
2851 WARN("Requested to breakloop of closed device %d!\n", wDevID);
2852 return MMSYSERR_BADDEVICEID;
2855 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
2856 return MMSYSERR_NOERROR;
2859 /**************************************************************************
2860 * wodGetVolume [internal]
2862 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
2870 TRACE("(%u, %p);\n", wDevID, lpdwVol);
2871 if (wDevID >= ALSA_WodNumDevs) {
2872 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2873 return MMSYSERR_BADDEVICEID;
2876 if (lpdwVol == NULL)
2877 return MMSYSERR_NOTENABLED;
2879 wwo = &WOutDev[wDevID];
2881 if (lpdwVol == NULL)
2882 return MMSYSERR_NOTENABLED;
2884 rc = ALSA_CheckSetVolume(wwo->hctl, &left, &right, &min, &max, NULL, NULL, NULL);
2885 if (rc == MMSYSERR_NOERROR)
2887 #define VOLUME_ALSA_TO_WIN(x) ( ( (((x)-min) * 65535) + (max-min)/2 ) /(max-min))
2888 wleft = VOLUME_ALSA_TO_WIN(left);
2889 wright = VOLUME_ALSA_TO_WIN(right);
2890 #undef VOLUME_ALSA_TO_WIN
2891 TRACE("left=%d,right=%d,converted to windows left %d, right %d\n", left, right, wleft, wright);
2892 *lpdwVol = MAKELONG( wleft, wright );
2895 TRACE("CheckSetVolume failed; rc %d\n", rc);
2900 /**************************************************************************
2901 * wodSetVolume [internal]
2903 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
2911 TRACE("(%u, %08X);\n", wDevID, dwParam);
2912 if (wDevID >= ALSA_WodNumDevs) {
2913 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
2914 return MMSYSERR_BADDEVICEID;
2917 wwo = &WOutDev[wDevID];
2919 rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, &min, &max, NULL, NULL, NULL);
2920 if (rc == MMSYSERR_NOERROR)
2922 wleft = LOWORD(dwParam);
2923 wright = HIWORD(dwParam);
2924 #define VOLUME_WIN_TO_ALSA(x) ( ( ( ((x) * (max-min)) + 32767) / 65535) + min )
2925 left = VOLUME_WIN_TO_ALSA(wleft);
2926 right = VOLUME_WIN_TO_ALSA(wright);
2927 #undef VOLUME_WIN_TO_ALSA
2928 rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, NULL, NULL, NULL, &left, &right);
2929 if (rc == MMSYSERR_NOERROR)
2930 TRACE("set volume: wleft=%d, wright=%d, converted to alsa left %d, right %d\n", wleft, wright, left, right);
2932 TRACE("SetVolume failed; rc %d\n", rc);
2938 /**************************************************************************
2939 * wodGetNumDevs [internal]
2941 static DWORD wodGetNumDevs(void)
2943 return ALSA_WodNumDevs;
2946 /**************************************************************************
2947 * wodDevInterfaceSize [internal]
2949 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
2951 TRACE("(%u, %p)\n", wDevID, dwParam1);
2953 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
2954 NULL, 0 ) * sizeof(WCHAR);
2955 return MMSYSERR_NOERROR;
2958 /**************************************************************************
2959 * wodDevInterface [internal]
2961 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
2963 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
2964 NULL, 0 ) * sizeof(WCHAR))
2966 MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
2967 dwParam1, dwParam2 / sizeof(WCHAR));
2968 return MMSYSERR_NOERROR;
2970 return MMSYSERR_INVALPARAM;
2973 /**************************************************************************
2974 * wodMessage (WINEALSA.@)
2976 DWORD WINAPI ALSA_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2977 DWORD dwParam1, DWORD dwParam2)
2979 TRACE("(%u, %s, %08X, %08X, %08X);\n",
2980 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
2987 /* FIXME: Pretend this is supported */
2989 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2990 case WODM_CLOSE: return wodClose (wDevID);
2991 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
2992 case WODM_GETNUMDEVS: return wodGetNumDevs ();
2993 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
2994 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
2995 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2996 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
2997 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2998 case WODM_PAUSE: return wodPause (wDevID);
2999 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
3000 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
3001 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
3002 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
3003 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
3004 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
3005 case WODM_RESTART: return wodRestart (wDevID);
3006 case WODM_RESET: return wodReset (wDevID);
3007 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
3008 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
3009 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
3010 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
3013 FIXME("unknown message %d!\n", wMsg);
3015 return MMSYSERR_NOTSUPPORTED;
3018 /*======================================================================*
3019 * Low level DSOUND implementation *
3020 *======================================================================*/
3022 typedef struct IDsDriverImpl IDsDriverImpl;
3023 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
3025 struct IDsDriverImpl
3027 /* IUnknown fields */
3028 const IDsDriverVtbl *lpVtbl;
3030 /* IDsDriverImpl fields */
3032 IDsDriverBufferImpl*primary;
3035 struct IDsDriverBufferImpl
3037 /* IUnknown fields */
3038 const IDsDriverBufferVtbl *lpVtbl;
3040 /* IDsDriverBufferImpl fields */
3043 CRITICAL_SECTION mmap_crst;
3045 DWORD mmap_buflen_bytes;
3046 snd_pcm_uframes_t mmap_buflen_frames;
3047 snd_pcm_channel_area_t * mmap_areas;
3048 snd_async_handler_t * mmap_async_handler;
3049 snd_pcm_uframes_t mmap_ppos; /* play position */
3051 /* Do we have a direct hardware buffer - SND_PCM_TYPE_HW? */
3055 static void DSDB_CheckXRUN(IDsDriverBufferImpl* pdbi)
3057 WINE_WAVEDEV * wwo = &(WOutDev[pdbi->drv->wDevID]);
3058 snd_pcm_state_t state = snd_pcm_state(wwo->pcm);
3060 if ( state == SND_PCM_STATE_XRUN )
3062 int err = snd_pcm_prepare(wwo->pcm);
3063 TRACE("xrun occurred\n");
3065 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
3067 else if ( state == SND_PCM_STATE_SUSPENDED )
3069 int err = snd_pcm_resume(wwo->pcm);
3070 TRACE("recovery from suspension occurred\n");
3071 if (err < 0 && err != -EAGAIN){
3072 err = snd_pcm_prepare(wwo->pcm);
3074 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
3079 static void DSDB_MMAPCopy(IDsDriverBufferImpl* pdbi, int mul)
3081 WINE_WAVEDEV * wwo = &(WOutDev[pdbi->drv->wDevID]);
3082 snd_pcm_uframes_t period_size;
3083 snd_pcm_sframes_t avail;
3087 const snd_pcm_channel_area_t *areas;
3088 snd_pcm_uframes_t ofs;
3089 snd_pcm_uframes_t frames;
3090 snd_pcm_uframes_t wanted;
3092 if ( !pdbi->mmap_buffer || !wwo->hw_params || !wwo->pcm)
3095 err = snd_pcm_hw_params_get_period_size(wwo->hw_params, &period_size, &dir);
3096 avail = snd_pcm_avail_update(wwo->pcm);
3098 DSDB_CheckXRUN(pdbi);
3100 TRACE("avail=%d, mul=%d\n", (int)avail, mul);
3102 frames = pdbi->mmap_buflen_frames;
3104 EnterCriticalSection(&pdbi->mmap_crst);
3106 /* we want to commit the given number of periods, or the whole lot */
3107 wanted = mul == 0 ? frames : period_size * 2;
3109 snd_pcm_mmap_begin(wwo->pcm, &areas, &ofs, &frames);
3110 if (areas != pdbi->mmap_areas || areas->addr != pdbi->mmap_areas->addr)
3111 FIXME("Can't access sound driver's buffer directly.\n");
3113 /* mark our current play position */
3114 pdbi->mmap_ppos = ofs;
3116 if (frames > wanted)
3119 err = snd_pcm_mmap_commit(wwo->pcm, ofs, frames);
3121 /* Check to make sure we committed all we want to commit. ALSA
3122 * only gives a contiguous linear region, so we need to check this
3123 * in case we've reached the end of the buffer, in which case we
3124 * can wrap around back to the beginning. */
3125 if (frames < wanted) {
3126 frames = wanted -= frames;
3127 snd_pcm_mmap_begin(wwo->pcm, &areas, &ofs, &frames);
3128 snd_pcm_mmap_commit(wwo->pcm, ofs, frames);
3131 LeaveCriticalSection(&pdbi->mmap_crst);
3134 static void DSDB_PCMCallback(snd_async_handler_t *ahandler)
3137 /* snd_pcm_t * handle = snd_async_handler_get_pcm(ahandler); */
3138 IDsDriverBufferImpl* pdbi = snd_async_handler_get_callback_private(ahandler);
3139 TRACE("callback called\n");
3141 /* Commit another block (the entire buffer if it's a direct hw buffer) */
3142 periods = pdbi->mmap_mode == SND_PCM_TYPE_HW ? 0 : 1;
3143 DSDB_MMAPCopy(pdbi, periods);
3147 * Allocate the memory-mapped buffer for direct sound, and set up the
3150 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
3152 WINE_WAVEDEV * wwo = &(WOutDev[pdbi->drv->wDevID]);
3153 snd_pcm_format_t format;
3154 snd_pcm_uframes_t frames;
3155 snd_pcm_uframes_t ofs;
3156 snd_pcm_uframes_t avail;
3157 unsigned int channels;
3158 unsigned int bits_per_sample;
3159 unsigned int bits_per_frame;
3162 err = snd_pcm_hw_params_get_format(wwo->hw_params, &format);
3163 err = snd_pcm_hw_params_get_buffer_size(wwo->hw_params, &frames);
3164 err = snd_pcm_hw_params_get_channels(wwo->hw_params, &channels);
3165 bits_per_sample = snd_pcm_format_physical_width(format);
3166 bits_per_frame = bits_per_sample * channels;
3167 pdbi->mmap_mode = snd_pcm_type(wwo->pcm);
3169 if (pdbi->mmap_mode == SND_PCM_TYPE_HW) {
3170 TRACE("mmap'd buffer is a hardware buffer.\n");
3173 TRACE("mmap'd buffer is an ALSA emulation of hardware buffer.\n");
3177 ALSA_TraceParameters(wwo->hw_params, NULL, FALSE);
3179 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
3180 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
3182 pdbi->mmap_buflen_frames = frames;
3183 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( wwo->pcm, frames );
3185 avail = snd_pcm_avail_update(wwo->pcm);
3188 ERR("No buffer is available: %s.\n", snd_strerror(avail));
3189 return DSERR_GENERIC;
3191 err = snd_pcm_mmap_begin(wwo->pcm, (const snd_pcm_channel_area_t **)&pdbi->mmap_areas, &ofs, &avail);
3194 ERR("Can't map sound device for direct access: %s\n", snd_strerror(err));
3195 return DSERR_GENERIC;
3197 avail = 0;/* We don't have any data to commit yet */
3198 err = snd_pcm_mmap_commit(wwo->pcm, ofs, avail);
3200 err = snd_pcm_rewind(wwo->pcm, ofs);
3201 pdbi->mmap_buffer = pdbi->mmap_areas->addr;
3203 snd_pcm_format_set_silence(format, pdbi->mmap_buffer, frames );
3205 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
3206 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
3208 InitializeCriticalSection(&pdbi->mmap_crst);
3209 pdbi->mmap_crst.DebugInfo->Spare[0] = (DWORD_PTR)"WINEALSA_mmap_crst";
3211 err = snd_async_add_pcm_handler(&pdbi->mmap_async_handler, wwo->pcm, DSDB_PCMCallback, pdbi);
3214 ERR("add_pcm_handler failed. reason: %s\n", snd_strerror(err));
3215 return DSERR_GENERIC;
3221 static void DSDB_DestroyMMAP(IDsDriverBufferImpl* pdbi)
3223 TRACE("mmap buffer %p destroyed\n", pdbi->mmap_buffer);
3224 pdbi->mmap_areas = NULL;
3225 pdbi->mmap_buffer = NULL;
3226 pdbi->mmap_crst.DebugInfo->Spare[0] = 0;
3227 DeleteCriticalSection(&pdbi->mmap_crst);
3231 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
3233 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
3234 FIXME("(): stub!\n");
3235 return DSERR_UNSUPPORTED;
3238 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
3240 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
3241 ULONG refCount = InterlockedIncrement(&This->ref);
3243 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
3248 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
3250 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
3251 ULONG refCount = InterlockedDecrement(&This->ref);
3253 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
3257 if (This == This->drv->primary)
3258 This->drv->primary = NULL;
3259 DSDB_DestroyMMAP(This);
3260 HeapFree(GetProcessHeap(), 0, This);
3264 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
3265 LPVOID*ppvAudio1,LPDWORD pdwLen1,
3266 LPVOID*ppvAudio2,LPDWORD pdwLen2,
3267 DWORD dwWritePosition,DWORD dwWriteLen,
3270 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
3271 TRACE("(%p)\n",iface);
3272 return DSERR_UNSUPPORTED;
3275 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
3276 LPVOID pvAudio1,DWORD dwLen1,
3277 LPVOID pvAudio2,DWORD dwLen2)
3279 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
3280 TRACE("(%p)\n",iface);
3281 return DSERR_UNSUPPORTED;
3284 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
3285 LPWAVEFORMATEX pwfx)
3287 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
3288 TRACE("(%p,%p)\n",iface,pwfx);
3289 return DSERR_BUFFERLOST;
3292 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
3294 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
3295 TRACE("(%p,%d): stub\n",iface,dwFreq);
3296 return DSERR_UNSUPPORTED;
3299 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
3302 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
3303 TRACE("(%p,%p)\n",iface,pVolPan);
3304 vol = pVolPan->dwTotalLeftAmpFactor | (pVolPan->dwTotalRightAmpFactor << 16);
3306 if (wodSetVolume(This->drv->wDevID, vol) != MMSYSERR_NOERROR) {
3307 WARN("wodSetVolume failed\n");
3308 return DSERR_INVALIDPARAM;
3314 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
3316 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
3317 TRACE("(%p,%d): stub\n",iface,dwNewPos);
3318 return DSERR_UNSUPPORTED;
3321 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
3322 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
3324 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
3325 WINE_WAVEDEV * wwo = &(WOutDev[This->drv->wDevID]);
3326 snd_pcm_uframes_t hw_ptr;
3327 snd_pcm_uframes_t period_size;
3328 snd_pcm_state_t state;
3332 if (wwo->hw_params == NULL) return DSERR_GENERIC;
3335 err = snd_pcm_hw_params_get_period_size(wwo->hw_params, &period_size, &dir);
3337 if (wwo->pcm == NULL) return DSERR_GENERIC;
3338 /** we need to track down buffer underruns */
3339 DSDB_CheckXRUN(This);
3341 EnterCriticalSection(&This->mmap_crst);
3342 hw_ptr = This->mmap_ppos;
3344 state = snd_pcm_state(wwo->pcm);
3345 if (state != SND_PCM_STATE_RUNNING)
3349 *lpdwPlay = snd_pcm_frames_to_bytes(wwo->pcm, hw_ptr) % This->mmap_buflen_bytes;
3351 *lpdwWrite = snd_pcm_frames_to_bytes(wwo->pcm, hw_ptr + period_size * 2) % This->mmap_buflen_bytes;
3352 LeaveCriticalSection(&This->mmap_crst);
3354 TRACE("hw_ptr=0x%08x, playpos=%d, writepos=%d\n", (unsigned int)hw_ptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
3358 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
3360 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
3361 WINE_WAVEDEV * wwo = &(WOutDev[This->drv->wDevID]);
3362 snd_pcm_state_t state;
3365 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
3367 if (wwo->pcm == NULL) return DSERR_GENERIC;
3369 state = snd_pcm_state(wwo->pcm);
3370 if ( state == SND_PCM_STATE_SETUP )
3372 err = snd_pcm_prepare(wwo->pcm);
3373 state = snd_pcm_state(wwo->pcm);
3375 if ( state == SND_PCM_STATE_PREPARED )
3377 /* If we have a direct hardware buffer, we can commit the whole lot
3378 * immediately (periods = 0), otherwise we prime the queue with only
3381 * Why 2? We want a small number so that we don't get ahead of the
3382 * DirectSound mixer. But we don't want to ever let the buffer get
3383 * completely empty - having 2 periods gives us time to commit another
3384 * period when the first expires.
3386 * The potential for buffer underrun is high, but that's the reality
3387 * of using a translated buffer (the whole point of DirectSound is
3388 * to provide direct access to the hardware).
3390 * A better implementation would use the buffer Lock() and Unlock()
3391 * methods to determine how far ahead we can commit, and to rewind if
3394 int periods = This->mmap_mode == SND_PCM_TYPE_HW ? 0 : 2;
3396 DSDB_MMAPCopy(This, periods);
3397 err = snd_pcm_start(wwo->pcm);
3402 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
3404 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
3405 WINE_WAVEDEV * wwo = &(WOutDev[This->drv->wDevID]);
3410 TRACE("(%p)\n",iface);
3412 if (wwo->pcm == NULL) return DSERR_GENERIC;
3414 /* ring buffer wrap up detection */
3415 IDsDriverBufferImpl_GetPosition(iface, &play, &write);
3418 TRACE("writepos wrapper up\n");
3422 if ( ( err = snd_pcm_drop(wwo->pcm)) < 0 )
3424 ERR("error while stopping pcm: %s\n", snd_strerror(err));
3425 return DSERR_GENERIC;
3430 static const IDsDriverBufferVtbl dsdbvt =
3432 IDsDriverBufferImpl_QueryInterface,
3433 IDsDriverBufferImpl_AddRef,
3434 IDsDriverBufferImpl_Release,
3435 IDsDriverBufferImpl_Lock,
3436 IDsDriverBufferImpl_Unlock,
3437 IDsDriverBufferImpl_SetFormat,
3438 IDsDriverBufferImpl_SetFrequency,
3439 IDsDriverBufferImpl_SetVolumePan,
3440 IDsDriverBufferImpl_SetPosition,
3441 IDsDriverBufferImpl_GetPosition,
3442 IDsDriverBufferImpl_Play,
3443 IDsDriverBufferImpl_Stop
3446 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
3448 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
3449 FIXME("(%p): stub!\n",iface);
3450 return DSERR_UNSUPPORTED;
3453 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
3455 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3456 ULONG refCount = InterlockedIncrement(&This->ref);
3458 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
3463 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
3465 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3466 ULONG refCount = InterlockedDecrement(&This->ref);
3468 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
3472 HeapFree(GetProcessHeap(),0,This);
3476 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
3478 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3479 TRACE("(%p,%p)\n",iface,pDesc);
3480 memcpy(pDesc, &(WOutDev[This->wDevID].ds_desc), sizeof(DSDRIVERDESC));
3481 pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
3482 DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
3483 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
3485 pDesc->wReserved = 0;
3486 pDesc->ulDeviceNum = This->wDevID;
3487 pDesc->dwHeapType = DSDHEAP_NOHEAP;
3488 pDesc->pvDirectDrawHeap = NULL;
3489 pDesc->dwMemStartAddress = 0;
3490 pDesc->dwMemEndAddress = 0;
3491 pDesc->dwMemAllocExtra = 0;
3492 pDesc->pvReserved1 = NULL;
3493 pDesc->pvReserved2 = NULL;
3497 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
3499 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
3500 TRACE("(%p)\n",iface);
3504 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
3506 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
3507 TRACE("(%p)\n",iface);
3511 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
3513 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3514 TRACE("(%p,%p)\n",iface,pCaps);
3515 memcpy(pCaps, &(WOutDev[This->wDevID].ds_caps), sizeof(DSDRIVERCAPS));
3519 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
3520 LPWAVEFORMATEX pwfx,
3521 DWORD dwFlags, DWORD dwCardAddress,
3522 LPDWORD pdwcbBufferSize,
3526 IDsDriverImpl *This = (IDsDriverImpl *)iface;
3527 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
3530 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
3531 /* we only support primary buffers */
3532 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
3533 return DSERR_UNSUPPORTED;
3535 return DSERR_ALLOCATED;
3536 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
3537 return DSERR_CONTROLUNAVAIL;
3539 *ippdsdb = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
3540 if (*ippdsdb == NULL)
3541 return DSERR_OUTOFMEMORY;
3542 (*ippdsdb)->lpVtbl = &dsdbvt;
3543 (*ippdsdb)->ref = 1;
3544 (*ippdsdb)->drv = This;
3546 err = DSDB_CreateMMAP((*ippdsdb));
3549 HeapFree(GetProcessHeap(), 0, *ippdsdb);
3553 *ppbBuffer = (*ippdsdb)->mmap_buffer;
3554 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
3556 This->primary = *ippdsdb;
3558 /* buffer is ready to go */
3559 TRACE("buffer created at %p\n", *ippdsdb);
3563 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
3564 PIDSDRIVERBUFFER pBuffer,
3567 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
3568 TRACE("(%p,%p): stub\n",iface,pBuffer);
3569 return DSERR_INVALIDCALL;
3572 static const IDsDriverVtbl dsdvt =
3574 IDsDriverImpl_QueryInterface,
3575 IDsDriverImpl_AddRef,
3576 IDsDriverImpl_Release,
3577 IDsDriverImpl_GetDriverDesc,
3579 IDsDriverImpl_Close,
3580 IDsDriverImpl_GetCaps,
3581 IDsDriverImpl_CreateSoundBuffer,
3582 IDsDriverImpl_DuplicateSoundBuffer
3585 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
3587 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
3589 TRACE("driver created\n");
3591 /* the HAL isn't much better than the HEL if we can't do mmap() */
3592 if (!(WOutDev[wDevID].outcaps.dwSupport & WAVECAPS_DIRECTSOUND)) {
3593 ERR("DirectSound flag not set\n");
3594 MESSAGE("This sound card's driver does not support direct access\n");
3595 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
3596 return MMSYSERR_NOTSUPPORTED;
3599 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
3601 return MMSYSERR_NOMEM;
3602 (*idrv)->lpVtbl = &dsdvt;
3605 (*idrv)->wDevID = wDevID;
3606 (*idrv)->primary = NULL;
3607 return MMSYSERR_NOERROR;
3610 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
3612 memcpy(desc, &(WOutDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
3613 return MMSYSERR_NOERROR;
3616 /*======================================================================*
3617 * Low level WAVE IN implementation *
3618 *======================================================================*/
3620 /**************************************************************************
3621 * widNotifyClient [internal]
3623 static DWORD widNotifyClient(WINE_WAVEDEV* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
3625 TRACE("wMsg = 0x%04x dwParm1 = %04X dwParam2 = %04X\n", wMsg, dwParam1, dwParam2);
3631 if (wwi->wFlags != DCB_NULL &&
3632 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags, (HDRVR)wwi->waveDesc.hWave,
3633 wMsg, wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
3634 WARN("can't notify client !\n");
3635 return MMSYSERR_ERROR;
3639 FIXME("Unknown callback message %u\n", wMsg);
3640 return MMSYSERR_INVALPARAM;
3642 return MMSYSERR_NOERROR;
3645 /**************************************************************************
3646 * widGetDevCaps [internal]
3648 static DWORD widGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
3650 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
3652 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
3654 if (wDevID >= ALSA_WidNumDevs) {
3655 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
3656 return MMSYSERR_BADDEVICEID;
3659 memcpy(lpCaps, &WInDev[wDevID].incaps, min(dwSize, sizeof(*lpCaps)));
3660 return MMSYSERR_NOERROR;
3663 /**************************************************************************
3664 * widRecorder_ReadHeaders [internal]
3666 static void widRecorder_ReadHeaders(WINE_WAVEDEV * wwi)
3668 enum win_wm_message tmp_msg;
3673 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
3674 if (tmp_msg == WINE_WM_HEADER) {
3676 lpWaveHdr = (LPWAVEHDR)tmp_param;
3677 lpWaveHdr->lpNext = 0;
3679 if (wwi->lpQueuePtr == 0)
3680 wwi->lpQueuePtr = lpWaveHdr;
3682 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
3686 ERR("should only have headers left\n");
3691 /**************************************************************************
3692 * widRecorder [internal]
3694 static DWORD CALLBACK widRecorder(LPVOID pmt)
3696 WORD uDevID = (DWORD)pmt;
3697 WINE_WAVEDEV* wwi = (WINE_WAVEDEV*)&WInDev[uDevID];
3701 LPVOID buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wwi->dwPeriodSize);
3702 char *pOffset = buffer;
3703 enum win_wm_message msg;
3706 DWORD frames_per_period;
3708 wwi->state = WINE_WS_STOPPED;
3709 wwi->dwTotalRecorded = 0;
3710 wwi->lpQueuePtr = NULL;
3712 SetEvent(wwi->hStartUpEvent);
3714 /* make sleep time to be # of ms to output a period */
3715 dwSleepTime = (1024/*wwi-dwPeriodSize => overrun!*/ * 1000) / wwi->format.Format.nAvgBytesPerSec;
3716 frames_per_period = snd_pcm_bytes_to_frames(wwi->pcm, wwi->dwPeriodSize);
3717 TRACE("sleeptime=%d ms\n", dwSleepTime);
3720 /* wait for dwSleepTime or an event in thread's queue */
3721 /* FIXME: could improve wait time depending on queue state,
3722 * ie, number of queued fragments
3724 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
3731 lpWaveHdr = wwi->lpQueuePtr;
3732 /* read all the fragments accumulated so far */
3733 frames = snd_pcm_avail_update(wwi->pcm);
3734 bytes = snd_pcm_frames_to_bytes(wwi->pcm, frames);
3735 TRACE("frames = %d bytes = %d\n", frames, bytes);
3736 periods = bytes / wwi->dwPeriodSize;
3737 while ((periods > 0) && (wwi->lpQueuePtr))
3740 bytes = wwi->dwPeriodSize;
3741 TRACE("bytes = %d\n",bytes);
3742 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwPeriodSize)
3744 /* directly read fragment in wavehdr */
3745 read = wwi->read(wwi->pcm, lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded, frames_per_period);
3746 bytesRead = snd_pcm_frames_to_bytes(wwi->pcm, read);
3748 TRACE("bytesRead=%d (direct)\n", bytesRead);
3749 if (bytesRead != (DWORD) -1)
3751 /* update number of bytes recorded in current buffer and by this device */
3752 lpWaveHdr->dwBytesRecorded += bytesRead;
3753 wwi->dwTotalRecorded += bytesRead;
3755 /* buffer is full. notify client */
3756 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
3758 /* must copy the value of next waveHdr, because we have no idea of what
3759 * will be done with the content of lpWaveHdr in callback
3761 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
3763 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3764 lpWaveHdr->dwFlags |= WHDR_DONE;
3766 wwi->lpQueuePtr = lpNext;
3767 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3771 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->pcmname,
3772 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
3773 frames_per_period, strerror(errno));
3778 /* read the fragment in a local buffer */
3779 read = wwi->read(wwi->pcm, buffer, frames_per_period);
3780 bytesRead = snd_pcm_frames_to_bytes(wwi->pcm, read);
3783 TRACE("bytesRead=%d (local)\n", bytesRead);
3785 if (bytesRead == (DWORD) -1) {
3786 TRACE("read(%s, %p, %d) failed (%s)\n", wwi->pcmname,
3787 buffer, frames_per_period, strerror(errno));
3791 /* copy data in client buffers */
3792 while (bytesRead != (DWORD) -1 && bytesRead > 0)
3794 DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
3796 memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
3800 /* update number of bytes recorded in current buffer and by this device */
3801 lpWaveHdr->dwBytesRecorded += dwToCopy;
3802 wwi->dwTotalRecorded += dwToCopy;
3803 bytesRead -= dwToCopy;
3804 pOffset += dwToCopy;
3806 /* client buffer is full. notify client */
3807 if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
3809 /* must copy the value of next waveHdr, because we have no idea of what
3810 * will be done with the content of lpWaveHdr in callback
3812 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
3813 TRACE("lpNext=%p\n", lpNext);
3815 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3816 lpWaveHdr->dwFlags |= WHDR_DONE;
3818 wwi->lpQueuePtr = lpNext;
3819 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3822 if (!lpNext && bytesRead) {
3823 /* before we give up, check for more header messages */
3824 while (ALSA_PeekRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
3826 if (msg == WINE_WM_HEADER) {
3828 ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev);
3829 hdr = ((LPWAVEHDR)param);
3830 TRACE("msg = %s, hdr = %p, ev = %p\n", getCmdString(msg), hdr, ev);
3832 if (lpWaveHdr == 0) {
3833 /* new head of queue */
3834 wwi->lpQueuePtr = lpWaveHdr = hdr;
3836 /* insert buffer at the end of queue */
3838 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
3845 if (lpWaveHdr == 0) {
3846 /* no more buffer to copy data to, but we did read more.
3847 * what hasn't been copied will be dropped
3849 WARN("buffer under run! %u bytes dropped.\n", bytesRead);
3850 wwi->lpQueuePtr = NULL;
3860 WAIT_OMR(&wwi->msgRing, dwSleepTime);
3862 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
3864 TRACE("msg=%s param=0x%x\n", getCmdString(msg), param);
3866 case WINE_WM_PAUSING:
3867 wwi->state = WINE_WS_PAUSED;
3868 /*FIXME("Device should stop recording\n");*/
3871 case WINE_WM_STARTING:
3872 wwi->state = WINE_WS_PLAYING;
3873 snd_pcm_start(wwi->pcm);
3876 case WINE_WM_HEADER:
3877 lpWaveHdr = (LPWAVEHDR)param;
3878 lpWaveHdr->lpNext = 0;
3880 /* insert buffer at the end of queue */
3883 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
3887 case WINE_WM_STOPPING:
3888 if (wwi->state != WINE_WS_STOPPED)
3890 snd_pcm_drain(wwi->pcm);
3892 /* read any headers in queue */
3893 widRecorder_ReadHeaders(wwi);
3895 /* return current buffer to app */
3896 lpWaveHdr = wwi->lpQueuePtr;
3899 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
3900 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
3901 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3902 lpWaveHdr->dwFlags |= WHDR_DONE;
3903 wwi->lpQueuePtr = lpNext;
3904 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3907 wwi->state = WINE_WS_STOPPED;
3910 case WINE_WM_RESETTING:
3911 if (wwi->state != WINE_WS_STOPPED)
3913 snd_pcm_drain(wwi->pcm);
3915 wwi->state = WINE_WS_STOPPED;
3916 wwi->dwTotalRecorded = 0;
3918 /* read any headers in queue */
3919 widRecorder_ReadHeaders(wwi);
3921 /* return all buffers to the app */
3922 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
3923 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
3924 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
3925 lpWaveHdr->dwFlags |= WHDR_DONE;
3926 wwi->lpQueuePtr = lpWaveHdr->lpNext;
3927 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
3930 wwi->lpQueuePtr = NULL;
3933 case WINE_WM_CLOSING:
3935 wwi->state = WINE_WS_CLOSED;
3937 HeapFree(GetProcessHeap(), 0, buffer);
3939 /* shouldn't go here */
3940 case WINE_WM_UPDATE:
3945 FIXME("unknown message %d\n", msg);
3951 /* just for not generating compilation warnings... should never be executed */
3955 /**************************************************************************
3956 * widOpen [internal]
3958 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
3961 snd_pcm_hw_params_t * hw_params;
3962 snd_pcm_sw_params_t * sw_params;
3963 snd_pcm_access_t access;
3964 snd_pcm_format_t format;
3966 unsigned int buffer_time = 500000;
3967 unsigned int period_time = 10000;
3968 snd_pcm_uframes_t buffer_size;
3969 snd_pcm_uframes_t period_size;
3975 snd_pcm_hw_params_alloca(&hw_params);
3976 snd_pcm_sw_params_alloca(&sw_params);
3978 /* JPW TODO - review this code */
3979 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
3980 if (lpDesc == NULL) {
3981 WARN("Invalid Parameter !\n");
3982 return MMSYSERR_INVALPARAM;
3984 if (wDevID >= ALSA_WidNumDevs) {
3985 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
3986 return MMSYSERR_BADDEVICEID;
3989 /* only PCM format is supported so far... */
3990 if (!supportedFormat(lpDesc->lpFormat)) {
3991 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
3992 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
3993 lpDesc->lpFormat->nSamplesPerSec);
3994 return WAVERR_BADFORMAT;
3997 if (dwFlags & WAVE_FORMAT_QUERY) {
3998 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
3999 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
4000 lpDesc->lpFormat->nSamplesPerSec);
4001 return MMSYSERR_NOERROR;
4004 wwi = &WInDev[wDevID];
4006 if (wwi->pcm != NULL) {
4007 WARN("already allocated\n");
4008 return MMSYSERR_ALLOCATED;
4011 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwi->dwSupport & WAVECAPS_DIRECTSOUND))
4012 /* not supported, ignore it */
4013 dwFlags &= ~WAVE_DIRECTSOUND;
4016 flags = SND_PCM_NONBLOCK;
4018 if ( dwFlags & WAVE_DIRECTSOUND )
4019 flags |= SND_PCM_ASYNC;
4022 if ( (err=snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, flags)) < 0 )
4024 ERR("Error open: %s\n", snd_strerror(err));
4025 return MMSYSERR_NOTENABLED;
4028 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
4030 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
4031 copy_format(lpDesc->lpFormat, &wwi->format);
4033 if (wwi->format.Format.wBitsPerSample == 0) {
4034 WARN("Resetting zeroed wBitsPerSample\n");
4035 wwi->format.Format.wBitsPerSample = 8 *
4036 (wwi->format.Format.nAvgBytesPerSec /
4037 wwi->format.Format.nSamplesPerSec) /
4038 wwi->format.Format.nChannels;
4041 snd_pcm_hw_params_any(pcm, hw_params);
4043 #define EXIT_ON_ERROR(f,e,txt) do \
4046 if ( (err = (f) ) < 0) \
4048 WARN(txt ": %s\n", snd_strerror(err)); \
4049 snd_pcm_close(pcm); \
4054 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
4055 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
4056 WARN("mmap not available. switching to standard write.\n");
4057 access = SND_PCM_ACCESS_RW_INTERLEAVED;
4058 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
4059 wwi->read = snd_pcm_readi;
4062 wwi->read = snd_pcm_mmap_readi;
4064 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwi->format.Format.nChannels), WAVERR_BADFORMAT, "unable to set required channels");
4066 if ((wwi->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
4067 ((wwi->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
4068 IsEqualGUID(&wwi->format.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) {
4069 format = (wwi->format.Format.wBitsPerSample == 8) ? SND_PCM_FORMAT_U8 :
4070 (wwi->format.Format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE :
4071 (wwi->format.Format.wBitsPerSample == 24) ? SND_PCM_FORMAT_S24_LE :
4072 (wwi->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_S32_LE : -1;
4073 } else if ((wwi->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
4074 IsEqualGUID(&wwi->format.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
4075 format = (wwi->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_FLOAT_LE : -1;
4076 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_MULAW) {
4077 FIXME("unimplemented format: WAVE_FORMAT_MULAW\n");
4079 return WAVERR_BADFORMAT;
4080 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_ALAW) {
4081 FIXME("unimplemented format: WAVE_FORMAT_ALAW\n");
4083 return WAVERR_BADFORMAT;
4084 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_ADPCM) {
4085 FIXME("unimplemented format: WAVE_FORMAT_ADPCM\n");
4087 return WAVERR_BADFORMAT;
4089 ERR("invalid format: %0x04x\n", wwi->format.Format.wFormatTag);
4091 return WAVERR_BADFORMAT;
4094 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), WAVERR_BADFORMAT, "unable to set required format");
4096 rate = wwi->format.Format.nSamplesPerSec;
4098 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
4100 WARN("Rate %d Hz not available for playback: %s\n", wwi->format.Format.nSamplesPerSec, snd_strerror(rate));
4102 return WAVERR_BADFORMAT;
4104 if (!NearMatch(rate, wwi->format.Format.nSamplesPerSec)) {
4105 WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwi->format.Format.nSamplesPerSec, rate);
4107 return WAVERR_BADFORMAT;
4111 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
4113 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
4115 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
4118 err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
4119 err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
4121 snd_pcm_sw_params_current(pcm, sw_params);
4122 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");
4123 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
4124 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
4125 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
4126 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
4127 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
4128 #undef EXIT_ON_ERROR
4130 snd_pcm_prepare(pcm);
4133 ALSA_TraceParameters(hw_params, sw_params, FALSE);
4135 /* now, we can save all required data for later use... */
4136 if ( wwi->hw_params )
4137 snd_pcm_hw_params_free(wwi->hw_params);
4138 snd_pcm_hw_params_malloc(&(wwi->hw_params));
4139 snd_pcm_hw_params_copy(wwi->hw_params, hw_params);
4141 wwi->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
4142 wwi->lpQueuePtr = wwi->lpPlayPtr = wwi->lpLoopPtr = NULL;
4145 ALSA_InitRingMessage(&wwi->msgRing);
4147 wwi->dwPeriodSize = period_size;
4148 /*if (wwi->dwFragmentSize % wwi->format.Format.nBlockAlign)
4149 ERR("Fragment doesn't contain an integral number of data blocks\n");
4151 TRACE("dwPeriodSize=%u\n", wwi->dwPeriodSize);
4152 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
4153 wwi->format.Format.wBitsPerSample, wwi->format.Format.nAvgBytesPerSec,
4154 wwi->format.Format.nSamplesPerSec, wwi->format.Format.nChannels,
4155 wwi->format.Format.nBlockAlign);
4157 if (!(dwFlags & WAVE_DIRECTSOUND)) {
4158 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
4159 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
4161 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
4162 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
4163 CloseHandle(wwi->hStartUpEvent);
4165 wwi->hThread = INVALID_HANDLE_VALUE;
4166 wwi->dwThreadID = 0;
4168 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
4170 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
4174 /**************************************************************************
4175 * widClose [internal]
4177 static DWORD widClose(WORD wDevID)
4179 DWORD ret = MMSYSERR_NOERROR;
4182 TRACE("(%u);\n", wDevID);
4184 if (wDevID >= ALSA_WidNumDevs) {
4185 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
4186 return MMSYSERR_BADDEVICEID;
4189 if (WInDev[wDevID].pcm == NULL) {
4190 WARN("Requested to close already closed device %d!\n", wDevID);
4191 return MMSYSERR_BADDEVICEID;
4194 wwi = &WInDev[wDevID];
4195 if (wwi->lpQueuePtr) {
4196 WARN("buffers still playing !\n");
4197 ret = WAVERR_STILLPLAYING;
4199 if (wwi->hThread != INVALID_HANDLE_VALUE) {
4200 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
4202 ALSA_DestroyRingMessage(&wwi->msgRing);
4204 snd_pcm_hw_params_free(wwi->hw_params);
4205 wwi->hw_params = NULL;
4207 snd_pcm_close(wwi->pcm);
4210 ret = widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
4216 /**************************************************************************
4217 * widAddBuffer [internal]
4220 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
4222 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
4224 /* first, do the sanity checks... */
4225 if (wDevID >= ALSA_WidNumDevs) {
4226 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
4227 return MMSYSERR_BADDEVICEID;
4230 if (WInDev[wDevID].pcm == NULL) {
4231 WARN("Requested to add buffer to already closed device %d!\n", wDevID);
4232 return MMSYSERR_BADDEVICEID;
4235 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
4236 return WAVERR_UNPREPARED;
4238 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
4239 return WAVERR_STILLPLAYING;
4241 lpWaveHdr->dwFlags &= ~WHDR_DONE;
4242 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
4243 lpWaveHdr->lpNext = 0;
4245 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
4247 return MMSYSERR_NOERROR;
4250 /**************************************************************************
4251 * widStart [internal]
4254 static DWORD widStart(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
4256 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
4258 /* first, do the sanity checks... */
4259 if (wDevID >= ALSA_WidNumDevs) {
4260 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
4261 return MMSYSERR_BADDEVICEID;
4264 if (WInDev[wDevID].pcm == NULL) {
4265 WARN("Requested to start closed device %d!\n", wDevID);
4266 return MMSYSERR_BADDEVICEID;
4269 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
4271 return MMSYSERR_NOERROR;
4274 /**************************************************************************
4275 * widStop [internal]
4278 static DWORD widStop(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
4280 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
4282 /* first, do the sanity checks... */
4283 if (wDevID >= ALSA_WidNumDevs) {
4284 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
4285 return MMSYSERR_BADDEVICEID;
4288 if (WInDev[wDevID].pcm == NULL) {
4289 WARN("Requested to stop closed device %d!\n", wDevID);
4290 return MMSYSERR_BADDEVICEID;
4293 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
4295 return MMSYSERR_NOERROR;
4298 /**************************************************************************
4299 * widReset [internal]
4301 static DWORD widReset(WORD wDevID)
4303 TRACE("(%u);\n", wDevID);
4304 if (wDevID >= ALSA_WidNumDevs) {
4305 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
4306 return MMSYSERR_BADDEVICEID;
4309 if (WInDev[wDevID].pcm == NULL) {
4310 WARN("Requested to reset closed device %d!\n", wDevID);
4311 return MMSYSERR_BADDEVICEID;
4314 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
4315 return MMSYSERR_NOERROR;
4318 /**************************************************************************
4319 * widGetPosition [internal]
4321 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
4325 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
4327 if (wDevID >= ALSA_WidNumDevs) {
4328 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
4329 return MMSYSERR_BADDEVICEID;
4332 if (WInDev[wDevID].state == WINE_WS_CLOSED) {
4333 WARN("Requested position of closed device %d!\n", wDevID);
4334 return MMSYSERR_BADDEVICEID;
4337 if (lpTime == NULL) {
4338 WARN("invalid parameter: lpTime = NULL\n");
4339 return MMSYSERR_INVALPARAM;
4342 wwi = &WInDev[wDevID];
4343 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_UPDATE, 0, TRUE);
4345 return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->format);
4348 /**************************************************************************
4349 * widGetNumDevs [internal]
4351 static DWORD widGetNumDevs(void)
4353 return ALSA_WidNumDevs;
4356 /**************************************************************************
4357 * widDevInterfaceSize [internal]
4359 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
4361 TRACE("(%u, %p)\n", wDevID, dwParam1);
4363 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
4364 NULL, 0 ) * sizeof(WCHAR);
4365 return MMSYSERR_NOERROR;
4368 /**************************************************************************
4369 * widDevInterface [internal]
4371 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
4373 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
4374 NULL, 0 ) * sizeof(WCHAR))
4376 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
4377 dwParam1, dwParam2 / sizeof(WCHAR));
4378 return MMSYSERR_NOERROR;
4380 return MMSYSERR_INVALPARAM;
4383 /**************************************************************************
4384 * widDsCreate [internal]
4386 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
4388 TRACE("(%d,%p)\n",wDevID,drv);
4390 /* the HAL isn't much better than the HEL if we can't do mmap() */
4391 FIXME("DirectSoundCapture not implemented\n");
4392 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
4393 return MMSYSERR_NOTSUPPORTED;
4396 /**************************************************************************
4397 * widDsDesc [internal]
4399 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
4401 memcpy(desc, &(WInDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
4402 return MMSYSERR_NOERROR;
4405 /**************************************************************************
4406 * widMessage (WINEALSA.@)
4408 DWORD WINAPI ALSA_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
4409 DWORD dwParam1, DWORD dwParam2)
4411 TRACE("(%u, %s, %08X, %08X, %08X);\n",
4412 wDevID, getMessage(wMsg), dwUser, dwParam1, dwParam2);
4419 /* FIXME: Pretend this is supported */
4421 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
4422 case WIDM_CLOSE: return widClose (wDevID);
4423 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
4424 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
4425 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
4426 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
4427 case WIDM_GETNUMDEVS: return widGetNumDevs ();
4428 case WIDM_GETPOS: return widGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
4429 case WIDM_RESET: return widReset (wDevID);
4430 case WIDM_START: return widStart (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
4431 case WIDM_STOP: return widStop (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
4432 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
4433 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
4434 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
4435 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
4437 FIXME("unknown message %d!\n", wMsg);
4439 return MMSYSERR_NOTSUPPORTED;
4444 /**************************************************************************
4445 * widMessage (WINEALSA.@)
4447 DWORD WINAPI ALSA_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
4448 DWORD dwParam1, DWORD dwParam2)
4450 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
4451 return MMSYSERR_NOTENABLED;
4454 /**************************************************************************
4455 * wodMessage (WINEALSA.@)
4457 DWORD WINAPI ALSA_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
4458 DWORD dwParam1, DWORD dwParam2)
4460 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
4461 return MMSYSERR_NOTENABLED;
4464 #endif /* HAVE_ALSA */