2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * Copyright 2002 Eric Pouech
6 * 2002 Marco Pietrobono
7 * 2003 Christian Costa : WaveIn support
8 * 2006-2007 Maarten Lankhorst
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 /*======================================================================*
26 * Low level WAVE OUT implementation *
27 *======================================================================*/
30 #include "wine/port.h"
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
45 #ifdef HAVE_SYS_MMAN_H
46 # include <sys/mman.h>
57 #include "wine/library.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(wave);
65 WINE_WAVEDEV *WOutDev;
66 DWORD ALSA_WodNumMallocedDevs;
67 DWORD ALSA_WodNumDevs;
69 /**************************************************************************
70 * wodNotifyClient [internal]
72 static DWORD wodNotifyClient(WINE_WAVEDEV* wwo, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
74 TRACE("wMsg = 0x%04x dwParm1 = %lx dwParam2 = %lx\n", wMsg, dwParam1, dwParam2);
80 if (wwo->wFlags != DCB_NULL &&
81 !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
82 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
83 WARN("can't notify client !\n");
84 return MMSYSERR_ERROR;
88 FIXME("Unknown callback message %u\n", wMsg);
89 return MMSYSERR_INVALPARAM;
91 return MMSYSERR_NOERROR;
94 /**************************************************************************
95 * wodUpdatePlayedTotal [internal]
98 static BOOL wodUpdatePlayedTotal(WINE_WAVEDEV* wwo, snd_pcm_status_t* ps)
100 snd_pcm_sframes_t delay;
101 snd_pcm_sframes_t avail;
102 snd_pcm_uframes_t buf_size = 0;
103 snd_pcm_state_t state;
105 state = snd_pcm_state(wwo->pcm);
106 avail = snd_pcm_avail_update(wwo->pcm);
107 snd_pcm_hw_params_get_buffer_size(wwo->hw_params, &buf_size);
108 delay = buf_size - avail;
110 if (state != SND_PCM_STATE_RUNNING && state != SND_PCM_STATE_PREPARED)
112 WARN("Unexpected state (%d) while updating Total Played, resetting\n", state);
113 snd_pcm_recover(wwo->pcm, -EPIPE, 0);
117 /* A delay < 0 indicates an underrun; for our purposes that's 0. */
120 WARN("Unexpected delay (%ld) while updating Total Played, resetting\n", delay);
124 InterlockedExchange((LONG*)&wwo->dwPlayedTotal, wwo->dwWrittenTotal - snd_pcm_frames_to_bytes(wwo->pcm, delay));
128 /**************************************************************************
129 * wodPlayer_BeginWaveHdr [internal]
131 * Makes the specified lpWaveHdr the currently playing wave header.
132 * If the specified wave header is a begin loop and we're not already in
133 * a loop, setup the loop.
135 static void wodPlayer_BeginWaveHdr(WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
137 wwo->lpPlayPtr = lpWaveHdr;
139 if (!lpWaveHdr) return;
141 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
142 if (wwo->lpLoopPtr) {
143 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
145 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
146 wwo->lpLoopPtr = lpWaveHdr;
147 /* Windows does not touch WAVEHDR.dwLoops,
148 * so we need to make an internal copy */
149 wwo->dwLoops = lpWaveHdr->dwLoops;
152 wwo->dwPartialOffset = 0;
155 /**************************************************************************
156 * wodPlayer_PlayPtrNext [internal]
158 * Advance the play pointer to the next waveheader, looping if required.
160 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEDEV* wwo)
162 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
164 wwo->dwPartialOffset = 0;
165 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
166 /* We're at the end of a loop, loop if required */
167 if (--wwo->dwLoops > 0) {
168 wwo->lpPlayPtr = wwo->lpLoopPtr;
170 /* Handle overlapping loops correctly */
171 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
172 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
173 /* shall we consider the END flag for the closing loop or for
174 * the opening one or for both ???
175 * code assumes for closing loop only
178 lpWaveHdr = lpWaveHdr->lpNext;
180 wwo->lpLoopPtr = NULL;
181 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
184 /* We're not in a loop. Advance to the next wave header */
185 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
191 /**************************************************************************
192 * wodPlayer_DSPWait [internal]
193 * Returns the number of milliseconds to wait for the DSP buffer to play a
196 static DWORD wodPlayer_DSPWait(const WINE_WAVEDEV *wwo)
198 /* time for one period to be played */
202 err = snd_pcm_hw_params_get_period_time(wwo->hw_params, &val, &dir);
206 /**************************************************************************
207 * wodPlayer_NotifyWait [internal]
208 * Returns the number of milliseconds to wait before attempting to notify
209 * completion of the specified wavehdr.
210 * This is based on the number of bytes remaining to be written in the
213 static DWORD wodPlayer_NotifyWait(const WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
217 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
220 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.Format.nAvgBytesPerSec;
221 if (!dwMillis) dwMillis = 1;
228 /**************************************************************************
229 * wodPlayer_WriteMaxFrags [internal]
230 * Writes the maximum number of frames possible to the DSP and returns
231 * the number of frames written.
233 static int wodPlayer_WriteMaxFrags(WINE_WAVEDEV* wwo, DWORD* frames)
235 /* Only attempt to write to free frames */
236 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
237 DWORD dwLength = snd_pcm_bytes_to_frames(wwo->pcm, lpWaveHdr->dwBufferLength - wwo->dwPartialOffset);
238 int toWrite = min(dwLength, *frames);
241 TRACE("Writing wavehdr %p.%u[%u]\n", lpWaveHdr, wwo->dwPartialOffset, lpWaveHdr->dwBufferLength);
244 written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
246 /* XRUN occurred. let's try to recover */
247 ALSA_XRUNRecovery(wwo, written);
248 written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
252 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written));
258 wwo->dwPartialOffset += snd_pcm_frames_to_bytes(wwo->pcm, written);
259 if (wwo->dwPartialOffset + wwo->format.Format.nBlockAlign - 1 >= lpWaveHdr->dwBufferLength) {
260 /* this will be used to check if the given wave header has been fully played or not... */
261 wwo->dwPartialOffset = lpWaveHdr->dwBufferLength;
262 /* If we wrote all current wavehdr, skip to the next one */
263 wodPlayer_PlayPtrNext(wwo);
266 wwo->dwWrittenTotal += snd_pcm_frames_to_bytes(wwo->pcm, written);
267 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
273 /**************************************************************************
274 * wodPlayer_NotifyCompletions [internal]
276 * Notifies and remove from queue all wavehdrs which have been played to
277 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
278 * we notify all wavehdrs and remove them all from the queue even if they
279 * are unplayed or part of a loop.
281 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEDEV* wwo, BOOL force)
285 /* Start from lpQueuePtr and keep notifying until:
286 * - we hit an unwritten wavehdr
287 * - we hit the beginning of a running loop
288 * - we hit a wavehdr which hasn't finished playing
292 lpWaveHdr = wwo->lpQueuePtr;
293 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
296 snd_pcm_uframes_t frames;
297 snd_pcm_hw_params_get_period_size(wwo->hw_params, &frames, NULL);
299 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
300 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
301 if (lpWaveHdr->reserved > wwo->dwPlayedTotal + frames) {TRACE("still playing %p (%lu/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
303 wwo->dwPlayedTotal += lpWaveHdr->reserved - wwo->dwPlayedTotal;
304 wwo->lpQueuePtr = lpWaveHdr->lpNext;
306 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
307 lpWaveHdr->dwFlags |= WHDR_DONE;
309 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
311 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
312 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
316 /**************************************************************************
317 * wodPlayer_Reset [internal]
319 * wodPlayer helper. Resets current output stream.
321 static void wodPlayer_Reset(WINE_WAVEDEV* wwo, BOOL reset)
324 TRACE("(%p)\n", wwo);
326 wodUpdatePlayedTotal(wwo, NULL);
327 /* updates current notify list */
328 wodPlayer_NotifyCompletions(wwo, FALSE);
330 if ( (err = snd_pcm_drop(wwo->pcm)) < 0) {
331 FIXME("flush: %s\n", snd_strerror(err));
333 wwo->state = WINE_WS_STOPPED;
336 if ( (err = snd_pcm_prepare(wwo->pcm)) < 0 )
337 ERR("pcm prepare failed: %s\n", snd_strerror(err));
340 enum win_wm_message msg;
344 /* remove any buffer */
345 wodPlayer_NotifyCompletions(wwo, TRUE);
347 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
348 wwo->state = WINE_WS_STOPPED;
349 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
350 /* Clear partial wavehdr */
351 wwo->dwPartialOffset = 0;
353 /* remove any existing message in the ring */
354 EnterCriticalSection(&wwo->msgRing.msg_crst);
355 /* return all pending headers in queue */
356 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
358 if (msg != WINE_WM_HEADER)
360 FIXME("shouldn't have headers left\n");
364 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
365 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
367 wodNotifyClient(wwo, WOM_DONE, param, 0);
369 ALSA_ResetRingMessage(&wwo->msgRing);
370 LeaveCriticalSection(&wwo->msgRing.msg_crst);
372 if (wwo->lpLoopPtr) {
373 /* complicated case, not handled yet (could imply modifying the loop counter */
374 FIXME("Pausing while in loop isn't correctly handled yet, expect strange results\n");
375 wwo->lpPlayPtr = wwo->lpLoopPtr;
376 wwo->dwPartialOffset = 0;
377 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
380 DWORD sz = wwo->dwPartialOffset;
382 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
383 /* compute the max size playable from lpQueuePtr */
384 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
385 sz += ptr->dwBufferLength;
387 /* because the reset lpPlayPtr will be lpQueuePtr */
388 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
389 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
390 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
391 wwo->lpPlayPtr = wwo->lpQueuePtr;
393 wwo->state = WINE_WS_PAUSED;
397 /**************************************************************************
398 * wodPlayer_ProcessMessages [internal]
400 static void wodPlayer_ProcessMessages(WINE_WAVEDEV* wwo)
403 enum win_wm_message msg;
408 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
409 TRACE("Received %s %lx\n", ALSA_getCmdString(msg), param);
412 case WINE_WM_PAUSING:
413 if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_RUNNING )
415 if ( snd_pcm_hw_params_can_pause(wwo->hw_params) )
417 err = snd_pcm_pause(wwo->pcm, 1);
419 ERR("pcm_pause failed: %s\n", snd_strerror(err));
420 wwo->state = WINE_WS_PAUSED;
424 wodPlayer_Reset(wwo,FALSE);
429 case WINE_WM_RESTARTING:
430 if (wwo->state == WINE_WS_PAUSED)
432 if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_PAUSED )
434 err = snd_pcm_pause(wwo->pcm, 0);
436 ERR("pcm_pause failed: %s\n", snd_strerror(err));
438 wwo->state = WINE_WS_PLAYING;
443 lpWaveHdr = (LPWAVEHDR)param;
445 /* insert buffer at the end of queue */
448 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
452 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
453 if (wwo->state == WINE_WS_STOPPED)
454 wwo->state = WINE_WS_PLAYING;
456 case WINE_WM_RESETTING:
457 wodPlayer_Reset(wwo,TRUE);
460 case WINE_WM_BREAKLOOP:
461 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
462 /* ensure exit at end of current loop */
467 case WINE_WM_CLOSING:
468 /* sanity check: this should not happen since the device must have been reset before */
469 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
471 wwo->state = WINE_WS_CLOSED;
474 /* shouldn't go here */
476 FIXME("unknown message %d\n", msg);
482 /**************************************************************************
483 * wodPlayer_FeedDSP [internal]
484 * Feed as much sound data as we can into the DSP and return the number of
485 * milliseconds before it will be necessary to feed the DSP again.
487 static DWORD wodPlayer_FeedDSP(WINE_WAVEDEV* wwo)
491 wodUpdatePlayedTotal(wwo, NULL);
492 availInQ = snd_pcm_avail_update(wwo->pcm);
494 /* no more room... no need to try to feed */
496 /* Feed from partial wavehdr */
497 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
498 wodPlayer_WriteMaxFrags(wwo, &availInQ);
501 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
502 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
504 TRACE("Setting time to elapse for %p to %u\n",
505 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
506 /* note the value that dwPlayedTotal will return when this wave finishes playing */
507 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
508 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
512 return wodPlayer_DSPWait(wwo);
515 /**************************************************************************
516 * wodPlayer [internal]
518 static DWORD CALLBACK wodPlayer(LPVOID pmt)
520 WORD uDevID = (DWORD_PTR)pmt;
521 WINE_WAVEDEV* wwo = &WOutDev[uDevID];
522 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
523 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
526 wwo->state = WINE_WS_STOPPED;
527 SetEvent(wwo->hStartUpEvent);
530 /** Wait for the shortest time before an action is required. If there
531 * are no pending actions, wait forever for a command.
533 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
534 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
535 ALSA_WaitRingMessage(&wwo->msgRing, dwSleepTime);
536 wodPlayer_ProcessMessages(wwo);
537 if (wwo->state == WINE_WS_PLAYING) {
538 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
539 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
540 if (dwNextFeedTime == INFINITE) {
541 /* FeedDSP ran out of data, but before giving up, */
542 /* check that a notification didn't give us more */
543 wodPlayer_ProcessMessages(wwo);
544 if (wwo->lpPlayPtr) {
545 TRACE("recovering\n");
546 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
550 dwNextFeedTime = dwNextNotifyTime = INFINITE;
556 /**************************************************************************
557 * wodGetDevCaps [internal]
559 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
561 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
563 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
565 if (wDevID >= ALSA_WodNumDevs) {
566 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
567 return MMSYSERR_BADDEVICEID;
570 memcpy(lpCaps, &WOutDev[wDevID].outcaps, min(dwSize, sizeof(*lpCaps)));
571 return MMSYSERR_NOERROR;
574 /**************************************************************************
577 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
580 snd_pcm_t * pcm = NULL;
581 snd_hctl_t * hctl = NULL;
582 snd_pcm_hw_params_t * hw_params = NULL;
583 snd_pcm_sw_params_t * sw_params;
584 snd_pcm_access_t access;
585 snd_pcm_format_t format = -1;
587 unsigned int buffer_time = 120000;
588 unsigned int period_time = 22000;
589 snd_pcm_uframes_t buffer_size;
590 snd_pcm_uframes_t period_size;
596 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
597 if (lpDesc == NULL) {
598 WARN("Invalid Parameter !\n");
599 return MMSYSERR_INVALPARAM;
601 if (wDevID >= ALSA_WodNumDevs) {
602 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
603 return MMSYSERR_BADDEVICEID;
606 /* only PCM format is supported so far... */
607 if (!ALSA_supportedFormat(lpDesc->lpFormat)) {
608 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
609 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
610 lpDesc->lpFormat->nSamplesPerSec);
611 return WAVERR_BADFORMAT;
614 if (dwFlags & WAVE_FORMAT_QUERY) {
615 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
616 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
617 lpDesc->lpFormat->nSamplesPerSec);
618 return MMSYSERR_NOERROR;
621 wwo = &WOutDev[wDevID];
623 if (wwo->pcm != NULL) {
624 WARN("%d already allocated\n", wDevID);
625 return MMSYSERR_ALLOCATED;
628 if (dwFlags & WAVE_DIRECTSOUND)
629 FIXME("Why are we called with DirectSound flag? It doesn't use MMSYSTEM any more\n");
630 /* not supported, ignore it */
631 dwFlags &= ~WAVE_DIRECTSOUND;
633 flags = SND_PCM_NONBLOCK;
635 if ( (err = snd_pcm_open(&pcm, wwo->pcmname, SND_PCM_STREAM_PLAYBACK, flags)) < 0)
637 ERR("Error open: %s\n", snd_strerror(err));
638 return MMSYSERR_NOTENABLED;
643 err = snd_hctl_open(&hctl, wwo->ctlname, 0);
650 WARN("Could not open hctl for [%s]: %s\n", wwo->ctlname, snd_strerror(err));
655 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
657 wwo->waveDesc = *lpDesc;
658 ALSA_copyFormat(lpDesc->lpFormat, &wwo->format);
660 TRACE("Requested this format: %dx%dx%d %s\n",
661 wwo->format.Format.nSamplesPerSec,
662 wwo->format.Format.wBitsPerSample,
663 wwo->format.Format.nChannels,
664 ALSA_getFormat(wwo->format.Format.wFormatTag));
666 if (wwo->format.Format.wBitsPerSample == 0) {
667 WARN("Resetting zeroed wBitsPerSample\n");
668 wwo->format.Format.wBitsPerSample = 8 *
669 (wwo->format.Format.nAvgBytesPerSec /
670 wwo->format.Format.nSamplesPerSec) /
671 wwo->format.Format.nChannels;
674 #define EXIT_ON_ERROR(f,e,txt) do \
677 if ( (err = (f) ) < 0) \
679 WARN(txt ": %s\n", snd_strerror(err)); \
685 sw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof() );
686 snd_pcm_hw_params_malloc(&hw_params);
689 retcode = MMSYSERR_NOMEM;
692 snd_pcm_hw_params_any(pcm, hw_params);
694 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
695 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
696 WARN("mmap not available. switching to standard write.\n");
697 access = SND_PCM_ACCESS_RW_INTERLEAVED;
698 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
699 wwo->write = snd_pcm_writei;
702 wwo->write = snd_pcm_mmap_writei;
704 if ((err = snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels)) < 0) {
705 WARN("unable to set required channels: %d\n", wwo->format.Format.nChannels);
706 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels ), WAVERR_BADFORMAT, "unable to set required channels" );
709 if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
710 ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
711 IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) {
712 format = (wwo->format.Format.wBitsPerSample == 8) ? SND_PCM_FORMAT_U8 :
713 (wwo->format.Format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE :
714 (wwo->format.Format.wBitsPerSample == 24) ? SND_PCM_FORMAT_S24_3LE :
715 (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_S32_LE : -1;
716 } else if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
717 IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
718 format = (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_FLOAT_LE : -1;
719 } else if (wwo->format.Format.wFormatTag == WAVE_FORMAT_MULAW) {
720 FIXME("unimplemented format: WAVE_FORMAT_MULAW\n");
721 retcode = WAVERR_BADFORMAT;
723 } else if (wwo->format.Format.wFormatTag == WAVE_FORMAT_ALAW) {
724 FIXME("unimplemented format: WAVE_FORMAT_ALAW\n");
725 retcode = WAVERR_BADFORMAT;
727 } else if (wwo->format.Format.wFormatTag == WAVE_FORMAT_ADPCM) {
728 FIXME("unimplemented format: WAVE_FORMAT_ADPCM\n");
729 retcode = WAVERR_BADFORMAT;
732 ERR("invalid format: %0x04x\n", wwo->format.Format.wFormatTag);
733 retcode = WAVERR_BADFORMAT;
737 if ((err = snd_pcm_hw_params_set_format(pcm, hw_params, format)) < 0) {
738 WARN("unable to set required format: %s\n", snd_pcm_format_name(format));
739 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), WAVERR_BADFORMAT, "unable to set required format" );
742 rate = wwo->format.Format.nSamplesPerSec;
744 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
746 WARN("Rate %d Hz not available for playback: %s\n", wwo->format.Format.nSamplesPerSec, snd_strerror(rate));
747 retcode = WAVERR_BADFORMAT;
750 if (!ALSA_NearMatch(rate, wwo->format.Format.nSamplesPerSec)) {
751 WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwo->format.Format.nSamplesPerSec, rate);
752 retcode = WAVERR_BADFORMAT;
756 TRACE("Got this format: %dx%dx%d %s\n",
757 wwo->format.Format.nSamplesPerSec,
758 wwo->format.Format.wBitsPerSample,
759 wwo->format.Format.nChannels,
760 ALSA_getFormat(wwo->format.Format.wFormatTag));
763 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
765 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
767 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
769 err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
770 err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
772 snd_pcm_sw_params_current(pcm, sw_params);
774 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set start threshold");
775 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
776 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
777 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
778 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
781 snd_pcm_prepare(pcm);
784 ALSA_TraceParameters(hw_params, sw_params, FALSE);
786 /* now, we can save all required data for later use... */
788 wwo->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
789 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
790 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
791 wwo->dwPartialOffset = 0;
793 ALSA_InitRingMessage(&wwo->msgRing);
795 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
796 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwo->dwThreadID));
798 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
801 ERR("Thread creation for the wodPlayer failed!\n");
802 CloseHandle(wwo->hStartUpEvent);
803 retcode = MMSYSERR_NOMEM;
806 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
807 CloseHandle(wwo->hStartUpEvent);
808 wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
810 TRACE("handle=%p\n", pcm);
811 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
812 wwo->format.Format.wBitsPerSample, wwo->format.Format.nAvgBytesPerSec,
813 wwo->format.Format.nSamplesPerSec, wwo->format.Format.nChannels,
814 wwo->format.Format.nBlockAlign);
816 HeapFree( GetProcessHeap(), 0, sw_params );
819 if ( wwo->hw_params )
820 snd_pcm_hw_params_free(wwo->hw_params);
821 wwo->hw_params = hw_params;
823 return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
832 snd_hctl_close(hctl);
836 snd_pcm_hw_params_free(hw_params);
838 HeapFree( GetProcessHeap(), 0, sw_params );
839 if (wwo->msgRing.ring_buffer_size > 0)
840 ALSA_DestroyRingMessage(&wwo->msgRing);
846 /**************************************************************************
847 * wodClose [internal]
849 static DWORD wodClose(WORD wDevID)
851 DWORD ret = MMSYSERR_NOERROR;
854 TRACE("(%u);\n", wDevID);
856 if (wDevID >= ALSA_WodNumDevs) {
857 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
858 return MMSYSERR_BADDEVICEID;
861 if (WOutDev[wDevID].pcm == NULL) {
862 WARN("Requested to close already closed device %d!\n", wDevID);
863 return MMSYSERR_BADDEVICEID;
866 wwo = &WOutDev[wDevID];
867 if (wwo->lpQueuePtr) {
868 WARN("buffers still playing !\n");
869 ret = WAVERR_STILLPLAYING;
871 if (wwo->hThread != INVALID_HANDLE_VALUE) {
872 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
874 ALSA_DestroyRingMessage(&wwo->msgRing);
877 snd_pcm_hw_params_free(wwo->hw_params);
878 wwo->hw_params = NULL;
881 snd_pcm_close(wwo->pcm);
886 snd_hctl_free(wwo->hctl);
887 snd_hctl_close(wwo->hctl);
891 ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
898 /**************************************************************************
899 * wodWrite [internal]
902 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
904 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
906 if (wDevID >= ALSA_WodNumDevs) {
907 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
908 return MMSYSERR_BADDEVICEID;
911 if (WOutDev[wDevID].pcm == NULL) {
912 WARN("Requested to write to closed device %d!\n", wDevID);
913 return MMSYSERR_BADDEVICEID;
916 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
917 return WAVERR_UNPREPARED;
919 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
920 return WAVERR_STILLPLAYING;
922 lpWaveHdr->dwFlags &= ~WHDR_DONE;
923 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
924 lpWaveHdr->lpNext = 0;
926 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
928 return MMSYSERR_NOERROR;
931 /**************************************************************************
932 * wodPause [internal]
934 static DWORD wodPause(WORD wDevID)
936 TRACE("(%u);!\n", wDevID);
938 if (wDevID >= ALSA_WodNumDevs) {
939 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
940 return MMSYSERR_BADDEVICEID;
943 if (WOutDev[wDevID].pcm == NULL) {
944 WARN("Requested to pause closed device %d!\n", wDevID);
945 return MMSYSERR_BADDEVICEID;
948 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
950 return MMSYSERR_NOERROR;
953 /**************************************************************************
954 * wodRestart [internal]
956 static DWORD wodRestart(WORD wDevID)
958 TRACE("(%u);\n", wDevID);
960 if (wDevID >= ALSA_WodNumDevs) {
961 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
962 return MMSYSERR_BADDEVICEID;
965 if (WOutDev[wDevID].pcm == NULL) {
966 WARN("Requested to restart closed device %d!\n", wDevID);
967 return MMSYSERR_BADDEVICEID;
970 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
971 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
974 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
975 /* FIXME: Myst crashes with this ... hmm -MM
976 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
979 return MMSYSERR_NOERROR;
982 /**************************************************************************
983 * wodReset [internal]
985 static DWORD wodReset(WORD wDevID)
987 TRACE("(%u);\n", wDevID);
989 if (wDevID >= ALSA_WodNumDevs) {
990 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
991 return MMSYSERR_BADDEVICEID;
994 if (WOutDev[wDevID].pcm == NULL) {
995 WARN("Requested to reset closed device %d!\n", wDevID);
996 return MMSYSERR_BADDEVICEID;
999 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
1001 return MMSYSERR_NOERROR;
1004 /**************************************************************************
1005 * wodGetPosition [internal]
1007 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1011 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
1013 if (wDevID >= ALSA_WodNumDevs) {
1014 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1015 return MMSYSERR_BADDEVICEID;
1018 if (WOutDev[wDevID].pcm == NULL) {
1019 WARN("Requested to get position of closed device %d!\n", wDevID);
1020 return MMSYSERR_BADDEVICEID;
1023 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1025 wwo = &WOutDev[wDevID];
1026 return ALSA_bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->format);
1029 /**************************************************************************
1030 * wodBreakLoop [internal]
1032 static DWORD wodBreakLoop(WORD wDevID)
1034 TRACE("(%u);\n", wDevID);
1036 if (wDevID >= ALSA_WodNumDevs) {
1037 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1038 return MMSYSERR_BADDEVICEID;
1041 if (WOutDev[wDevID].pcm == NULL) {
1042 WARN("Requested to breakloop of closed device %d!\n", wDevID);
1043 return MMSYSERR_BADDEVICEID;
1046 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1047 return MMSYSERR_NOERROR;
1050 /**************************************************************************
1051 * wodGetVolume [internal]
1053 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1061 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1062 if (wDevID >= ALSA_WodNumDevs) {
1063 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1064 return MMSYSERR_BADDEVICEID;
1067 if (lpdwVol == NULL)
1068 return MMSYSERR_NOTENABLED;
1070 wwo = &WOutDev[wDevID];
1072 rc = ALSA_CheckSetVolume(wwo->hctl, &left, &right, &min, &max, NULL, NULL, NULL);
1073 if (rc == MMSYSERR_NOERROR)
1075 #define VOLUME_ALSA_TO_WIN(x) ( ( (((x)-min) * 65535) + (max-min)/2 ) /(max-min))
1076 wleft = VOLUME_ALSA_TO_WIN(left);
1077 wright = VOLUME_ALSA_TO_WIN(right);
1078 #undef VOLUME_ALSA_TO_WIN
1079 TRACE("left=%d,right=%d,converted to windows left %d, right %d\n", left, right, wleft, wright);
1080 *lpdwVol = MAKELONG( wleft, wright );
1083 TRACE("CheckSetVolume failed; rc %d\n", rc);
1088 /**************************************************************************
1089 * wodSetVolume [internal]
1091 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1099 TRACE("(%u, %08X);\n", wDevID, dwParam);
1100 if (wDevID >= ALSA_WodNumDevs) {
1101 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1102 return MMSYSERR_BADDEVICEID;
1105 wwo = &WOutDev[wDevID];
1107 rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, &min, &max, NULL, NULL, NULL);
1108 if (rc == MMSYSERR_NOERROR)
1110 wleft = LOWORD(dwParam);
1111 wright = HIWORD(dwParam);
1112 #define VOLUME_WIN_TO_ALSA(x) ( ( ( ((x) * (max-min)) + 32767) / 65535) + min )
1113 left = VOLUME_WIN_TO_ALSA(wleft);
1114 right = VOLUME_WIN_TO_ALSA(wright);
1115 #undef VOLUME_WIN_TO_ALSA
1116 rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, NULL, NULL, NULL, &left, &right);
1117 if (rc == MMSYSERR_NOERROR)
1118 TRACE("set volume: wleft=%d, wright=%d, converted to alsa left %d, right %d\n", wleft, wright, left, right);
1120 TRACE("SetVolume failed; rc %d\n", rc);
1126 /**************************************************************************
1127 * wodGetNumDevs [internal]
1129 static DWORD wodGetNumDevs(void)
1131 return ALSA_WodNumDevs;
1134 /**************************************************************************
1135 * wodDevInterfaceSize [internal]
1137 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1139 TRACE("(%u, %p)\n", wDevID, dwParam1);
1141 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1142 NULL, 0 ) * sizeof(WCHAR);
1143 return MMSYSERR_NOERROR;
1146 /**************************************************************************
1147 * wodDevInterface [internal]
1149 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1151 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1152 NULL, 0 ) * sizeof(WCHAR))
1154 MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1155 dwParam1, dwParam2 / sizeof(WCHAR));
1156 return MMSYSERR_NOERROR;
1158 return MMSYSERR_INVALPARAM;
1161 /**************************************************************************
1162 * wodMessage (WINEALSA.@)
1164 DWORD WINAPI ALSA_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1165 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1167 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
1168 wDevID, ALSA_getMessage(wMsg), dwUser, dwParam1, dwParam2);
1176 /* FIXME: Pretend this is supported */
1178 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1179 case WODM_CLOSE: return wodClose (wDevID);
1180 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
1181 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1182 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1183 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1184 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1185 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1186 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1187 case WODM_PAUSE: return wodPause (wDevID);
1188 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1189 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1190 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
1191 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
1192 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1193 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1194 case WODM_RESTART: return wodRestart (wDevID);
1195 case WODM_RESET: return wodReset (wDevID);
1196 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1197 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1198 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1199 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1202 FIXME("unknown message %d!\n", wMsg);
1204 return MMSYSERR_NOTSUPPORTED;
1207 #else /* HAVE_ALSA */
1209 /**************************************************************************
1210 * wodMessage (WINEALSA.@)
1212 DWORD WINAPI ALSA_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
1213 DWORD dwParam1, DWORD dwParam2)
1215 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
1216 return MMSYSERR_NOTENABLED;
1219 #endif /* HAVE_ALSA */