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 const IDsDriverVtbl *lpVtbl;
74 /* IDsDriverImpl fields */
75 IDsDriverBufferImpl* primary;
79 struct IDsDriverBufferImpl
81 const IDsDriverBufferVtbl *lpVtbl;
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 /** Fill buffers, for starting and stopping
97 * Alsa won't start playing until everything is filled up
98 * This also updates mmap_pos
100 * Returns: Amount of periods in use so snd_pcm_avail_update
101 * doesn't have to be called up to 4x in GetPosition()
103 static snd_pcm_uframes_t CommitAll(IDsDriverBufferImpl *This)
105 const snd_pcm_channel_area_t *areas;
106 snd_pcm_sframes_t used;
107 const snd_pcm_uframes_t commitahead = This->mmap_commitahead;
109 used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
110 if (used < 0) used = 0;
111 TRACE("%p needs to commit to %lu, used: %ld\n", This, commitahead, used);
112 if (used < commitahead)
114 snd_pcm_sframes_t done;
115 snd_pcm_uframes_t putin = commitahead - used;
118 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
119 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
123 if (putin + This->mmap_pos > This->mmap_buflen_frames)
124 putin = This->mmap_buflen_frames - This->mmap_pos;
125 done = snd_pcm_writei(This->pcm, This->mmap_buffer + snd_pcm_frames_to_bytes(This->pcm, This->mmap_pos), putin);
126 if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
128 if (done < 0) done = 0;
129 This->mmap_pos += done;
131 putin = commitahead - used;
133 if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0)
137 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
138 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
139 This->mmap_pos += done;
143 done = snd_pcm_writei(This->pcm, This->mmap_buffer, putin);
144 if (done < putin) WARN("Short write %ld/%ld\n", putin, done);
145 if (done < 0) done = 0;
146 This->mmap_pos = done;
152 if (This->mmap_pos == This->mmap_buflen_frames)
158 static void CheckXRUN(IDsDriverBufferImpl* This)
160 snd_pcm_state_t state = snd_pcm_state(This->pcm);
161 snd_pcm_sframes_t delay;
164 snd_pcm_hwsync(This->pcm);
165 snd_pcm_delay(This->pcm, &delay);
166 if ( state == SND_PCM_STATE_XRUN )
168 err = snd_pcm_prepare(This->pcm);
170 snd_pcm_start(This->pcm);
171 WARN("xrun occurred\n");
173 ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
175 else if ( state == SND_PCM_STATE_SUSPENDED )
177 int err = snd_pcm_resume(This->pcm);
178 TRACE("recovery from suspension occurred\n");
179 if (err < 0 && err != -EAGAIN){
180 err = snd_pcm_prepare(This->pcm);
182 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
184 } else if ( state != SND_PCM_STATE_RUNNING ) {
185 FIXME("Unhandled state: %d\n", state);
190 * Allocate the memory-mapped buffer for direct sound, and set up the
193 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
195 snd_pcm_t *pcm = pdbi->pcm;
196 snd_pcm_format_t format;
197 snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
198 unsigned int channels, bits_per_sample, bits_per_frame;
200 const snd_pcm_channel_area_t *areas;
201 snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
202 snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
205 mmap_mode = snd_pcm_type(pcm);
207 if (mmap_mode == SND_PCM_TYPE_HW)
208 TRACE("mmap'd buffer is a direct hardware buffer.\n");
209 else if (mmap_mode == SND_PCM_TYPE_DMIX)
210 TRACE("mmap'd buffer is an ALSA dmix buffer\n");
212 TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
214 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
216 err = snd_pcm_hw_params_get_format(hw_params, &format);
217 err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
218 err = snd_pcm_hw_params_get_channels(hw_params, &channels);
219 bits_per_sample = snd_pcm_format_physical_width(format);
220 bits_per_frame = bits_per_sample * channels;
222 if (TRACE_ON(dsalsa))
223 ALSA_TraceParameters(hw_params, NULL, FALSE);
225 TRACE("format=%s frames=%ld channels=%d bits_per_sample=%d bits_per_frame=%d\n",
226 snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
228 pdbi->mmap_buflen_frames = frames;
229 pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( pcm, frames );
231 snd_pcm_sw_params_current(pcm, sw_params);
232 snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
233 snd_pcm_sw_params_get_boundary(sw_params, &boundary);
234 snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
235 snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, boundary);
236 snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
237 snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
238 err = snd_pcm_sw_params(pcm, sw_params);
240 avail = snd_pcm_avail_update(pcm);
241 if ((snd_pcm_sframes_t)avail < 0)
243 ERR("No buffer is available: %s.\n", snd_strerror(avail));
244 return DSERR_GENERIC;
249 buf = pdbi->mmap_buffer = HeapAlloc(GetProcessHeap(), 0, pdbi->mmap_buflen_bytes);
251 return DSERR_OUTOFMEMORY;
253 snd_pcm_format_set_silence(format, buf, pdbi->mmap_buflen_frames);
258 err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
261 ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err);
262 return DSERR_GENERIC;
264 snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
265 pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
266 pdbi->mmap_buffer = areas->addr;
269 TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
270 frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
275 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
277 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
278 FIXME("(): stub!\n");
279 return DSERR_UNSUPPORTED;
282 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
284 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
285 ULONG refCount = InterlockedIncrement(&This->ref);
287 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
292 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
294 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
295 ULONG refCount = InterlockedDecrement(&This->ref);
297 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
302 TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
304 if (This == This->drv->primary)
305 This->drv->primary = NULL;
307 This->pcm_crst.DebugInfo->Spare[0] = 0;
308 DeleteCriticalSection(&This->pcm_crst);
310 snd_pcm_drop(This->pcm);
311 snd_pcm_close(This->pcm);
313 HeapFree(GetProcessHeap(), 0, This->sw_params);
314 HeapFree(GetProcessHeap(), 0, This->hw_params);
316 HeapFree(GetProcessHeap(), 0, This->mmap_buffer);
317 HeapFree(GetProcessHeap(), 0, This);
321 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
322 LPVOID*ppvAudio1,LPDWORD pdwLen1,
323 LPVOID*ppvAudio2,LPDWORD pdwLen2,
324 DWORD dwWritePosition,DWORD dwWriteLen,
327 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
328 snd_pcm_uframes_t writepos;
330 TRACE("%d bytes from %d\n", dwWriteLen, dwWritePosition);
333 EnterCriticalSection(&This->pcm_crst);
335 if (dwFlags & DSBLOCK_ENTIREBUFFER)
336 dwWriteLen = This->mmap_buflen_bytes;
338 if (dwWriteLen > This->mmap_buflen_bytes || dwWritePosition >= This->mmap_buflen_bytes)
341 LeaveCriticalSection(&This->pcm_crst);
342 return DSERR_INVALIDPARAM;
345 if (ppvAudio2) *ppvAudio2 = NULL;
346 if (pdwLen2) *pdwLen2 = 0;
348 *ppvAudio1 = This->mmap_buffer + dwWritePosition;
349 *pdwLen1 = dwWriteLen;
351 if (dwWritePosition+dwWriteLen > This->mmap_buflen_bytes)
353 DWORD remainder = This->mmap_buflen_bytes - dwWritePosition;
354 *pdwLen1 = remainder;
356 if (ppvAudio2 && pdwLen2)
358 *ppvAudio2 = This->mmap_buffer;
359 *pdwLen2 = dwWriteLen - remainder;
361 else dwWriteLen = remainder;
364 writepos = snd_pcm_bytes_to_frames(This->pcm, dwWritePosition);
365 if (writepos == This->mmap_pos)
367 const snd_pcm_channel_area_t *areas;
368 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwWriteLen), putin = writelen;
369 TRACE("Hit mmap_pos, locking data!\n");
371 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
374 WARN("mmap_pos (%lu) != writepos (%lu) not locking data!\n", This->mmap_pos, writepos);
376 LeaveCriticalSection(&This->pcm_crst);
381 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
382 LPVOID pvAudio1,DWORD dwLen1,
383 LPVOID pvAudio2,DWORD dwLen2)
385 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
386 snd_pcm_uframes_t writepos;
392 EnterCriticalSection(&This->pcm_crst);
394 writepos = snd_pcm_bytes_to_frames(This->pcm, (DWORD_PTR)pvAudio1 - (DWORD_PTR)This->mmap_buffer);
395 if (writepos == This->mmap_pos)
397 const snd_pcm_channel_area_t *areas;
398 snd_pcm_uframes_t writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen1);
399 TRACE("Committing data\n");
401 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
405 ret = snd_pcm_writei(This->pcm, pvAudio1, writelen);
408 WARN("Underrun occurred\n");
409 snd_pcm_recover(This->pcm, -EPIPE, 1);
410 ret = snd_pcm_writei(This->pcm, pvAudio1, writelen);
412 /* Advance mmap pointer a little to make dsound notice the underrun and respond to it */
413 if (ret < writelen) WARN("Short write %ld/%d\n", writelen, ret);
414 This->mmap_pos += This->mmap_commitahead + ret;
415 This->mmap_pos %= This->mmap_buflen_frames;
418 This->mmap_pos += ret;
420 WARN("Committing data: %d / %s (%p %ld)\n", ret, snd_strerror(ret), pvAudio1, writelen);
423 if (This->mmap_pos == This->mmap_buflen_frames)
427 writelen = snd_pcm_bytes_to_frames(This->pcm, dwLen2);
430 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &writelen);
431 This->mmap_pos += snd_pcm_mmap_commit(This->pcm, This->mmap_pos, writelen);
436 ret = snd_pcm_writei(This->pcm, pvAudio2, writelen);
437 if (ret < writelen) WARN("Short write %ld/%d\n", writelen, ret);
438 This->mmap_pos = ret > 0 ? ret : 0;
440 assert(This->mmap_pos < This->mmap_buflen_frames);
443 LeaveCriticalSection(&This->pcm_crst);
449 static HRESULT SetFormat(IDsDriverBufferImpl *This, LPWAVEFORMATEX pwfx)
451 snd_pcm_t *pcm = NULL;
452 snd_pcm_hw_params_t *hw_params = This->hw_params;
453 unsigned int buffer_time = 500000;
454 snd_pcm_format_t format = -1;
455 snd_pcm_uframes_t psize;
456 DWORD rate = pwfx->nSamplesPerSec;
459 switch (pwfx->wBitsPerSample)
461 case 8: format = SND_PCM_FORMAT_U8; break;
462 case 16: format = SND_PCM_FORMAT_S16_LE; break;
463 case 24: format = SND_PCM_FORMAT_S24_3LE; break;
464 case 32: format = SND_PCM_FORMAT_S32_LE; break;
465 default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
468 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
471 if (errno != EBUSY || !This->pcm)
473 WARN("Cannot open sound device: %s\n", snd_strerror(err));
474 return DSERR_GENERIC;
476 snd_pcm_drop(This->pcm);
477 snd_pcm_close(This->pcm);
479 err = snd_pcm_open(&pcm, WOutDev[This->drv->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
482 WARN("Cannot open sound device: %s\n", snd_strerror(err));
483 return DSERR_BUFFERLOST;
487 /* Set some defaults */
488 snd_pcm_hw_params_any(pcm, hw_params);
489 err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
490 if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
492 err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
493 if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
495 /* Alsa's rate resampling is only used if the application specifically requests
496 * a buffer at a certain frequency, else it is better to disable it due to unwanted
497 * side effects, which may include: Less granular pointer, changing buffer sizes, etc
499 #if SND_LIB_VERSION >= 0x010009
500 snd_pcm_hw_params_set_rate_resample(pcm, hw_params, 0);
503 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
504 if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
506 if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
508 WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
509 pwfx->nSamplesPerSec = rate;
510 pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
511 /* Let DirectSound detect this */
514 snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
515 snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, NULL);
517 snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &buffer_time, NULL);
519 err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
521 snd_pcm_hw_params_set_periods_near(pcm, hw_params, &buffer_time, NULL);
525 HeapFree(GetProcessHeap(), 0, This->mmap_buffer);
526 This->mmap_buffer = NULL;
529 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
535 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
538 err = snd_pcm_hw_params(pcm, hw_params);
540 /* ALSA needs at least 3 buffers to work successfully */
541 This->mmap_commitahead = 3 * psize;
542 while (This->mmap_commitahead <= 512)
543 This->mmap_commitahead += psize;
547 snd_pcm_drop(This->pcm);
548 snd_pcm_close(This->pcm);
551 snd_pcm_prepare(This->pcm);
552 DSDB_CreateMMAP(This);
557 WARN("Failed to apply changes: %s\n", snd_strerror(err));
565 snd_pcm_hw_params_current(This->pcm, This->hw_params);
567 return DSERR_BADFORMAT;
570 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
572 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
575 TRACE("(%p, %p)\n", iface, pwfx);
578 EnterCriticalSection(&This->pcm_crst);
579 hr = SetFormat(This, pwfx);
581 LeaveCriticalSection(&This->pcm_crst);
588 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
590 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
591 FIXME("(%p,%d): stub\n",iface,dwFreq);
595 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
597 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
598 FIXME("(%p,%p): stub\n",This,pVolPan);
599 /* TODO: Bring volume control back */
603 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
605 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
606 /* I don't even think alsa allows this */
607 FIXME("(%p,%d): stub\n",iface,dwNewPos);
608 return DSERR_UNSUPPORTED;
611 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
612 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
614 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
615 snd_pcm_uframes_t hw_pptr, hw_wptr;
616 snd_pcm_state_t state;
619 EnterCriticalSection(&This->pcm_crst);
623 FIXME("Bad pointer for pcm: %p\n", This->pcm);
624 LeaveCriticalSection(&This->pcm_crst);
625 return DSERR_GENERIC;
628 if (!lpdwPlay && !lpdwWrite)
631 state = snd_pcm_state(This->pcm);
633 if (state != SND_PCM_STATE_PREPARED && state != SND_PCM_STATE_RUNNING)
636 state = snd_pcm_state(This->pcm);
638 if (state == SND_PCM_STATE_RUNNING)
640 snd_pcm_sframes_t used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
644 WARN("Underrun: %ld / %ld\n", used, snd_pcm_avail_update(This->pcm));
647 snd_pcm_forward(This->pcm, -used);
648 This->mmap_pos += -used;
649 This->mmap_pos %= This->mmap_buflen_frames;
654 if (This->mmap_pos > used)
655 hw_pptr = This->mmap_pos - used;
657 hw_pptr = This->mmap_buflen_frames + This->mmap_pos - used;
658 hw_pptr %= This->mmap_buflen_frames;
660 TRACE("At position: %ld (%ld) - Used %ld\n", hw_pptr, This->mmap_pos, used);
662 else hw_pptr = This->mmap_pos;
663 hw_wptr = This->mmap_pos;
665 LeaveCriticalSection(&This->pcm_crst);
669 *lpdwPlay = snd_pcm_frames_to_bytes(This->pcm, hw_pptr);
671 *lpdwWrite = snd_pcm_frames_to_bytes(This->pcm, hw_wptr);
673 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);
677 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
679 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
680 TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
683 EnterCriticalSection(&This->pcm_crst);
684 snd_pcm_start(This->pcm);
686 LeaveCriticalSection(&This->pcm_crst);
690 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
692 const snd_pcm_channel_area_t *areas;
693 snd_pcm_uframes_t avail;
694 snd_pcm_format_t format;
695 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
696 TRACE("(%p)\n",iface);
699 EnterCriticalSection(&This->pcm_crst);
700 avail = This->mmap_buflen_frames;
701 snd_pcm_drop(This->pcm);
702 snd_pcm_prepare(This->pcm);
703 avail = snd_pcm_avail_update(This->pcm);
704 snd_pcm_hw_params_get_format(This->hw_params, &format);
707 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &avail);
708 snd_pcm_format_set_silence(format, areas->addr, This->mmap_buflen_frames);
709 snd_pcm_mmap_commit(This->pcm, This->mmap_pos, 0);
713 snd_pcm_format_set_silence(format, This->mmap_buffer, This->mmap_buflen_frames);
714 snd_pcm_writei(This->pcm, This->mmap_buffer, This->mmap_buflen_frames);
719 LeaveCriticalSection(&This->pcm_crst);
723 static const IDsDriverBufferVtbl dsdbvt =
725 IDsDriverBufferImpl_QueryInterface,
726 IDsDriverBufferImpl_AddRef,
727 IDsDriverBufferImpl_Release,
728 IDsDriverBufferImpl_Lock,
729 IDsDriverBufferImpl_Unlock,
730 IDsDriverBufferImpl_SetFormat,
731 IDsDriverBufferImpl_SetFrequency,
732 IDsDriverBufferImpl_SetVolumePan,
733 IDsDriverBufferImpl_SetPosition,
734 IDsDriverBufferImpl_GetPosition,
735 IDsDriverBufferImpl_Play,
736 IDsDriverBufferImpl_Stop
739 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
741 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
742 FIXME("(%p): stub!\n",iface);
743 return DSERR_UNSUPPORTED;
746 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
748 IDsDriverImpl *This = (IDsDriverImpl *)iface;
749 ULONG refCount = InterlockedIncrement(&This->ref);
751 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
756 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
758 IDsDriverImpl *This = (IDsDriverImpl *)iface;
759 ULONG refCount = InterlockedDecrement(&This->ref);
761 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
766 HeapFree(GetProcessHeap(), 0, This);
770 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
772 IDsDriverImpl *This = (IDsDriverImpl *)iface;
773 TRACE("(%p,%p)\n",iface,pDesc);
774 *pDesc = WOutDev[This->wDevID].ds_desc;
775 pDesc->dwFlags = DSDDESC_DONTNEEDSECONDARYLOCK | DSDDESC_DONTNEEDWRITELEAD;
776 pDesc->dnDevNode = WOutDev[This->wDevID].waveDesc.dnDevNode;
778 pDesc->wReserved = 0;
779 pDesc->ulDeviceNum = This->wDevID;
780 pDesc->dwHeapType = DSDHEAP_NOHEAP;
781 pDesc->pvDirectDrawHeap = NULL;
782 pDesc->dwMemStartAddress = 0xDEAD0000;
783 pDesc->dwMemEndAddress = 0xDEAF0000;
784 pDesc->dwMemAllocExtra = 0;
785 pDesc->pvReserved1 = NULL;
786 pDesc->pvReserved2 = NULL;
790 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
793 IDsDriverImpl *This = (IDsDriverImpl *)iface;
795 snd_pcm_t *pcm = NULL;
796 snd_pcm_hw_params_t *hw_params;
798 /* While this is not really needed, it is a good idea to do this,
799 * to see if sound can be initialized */
801 hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
805 hr = DSERR_OUTOFMEMORY;
809 err = snd_pcm_open(&pcm, WOutDev[This->wDevID].pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
810 if (err < 0) goto err;
811 err = snd_pcm_hw_params_any(pcm, hw_params);
812 if (err < 0) goto err;
813 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
815 err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
816 if (err < 0) goto err;
824 FIXME("Failed to open device: %s\n", snd_strerror(err));
828 HeapFree(GetProcessHeap(), 0, hw_params);
830 WARN("--> %08x\n", hr);
834 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
836 IDsDriverImpl *This = (IDsDriverImpl *)iface;
837 TRACE("(%p) stub, harmless\n",This);
841 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
843 IDsDriverImpl *This = (IDsDriverImpl *)iface;
844 TRACE("(%p,%p)\n",iface,pCaps);
845 *pCaps = WOutDev[This->wDevID].ds_caps;
849 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
851 DWORD dwFlags, DWORD dwCardAddress,
852 LPDWORD pdwcbBufferSize,
856 IDsDriverImpl *This = (IDsDriverImpl *)iface;
857 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
860 TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
861 /* we only support primary buffers... for now */
862 if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
863 return DSERR_UNSUPPORTED;
865 return DSERR_ALLOCATED;
867 *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsDriverBufferImpl));
868 if (*ippdsdb == NULL)
869 return DSERR_OUTOFMEMORY;
871 (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
872 (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
873 if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params)
875 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
876 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
877 return DSERR_OUTOFMEMORY;
879 (*ippdsdb)->lpVtbl = &dsdbvt;
881 (*ippdsdb)->drv = This;
882 InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
883 (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSOUTPUT.pcm_crst");
885 /* SetFormat has to re-initialize pcm here anyway */
886 err = SetFormat(*ippdsdb, pwfx);
889 WARN("Error occurred: %08x\n", err);
893 if (dwFlags & DSBCAPS_PRIMARYBUFFER)
894 This->primary = *ippdsdb;
896 *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
897 *ppbBuffer = (*ippdsdb)->mmap_buffer;
899 /* buffer is ready to go */
900 TRACE("buffer created at %p\n", *ippdsdb);
904 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
905 HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
906 HeapFree(GetProcessHeap(), 0, *ippdsdb);
911 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
912 PIDSDRIVERBUFFER pBuffer,
915 IDsDriverImpl *This = (IDsDriverImpl *)iface;
916 FIXME("(%p,%p): stub\n",This,pBuffer);
917 return DSERR_INVALIDCALL;
920 static const IDsDriverVtbl dsdvt =
922 IDsDriverImpl_QueryInterface,
923 IDsDriverImpl_AddRef,
924 IDsDriverImpl_Release,
925 IDsDriverImpl_GetDriverDesc,
928 IDsDriverImpl_GetCaps,
929 IDsDriverImpl_CreateSoundBuffer,
930 IDsDriverImpl_DuplicateSoundBuffer
933 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
935 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
937 TRACE("driver created\n");
939 *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
941 return MMSYSERR_NOMEM;
942 (*idrv)->lpVtbl = &dsdvt;
945 (*idrv)->wDevID = wDevID;
946 (*idrv)->primary = NULL;
947 return MMSYSERR_NOERROR;
950 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
952 *desc = WOutDev[wDevID].ds_desc;
953 return MMSYSERR_NOERROR;
956 #endif /* HAVE_ALSA */