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 dsound output implementation *
27 *======================================================================*/
30 #include "wine/port.h"
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
46 #ifdef HAVE_SYS_MMAN_H
47 # include <sys/mman.h>
57 #include "wine/library.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
65 typedef struct IDsDriverImpl IDsDriverImpl;
66 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
71 IDsDriver IDsDriver_iface;
74 /* IDsDriverImpl fields */
75 IDsDriverBufferImpl* primary;
79 struct IDsDriverBufferImpl
81 IDsDriverBuffer IDsDriverBuffer_iface;
85 CRITICAL_SECTION pcm_crst;
87 DWORD mmap_buflen_bytes;
91 snd_pcm_hw_params_t *hw_params;
92 snd_pcm_sw_params_t *sw_params;
93 snd_pcm_uframes_t mmap_buflen_frames, mmap_pos, mmap_commitahead;
96 static inline IDsDriverImpl *impl_from_IDsDriver(IDsDriver *iface)
98 return CONTAINING_RECORD(iface, IDsDriverImpl, IDsDriver_iface);
101 static inline IDsDriverBufferImpl *impl_from_IDsDriverBuffer(IDsDriverBuffer *iface)
103 return CONTAINING_RECORD(iface, IDsDriverBufferImpl, IDsDriverBuffer_iface);
106 /** Fill buffers, for starting and stopping
107 * Alsa won't start playing until everything is filled up
108 * This also updates mmap_pos
110 * Returns: Amount of periods in use so snd_pcm_avail_update
111 * doesn't have to be called up to 4x in GetPosition()
113 static snd_pcm_uframes_t CommitAll(IDsDriverBufferImpl *This)
115 const snd_pcm_channel_area_t *areas;
116 snd_pcm_sframes_t used;
117 const snd_pcm_uframes_t commitahead = This->mmap_commitahead;
119 used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
120 if (used < 0) used = 0;
121 TRACE("%p needs to commit to %lu, used: %ld\n", This, commitahead, used);
122 if (used < commitahead)
124 snd_pcm_sframes_t done;
125 snd_pcm_uframes_t putin = commitahead - used;
128 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
129 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
133 if (putin + This->mmap_pos > This->mmap_buflen_frames)
134 putin = This->mmap_buflen_frames - This->mmap_pos;
135 done = snd_pcm_writei(This->pcm, This->mmap_buffer + snd_pcm_frames_to_bytes(This->pcm, This->mmap_pos), putin);
136 if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
138 if (done < 0) done = 0;
139 This->mmap_pos += done;
141 putin = commitahead - used;
143 if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0)
147 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
148 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
149 This->mmap_pos += done;
153 done = snd_pcm_writei(This->pcm, This->mmap_buffer, putin);
154 if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
155 if (done < 0) done = 0;
156 This->mmap_pos = done;
162 if (This->mmap_pos == This->mmap_buflen_frames)
168 static void CheckXRUN(IDsDriverBufferImpl* This)
170 snd_pcm_state_t state = snd_pcm_state(This->pcm);
173 if ( state == SND_PCM_STATE_XRUN )
175 err = snd_pcm_prepare(This->pcm);
177 snd_pcm_start(This->pcm);
178 WARN("xrun occurred\n");
180 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
182 else if ( state == SND_PCM_STATE_SUSPENDED )
184 int err = snd_pcm_resume(This->pcm);
185 TRACE("recovery from suspension occurred\n");
186 if (err < 0 && err != -EAGAIN){
187 err = snd_pcm_prepare(This->pcm);
189 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
191 } else if ( state != SND_PCM_STATE_RUNNING ) {
192 FIXME("Unhandled state: %d\n", state);
197 * Allocate the memory-mapped buffer for direct sound, and set up the
200 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
202 snd_pcm_t *pcm = pdbi->pcm;
203 snd_pcm_format_t format;
204 snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
205 unsigned int channels, bits_per_sample, bits_per_frame;
207 const snd_pcm_channel_area_t *areas;
208 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
209 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
212 mmap_mode = snd_pcm_type(pcm);
214 if (mmap_mode == SND_PCM_TYPE_HW)
215 TRACE("mmap'd buffer is a direct hardware buffer.\n");
216 else if (mmap_mode == SND_PCM_TYPE_DMIX)
217 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
219 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
221 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
223 err = snd_pcm_hw_params_get_format(hw_params, &format);
224 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
225 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
226 bits_per_sample = snd_pcm_format_physical_width(format);
227 bits_per_frame = bits_per_sample * channels;
229 if (TRACE_ON(dsalsa))
230 ALSA_TraceParameters(hw_params, NULL, FALSE);
232 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
233 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
235 pdbi->mmap_buflen_frames = frames;
236 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( pcm, frames );
238 snd_pcm_sw_params_current(pcm, sw_params);
239 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
240 snd_pcm_sw_params_get_boundary(sw_params, &boundary);
241 snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
242 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, boundary);
243 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
244 snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
245 err = snd_pcm_sw_params(pcm, sw_params);
247 avail = snd_pcm_avail_update(pcm);
248 if ((snd_pcm_sframes_t)avail < 0)
250 ERR("No buffer is available: %s.\n", snd_strerror(avail));
251 return DSERR_GENERIC;
256 buf = pdbi->mmap_buffer = HeapAlloc(GetProcessHeap(), 0, pdbi->mmap_buflen_bytes);
258 return DSERR_OUTOFMEMORY;
260 snd_pcm_format_set_silence(format, buf, pdbi->mmap_buflen_frames);
265 err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
268 ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err);
269 return DSERR_GENERIC;
271 snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
272 pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
273 pdbi->mmap_buffer = areas->addr;
276 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
277 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
282 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
284 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
285 FIXME("(): stub!\n");
286 return DSERR_UNSUPPORTED;
289 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
291 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
292 ULONG refCount = InterlockedIncrement(&This->ref);
294 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
299 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
301 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
302 ULONG refCount = InterlockedDecrement(&This->ref);
304 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
309 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
311 if (This == This->drv->primary)
312 This->drv->primary = NULL;
314 This->pcm_crst.DebugInfo->Spare[0] = 0;
315 DeleteCriticalSection(&This->pcm_crst);
317 snd_pcm_drop(This->pcm);
318 snd_pcm_close(This->pcm);
320 HeapFree(GetProcessHeap(), 0, This->sw_params);
321 HeapFree(GetProcessHeap(), 0, This->hw_params);
323 HeapFree(GetProcessHeap(), 0, This->mmap_buffer);
324 HeapFree(GetProcessHeap(), 0, This);
328 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
329 LPVOID*ppvAudio1,LPDWORD pdwLen1,
330 LPVOID*ppvAudio2,LPDWORD pdwLen2,
331 DWORD dwWritePosition,DWORD dwWriteLen,
334 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
335 snd_pcm_uframes_t writepos;
337 TRACE("%d bytes from %d\n", dwWriteLen, dwWritePosition);
340 EnterCriticalSection(&This->pcm_crst);
342 if (dwFlags & DSBLOCK_ENTIREBUFFER)
343 dwWriteLen = This->mmap_buflen_bytes;
345 if (dwWriteLen > This->mmap_buflen_bytes || dwWritePosition >= This->mmap_buflen_bytes)
348 LeaveCriticalSection(&This->pcm_crst);
349 return DSERR_INVALIDPARAM;
352 if (ppvAudio2) *ppvAudio2 = NULL;
353 if (pdwLen2) *pdwLen2 = 0;
355 *ppvAudio1 = This->mmap_buffer + dwWritePosition;
356 *pdwLen1 = dwWriteLen;
358 if (dwWritePosition+dwWriteLen > This->mmap_buflen_bytes)
360 DWORD remainder = This->mmap_buflen_bytes - dwWritePosition;
361 *pdwLen1 = remainder;
363 if (ppvAudio2 && pdwLen2)
365 *ppvAudio2 = This->mmap_buffer;
366 *pdwLen2 = dwWriteLen - remainder;
368 else dwWriteLen = remainder;
371 writepos = snd_pcm_bytes_to_frames(This->pcm, dwWritePosition);
372 if (writepos == This->mmap_pos)
374 const snd_pcm_channel_area_t *areas;
375 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwWriteLen), putin = writelen;
376 TRACE("Hit mmap_pos, locking data!\n");
378 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
381 WARN("mmap_pos (%lu) != writepos (%lu) not locking data!\n", This->mmap_pos, writepos);
383 LeaveCriticalSection(&This->pcm_crst);
388 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
389 LPVOID pvAudio1,DWORD dwLen1,
390 LPVOID pvAudio2,DWORD dwLen2)
392 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
393 snd_pcm_uframes_t writepos;
399 EnterCriticalSection(&This->pcm_crst);
401 writepos = snd_pcm_bytes_to_frames(This->pcm, (DWORD_PTR)pvAudio1 - (DWORD_PTR)This->mmap_buffer);
402 if (writepos == This->mmap_pos)
404 const snd_pcm_channel_area_t *areas;
405 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen1);
406 TRACE("Committing data\n");
408 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
412 ret = snd_pcm_writei(This->pcm, pvAudio1, writelen);
415 WARN("Underrun occurred\n");
416 wine_snd_pcm_recover(This->pcm, -EPIPE, 1);
417 ret = snd_pcm_writei(This->pcm, pvAudio1, writelen);
419 /* Advance mmap pointer a little to make dsound notice the underrun and respond to it */
420 if (ret < writelen) WARN("Short write %ld/%d\n", writelen, ret);
421 This->mmap_pos += This->mmap_commitahead + ret;
422 This->mmap_pos %= This->mmap_buflen_frames;
425 This->mmap_pos += ret;
427 WARN("Committing data: %d / %s (%p %ld)\n", ret, snd_strerror(ret), pvAudio1, writelen);
430 if (This->mmap_pos == This->mmap_buflen_frames)
434 writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen2);
437 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &writelen);
438 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
443 ret = snd_pcm_writei(This->pcm, pvAudio2, writelen);
444 if (ret < writelen) WARN("Short write %ld/%d\n", writelen, ret);
445 This->mmap_pos = ret > 0 ? ret : 0;
447 assert(This->mmap_pos < This->mmap_buflen_frames);
450 LeaveCriticalSection(&This->pcm_crst);
456 static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx)
458 snd_pcm_t *pcm = NULL;
459 snd_pcm_hw_params_t *hw_params = This->hw_params;
460 unsigned int buffer_time = 500000;
461 snd_pcm_format_t format = -1;
462 snd_pcm_uframes_t psize;
463 DWORD rate = pwfx->nSamplesPerSec;
466 switch (pwfx->wBitsPerSample)
468 case 8: format = SND_PCM_FORMAT_U8; break;
469 case 16: format = SND_PCM_FORMAT_S16_LE; break;
470 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
471 case 32: format = SND_PCM_FORMAT_S32_LE; break;
472 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
475 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
478 if (errno != EBUSY || !This->pcm)
480 WARN("Cannot open sound device: %s\n", snd_strerror(err));
481 return DSERR_GENERIC;
483 snd_pcm_drop(This->pcm);
484 snd_pcm_close(This->pcm);
486 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
489 WARN("Cannot open sound device: %s\n", snd_strerror(err));
490 return DSERR_BUFFERLOST;
494 /* Set some defaults */
495 snd_pcm_hw_params_any(pcm, hw_params);
496 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
497 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
499 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
500 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
502 /* Alsa's rate resampling is only used if the application specifically requests
503 * a buffer at a certain frequency, else it is better to disable it due to unwanted
504 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
506 #if SND_LIB_VERSION >= 0x010009
507 snd_pcm_hw_params_set_rate_resample(pcm, hw_params, 0);
510 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
511 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
513 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
515 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
516 pwfx->nSamplesPerSec = rate;
517 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
518 /* Let DirectSound detect this */
521 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
522 snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, NULL);
524 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &buffer_time, NULL);
526 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
528 snd_pcm_hw_params_set_periods_near(pcm, hw_params, &buffer_time, NULL);
532 HeapFree(GetProcessHeap(), 0, This->mmap_buffer);
533 This->mmap_buffer = NULL;
536 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
542 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
545 err = snd_pcm_hw_params(pcm, hw_params);
547 /* ALSA needs at least 3 buffers to work successfully */
548 This->mmap_commitahead = 3 * psize;
549 while (This->mmap_commitahead <= 512)
550 This->mmap_commitahead += psize;
554 snd_pcm_drop(This->pcm);
555 snd_pcm_close(This->pcm);
558 snd_pcm_prepare(This->pcm);
559 DSDB_CreateMMAP(This);
564 WARN("Failed to apply changes: %s\n", snd_strerror(err));
572 snd_pcm_hw_params_current(This->pcm, This->hw_params);
574 return DSERR_BADFORMAT;
577 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
579 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
582 TRACE("(%p, %p)\n", iface, pwfx);
585 EnterCriticalSection(&This->pcm_crst);
586 hr = SetFormat(This, pwfx);
588 LeaveCriticalSection(&This->pcm_crst);
595 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
597 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
598 FIXME("(%p,%d): stub\n",iface,dwFreq);
602 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
604 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
605 FIXME("(%p,%p): stub\n",This,pVolPan);
606 /* TODO: Bring volume control back */
610 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
612 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
613 /* I don't even think alsa allows this */
614 FIXME("(%p,%d): stub\n",iface,dwNewPos);
615 return DSERR_UNSUPPORTED;
618 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
619 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
621 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
622 snd_pcm_uframes_t hw_pptr, hw_wptr;
623 snd_pcm_state_t state;
626 EnterCriticalSection(&This->pcm_crst);
630 FIXME("Bad pointer for pcm: %p\n", This->pcm);
631 LeaveCriticalSection(&This->pcm_crst);
632 return DSERR_GENERIC;
635 if (!lpdwPlay && !lpdwWrite)
638 state = snd_pcm_state(This->pcm);
640 if (state != SND_PCM_STATE_PREPARED && state != SND_PCM_STATE_RUNNING)
643 state = snd_pcm_state(This->pcm);
645 if (state == SND_PCM_STATE_RUNNING)
647 snd_pcm_sframes_t used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
651 WARN("Underrun: %ld / %ld\n", used, snd_pcm_avail_update(This->pcm));
654 snd_pcm_forward(This->pcm, -used);
655 This->mmap_pos += -used;
656 This->mmap_pos %= This->mmap_buflen_frames;
661 if (This->mmap_pos > used)
662 hw_pptr = This->mmap_pos - used;
664 hw_pptr = This->mmap_buflen_frames + This->mmap_pos - used;
665 hw_pptr %= This->mmap_buflen_frames;
667 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr, This->mmap_pos, used);
669 else hw_pptr = This->mmap_pos;
670 hw_wptr = This->mmap_pos;
672 LeaveCriticalSection(&This->pcm_crst);
676 *lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
678 *lpdwWrite = snd_pcm_frames_to_bytes(This->pcm, hw_wptr);
680 TRACE("hw_pptr=0x%08x, hw_wptr=0x%08x playpos=%d, writepos=%d\n", (unsigned int)hw_pptr, (unsigned int)hw_wptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
684 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
686 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
687 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
690 EnterCriticalSection(&This->pcm_crst);
691 snd_pcm_start(This->pcm);
693 LeaveCriticalSection(&This->pcm_crst);
697 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
699 const snd_pcm_channel_area_t *areas;
700 snd_pcm_uframes_t avail;
701 snd_pcm_format_t format;
702 IDsDriverBufferImpl *This = impl_from_IDsDriverBuffer(iface);
703 TRACE("(%p)\n",iface);
706 EnterCriticalSection(&This->pcm_crst);
707 avail = This->mmap_buflen_frames;
708 snd_pcm_drop(This->pcm);
709 snd_pcm_prepare(This->pcm);
710 avail = snd_pcm_avail_update(This->pcm);
711 snd_pcm_hw_params_get_format(This->hw_params, &format);
714 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &avail);
715 snd_pcm_format_set_silence(format, areas->addr, This->mmap_buflen_frames);
716 snd_pcm_mmap_commit(This->pcm, This->mmap_pos, 0);
720 snd_pcm_format_set_silence(format, This->mmap_buffer, This->mmap_buflen_frames);
721 snd_pcm_writei(This->pcm, This->mmap_buffer, This->mmap_buflen_frames);
726 LeaveCriticalSection(&This->pcm_crst);
730 static const IDsDriverBufferVtbl dsdbvt =
732 IDsDriverBufferImpl_QueryInterface,
733 IDsDriverBufferImpl_AddRef,
734 IDsDriverBufferImpl_Release,
735 IDsDriverBufferImpl_Lock,
736 IDsDriverBufferImpl_Unlock,
737 IDsDriverBufferImpl_SetFormat,
738 IDsDriverBufferImpl_SetFrequency,
739 IDsDriverBufferImpl_SetVolumePan,
740 IDsDriverBufferImpl_SetPosition,
741 IDsDriverBufferImpl_GetPosition,
742 IDsDriverBufferImpl_Play,
743 IDsDriverBufferImpl_Stop
746 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
748 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
749 FIXME("(%p): stub!\n",iface);
750 return DSERR_UNSUPPORTED;
753 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
755 IDsDriverImpl *This = impl_from_IDsDriver(iface);
756 ULONG refCount = InterlockedIncrement(&This->ref);
758 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
763 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
765 IDsDriverImpl *This = impl_from_IDsDriver(iface);
766 ULONG refCount = InterlockedDecrement(&This->ref);
768 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
773 HeapFree(GetProcessHeap(), 0, This);
777 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
779 IDsDriverImpl *This = impl_from_IDsDriver(iface);
780 TRACE("(%p,%p)\n",iface,pDesc);
781 *pDesc = WOutDev[This->wDevID].ds_desc;
782 pDesc->dwFlags = DSDDESC_DONTNEEDSECONDARYLOCK | DSDDESC_DONTNEEDWRITELEAD;
783 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
785 pDesc->wReserved = 0;
786 pDesc->ulDeviceNum = This->wDevID;
787 pDesc->dwHeapType = DSDHEAP_NOHEAP;
788 pDesc->pvDirectDrawHeap = NULL;
789 pDesc->dwMemStartAddress = 0xDEAD0000;
790 pDesc->dwMemEndAddress = 0xDEAF0000;
791 pDesc->dwMemAllocExtra = 0;
792 pDesc->pvReserved1 = NULL;
793 pDesc->pvReserved2 = NULL;
797 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
800 IDsDriverImpl *This = impl_from_IDsDriver(iface);
802 snd_pcm_t *pcm = NULL;
803 snd_pcm_hw_params_t *hw_params;
805 /* While this is not really needed, it is a good idea to do this,
806 * to see if sound can be initialized */
808 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
812 hr = DSERR_OUTOFMEMORY;
816 err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
817 if (err < 0) goto err;
818 err = snd_pcm_hw_params_any(pcm, hw_params);
819 if (err < 0) goto err;
820 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
822 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
823 if (err < 0) goto err;
831 FIXME("Failed to open device: %s\n", snd_strerror(err));
835 HeapFree(GetProcessHeap(), 0, hw_params);
837 WARN("--> %08x\n", hr);
841 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
843 IDsDriverImpl *This = impl_from_IDsDriver(iface);
844 TRACE("(%p) stub, harmless\n",This);
848 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
850 IDsDriverImpl *This = impl_from_IDsDriver(iface);
851 TRACE("(%p,%p)\n",iface,pCaps);
852 *pCaps = WOutDev[This->wDevID].ds_caps;
856 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
858 DWORD dwFlags, DWORD dwCardAddress,
859 LPDWORD pdwcbBufferSize,
863 IDsDriverImpl *This = impl_from_IDsDriver(iface);
864 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
867 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
868 /* we only support primary buffers... for now */
869 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
870 return DSERR_UNSUPPORTED;
872 return DSERR_ALLOCATED;
874 *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
875 if (*ippdsdb == NULL)
876 return DSERR_OUTOFMEMORY;
878 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
879 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
880 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
882 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
883 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
884 return DSERR_OUTOFMEMORY;
886 (*ippdsdb)->IDsDriverBuffer_iface.lpVtbl = &dsdbvt;
888 (*ippdsdb)->drv = This;
889 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
890 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSOUTPUT.pcm_crst");
892 /* SetFormat has to re-initialize pcm here anyway */
893 err = SetFormat(*ippdsdb, pwfx);
896 WARN("Error occurred: %08x\n", err);
900 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
901 This->primary = *ippdsdb;
903 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
904 *ppbBuffer = (*ippdsdb)->mmap_buffer;
906 /* buffer is ready to go */
907 TRACE("buffer created at %p\n", *ippdsdb);
911 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
912 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
913 HeapFree(GetProcessHeap(), 0, *ippdsdb);
918 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
919 PIDSDRIVERBUFFER pBuffer,
922 IDsDriverImpl *This = impl_from_IDsDriver(iface);
923 FIXME("(%p,%p): stub\n",This,pBuffer);
924 return DSERR_INVALIDCALL;
927 static const IDsDriverVtbl dsdvt =
929 IDsDriverImpl_QueryInterface,
930 IDsDriverImpl_AddRef,
931 IDsDriverImpl_Release,
932 IDsDriverImpl_GetDriverDesc,
935 IDsDriverImpl_GetCaps,
936 IDsDriverImpl_CreateSoundBuffer,
937 IDsDriverImpl_DuplicateSoundBuffer
940 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
942 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
944 TRACE("driver created\n");
946 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
948 return MMSYSERR_NOMEM;
949 (*idrv)->IDsDriver_iface.lpVtbl = &dsdvt;
952 (*idrv)->wDevID = wDevID;
953 (*idrv)->primary = NULL;
954 return MMSYSERR_NOERROR;
957 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
959 *desc = WOutDev[wDevID].ds_desc;
960 return MMSYSERR_NOERROR;
963 #endif /* HAVE_ALSA */