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);
63 WINE_WAVEDEV *WOutDev;
64 DWORD ALSA_WodNumMallocedDevs;
65 DWORD ALSA_WodNumDevs;
67 /**************************************************************************
68 * wodNotifyClient [internal]
70 static void wodNotifyClient(WINE_WAVEDEV* wwo, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
72 TRACE("wMsg = 0x%04x dwParm1 = %lx dwParam2 = %lx\n", wMsg, dwParam1, dwParam2);
78 DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
79 wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2);
82 FIXME("Unknown callback message %u\n", wMsg);
86 /**************************************************************************
87 * wodUpdatePlayedTotal [internal]
90 static BOOL wodUpdatePlayedTotal(WINE_WAVEDEV* wwo, snd_pcm_status_t* ps)
92 snd_pcm_sframes_t delay;
93 snd_pcm_sframes_t avail;
94 snd_pcm_uframes_t buf_size = 0;
95 snd_pcm_state_t state;
97 state = snd_pcm_state(wwo->pcm);
98 avail = snd_pcm_avail_update(wwo->pcm);
99 snd_pcm_hw_params_get_buffer_size(wwo->hw_params, &buf_size);
100 delay = buf_size - avail;
102 if (state != SND_PCM_STATE_RUNNING && state != SND_PCM_STATE_PREPARED)
104 WARN("Unexpected state (%d) while updating Total Played, resetting\n", state);
105 wine_snd_pcm_recover(wwo->pcm, -EPIPE, 0);
109 /* A delay < 0 indicates an underrun; for our purposes that's 0. */
112 WARN("Unexpected delay (%ld) while updating Total Played, resetting\n", delay);
116 InterlockedExchange((LONG*)&wwo->dwPlayedTotal, wwo->dwWrittenTotal - snd_pcm_frames_to_bytes(wwo->pcm, delay));
120 /**************************************************************************
121 * wodPlayer_BeginWaveHdr [internal]
123 * Makes the specified lpWaveHdr the currently playing wave header.
124 * If the specified wave header is a begin loop and we're not already in
125 * a loop, setup the loop.
127 static void wodPlayer_BeginWaveHdr(WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
129 wwo->lpPlayPtr = lpWaveHdr;
131 if (!lpWaveHdr) return;
133 if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
134 if (wwo->lpLoopPtr) {
135 WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
137 TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
138 wwo->lpLoopPtr = lpWaveHdr;
139 /* Windows does not touch WAVEHDR.dwLoops,
140 * so we need to make an internal copy */
141 wwo->dwLoops = lpWaveHdr->dwLoops;
144 wwo->dwPartialOffset = 0;
147 /**************************************************************************
148 * wodPlayer_PlayPtrNext [internal]
150 * Advance the play pointer to the next waveheader, looping if required.
152 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEDEV* wwo)
154 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
156 wwo->dwPartialOffset = 0;
157 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
158 /* We're at the end of a loop, loop if required */
159 if (--wwo->dwLoops > 0) {
160 wwo->lpPlayPtr = wwo->lpLoopPtr;
162 /* Handle overlapping loops correctly */
163 if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
164 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
165 /* shall we consider the END flag for the closing loop or for
166 * the opening one or for both ???
167 * code assumes for closing loop only
170 lpWaveHdr = lpWaveHdr->lpNext;
172 wwo->lpLoopPtr = NULL;
173 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
176 /* We're not in a loop. Advance to the next wave header */
177 wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
183 /**************************************************************************
184 * wodPlayer_DSPWait [internal]
185 * Returns the number of milliseconds to wait for the DSP buffer to play a
188 static DWORD wodPlayer_DSPWait(const WINE_WAVEDEV *wwo)
190 /* time for one period to be played */
194 err = snd_pcm_hw_params_get_period_time(wwo->hw_params, &val, &dir);
198 /**************************************************************************
199 * wodPlayer_NotifyWait [internal]
200 * Returns the number of milliseconds to wait before attempting to notify
201 * completion of the specified wavehdr.
202 * This is based on the number of bytes remaining to be written in the
205 static DWORD wodPlayer_NotifyWait(const WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
209 if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
212 dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.Format.nAvgBytesPerSec;
213 if (!dwMillis) dwMillis = 1;
220 /**************************************************************************
221 * wodPlayer_WriteMaxFrags [internal]
222 * Writes the maximum number of frames possible to the DSP and returns
223 * the number of frames written.
225 static int wodPlayer_WriteMaxFrags(WINE_WAVEDEV* wwo, DWORD* frames)
227 /* Only attempt to write to free frames */
228 LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
229 DWORD dwLength = snd_pcm_bytes_to_frames(wwo->pcm, lpWaveHdr->dwBufferLength - wwo->dwPartialOffset);
230 int toWrite = min(dwLength, *frames);
233 TRACE("Writing wavehdr %p.%u[%u]\n", lpWaveHdr, wwo->dwPartialOffset, lpWaveHdr->dwBufferLength);
236 written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
238 /* XRUN occurred. let's try to recover */
239 wine_snd_pcm_recover(wwo->pcm, written, 0);
240 written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
244 ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written));
250 wwo->dwPartialOffset += snd_pcm_frames_to_bytes(wwo->pcm, written);
251 if (wwo->dwPartialOffset + wwo->format.Format.nBlockAlign - 1 >= lpWaveHdr->dwBufferLength) {
252 /* this will be used to check if the given wave header has been fully played or not... */
253 wwo->dwPartialOffset = lpWaveHdr->dwBufferLength;
254 /* If we wrote all current wavehdr, skip to the next one */
255 wodPlayer_PlayPtrNext(wwo);
258 wwo->dwWrittenTotal += snd_pcm_frames_to_bytes(wwo->pcm, written);
259 TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
265 /**************************************************************************
266 * wodPlayer_NotifyCompletions [internal]
268 * Notifies and remove from queue all wavehdrs which have been played to
269 * the speaker (ie. they have cleared the ALSA buffer). If force is true,
270 * we notify all wavehdrs and remove them all from the queue even if they
271 * are unplayed or part of a loop.
273 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEDEV* wwo, BOOL force)
277 /* Start from lpQueuePtr and keep notifying until:
278 * - we hit an unwritten wavehdr
279 * - we hit the beginning of a running loop
280 * - we hit a wavehdr which hasn't finished playing
284 lpWaveHdr = wwo->lpQueuePtr;
285 if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
288 snd_pcm_uframes_t frames;
289 snd_pcm_hw_params_get_period_size(wwo->hw_params, &frames, NULL);
291 if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
292 if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
293 if (lpWaveHdr->reserved > wwo->dwPlayedTotal + frames) {TRACE("still playing %p (%lu/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
295 wwo->dwPlayedTotal += lpWaveHdr->reserved - wwo->dwPlayedTotal;
296 wwo->lpQueuePtr = lpWaveHdr->lpNext;
298 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
299 lpWaveHdr->dwFlags |= WHDR_DONE;
301 wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
303 return (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
304 wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
308 /**************************************************************************
309 * wodPlayer_Reset [internal]
311 * wodPlayer helper. Resets current output stream.
313 static void wodPlayer_Reset(WINE_WAVEDEV* wwo, BOOL reset)
316 TRACE("(%p)\n", wwo);
318 wodUpdatePlayedTotal(wwo, NULL);
319 /* updates current notify list */
320 wodPlayer_NotifyCompletions(wwo, FALSE);
322 if ( (err = snd_pcm_drop(wwo->pcm)) < 0) {
323 FIXME("flush: %s\n", snd_strerror(err));
325 wwo->state = WINE_WS_STOPPED;
328 if ( (err = snd_pcm_prepare(wwo->pcm)) < 0 )
329 ERR("pcm prepare failed: %s\n", snd_strerror(err));
332 enum win_wm_message msg;
336 /* remove any buffer */
337 wodPlayer_NotifyCompletions(wwo, TRUE);
339 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
340 wwo->state = WINE_WS_STOPPED;
341 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
342 /* Clear partial wavehdr */
343 wwo->dwPartialOffset = 0;
345 /* remove any existing message in the ring */
346 EnterCriticalSection(&wwo->msgRing.msg_crst);
347 /* return all pending headers in queue */
348 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev))
350 if (msg != WINE_WM_HEADER)
352 FIXME("shouldn't have headers left\n");
356 ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
357 ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
359 wodNotifyClient(wwo, WOM_DONE, param, 0);
361 ALSA_ResetRingMessage(&wwo->msgRing);
362 LeaveCriticalSection(&wwo->msgRing.msg_crst);
364 if (wwo->lpLoopPtr) {
365 /* complicated case, not handled yet (could imply modifying the loop counter */
366 FIXME("Pausing while in loop isn't correctly handled yet, expect strange results\n");
367 wwo->lpPlayPtr = wwo->lpLoopPtr;
368 wwo->dwPartialOffset = 0;
369 wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
372 DWORD sz = wwo->dwPartialOffset;
374 /* reset all the data as if we had written only up to lpPlayedTotal bytes */
375 /* compute the max size playable from lpQueuePtr */
376 for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
377 sz += ptr->dwBufferLength;
379 /* because the reset lpPlayPtr will be lpQueuePtr */
380 if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
381 wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
382 wwo->dwWrittenTotal = wwo->dwPlayedTotal;
383 wwo->lpPlayPtr = wwo->lpQueuePtr;
385 wwo->state = WINE_WS_PAUSED;
389 /**************************************************************************
390 * wodPlayer_ProcessMessages [internal]
392 static void wodPlayer_ProcessMessages(WINE_WAVEDEV* wwo)
395 enum win_wm_message msg;
400 while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, ¶m, &ev)) {
401 TRACE("Received %s %lx\n", ALSA_getCmdString(msg), param);
404 case WINE_WM_PAUSING:
405 if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_RUNNING )
407 if ( snd_pcm_hw_params_can_pause(wwo->hw_params) )
409 err = snd_pcm_pause(wwo->pcm, 1);
411 ERR("pcm_pause failed: %s\n", snd_strerror(err));
412 wwo->state = WINE_WS_PAUSED;
416 wodPlayer_Reset(wwo,FALSE);
421 case WINE_WM_RESTARTING:
422 if (wwo->state == WINE_WS_PAUSED)
424 if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_PAUSED )
426 err = snd_pcm_pause(wwo->pcm, 0);
428 ERR("pcm_pause failed: %s\n", snd_strerror(err));
430 wwo->state = WINE_WS_PLAYING;
435 lpWaveHdr = (LPWAVEHDR)param;
437 /* insert buffer at the end of queue */
440 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
444 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
445 if (wwo->state == WINE_WS_STOPPED)
446 wwo->state = WINE_WS_PLAYING;
448 case WINE_WM_RESETTING:
449 wodPlayer_Reset(wwo,TRUE);
452 case WINE_WM_BREAKLOOP:
453 if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
454 /* ensure exit at end of current loop */
459 case WINE_WM_CLOSING:
460 /* sanity check: this should not happen since the device must have been reset before */
461 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
463 wwo->state = WINE_WS_CLOSED;
466 /* shouldn't go here */
468 FIXME("unknown message %d\n", msg);
474 /**************************************************************************
475 * wodPlayer_FeedDSP [internal]
476 * Feed as much sound data as we can into the DSP and return the number of
477 * milliseconds before it will be necessary to feed the DSP again.
479 static DWORD wodPlayer_FeedDSP(WINE_WAVEDEV* wwo)
483 wodUpdatePlayedTotal(wwo, NULL);
484 availInQ = snd_pcm_avail_update(wwo->pcm);
486 /* no more room... no need to try to feed */
488 /* Feed from partial wavehdr */
489 if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
490 wodPlayer_WriteMaxFrags(wwo, &availInQ);
493 /* Feed wavehdrs until we run out of wavehdrs or DSP space */
494 if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
496 TRACE("Setting time to elapse for %p to %u\n",
497 wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
498 /* note the value that dwPlayedTotal will return when this wave finishes playing */
499 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
500 } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
504 return wodPlayer_DSPWait(wwo);
507 /**************************************************************************
508 * wodPlayer [internal]
510 static DWORD CALLBACK wodPlayer(LPVOID pmt)
512 WORD uDevID = (DWORD_PTR)pmt;
513 WINE_WAVEDEV* wwo = &WOutDev[uDevID];
514 DWORD dwNextFeedTime = INFINITE; /* Time before DSP needs feeding */
515 DWORD dwNextNotifyTime = INFINITE; /* Time before next wave completion */
518 wwo->state = WINE_WS_STOPPED;
519 SetEvent(wwo->hStartUpEvent);
522 /** Wait for the shortest time before an action is required. If there
523 * are no pending actions, wait forever for a command.
525 dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
526 TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
527 ALSA_WaitRingMessage(&wwo->msgRing, dwSleepTime);
528 wodPlayer_ProcessMessages(wwo);
529 if (wwo->state == WINE_WS_PLAYING) {
530 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
531 dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
532 if (dwNextFeedTime == INFINITE) {
533 /* FeedDSP ran out of data, but before giving up, */
534 /* check that a notification didn't give us more */
535 wodPlayer_ProcessMessages(wwo);
536 if (wwo->lpPlayPtr) {
537 TRACE("recovering\n");
538 dwNextFeedTime = wodPlayer_FeedDSP(wwo);
542 dwNextFeedTime = dwNextNotifyTime = INFINITE;
548 /**************************************************************************
549 * wodGetDevCaps [internal]
551 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
553 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
555 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
557 if (wDevID >= ALSA_WodNumDevs) {
558 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
559 return MMSYSERR_BADDEVICEID;
562 memcpy(lpCaps, &WOutDev[wDevID].outcaps, min(dwSize, sizeof(*lpCaps)));
563 return MMSYSERR_NOERROR;
566 /**************************************************************************
569 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
572 snd_pcm_t * pcm = NULL;
573 snd_hctl_t * hctl = NULL;
574 snd_pcm_hw_params_t * hw_params;
575 snd_pcm_sw_params_t * sw_params;
576 snd_pcm_access_t access;
577 snd_pcm_format_t format = -1;
579 unsigned int buffer_time = 120000;
580 unsigned int period_time = 22000;
581 snd_pcm_uframes_t buffer_size;
582 snd_pcm_uframes_t period_size;
588 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
589 if (lpDesc == NULL) {
590 WARN("Invalid Parameter !\n");
591 return MMSYSERR_INVALPARAM;
593 if (wDevID >= ALSA_WodNumDevs) {
594 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
595 return MMSYSERR_BADDEVICEID;
598 /* only PCM format is supported so far... */
599 if (!ALSA_supportedFormat(lpDesc->lpFormat)) {
600 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
601 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
602 lpDesc->lpFormat->nSamplesPerSec);
603 return WAVERR_BADFORMAT;
606 if (dwFlags & WAVE_FORMAT_QUERY) {
607 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
608 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
609 lpDesc->lpFormat->nSamplesPerSec);
610 return MMSYSERR_NOERROR;
613 wwo = &WOutDev[wDevID];
615 if (wwo->pcm != NULL) {
616 WARN("%d already allocated\n", wDevID);
617 return MMSYSERR_ALLOCATED;
620 if (dwFlags & WAVE_DIRECTSOUND)
621 FIXME("Why are we called with DirectSound flag? It doesn't use MMSYSTEM any more\n");
622 /* not supported, ignore it */
623 dwFlags &= ~WAVE_DIRECTSOUND;
625 flags = SND_PCM_NONBLOCK;
627 if ( (err = snd_pcm_open(&pcm, wwo->pcmname, SND_PCM_STREAM_PLAYBACK, flags)) < 0)
629 ERR("Error open: %s\n", snd_strerror(err));
630 return MMSYSERR_NOTENABLED;
635 err = snd_hctl_open(&hctl, wwo->ctlname, 0);
642 WARN("Could not open hctl for [%s]: %s\n", wwo->ctlname, snd_strerror(err));
647 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
649 wwo->waveDesc = *lpDesc;
650 ALSA_copyFormat(lpDesc->lpFormat, &wwo->format);
652 TRACE("Requested this format: %dx%dx%d %s\n",
653 wwo->format.Format.nSamplesPerSec,
654 wwo->format.Format.wBitsPerSample,
655 wwo->format.Format.nChannels,
656 ALSA_getFormat(wwo->format.Format.wFormatTag));
658 if (wwo->format.Format.wBitsPerSample == 0) {
659 WARN("Resetting zeroed wBitsPerSample\n");
660 wwo->format.Format.wBitsPerSample = 8 *
661 (wwo->format.Format.nAvgBytesPerSec /
662 wwo->format.Format.nSamplesPerSec) /
663 wwo->format.Format.nChannels;
666 #define EXIT_ON_ERROR(f,e,txt) do \
669 if ( (err = (f) ) < 0) \
671 WARN(txt ": %s\n", snd_strerror(err)); \
677 sw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof() );
678 hw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof() );
679 if (!hw_params || !sw_params)
681 retcode = MMSYSERR_NOMEM;
684 snd_pcm_hw_params_any(pcm, hw_params);
686 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
687 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
688 WARN("mmap not available. switching to standard write.\n");
689 access = SND_PCM_ACCESS_RW_INTERLEAVED;
690 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
691 wwo->write = snd_pcm_writei;
694 wwo->write = snd_pcm_mmap_writei;
696 if ((err = snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels)) < 0) {
697 WARN("unable to set required channels: %d\n", wwo->format.Format.nChannels);
698 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels ), WAVERR_BADFORMAT, "unable to set required channels" );
701 if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
702 ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
703 IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) {
704 format = (wwo->format.Format.wBitsPerSample == 8) ? SND_PCM_FORMAT_U8 :
705 (wwo->format.Format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE :
706 (wwo->format.Format.wBitsPerSample == 24) ? SND_PCM_FORMAT_S24_3LE :
707 (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_S32_LE : -1;
708 } else if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
709 IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
710 format = (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_FLOAT_LE : -1;
712 ERR("invalid format: %0x04x\n", wwo->format.Format.wFormatTag);
713 retcode = WAVERR_BADFORMAT;
717 if ((err = snd_pcm_hw_params_set_format(pcm, hw_params, format)) < 0) {
718 WARN("unable to set required format: %s\n", snd_pcm_format_name(format));
719 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), WAVERR_BADFORMAT, "unable to set required format" );
722 rate = wwo->format.Format.nSamplesPerSec;
724 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
726 WARN("Rate %d Hz not available for playback: %s\n", wwo->format.Format.nSamplesPerSec, snd_strerror(rate));
727 retcode = WAVERR_BADFORMAT;
730 if (!ALSA_NearMatch(rate, wwo->format.Format.nSamplesPerSec)) {
731 WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwo->format.Format.nSamplesPerSec, rate);
732 retcode = WAVERR_BADFORMAT;
736 TRACE("Got this format: %dx%dx%d %s\n",
737 wwo->format.Format.nSamplesPerSec,
738 wwo->format.Format.wBitsPerSample,
739 wwo->format.Format.nChannels,
740 ALSA_getFormat(wwo->format.Format.wFormatTag));
743 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
745 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
747 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
749 err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
750 err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
752 snd_pcm_sw_params_current(pcm, sw_params);
754 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set start threshold");
755 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
756 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
757 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
758 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
761 snd_pcm_prepare(pcm);
764 ALSA_TraceParameters(hw_params, sw_params, FALSE);
766 /* now, we can save all required data for later use... */
768 wwo->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
769 wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
770 wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
771 wwo->dwPartialOffset = 0;
773 ALSA_InitRingMessage(&wwo->msgRing);
775 wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
776 wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwo->dwThreadID));
778 ERR("Thread creation for the wodPlayer failed!\n");
779 CloseHandle(wwo->hStartUpEvent);
780 retcode = MMSYSERR_NOMEM;
783 SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
784 WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
785 CloseHandle(wwo->hStartUpEvent);
786 wwo->hStartUpEvent = NULL;
788 TRACE("handle=%p\n", pcm);
789 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
790 wwo->format.Format.wBitsPerSample, wwo->format.Format.nAvgBytesPerSec,
791 wwo->format.Format.nSamplesPerSec, wwo->format.Format.nChannels,
792 wwo->format.Format.nBlockAlign);
794 HeapFree( GetProcessHeap(), 0, sw_params );
795 wwo->hw_params = hw_params;
799 wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
800 return MMSYSERR_NOERROR;
809 snd_hctl_close(hctl);
812 HeapFree( GetProcessHeap(), 0, hw_params );
813 HeapFree( GetProcessHeap(), 0, sw_params );
814 if (wwo->msgRing.ring_buffer_size > 0)
815 ALSA_DestroyRingMessage(&wwo->msgRing);
821 /**************************************************************************
822 * wodClose [internal]
824 static DWORD wodClose(WORD wDevID)
828 TRACE("(%u);\n", wDevID);
830 if (wDevID >= ALSA_WodNumDevs) {
831 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
832 return MMSYSERR_BADDEVICEID;
835 wwo = &WOutDev[wDevID];
836 if (wwo->pcm == NULL) {
837 WARN("Requested to close already closed device %d!\n", wDevID);
838 return MMSYSERR_BADDEVICEID;
841 if (wwo->lpQueuePtr) {
842 WARN("buffers still playing !\n");
843 return WAVERR_STILLPLAYING;
846 ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
848 ALSA_DestroyRingMessage(&wwo->msgRing);
850 HeapFree( GetProcessHeap(), 0, wwo->hw_params );
851 wwo->hw_params = NULL;
855 snd_hctl_free(wwo->hctl);
856 snd_hctl_close(wwo->hctl);
860 snd_pcm_close(wwo->pcm);
863 wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
866 return MMSYSERR_NOERROR;
870 /**************************************************************************
871 * wodWrite [internal]
874 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
876 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
878 if (wDevID >= ALSA_WodNumDevs) {
879 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
880 return MMSYSERR_BADDEVICEID;
883 if (WOutDev[wDevID].pcm == NULL) {
884 WARN("Requested to write to closed device %d!\n", wDevID);
885 return MMSYSERR_BADDEVICEID;
888 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
889 return WAVERR_UNPREPARED;
891 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
892 return WAVERR_STILLPLAYING;
894 lpWaveHdr->dwFlags &= ~WHDR_DONE;
895 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
896 lpWaveHdr->lpNext = 0;
898 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
900 return MMSYSERR_NOERROR;
903 /**************************************************************************
904 * wodPause [internal]
906 static DWORD wodPause(WORD wDevID)
908 TRACE("(%u);!\n", wDevID);
910 if (wDevID >= ALSA_WodNumDevs) {
911 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
912 return MMSYSERR_BADDEVICEID;
915 if (WOutDev[wDevID].pcm == NULL) {
916 WARN("Requested to pause closed device %d!\n", wDevID);
917 return MMSYSERR_BADDEVICEID;
920 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
922 return MMSYSERR_NOERROR;
925 /**************************************************************************
926 * wodRestart [internal]
928 static DWORD wodRestart(WORD wDevID)
930 TRACE("(%u);\n", wDevID);
932 if (wDevID >= ALSA_WodNumDevs) {
933 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
934 return MMSYSERR_BADDEVICEID;
937 if (WOutDev[wDevID].pcm == NULL) {
938 WARN("Requested to restart closed device %d!\n", wDevID);
939 return MMSYSERR_BADDEVICEID;
942 if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
943 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
946 /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
947 /* FIXME: Myst crashes with this ... hmm -MM
948 return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
951 return MMSYSERR_NOERROR;
954 /**************************************************************************
955 * wodReset [internal]
957 static DWORD wodReset(WORD wDevID)
959 TRACE("(%u);\n", wDevID);
961 if (wDevID >= ALSA_WodNumDevs) {
962 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
963 return MMSYSERR_BADDEVICEID;
966 if (WOutDev[wDevID].pcm == NULL) {
967 WARN("Requested to reset closed device %d!\n", wDevID);
968 return MMSYSERR_BADDEVICEID;
971 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
973 return MMSYSERR_NOERROR;
976 /**************************************************************************
977 * wodGetPosition [internal]
979 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
983 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
985 if (wDevID >= ALSA_WodNumDevs) {
986 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
987 return MMSYSERR_BADDEVICEID;
990 if (WOutDev[wDevID].pcm == NULL) {
991 WARN("Requested to get position of closed device %d!\n", wDevID);
992 return MMSYSERR_BADDEVICEID;
995 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
997 wwo = &WOutDev[wDevID];
998 return ALSA_bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->format);
1001 /**************************************************************************
1002 * wodBreakLoop [internal]
1004 static DWORD wodBreakLoop(WORD wDevID)
1006 TRACE("(%u);\n", wDevID);
1008 if (wDevID >= ALSA_WodNumDevs) {
1009 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1010 return MMSYSERR_BADDEVICEID;
1013 if (WOutDev[wDevID].pcm == NULL) {
1014 WARN("Requested to breakloop of closed device %d!\n", wDevID);
1015 return MMSYSERR_BADDEVICEID;
1018 ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1019 return MMSYSERR_NOERROR;
1022 /**************************************************************************
1023 * wodGetVolume [internal]
1025 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1033 TRACE("(%u, %p);\n", wDevID, lpdwVol);
1034 if (wDevID >= ALSA_WodNumDevs) {
1035 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1036 return MMSYSERR_BADDEVICEID;
1039 if (lpdwVol == NULL)
1040 return MMSYSERR_NOTENABLED;
1042 wwo = &WOutDev[wDevID];
1044 rc = ALSA_CheckSetVolume(wwo->hctl, &left, &right, &min, &max, NULL, NULL, NULL);
1045 if (rc == MMSYSERR_NOERROR)
1047 #define VOLUME_ALSA_TO_WIN(x) ( ( (((x)-min) * 65535) + (max-min)/2 ) /(max-min))
1048 wleft = VOLUME_ALSA_TO_WIN(left);
1049 wright = VOLUME_ALSA_TO_WIN(right);
1050 #undef VOLUME_ALSA_TO_WIN
1051 TRACE("left=%d,right=%d,converted to windows left %d, right %d\n", left, right, wleft, wright);
1052 *lpdwVol = MAKELONG( wleft, wright );
1055 TRACE("CheckSetVolume failed; rc %d\n", rc);
1060 /**************************************************************************
1061 * wodSetVolume [internal]
1063 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1071 TRACE("(%u, %08X);\n", wDevID, dwParam);
1072 if (wDevID >= ALSA_WodNumDevs) {
1073 TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1074 return MMSYSERR_BADDEVICEID;
1077 wwo = &WOutDev[wDevID];
1079 rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, &min, &max, NULL, NULL, NULL);
1080 if (rc == MMSYSERR_NOERROR)
1082 wleft = LOWORD(dwParam);
1083 wright = HIWORD(dwParam);
1084 #define VOLUME_WIN_TO_ALSA(x) ( ( ( ((x) * (max-min)) + 32767) / 65535) + min )
1085 left = VOLUME_WIN_TO_ALSA(wleft);
1086 right = VOLUME_WIN_TO_ALSA(wright);
1087 #undef VOLUME_WIN_TO_ALSA
1088 rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, NULL, NULL, NULL, &left, &right);
1089 if (rc == MMSYSERR_NOERROR)
1090 TRACE("set volume: wleft=%d, wright=%d, converted to alsa left %d, right %d\n", wleft, wright, left, right);
1092 TRACE("SetVolume failed; rc %d\n", rc);
1098 /**************************************************************************
1099 * wodGetNumDevs [internal]
1101 static DWORD wodGetNumDevs(void)
1103 return ALSA_WodNumDevs;
1106 /**************************************************************************
1107 * wodDevInterfaceSize [internal]
1109 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1111 TRACE("(%u, %p)\n", wDevID, dwParam1);
1113 *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1114 NULL, 0 ) * sizeof(WCHAR);
1115 return MMSYSERR_NOERROR;
1118 /**************************************************************************
1119 * wodDevInterface [internal]
1121 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1123 if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1124 NULL, 0 ) * sizeof(WCHAR))
1126 MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1127 dwParam1, dwParam2 / sizeof(WCHAR));
1128 return MMSYSERR_NOERROR;
1130 return MMSYSERR_INVALPARAM;
1133 /**************************************************************************
1134 * wodMessage (WINEALSA.@)
1136 DWORD WINAPI ALSA_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1137 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1139 TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
1140 wDevID, ALSA_getMessage(wMsg), dwUser, dwParam1, dwParam2);
1148 /* FIXME: Pretend this is supported */
1150 case WODM_OPEN: return wodOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
1151 case WODM_CLOSE: return wodClose (wDevID);
1152 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (LPWAVEOUTCAPSW)dwParam1, dwParam2);
1153 case WODM_GETNUMDEVS: return wodGetNumDevs ();
1154 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
1155 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
1156 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1157 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
1158 case WODM_WRITE: return wodWrite (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
1159 case WODM_PAUSE: return wodPause (wDevID);
1160 case WODM_GETPOS: return wodGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
1161 case WODM_BREAKLOOP: return wodBreakLoop (wDevID);
1162 case WODM_PREPARE: return MMSYSERR_NOTSUPPORTED;
1163 case WODM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
1164 case WODM_GETVOLUME: return wodGetVolume (wDevID, (LPDWORD)dwParam1);
1165 case WODM_SETVOLUME: return wodSetVolume (wDevID, dwParam1);
1166 case WODM_RESTART: return wodRestart (wDevID);
1167 case WODM_RESET: return wodReset (wDevID);
1168 case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
1169 case DRV_QUERYDEVICEINTERFACE: return wodDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
1170 case DRV_QUERYDSOUNDIFACE: return wodDsCreate (wDevID, (PIDSDRIVER*)dwParam1);
1171 case DRV_QUERYDSOUNDDESC: return wodDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
1174 FIXME("unknown message %d!\n", wMsg);
1176 return MMSYSERR_NOTSUPPORTED;