Avoid holding the device lock when it isn't needed
[dsound-openal.git] / buffer.c
blob5060d3468fb0205171f787a0389e51d99e1c32c4
1 /* DirectSound COM interface
3 * Copyright 2009 Maarten Lankhorst
5 * Some code taken from the original dsound-openal implementation
6 * Copyright 2007-2009 Chris Robinson
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdarg.h>
25 #ifdef __WINESRC__
27 #define COBJMACROS
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "winreg.h"
35 #include "vfwmsgs.h"
36 #include "mmsystem.h"
37 #include "winternl.h"
38 #include "mmddk.h"
39 #include "wine/debug.h"
40 #include "dsound.h"
42 #include "dsound_private.h"
44 #include "ks.h"
45 #include "ksmedia.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
49 #else
51 #define WINVER 0x0600
52 #define INITGUID
53 #include <windows.h>
54 #include <dsound.h>
56 #include "dsound_private.h"
58 DEFINE_GUID(CLSID_DirectSoundPrivate,0x11ab3ec0,0x25ec,0x11d1,0xa4,0xd8,0x00,0xc0,0x4f,0xc2,0x8a,0xca);
60 DEFINE_GUID(DSPROPSETID_DirectSoundDevice,0x84624f82,0x25ec,0x11d1,0xa4,0xd8,0x00,0xc0,0x4f,0xc2,0x8a,0xca);
62 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
63 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
65 #ifndef E_PROP_ID_UNSUPPORTED
66 #define E_PROP_ID_UNSUPPORTED ((HRESULT)0x80070490)
67 #endif
69 #endif
71 #ifndef WAVE_FORMAT_IEEE_FLOAT
72 #define WAVE_FORMAT_IEEE_FLOAT 3
73 #endif
75 /* TODO: when bufferlost is set, return from all calls except initialize with
76 * DSERR_BUFFERLOST
78 static const IDirectSoundBuffer8Vtbl DS8Buffer_Vtbl;
79 static const IDirectSoundBufferVtbl DSBuffer_Vtbl;
80 static const IDirectSound3DBufferVtbl DS8Buffer3d_Vtbl;
81 static const IDirectSoundNotifyVtbl DS8BufferNot_Vtbl;
82 static const IKsPropertySetVtbl DS8BufferProp_Vtbl;
85 static inline DS8Buffer *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
87 return CONTAINING_RECORD(iface, DS8Buffer, IDirectSoundBuffer8_iface);
90 static inline DS8Buffer *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
92 return CONTAINING_RECORD(iface, DS8Buffer, IDirectSoundBuffer_iface);
95 static inline DS8Buffer *impl_from_IDirectSound3DBuffer(IDirectSound3DBuffer *iface)
97 return CONTAINING_RECORD(iface, DS8Buffer, IDirectSound3DBuffer_iface);
100 static inline DS8Buffer *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
102 return CONTAINING_RECORD(iface, DS8Buffer, IDirectSoundNotify_iface);
105 static inline DS8Buffer *impl_from_IKsPropertySet(IKsPropertySet *iface)
107 return CONTAINING_RECORD(iface, DS8Buffer, IKsPropertySet_iface);
111 /* Should be called with critsect held and context set.. */
112 static void DS8Buffer_addnotify(DS8Buffer *buf)
114 DS8Buffer **list;
115 DWORD i;
117 list = buf->primary->notifies;
118 for(i = 0; i < buf->primary->nnotifies; ++i)
120 if(buf == list[i])
122 ERR("Buffer %p already in notification list\n", buf);
123 return;
126 if(buf->primary->nnotifies == buf->primary->sizenotifies)
128 list = HeapReAlloc(GetProcessHeap(), 0, list, (buf->primary->nnotifies + 1) * sizeof(*list));
129 if(!list)
130 return;
131 buf->primary->sizenotifies++;
133 list[buf->primary->nnotifies++] = buf;
134 buf->primary->notifies = list;
138 static const char *get_fmtstr_PCM(const DS8Primary *prim, const WAVEFORMATEX *format, WAVEFORMATEXTENSIBLE *out)
140 out->Format = *format;
141 out->Format.cbSize = 0;
143 if(out->Format.nChannels != 1 && out->Format.nChannels != 2 &&
144 !prim->SupportedExt[EXT_MCFORMATS])
146 WARN("Multi-channel not available\n");
147 return NULL;
150 if(format->wBitsPerSample == 8)
152 switch(format->nChannels)
154 case 1: return "AL_FORMAT_MONO8";
155 case 2: return "AL_FORMAT_STEREO8";
156 case 4: return "AL_FORMAT_QUAD8";
157 case 6: return "AL_FORMAT_51CHN8";
158 case 7: return "AL_FORMAT_61CHN8";
159 case 8: return "AL_FORMAT_71CHN8";
162 else if(format->wBitsPerSample == 16)
164 switch(format->nChannels)
166 case 1: return "AL_FORMAT_MONO16";
167 case 2: return "AL_FORMAT_STEREO16";
168 case 4: return "AL_FORMAT_QUAD16";
169 case 6: return "AL_FORMAT_51CHN16";
170 case 7: return "AL_FORMAT_61CHN16";
171 case 8: return "AL_FORMAT_71CHN16";
175 FIXME("Could not get OpenAL format (%d-bit, %d channels)\n",
176 format->wBitsPerSample, format->nChannels);
177 return NULL;
179 static ALenum get_fmt_PCM(const WAVEFORMATEX *format, WAVEFORMATEXTENSIBLE *out, ALenum *in_chans, ALenum *in_type)
181 out->Format = *format;
182 out->Format.cbSize = 0;
184 if(format->wBitsPerSample == 8)
186 *in_type = AL_UNSIGNED_BYTE;
187 switch(format->nChannels)
189 case 1: *in_chans = AL_MONO;
190 return AL_MONO8;
191 case 2: *in_chans = AL_STEREO;
192 return AL_STEREO8;
193 case 4: *in_chans = AL_QUAD;
194 return AL_QUAD8;
195 case 6: *in_chans = AL_5POINT1;
196 return AL_5POINT1_8;
197 case 7: *in_chans = AL_6POINT1;
198 return AL_6POINT1_8;
199 case 8: *in_chans = AL_7POINT1;
200 return AL_7POINT1_8;
203 else if(format->wBitsPerSample == 16)
205 *in_type = AL_SHORT;
206 switch(format->nChannels)
208 case 1: *in_chans = AL_MONO;
209 return AL_MONO16;
210 case 2: *in_chans = AL_STEREO;
211 return AL_STEREO16;
212 case 4: *in_chans = AL_QUAD;
213 return AL_QUAD16;
214 case 6: *in_chans = AL_5POINT1;
215 return AL_5POINT1_16;
216 case 7: *in_chans = AL_6POINT1;
217 return AL_6POINT1_16;
218 case 8: *in_chans = AL_7POINT1;
219 return AL_7POINT1_16;
222 #if 0 /* Will cause incorrect byte offsets */
223 else if(format->wBitsPerSample == 24)
225 *in_type = AL_BYTE3;
226 switch(format->nChannels)
228 case 1: *in_chans = AL_MONO;
229 return AL_MONO32F;
230 case 2: *in_chans = AL_STEREO;
231 return AL_STEREO32F;
232 case 4: *in_chans = AL_QUAD;
233 return AL_QUAD32F;
234 case 6: *in_chans = AL_5POINT1;
235 return AL_5POINT1_32F;
236 case 7: *in_chans = AL_6POINT1;
237 return AL_6POINT1_32F;
238 case 8: *in_chans = AL_7POINT1;
239 return AL_7POINT1_32F;
242 #endif
243 else if(format->wBitsPerSample == 32)
245 *in_type = AL_INT;
246 switch(format->nChannels)
248 case 1: *in_chans = AL_MONO;
249 return AL_MONO32F;
250 case 2: *in_chans = AL_STEREO;
251 return AL_STEREO32F;
252 case 4: *in_chans = AL_QUAD;
253 return AL_QUAD32F;
254 case 6: *in_chans = AL_5POINT1;
255 return AL_5POINT1_32F;
256 case 7: *in_chans = AL_6POINT1;
257 return AL_6POINT1_32F;
258 case 8: *in_chans = AL_7POINT1;
259 return AL_7POINT1_32F;
263 FIXME("Could not get OpenAL format (%d-bit, %d channels)\n",
264 format->wBitsPerSample, format->nChannels);
265 return AL_NONE;
268 static const char *get_fmtstr_FLOAT(const DS8Primary *prim, const WAVEFORMATEX *format, WAVEFORMATEXTENSIBLE *out)
270 out->Format = *format;
271 out->Format.cbSize = 0;
273 if(out->Format.nChannels != 1 && out->Format.nChannels != 2 &&
274 !prim->SupportedExt[EXT_MCFORMATS])
276 WARN("Multi-channel not available\n");
277 return NULL;
280 if(format->wBitsPerSample == 32 && prim->SupportedExt[EXT_FLOAT32])
282 switch(format->nChannels)
284 case 1: return "AL_FORMAT_MONO_FLOAT32";
285 case 2: return "AL_FORMAT_STEREO_FLOAT32";
286 case 4: return "AL_FORMAT_QUAD32";
287 case 6: return "AL_FORMAT_51CHN32";
288 case 7: return "AL_FORMAT_61CHN32";
289 case 8: return "AL_FORMAT_71CHN32";
293 FIXME("Could not get OpenAL format (%d-bit, %d channels)\n",
294 format->wBitsPerSample, format->nChannels);
295 return NULL;
297 static ALenum get_fmt_FLOAT(const WAVEFORMATEX *format, WAVEFORMATEXTENSIBLE *out, ALenum *in_chans, ALenum *in_type)
299 out->Format = *format;
300 out->Format.cbSize = 0;
302 if(format->wBitsPerSample == 32)
304 *in_type = AL_FLOAT;
305 switch(format->nChannels)
307 case 1: *in_chans = AL_MONO;
308 return AL_MONO32F;
309 case 2: *in_chans = AL_STEREO;
310 return AL_STEREO32F;
311 case 4: *in_chans = AL_QUAD;
312 return AL_QUAD32F;
313 case 6: *in_chans = AL_5POINT1;
314 return AL_5POINT1_32F;
315 case 7: *in_chans = AL_6POINT1;
316 return AL_6POINT1_32F;
317 case 8: *in_chans = AL_7POINT1;
318 return AL_7POINT1_32F;
321 #if 0 /* Will cause incorrect byte offsets */
322 else if(format->wBitsPerSample == 64)
324 *in_type = AL_DOUBLE;
325 switch(format->nChannels)
327 case 1: *in_chans = AL_MONO;
328 return AL_MONO32F;
329 case 2: *in_chans = AL_STEREO;
330 return AL_STEREO32F;
331 case 4: *in_chans = AL_QUAD;
332 return AL_QUAD32F;
333 case 6: *in_chans = AL_5POINT1;
334 return AL_5POINT1_32F;
335 case 7: *in_chans = AL_6POINT1;
336 return AL_6POINT1_32F;
337 case 8: *in_chans = AL_7POINT1;
338 return AL_7POINT1_32F;
341 #endif
343 FIXME("Could not get OpenAL format (%d-bit, %d channels)\n",
344 format->wBitsPerSample, format->nChannels);
345 return AL_NONE;
348 /* Speaker configs */
349 #define MONO SPEAKER_FRONT_CENTER
350 #define STEREO (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT)
351 #define REAR (SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
352 #define QUAD (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
353 #define X5DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)
354 #define X6DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_CENTER|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)
355 #define X7DOT1 (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT|SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY|SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT|SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)
357 static const char *get_fmtstr_EXT(const DS8Primary *prim, const WAVEFORMATEX *format, WAVEFORMATEXTENSIBLE *out)
359 *out = *CONTAINING_RECORD(format, const WAVEFORMATEXTENSIBLE, Format);
360 out->Format.cbSize = sizeof(*out) - sizeof(out->Format);
362 if(!out->Samples.wValidBitsPerSample)
363 out->Samples.wValidBitsPerSample = out->Format.wBitsPerSample;
364 else if(out->Samples.wValidBitsPerSample != out->Format.wBitsPerSample)
366 FIXME("Padded samples not supported (%u of %u)\n", out->Samples.wValidBitsPerSample, out->Format.wBitsPerSample);
367 return NULL;
370 if(out->dwChannelMask != MONO && out->dwChannelMask != STEREO &&
371 !prim->SupportedExt[EXT_MCFORMATS])
373 WARN("Multi-channel not available\n");
374 return NULL;
377 if(IsEqualGUID(&out->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))
379 if(out->Samples.wValidBitsPerSample == 8)
381 switch(out->dwChannelMask)
383 case MONO: return "AL_FORMAT_MONO8";
384 case STEREO: return "AL_FORMAT_STEREO8";
385 case REAR: return "AL_FORMAT_REAR8";
386 case QUAD: return "AL_FORMAT_QUAD8";
387 case X5DOT1: return "AL_FORMAT_51CHN8";
388 case X6DOT1: return "AL_FORMAT_61CHN8";
389 case X7DOT1: return "AL_FORMAT_71CHN8";
392 else if(out->Samples.wValidBitsPerSample == 16)
394 switch(out->dwChannelMask)
396 case MONO: return "AL_FORMAT_MONO16";
397 case STEREO: return "AL_FORMAT_STEREO16";
398 case REAR: return "AL_FORMAT_REAR16";
399 case QUAD: return "AL_FORMAT_QUAD16";
400 case X5DOT1: return "AL_FORMAT_51CHN16";
401 case X6DOT1: return "AL_FORMAT_61CHN16";
402 case X7DOT1: return "AL_FORMAT_71CHN16";
406 FIXME("Could not get OpenAL PCM format (%d-bit, channelmask %#"LONGFMT"x)\n",
407 out->Samples.wValidBitsPerSample, out->dwChannelMask);
408 return NULL;
410 else if(IsEqualGUID(&out->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) &&
411 prim->SupportedExt[EXT_FLOAT32])
413 if(out->Samples.wValidBitsPerSample == 32)
415 switch(out->dwChannelMask)
417 case MONO: return "AL_FORMAT_MONO_FLOAT32";
418 case STEREO: return "AL_FORMAT_STEREO_FLOAT32";
419 case REAR: return "AL_FORMAT_REAR32";
420 case QUAD: return "AL_FORMAT_QUAD32";
421 case X5DOT1: return "AL_FORMAT_51CHN32";
422 case X6DOT1: return "AL_FORMAT_61CHN32";
423 case X7DOT1: return "AL_FORMAT_71CHN32";
426 else
428 WARN("Invalid float bits: %u\n", out->Samples.wValidBitsPerSample);
429 return NULL;
432 FIXME("Could not get OpenAL float format (%d-bit, channelmask %#"LONGFMT"x)\n",
433 out->Samples.wValidBitsPerSample, out->dwChannelMask);
434 return NULL;
436 else if(!IsEqualGUID(&out->SubFormat, &GUID_NULL))
437 ERR("Unhandled extensible format: %s\n", debugstr_guid(&out->SubFormat));
438 return NULL;
440 static ALenum get_fmt_EXT(const WAVEFORMATEX *format, WAVEFORMATEXTENSIBLE *out, ALenum *in_chans, ALenum *in_type)
442 *out = *CONTAINING_RECORD(format, const WAVEFORMATEXTENSIBLE, Format);
443 out->Format.cbSize = sizeof(*out) - sizeof(out->Format);
445 if(!out->Samples.wValidBitsPerSample)
446 out->Samples.wValidBitsPerSample = out->Format.wBitsPerSample;
447 else if(out->Samples.wValidBitsPerSample != out->Format.wBitsPerSample)
449 FIXME("Padded samples not supported (%u of %u)\n", out->Samples.wValidBitsPerSample, out->Format.wBitsPerSample);
450 return AL_NONE;
453 if(IsEqualGUID(&out->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))
455 if(out->Samples.wValidBitsPerSample == 8)
457 *in_type = AL_UNSIGNED_BYTE;
458 switch(out->dwChannelMask)
460 case MONO: *in_chans = AL_MONO;
461 return AL_MONO8;
462 case STEREO: *in_chans = AL_STEREO;
463 return AL_STEREO8;
464 case REAR: *in_chans = AL_REAR;
465 return AL_REAR8;
466 case QUAD: *in_chans = AL_QUAD;
467 return AL_QUAD8;
468 case X5DOT1: *in_chans = AL_5POINT1;
469 return AL_5POINT1_8;
470 case X6DOT1: *in_chans = AL_6POINT1;
471 return AL_6POINT1_8;
472 case X7DOT1: *in_chans = AL_7POINT1;
473 return AL_7POINT1_8;
476 else if(out->Samples.wValidBitsPerSample == 16)
478 *in_type = AL_SHORT;
479 switch(out->dwChannelMask)
481 case MONO: *in_chans = AL_MONO;
482 return AL_MONO16;
483 case STEREO: *in_chans = AL_STEREO;
484 return AL_STEREO16;
485 case REAR: *in_chans = AL_REAR;
486 return AL_REAR16;
487 case QUAD: *in_chans = AL_QUAD;
488 return AL_QUAD16;
489 case X5DOT1: *in_chans = AL_5POINT1;
490 return AL_5POINT1_16;
491 case X6DOT1: *in_chans = AL_6POINT1;
492 return AL_6POINT1_16;
493 case X7DOT1: *in_chans = AL_7POINT1;
494 return AL_7POINT1_16;
497 #if 0
498 else if(out->Samples.wValidBitsPerSample == 24)
500 *in_type = AL_BYTE3;
501 switch(out->dwChannelMask)
503 case MONO: *in_chans = AL_MONO;
504 return AL_MONO32F;
505 case STEREO: *in_chans = AL_STEREO;
506 return AL_STEREO32F;
507 case REAR: *in_chans = AL_REAR;
508 return AL_REAR32F;
509 case QUAD: *in_chans = AL_QUAD;
510 return AL_QUAD32F;
511 case X5DOT1: *in_chans = AL_5POINT1;
512 return AL_5POINT1_32F;
513 case X6DOT1: *in_chans = AL_6POINT1;
514 return AL_6POINT1_32F;
515 case X7DOT1: *in_chans = AL_7POINT1;
516 return AL_7POINT1_32F;
519 #endif
520 else if(out->Samples.wValidBitsPerSample == 32)
522 *in_type = AL_INT;
523 switch(out->dwChannelMask)
525 case MONO: *in_chans = AL_MONO;
526 return AL_MONO32F;
527 case STEREO: *in_chans = AL_STEREO;
528 return AL_STEREO32F;
529 case REAR: *in_chans = AL_REAR;
530 return AL_REAR32F;
531 case QUAD: *in_chans = AL_QUAD;
532 return AL_QUAD32F;
533 case X5DOT1: *in_chans = AL_5POINT1;
534 return AL_5POINT1_32F;
535 case X6DOT1: *in_chans = AL_6POINT1;
536 return AL_6POINT1_32F;
537 case X7DOT1: *in_chans = AL_7POINT1;
538 return AL_7POINT1_32F;
542 FIXME("Could not get OpenAL PCM format (%d-bit, channelmask %#"LONGFMT"x)\n",
543 out->Samples.wValidBitsPerSample, out->dwChannelMask);
544 return AL_NONE;
546 else if(IsEqualGUID(&out->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))
548 if(out->Samples.wValidBitsPerSample == 32)
550 *in_type = AL_FLOAT;
551 switch(out->dwChannelMask)
553 case MONO: *in_chans = AL_MONO;
554 return AL_MONO32F;
555 case STEREO: *in_chans = AL_STEREO;
556 return AL_STEREO32F;
557 case REAR: *in_chans = AL_REAR;
558 return AL_REAR32F;
559 case QUAD: *in_chans = AL_QUAD;
560 return AL_QUAD32F;
561 case X5DOT1: *in_chans = AL_5POINT1;
562 return AL_5POINT1_32F;
563 case X6DOT1: *in_chans = AL_6POINT1;
564 return AL_6POINT1_32F;
565 case X7DOT1: *in_chans = AL_7POINT1;
566 return AL_7POINT1_32F;
569 #if 0
570 else if(out->Samples.wValidBitsPerSample == 64)
572 *in_type = AL_DOUBLE;
573 switch(out->dwChannelMask)
575 case MONO: *in_chans = AL_MONO;
576 return AL_MONO32F;
577 case STEREO: *in_chans = AL_STEREO;
578 return AL_STEREO32F;
579 case REAR: *in_chans = AL_REAR;
580 return AL_REAR32F;
581 case QUAD: *in_chans = AL_QUAD;
582 return AL_QUAD32F;
583 case X5DOT1: *in_chans = AL_5POINT1;
584 return AL_5POINT1_32F;
585 case X6DOT1: *in_chans = AL_6POINT1;
586 return AL_6POINT1_32F;
587 case X7DOT1: *in_chans = AL_7POINT1;
588 return AL_7POINT1_32F;
591 #endif
592 else
594 WARN("Invalid float bits: %u\n", out->Samples.wValidBitsPerSample);
595 return AL_NONE;
598 FIXME("Could not get OpenAL float format (%d-bit, channelmask %#"LONGFMT"x)\n",
599 out->Samples.wValidBitsPerSample, out->dwChannelMask);
600 return AL_NONE;
602 else if(!IsEqualGUID(&out->SubFormat, &GUID_NULL))
603 ERR("Unhandled extensible format: %s\n", debugstr_guid(&out->SubFormat));
604 return AL_NONE;
607 static void DS8Data_Release(DS8Data *This);
608 static HRESULT DS8Data_Create(DS8Data **ppv, const DSBUFFERDESC *desc, DS8Primary *prim)
610 HRESULT hr = DSERR_INVALIDPARAM;
611 const WAVEFORMATEX *format;
612 DS8Data *pBuffer;
614 format = desc->lpwfxFormat;
615 TRACE("Requested buffer format:\n"
616 " FormatTag = 0x%04x\n"
617 " Channels = %d\n"
618 " SamplesPerSec = %"LONGFMT"u\n"
619 " AvgBytesPerSec = %"LONGFMT"u\n"
620 " BlockAlign = %d\n"
621 " BitsPerSample = %d\n",
622 format->wFormatTag, format->nChannels,
623 format->nSamplesPerSec, format->nAvgBytesPerSec,
624 format->nBlockAlign, format->wBitsPerSample);
626 if(format->nBlockAlign == 0)
628 WARN("Invalid BlockAlign specified\n");
629 return DSERR_INVALIDPARAM;
632 /* Generate a new buffer. Supporting the DSBCAPS_LOC* flags properly
633 * will need the EAX-RAM extension. Currently, we just tell the app it
634 * gets what it wanted. */
635 pBuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pBuffer));
636 if(!pBuffer)
637 return E_OUTOFMEMORY;
638 pBuffer->ref = 1;
640 pBuffer->dsbflags = desc->dwFlags;
641 if((pBuffer->dsbflags&(DSBCAPS_LOCSOFTWARE|DSBCAPS_LOCHARDWARE)) == (DSBCAPS_LOCSOFTWARE|DSBCAPS_LOCHARDWARE))
643 WARN("Hardware and software location requested\n");
644 goto fail;
646 if(!(pBuffer->dsbflags&(DSBCAPS_LOCSOFTWARE|DSBCAPS_LOCHARDWARE|DSBCAPS_LOCDEFER)))
647 pBuffer->dsbflags |= DSBCAPS_LOCHARDWARE;
649 pBuffer->buf_size = desc->dwBufferBytes + format->nBlockAlign - 1;
650 pBuffer->buf_size -= pBuffer->buf_size%format->nBlockAlign;
652 hr = DSERR_BUFFERTOOSMALL;
653 if(pBuffer->buf_size < DSBSIZE_MIN)
654 goto fail;
656 hr = DSERR_INVALIDPARAM;
657 if(pBuffer->buf_size > DSBSIZE_MAX)
658 goto fail;
660 pBuffer->numsegs = 1;
661 pBuffer->segsize = pBuffer->buf_size;
662 pBuffer->lastsegsize = pBuffer->buf_size;
664 if(!prim->SupportedExt[SOFT_BUFFER_SAMPLES])
666 const char *fmt_str = NULL;
668 if(!(pBuffer->dsbflags&DSBCAPS_STATIC) &&
669 !prim->SupportedExt[SOFT_BUFFER_SUB_DATA] &&
670 !prim->SupportedExt[EXT_STATIC_BUFFER])
672 ALCint refresh = FAKE_REFRESH_COUNT;
673 ALuint newSize;
675 alcGetIntegerv(prim->parent->device, ALC_REFRESH, 1, &refresh);
676 checkALCError(prim->parent->device);
678 newSize = format->nAvgBytesPerSec/refresh + format->nBlockAlign - 1;
679 newSize -= newSize%format->nBlockAlign;
681 /* Make sure enough buffers are available */
682 if(newSize > pBuffer->buf_size/(QBUFFERS+2))
683 ERR("Buffer segments too large to stream (%u for %u)!\n",
684 newSize, pBuffer->buf_size);
685 else
687 pBuffer->numsegs = pBuffer->buf_size/newSize;
688 pBuffer->segsize = newSize;
689 pBuffer->lastsegsize = pBuffer->buf_size - (newSize*(pBuffer->numsegs-1));
690 TRACE("New streaming buffer (%u chunks, %u : %u sizes)\n",
691 pBuffer->numsegs, pBuffer->segsize, pBuffer->lastsegsize);
695 if(format->wFormatTag == WAVE_FORMAT_PCM)
696 fmt_str = get_fmtstr_PCM(prim, format, &pBuffer->format);
697 else if(format->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
698 fmt_str = get_fmtstr_FLOAT(prim, format, &pBuffer->format);
699 else if(format->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
701 const WAVEFORMATEXTENSIBLE *wfe;
703 hr = DSERR_CONTROLUNAVAIL;
704 if(format->cbSize != sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX) &&
705 format->cbSize != sizeof(WAVEFORMATEXTENSIBLE))
706 goto fail;
708 wfe = CONTAINING_RECORD(format, const WAVEFORMATEXTENSIBLE, Format);
709 TRACE("Extensible values:\n"
710 " Samples = %d\n"
711 " ChannelMask = 0x%"LONGFMT"x\n"
712 " SubFormat = %s\n",
713 wfe->Samples.wReserved, wfe->dwChannelMask,
714 debugstr_guid(&wfe->SubFormat));
716 fmt_str = get_fmtstr_EXT(prim, format, &pBuffer->format);
718 else
719 ERR("Unhandled formattag 0x%04x\n", format->wFormatTag);
721 hr = DSERR_INVALIDCALL;
722 if(!fmt_str)
723 goto fail;
725 pBuffer->buf_format = alGetEnumValue(fmt_str);
726 if(alGetError() != AL_NO_ERROR || pBuffer->buf_format == 0 ||
727 pBuffer->buf_format == -1)
729 WARN("Could not get OpenAL format from %s\n", fmt_str);
730 goto fail;
733 else
735 if(format->wFormatTag == WAVE_FORMAT_PCM)
736 pBuffer->buf_format = get_fmt_PCM(format, &pBuffer->format, &pBuffer->in_chans, &pBuffer->in_type);
737 else if(format->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
738 pBuffer->buf_format = get_fmt_FLOAT(format, &pBuffer->format, &pBuffer->in_chans, &pBuffer->in_type);
739 else if(format->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
741 const WAVEFORMATEXTENSIBLE *wfe;
743 hr = DSERR_CONTROLUNAVAIL;
744 if(format->cbSize != sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX) &&
745 format->cbSize != sizeof(WAVEFORMATEXTENSIBLE))
746 goto fail;
748 wfe = CONTAINING_RECORD(format, const WAVEFORMATEXTENSIBLE, Format);
749 TRACE("Extensible values:\n"
750 " Samples = %d\n"
751 " ChannelMask = 0x%"LONGFMT"x\n"
752 " SubFormat = %s\n",
753 wfe->Samples.wReserved, wfe->dwChannelMask,
754 debugstr_guid(&wfe->SubFormat));
756 pBuffer->buf_format = get_fmt_EXT(format, &pBuffer->format, &pBuffer->in_chans, &pBuffer->in_type);
758 else
759 ERR("Unhandled formattag 0x%04x\n", format->wFormatTag);
761 hr = DSERR_INVALIDCALL;
762 if(prim->ExtAL->IsBufferFormatSupportedSOFT(pBuffer->buf_format) == AL_FALSE)
764 WARN("Unsupported OpenAL format: 0x%x\n", pBuffer->buf_format);
765 goto fail;
769 hr = E_OUTOFMEMORY;
770 pBuffer->buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pBuffer->buffers)*pBuffer->numsegs);
771 pBuffer->data = HeapAlloc(GetProcessHeap(), 0, pBuffer->buf_size);
772 if(!pBuffer->buffers || !pBuffer->data)
773 goto fail;
775 alGenBuffers(pBuffer->numsegs, pBuffer->buffers);
776 checkALError();
778 *ppv = pBuffer;
779 return S_OK;
781 fail:
782 DS8Data_Release(pBuffer);
783 return hr;
786 static void DS8Data_AddRef(DS8Data *data)
788 InterlockedIncrement(&data->ref);
791 /* This function is always called with the device lock held */
792 static void DS8Data_Release(DS8Data *This)
794 if(InterlockedDecrement(&This->ref)) return;
796 TRACE("Deleting %p\n", This);
797 if (This->buffers && This->buffers[0])
799 alDeleteBuffers(This->numsegs, This->buffers);
800 checkALError();
802 HeapFree(GetProcessHeap(), 0, This->buffers);
803 HeapFree(GetProcessHeap(), 0, This->data);
804 HeapFree(GetProcessHeap(), 0, This);
807 HRESULT DS8Buffer_Create(DS8Buffer **ppv, DS8Primary *prim, IDirectSoundBuffer *orig)
809 DS8Buffer *This;
810 HRESULT hr;
812 *ppv = NULL;
813 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
814 if(!This) return DSERR_OUTOFMEMORY;
816 This->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl*)&DS8Buffer_Vtbl;
817 This->IDirectSoundBuffer_iface.lpVtbl = (IDirectSoundBufferVtbl*)&DSBuffer_Vtbl;
818 This->IDirectSound3DBuffer_iface.lpVtbl = (IDirectSound3DBufferVtbl*)&DS8Buffer3d_Vtbl;
819 This->IDirectSoundNotify_iface.lpVtbl = (IDirectSoundNotifyVtbl*)&DS8BufferNot_Vtbl;
820 This->IKsPropertySet_iface.lpVtbl = (IKsPropertySetVtbl*)&DS8BufferProp_Vtbl;
822 This->primary = prim;
823 This->ctx = prim->ctx;
824 This->ExtAL = prim->ExtAL;
825 This->crst = prim->crst;
826 This->ref = This->all_ref = 1;
828 if(orig)
830 DS8Buffer *org = impl_from_IDirectSoundBuffer(orig);
831 hr = DSERR_BUFFERLOST;
832 if(org->bufferlost)
833 goto fail;
834 DS8Data_AddRef(org->buffer);
835 This->buffer = org->buffer;
838 /* Append to buffer list */
839 if(prim->nbuffers == prim->sizebuffers)
841 void *bufs;
843 hr = DSERR_OUTOFMEMORY;
844 bufs = HeapReAlloc(GetProcessHeap(), 0, prim->buffers, sizeof(*bufs)*(prim->nbuffers+1));
845 if(!bufs) goto fail;
847 prim->buffers = bufs;
848 prim->sizebuffers++;
850 prim->buffers[prim->nbuffers++] = This;
852 /* Disable until initialized.. */
853 This->ds3dmode = DS3DMODE_DISABLE;
855 *ppv = This;
856 return DS_OK;
858 fail:
859 DS8Buffer_Destroy(This);
860 return hr;
863 void DS8Buffer_Destroy(DS8Buffer *This)
865 DS8Primary *prim = This->primary;
866 DWORD idx;
868 TRACE("Destroying %p\n", This);
870 EnterCriticalSection(prim->crst);
871 /* Remove from list, if in list */
872 for(idx = 0;idx < prim->nnotifies;++idx)
874 if(This == prim->notifies[idx])
876 prim->notifies[idx] = prim->notifies[--prim->nnotifies];
877 break;
880 for(idx = 0;idx < prim->nbuffers;++idx)
882 if(prim->buffers[idx] == This)
884 prim->buffers[idx] = prim->buffers[--prim->nbuffers];
885 break;
889 setALContext(This->ctx);
890 if(This->source)
892 alSourceStop(This->source);
893 alSourcei(This->source, AL_BUFFER, 0);
894 checkALError();
896 prim->sources[prim->parent->share->nsources++] = This->source;
897 This->source = 0;
899 LeaveCriticalSection(prim->crst);
901 if(This->buffer)
902 DS8Data_Release(This->buffer);
904 popALContext();
906 HeapFree(GetProcessHeap(), 0, This->notify);
907 HeapFree(GetProcessHeap(), 0, This);
911 static HRESULT WINAPI DS8Buffer_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid, void **ppv)
913 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
915 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
917 *ppv = NULL;
918 if(IsEqualIID(riid, &IID_IUnknown))
919 *ppv = &This->IDirectSoundBuffer8_iface;
920 else if(IsEqualIID(riid, &IID_IDirectSoundBuffer))
921 *ppv = &This->IDirectSoundBuffer_iface;
922 else if(IsEqualIID(riid, &IID_IDirectSoundBuffer8))
924 if(This->primary->parent->is_8)
925 *ppv = &This->IDirectSoundBuffer8_iface;
927 else if(IsEqualIID(riid, &IID_IDirectSound3DBuffer))
929 if((This->buffer->dsbflags&DSBCAPS_CTRL3D))
930 *ppv = &This->IDirectSound3DBuffer_iface;
932 else if(IsEqualIID(riid, &IID_IDirectSoundNotify))
934 if((This->buffer->dsbflags&DSBCAPS_CTRLPOSITIONNOTIFY))
935 *ppv = &This->IDirectSoundNotify_iface;
937 else if(IsEqualIID(riid, &IID_IKsPropertySet))
938 *ppv = &This->IKsPropertySet_iface;
939 else
940 FIXME("Unhandled GUID: %s\n", debugstr_guid(riid));
942 if(*ppv)
944 IUnknown_AddRef((IUnknown*)*ppv);
945 return S_OK;
948 return E_NOINTERFACE;
951 static ULONG WINAPI DS8Buffer_AddRef(IDirectSoundBuffer8 *iface)
953 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
954 LONG ret;
956 InterlockedIncrement(&This->all_ref);
957 ret = InterlockedIncrement(&This->ref);
958 TRACE("new refcount %"LONGFMT"d\n", ret);
960 return ret;
963 static ULONG WINAPI DS8Buffer_Release(IDirectSoundBuffer8 *iface)
965 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
966 LONG ret;
968 ret = InterlockedDecrement(&This->ref);
969 TRACE("new refcount %"LONGFMT"d\n", ret);
970 if(InterlockedDecrement(&This->all_ref) == 0)
971 DS8Buffer_Destroy(This);
973 return ret;
976 static HRESULT WINAPI DS8Buffer_GetCaps(IDirectSoundBuffer8 *iface, DSBCAPS *caps)
978 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
980 TRACE("(%p)->(%p)\n", iface, caps);
982 if(!caps || caps->dwSize < sizeof(*caps))
984 WARN("Invalid DSBCAPS (%p, %"LONGFMT"u)\n", caps, (caps ? caps->dwSize : 0));
985 return DSERR_INVALIDPARAM;
988 caps->dwFlags = This->buffer->dsbflags;
989 caps->dwBufferBytes = This->buffer->buf_size;
990 caps->dwUnlockTransferRate = 4096;
991 caps->dwPlayCpuOverhead = 0;
992 return S_OK;
995 static HRESULT WINAPI DS8Buffer_GetCurrentPosition(IDirectSoundBuffer8 *iface, DWORD *playpos, DWORD *curpos)
997 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
998 WAVEFORMATEX *format = &This->buffer->format.Format;
999 UINT writecursor, pos;
1001 TRACE("(%p)->(%p, %p)\n", iface, playpos, curpos);
1003 if(This->buffer->numsegs > 1)
1005 ALint queued = QBUFFERS;
1007 EnterCriticalSection(This->crst);
1009 setALContext(This->ctx);
1010 alGetSourcei(This->source, AL_BUFFERS_QUEUED, &queued);
1011 checkALError();
1012 popALContext();
1014 pos = (This->curidx+This->buffer->numsegs-queued)%This->buffer->numsegs;
1015 pos *= This->buffer->segsize;
1016 writecursor = This->curidx * This->buffer->segsize;
1018 LeaveCriticalSection(This->crst);
1020 else if(This->primary->SupportedExt[SOFT_BUFFER_SUB_DATA] ||
1021 This->primary->SupportedExt[SOFT_BUFFER_SAMPLES])
1023 ALint rwpos[2] = { 0, 0 };
1025 setALContext(This->ctx);
1026 alGetSourceiv(This->source, AL_BYTE_RW_OFFSETS_SOFT, rwpos);
1027 checkALError();
1028 popALContext();
1030 pos = rwpos[0];
1031 writecursor = rwpos[1];
1033 else
1035 ALint status = 0;
1036 ALint ofs = 0;
1038 setALContext(This->ctx);
1039 alGetSourcei(This->source, AL_BYTE_OFFSET, &ofs);
1040 alGetSourcei(This->source, AL_SOURCE_STATE, &status);
1041 checkALError();
1042 popALContext();
1044 pos = ofs;
1045 if(status == AL_PLAYING)
1047 writecursor = format->nSamplesPerSec / 100;
1048 writecursor *= format->nBlockAlign;
1050 else
1051 writecursor = 0;
1052 writecursor = (writecursor + pos) % This->buffer->buf_size;
1054 TRACE("%p Play pos = %u, write pos = %u\n", This, pos, writecursor);
1055 if(pos >= This->buffer->buf_size)
1057 ERR("playpos >= buf_size\n");
1058 pos %= This->buffer->buf_size;
1060 if(writecursor >= This->buffer->buf_size)
1062 ERR("writepos >= buf_size\n");
1063 writecursor %= This->buffer->buf_size;
1066 if(playpos) *playpos = pos;
1067 if(curpos) *curpos = writecursor;
1069 return S_OK;
1072 static HRESULT WINAPI DS8Buffer_GetFormat(IDirectSoundBuffer8 *iface, WAVEFORMATEX *wfx, DWORD allocated, DWORD *written)
1074 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1075 HRESULT hr = S_OK;
1076 UINT size;
1078 TRACE("(%p)->(%p, %"LONGFMT"u, %p)\n", iface, wfx, allocated, written);
1080 if(!wfx && !written)
1082 WARN("Cannot report format or format size\n");
1083 return DSERR_INVALIDPARAM;
1086 size = sizeof(This->buffer->format.Format) + This->buffer->format.Format.cbSize;
1087 if(wfx)
1089 if(allocated < size)
1090 hr = DSERR_INVALIDPARAM;
1091 else
1092 memcpy(wfx, &This->buffer->format.Format, size);
1094 if(written)
1095 *written = size;
1097 return hr;
1100 static HRESULT WINAPI DS8Buffer_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
1102 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1103 HRESULT hr;
1105 TRACE("(%p)->(%p)\n", iface, vol);
1107 if(!vol)
1109 WARN("Invalid pointer\n");
1110 return DSERR_INVALIDPARAM;
1113 hr = DSERR_CONTROLUNAVAIL;
1114 if(!(This->buffer->dsbflags&DSBCAPS_CTRLVOLUME))
1115 WARN("Volume control not set\n");
1116 else
1118 ALfloat gain = 1.0f;
1120 setALContext(This->ctx);
1121 alGetSourcef(This->source, AL_GAIN, &gain);
1122 checkALError();
1123 popALContext();
1125 *vol = clampI(gain_to_mB(gain), DSBVOLUME_MIN, DSBVOLUME_MAX);
1126 hr = DS_OK;
1129 return hr;
1132 static HRESULT WINAPI DS8Buffer_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
1134 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1135 HRESULT hr;
1137 TRACE("(%p)->(%p)\n", iface, pan);
1139 if(!pan)
1141 WARN("Invalid pointer\n");
1142 return DSERR_INVALIDPARAM;
1145 hr = DSERR_CONTROLUNAVAIL;
1146 if(!(This->buffer->dsbflags&DSBCAPS_CTRLPAN))
1147 WARN("Panning control not set\n");
1148 else
1150 ALfloat pos[3];
1152 setALContext(This->ctx);
1153 alGetSourcefv(This->source, AL_POSITION, pos);
1154 checkALError();
1155 popALContext();
1157 *pan = clampI(((pos[0]+1.0) * (DSBPAN_RIGHT-DSBPAN_LEFT) / 2.0 + 0.5) + DSBPAN_LEFT, DSBPAN_LEFT, DSBPAN_RIGHT);
1158 hr = DS_OK;
1161 return hr;
1164 static HRESULT WINAPI DS8Buffer_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
1166 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1167 HRESULT hr;
1169 TRACE("(%p)->(%p)\n", iface, freq);
1171 if(!freq)
1173 WARN("Invalid pointer\n");
1174 return DSERR_INVALIDPARAM;
1177 hr = DSERR_CONTROLUNAVAIL;
1178 if(!(This->buffer->dsbflags&DSBCAPS_CTRLFREQUENCY))
1179 WARN("Frequency control not set\n");
1180 else
1182 ALfloat pitch = 1.0f;
1184 setALContext(This->ctx);
1185 alGetSourcefv(This->source, AL_PITCH, &pitch);
1186 checkALError();
1187 popALContext();
1189 *freq = (DWORD)(This->buffer->format.Format.nSamplesPerSec * pitch);
1190 hr = DS_OK;
1193 return hr;
1196 static HRESULT WINAPI DS8Buffer_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
1198 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1199 ALint state, looping;
1201 TRACE("(%p)->(%p)\n", iface, status);
1203 if(!status)
1205 WARN("Invalid pointer\n");
1206 return DSERR_INVALIDPARAM;
1209 if(This->buffer->numsegs == 1)
1211 setALContext(This->ctx);
1212 alGetSourcei(This->source, AL_SOURCE_STATE, &state);
1213 alGetSourcei(This->source, AL_LOOPING, &looping);
1214 checkALError();
1215 popALContext();
1217 else
1219 EnterCriticalSection(This->crst);
1221 setALContext(This->ctx);
1222 alGetSourcei(This->source, AL_SOURCE_STATE, &state);
1223 checkALError();
1224 popALContext();
1226 if(state != AL_PLAYING)
1227 state = This->isplaying ? AL_PLAYING : AL_PAUSED;
1228 looping = This->islooping;
1230 LeaveCriticalSection(This->crst);
1233 *status = 0;
1234 if((This->buffer->dsbflags&DSBCAPS_LOCDEFER))
1236 if((This->buffer->dsbflags&DSBCAPS_LOCSOFTWARE))
1237 *status |= DSBSTATUS_LOCSOFTWARE;
1238 else if((This->buffer->dsbflags&DSBCAPS_LOCHARDWARE))
1239 *status |= DSBSTATUS_LOCHARDWARE;
1241 if(state == AL_PLAYING)
1242 *status |= DSBSTATUS_PLAYING | (looping ? DSBSTATUS_LOOPING : 0);
1244 return S_OK;
1247 static HRESULT WINAPI DS8Buffer_Initialize(IDirectSoundBuffer8 *iface, IDirectSound *ds, const DSBUFFERDESC *desc)
1249 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1250 DS3DBUFFER *ds3dbuffer;
1251 HRESULT hr;
1253 TRACE("(%p)->(%p, %p)\n", iface, ds, desc);
1255 EnterCriticalSection(This->crst);
1256 setALContext(This->ctx);
1258 hr = DSERR_ALREADYINITIALIZED;
1259 if(This->source)
1260 goto out;
1262 if(!This->buffer)
1264 hr = DSERR_INVALIDPARAM;
1265 if(!desc)
1267 WARN("Missing DSound buffer description\n");
1268 goto out;
1270 if(!desc->lpwfxFormat)
1272 WARN("Missing buffer format (%p)\n", This);
1273 goto out;
1275 if((desc->dwFlags&DSBCAPS_CTRL3D) && desc->lpwfxFormat->nChannels != 1)
1277 if(This->primary->parent->is_8)
1279 /* DirectSoundBuffer8 objects aren't allowed non-mono 3D
1280 * buffers */
1281 WARN("Can't create multi-channel 3D buffers\n");
1282 goto out;
1284 ERR("Multi-channel 3D sounds are not spatialized\n");
1287 hr = DS8Data_Create(&This->buffer, desc, This->primary);
1288 if(FAILED(hr))
1289 goto out;
1290 else
1292 DS8Data *buf = This->buffer;
1294 if(buf->format.Format.wBitsPerSample == 8)
1295 memset(buf->data, 0x80, buf->buf_size);
1296 else
1297 memset(buf->data, 0x00, buf->buf_size);
1299 if(This->primary->SupportedExt[EXT_STATIC_BUFFER])
1300 This->ExtAL->BufferDataStatic(buf->buffers[0], buf->buf_format,
1301 buf->data, buf->buf_size,
1302 buf->format.Format.nSamplesPerSec);
1303 else if(This->primary->SupportedExt[SOFT_BUFFER_SAMPLES])
1304 This->ExtAL->BufferSamplesSOFT(buf->buffers[0],
1305 buf->format.Format.nSamplesPerSec, buf->buf_format,
1306 buf->buf_size/buf->format.Format.nBlockAlign,
1307 buf->in_chans, buf->in_type, buf->data);
1308 else if(This->primary->SupportedExt[SOFT_BUFFER_SUB_DATA])
1309 alBufferData(buf->buffers[0], buf->buf_format,
1310 buf->data, buf->buf_size,
1311 buf->format.Format.nSamplesPerSec);
1313 checkALError();
1316 hr = DSERR_GENERIC;
1317 if(This->primary->parent->share->nsources)
1319 This->source = This->primary->sources[--(This->primary->parent->share->nsources)];
1320 alSourcef(This->source, AL_GAIN, 1.0f);
1321 alSourcef(This->source, AL_PITCH, 1.0f);
1322 checkALError();
1324 else
1325 goto out;
1327 ds3dbuffer = &This->params;
1328 ds3dbuffer->dwSize = sizeof(This->params);
1329 ds3dbuffer->vPosition.x = 0.0;
1330 ds3dbuffer->vPosition.y = 0.0;
1331 ds3dbuffer->vPosition.z = 0.0;
1332 ds3dbuffer->vVelocity.x = 0.0;
1333 ds3dbuffer->vVelocity.y = 0.0;
1334 ds3dbuffer->vVelocity.z = 0.0;
1335 ds3dbuffer->dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1336 ds3dbuffer->dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1337 ds3dbuffer->vConeOrientation.x = 0.0;
1338 ds3dbuffer->vConeOrientation.y = 0.0;
1339 ds3dbuffer->vConeOrientation.z = 1.0;
1340 ds3dbuffer->lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1341 ds3dbuffer->flMinDistance = DS3D_DEFAULTMINDISTANCE;
1342 ds3dbuffer->flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1343 ds3dbuffer->dwMode = DS3DMODE_NORMAL;
1345 if((This->buffer->dsbflags&DSBCAPS_CTRL3D))
1347 if(This->primary->auxslot != 0)
1349 alSource3i(This->source, AL_AUXILIARY_SEND_FILTER, This->primary->auxslot, 0, AL_FILTER_NULL);
1350 checkALError();
1353 hr = IDirectSound3DBuffer_SetAllParameters(&This->IDirectSound3DBuffer_iface, ds3dbuffer, DS3D_IMMEDIATE);
1354 if(FAILED(hr))
1356 ERR("SetAllParameters failed\n");
1357 goto out;
1360 else
1362 ALuint source = This->source;
1364 if(This->primary->auxslot != 0)
1366 /* Simple hack to make reverb affect non-3D sounds too */
1367 alSource3i(source, AL_AUXILIARY_SEND_FILTER, This->primary->auxslot, 0, AL_FILTER_NULL);
1368 /*alSource3i(source, AL_AUXILIARY_SEND_FILTER, 0, 0, AL_FILTER_NULL);*/
1371 /* Non-3D sources aren't distance attenuated */
1372 This->ds3dmode = DS3DMODE_DISABLE;
1373 alSource3f(source, AL_POSITION, 0.0f, 1.0f, 0.0f);
1374 alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
1375 alSource3f(source, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
1376 alSourcef(source, AL_CONE_OUTER_GAIN, 1.0f);
1377 alSourcef(source, AL_REFERENCE_DISTANCE, 1.0f);
1378 alSourcef(source, AL_MAX_DISTANCE, 1000.0f);
1379 alSourcef(source, AL_ROLLOFF_FACTOR, 0.0f);
1380 alSourcei(source, AL_CONE_INNER_ANGLE, 360);
1381 alSourcei(source, AL_CONE_OUTER_ANGLE, 360);
1382 alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
1383 checkALError();
1385 hr = S_OK;
1387 out:
1388 popALContext();
1389 LeaveCriticalSection(This->crst);
1391 return hr;
1394 static HRESULT WINAPI DS8Buffer_Lock(IDirectSoundBuffer8 *iface, DWORD ofs, DWORD bytes, void **ptr1, DWORD *len1, void **ptr2, DWORD *len2, DWORD flags)
1396 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1397 DWORD remain;
1399 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %p, %p, %p, %p, 0x%"LONGFMT"x)\n", This, ofs, bytes, ptr1, len1, ptr2, len2, flags);
1401 if(!ptr1 || !len1)
1403 WARN("Invalid pointer/len %p %p\n", ptr1, len1);
1404 return DSERR_INVALIDPARAM;
1407 *ptr1 = NULL;
1408 *len1 = 0;
1409 if(ptr2) *ptr2 = NULL;
1410 if(len2) *len2 = 0;
1412 if((flags&DSBLOCK_FROMWRITECURSOR))
1413 DS8Buffer_GetCurrentPosition(iface, NULL, &ofs);
1414 else if(ofs >= This->buffer->buf_size)
1416 WARN("Invalid ofs %"LONGFMT"u\n", ofs);
1417 return DSERR_INVALIDPARAM;
1419 if((flags&DSBLOCK_ENTIREBUFFER))
1420 bytes = This->buffer->buf_size;
1421 else if(bytes > This->buffer->buf_size)
1423 WARN("Invalid size %"LONGFMT"u\n", bytes);
1424 return DSERR_INVALIDPARAM;
1427 if(InterlockedExchange(&This->buffer->locked, TRUE) == TRUE)
1429 WARN("Already locked\n");
1430 return DSERR_INVALIDPARAM;
1433 *ptr1 = This->buffer->data + ofs;
1434 if(ofs+bytes >= This->buffer->buf_size)
1436 *len1 = This->buffer->buf_size - ofs;
1437 remain = bytes - *len1;
1439 else
1441 *len1 = bytes;
1442 remain = 0;
1445 if(ptr2 && len2 && remain)
1447 *ptr2 = This->buffer->data;
1448 *len2 = remain;
1451 return DS_OK;
1454 static HRESULT WINAPI DS8Buffer_Play(IDirectSoundBuffer8 *iface, DWORD res1, DWORD prio, DWORD flags)
1456 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1457 ALint type, state = AL_STOPPED;
1458 HRESULT hr;
1460 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %"LONGFMT"u)\n", iface, res1, prio, flags);
1462 EnterCriticalSection(This->crst);
1463 setALContext(This->ctx);
1465 hr = DSERR_BUFFERLOST;
1466 if(This->bufferlost)
1468 WARN("Buffer %p lost\n", This);
1469 goto out;
1472 if((This->buffer->dsbflags&DSBCAPS_LOCDEFER))
1474 if(!(This->buffer->dsbflags&(DSBCAPS_LOCHARDWARE|DSBCAPS_LOCSOFTWARE)))
1476 if(flags & DSBPLAY_LOCSOFTWARE)
1477 This->buffer->dsbflags |= DSBCAPS_LOCSOFTWARE;
1478 else
1479 This->buffer->dsbflags |= DSBCAPS_LOCHARDWARE;
1482 else if(prio)
1484 ERR("Invalid priority set for non-deferred buffer %p, %"LONGFMT"u!\n", This->buffer, prio);
1485 hr = DSERR_INVALIDPARAM;
1486 goto out;
1489 alGetSourcei(This->source, AL_SOURCE_STATE, &state);
1490 if(This->buffer->numsegs > 1)
1492 This->islooping = !!(flags&DSBPLAY_LOOPING);
1493 if(state != AL_PLAYING && This->isplaying)
1494 state = AL_PLAYING;
1496 else
1498 alGetSourcei(This->source, AL_SOURCE_TYPE, &type);
1499 alSourcei(This->source, AL_LOOPING, (flags&DSBPLAY_LOOPING) ? AL_TRUE : AL_FALSE);
1501 checkALError();
1503 hr = S_OK;
1504 if(state == AL_PLAYING)
1505 goto out;
1507 /* alSourceQueueBuffers will implicitly set type to streaming */
1508 if(This->buffer->numsegs == 1)
1510 if(type != AL_STATIC)
1511 alSourcei(This->source, AL_BUFFER, This->buffer->buffers[0]);
1512 alSourcePlay(This->source);
1514 if(alGetError() != AL_NO_ERROR)
1516 ERR("Couldn't start source\n");
1517 This->curidx = (This->buffer->numsegs-1+This->curidx)%This->buffer->numsegs;
1518 alSourcei(This->source, AL_BUFFER, 0);
1519 checkALError();
1520 hr = DSERR_GENERIC;
1521 goto out;
1523 This->isplaying = TRUE;
1525 if(This->nnotify)
1527 DS8Buffer_addnotify(This);
1528 DS8Primary_starttimer(This->primary);
1530 else if(This->buffer->numsegs > 1)
1531 DS8Primary_starttimer(This->primary);
1533 out:
1534 popALContext();
1535 LeaveCriticalSection(This->crst);
1536 return hr;
1539 static HRESULT WINAPI DS8Buffer_SetCurrentPosition(IDirectSoundBuffer8 *iface, DWORD pos)
1541 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1543 TRACE("(%p)->(%"LONGFMT"u)\n", iface, pos);
1545 if(pos >= This->buffer->buf_size)
1546 return DSERR_INVALIDPARAM;
1548 EnterCriticalSection(This->crst);
1550 if(This->buffer->numsegs > 1)
1552 DS8Data *buf = This->buffer;
1553 This->curidx = pos/buf->segsize;
1554 if(This->curidx >= buf->numsegs)
1555 This->curidx = buf->numsegs - 1;
1556 if(This->isplaying)
1558 setALContext(This->ctx);
1559 /* Perform a flush, so the next timer update will restart at the
1560 * proper position */
1561 alSourceStop(This->source);
1562 alSourcei(This->source, AL_BUFFER, 0);
1563 checkALError();
1564 popALContext();
1567 else
1569 setALContext(This->ctx);
1570 alSourcei(This->source, AL_BYTE_OFFSET, pos);
1571 popALContext();
1573 This->lastpos = pos;
1575 LeaveCriticalSection(This->crst);
1576 return DS_OK;
1579 static HRESULT WINAPI DS8Buffer_SetFormat(IDirectSoundBuffer8 *iface, const WAVEFORMATEX *wfx)
1581 /* This call only works on primary buffers */
1582 WARN("(%p)->(%p)\n", iface, wfx);
1583 return DSERR_INVALIDCALL;
1586 static HRESULT WINAPI DS8Buffer_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
1588 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1589 HRESULT hr = S_OK;
1591 TRACE("(%p)->(%"LONGFMT"d)\n", iface, vol);
1593 if(vol > DSBVOLUME_MAX || vol < DSBVOLUME_MIN)
1595 WARN("Invalid volume (%"LONGFMT"d)\n", vol);
1596 return DSERR_INVALIDPARAM;
1599 if(!(This->buffer->dsbflags&DSBCAPS_CTRLVOLUME))
1600 hr = DSERR_CONTROLUNAVAIL;
1601 if(SUCCEEDED(hr))
1603 ALfloat fvol = mB_to_gain(vol);
1604 setALContext(This->ctx);
1605 alSourcef(This->source, AL_GAIN, fvol);
1606 popALContext();
1609 return hr;
1612 static HRESULT WINAPI DS8Buffer_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
1614 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1615 HRESULT hr = S_OK;
1617 TRACE("(%p)->(%"LONGFMT"d)\n", iface, pan);
1619 if(pan > DSBPAN_RIGHT || pan < DSBPAN_LEFT)
1621 WARN("invalid parameter: pan = %"LONGFMT"d\n", pan);
1622 return DSERR_INVALIDPARAM;
1625 if(!(This->buffer->dsbflags&DSBCAPS_CTRLPAN))
1626 hr = DSERR_CONTROLUNAVAIL;
1627 else
1629 ALfloat pos[3];
1630 pos[0] = (pan-DSBPAN_LEFT) * 2.0 / (ALfloat)(DSBPAN_RIGHT-DSBPAN_LEFT) - 1.0;
1631 /* NOTE: Strict movement along the X plane can cause the sound to jump
1632 * between left and right sharply. Using a curved path helps smooth it
1633 * out */
1634 pos[1] = sqrt(1.0 - pos[0]*pos[0]);
1635 pos[2] = 0.0;
1637 setALContext(This->ctx);
1638 alSourcefv(This->source, AL_POSITION, pos);
1639 checkALError();
1640 popALContext();
1642 if(pan != 0 && This->buffer->format.Format.nChannels > 1)
1643 FIXME("Panning for multi-channel buffers is not supported\n");
1646 return hr;
1649 static HRESULT WINAPI DS8Buffer_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
1651 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1652 HRESULT hr = S_OK;
1654 TRACE("(%p)->(%"LONGFMT"u)\n", iface, freq);
1656 if(freq < DSBFREQUENCY_MIN || freq > DSBFREQUENCY_MAX)
1658 WARN("invalid parameter: freq = %"LONGFMT"u\n", freq);
1659 return DSERR_INVALIDPARAM;
1662 if(!(This->buffer->dsbflags&DSBCAPS_CTRLFREQUENCY))
1663 hr = DSERR_CONTROLUNAVAIL;
1664 else
1666 ALfloat pitch = 1.0f;
1667 if(freq != DSBFREQUENCY_ORIGINAL)
1668 pitch = freq / (ALfloat)This->buffer->format.Format.nSamplesPerSec;
1670 setALContext(This->ctx);
1671 alSourcef(This->source, AL_PITCH, pitch);
1672 checkALError();
1673 popALContext();
1676 return hr;
1679 static HRESULT WINAPI DS8Buffer_Stop(IDirectSoundBuffer8 *iface)
1681 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1682 ALint state;
1684 TRACE("(%p)->()\n", iface);
1686 EnterCriticalSection(This->crst);
1687 setALContext(This->ctx);
1689 alSourcePause(This->source);
1690 checkALError();
1691 /* Mac OS X doesn't immediately report state change
1692 * if Play() is immediately called after Stop, this can be fatal,
1693 * the buffer would never be restarted
1695 do {
1696 state = AL_PAUSED;
1697 alGetSourcei(This->source, AL_SOURCE_STATE, &state);
1698 if(state != AL_PLAYING)
1699 break;
1700 Sleep(1);
1701 } while(1);
1703 This->isplaying = FALSE;
1705 popALContext();
1706 LeaveCriticalSection(This->crst);
1708 return S_OK;
1711 static HRESULT WINAPI DS8Buffer_Unlock(IDirectSoundBuffer8 *iface, void *ptr1, DWORD len1, void *ptr2, DWORD len2)
1713 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1714 DS8Data *buf = This->buffer;
1715 DWORD bufsize = buf->buf_size;
1716 DWORD_PTR ofs1, ofs2;
1717 DWORD_PTR boundary = (DWORD_PTR)buf->data;
1718 HRESULT hr;
1720 TRACE("(%p)->(%p, %"LONGFMT"u, %p, %"LONGFMT"u)\n", iface, ptr1, len1, ptr2, len2);
1722 if(InterlockedExchange(&This->buffer->locked, FALSE) == FALSE)
1724 WARN("Not locked\n");
1725 return DSERR_INVALIDPARAM;
1728 hr = DSERR_INVALIDPARAM;
1729 /* Make sure offset is between boundary and boundary + bufsize */
1730 ofs1 = (DWORD_PTR)ptr1;
1731 ofs2 = (DWORD_PTR)ptr2;
1732 if(ofs1 < boundary)
1733 goto out;
1734 if(ofs2 && ofs2 != boundary)
1735 goto out;
1736 ofs1 -= boundary;
1737 ofs2 = 0;
1738 if(bufsize-ofs1 < len1 || len2 > ofs1)
1739 goto out;
1740 if(!ptr2)
1741 len2 = 0;
1743 hr = DS_OK;
1744 if(!len1 && !len2)
1745 goto out;
1746 if(This->primary->SupportedExt[EXT_STATIC_BUFFER])
1747 goto out;
1749 setALContext(This->ctx);
1750 if(This->primary->SupportedExt[SOFT_BUFFER_SAMPLES])
1752 const WAVEFORMATEX *format = &buf->format.Format;
1754 ptr1 = (BYTE*)ptr1 - (ofs1%format->nBlockAlign);
1755 ofs1 /= format->nBlockAlign;
1756 len1 /= format->nBlockAlign;
1757 if(len1 > 0)
1758 This->ExtAL->BufferSubSamplesSOFT(buf->buffers[0], ofs1, len1,
1759 buf->in_chans, buf->in_type, ptr1);
1760 ptr2 = (BYTE*)ptr2 - (ofs2%format->nBlockAlign);
1761 ofs2 /= format->nBlockAlign;
1762 len2 /= format->nBlockAlign;
1763 if(len2 > 0)
1764 This->ExtAL->BufferSubSamplesSOFT(buf->buffers[0], ofs2, len2,
1765 buf->in_chans, buf->in_type, ptr2);
1766 checkALError();
1768 else if(This->primary->SupportedExt[SOFT_BUFFER_SUB_DATA])
1770 const WAVEFORMATEX *format = &buf->format.Format;
1772 len1 -= len1%format->nBlockAlign;
1773 if(len1 > 0)
1774 This->ExtAL->BufferSubData(buf->buffers[0], buf->buf_format, ptr1,
1775 ofs1, len1);
1776 len2 -= len2%format->nBlockAlign;
1777 if(len2 > 0)
1778 This->ExtAL->BufferSubData(buf->buffers[0], buf->buf_format, ptr2,
1779 ofs2, len2);
1780 checkALError();
1782 else
1784 alBufferData(buf->buffers[0], buf->buf_format,
1785 buf->data, buf->buf_size,
1786 buf->format.Format.nSamplesPerSec);
1787 checkALError();
1789 popALContext();
1791 out:
1792 if(hr != S_OK)
1793 WARN("Invalid parameters (0x%lx,%"LONGFMT"u) (%p,%"LONGFMT"u,%p,%"LONGFMT"u)\n", boundary, bufsize, ptr1, len1, ptr2, len2);
1794 return hr;
1797 static HRESULT WINAPI DS8Buffer_Restore(IDirectSoundBuffer8 *iface)
1799 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1800 HRESULT hr;
1802 TRACE("(%p)->()\n", iface);
1804 EnterCriticalSection(This->crst);
1805 if(This->primary->parent->prio_level < DSSCL_WRITEPRIMARY ||
1806 iface == This->primary->write_emu)
1808 This->bufferlost = 0;
1809 hr = S_OK;
1811 else
1812 hr = DSERR_BUFFERLOST;
1813 LeaveCriticalSection(This->crst);
1815 return hr;
1818 static HRESULT WINAPI DS8Buffer_SetFX(IDirectSoundBuffer8 *iface, DWORD fxcount, DSEFFECTDESC *desc, DWORD *rescodes)
1820 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1821 DWORD i;
1823 TRACE("(%p)->(%"LONGFMT"u, %p, %p)\n", This, fxcount, desc, rescodes);
1825 if(!(This->buffer->dsbflags&DSBCAPS_CTRLFX))
1827 WARN("FX control not set\n");
1828 return DSERR_CONTROLUNAVAIL;
1831 if(fxcount == 0)
1833 if(desc || rescodes)
1835 WARN("Non-NULL desc and/or result pointer specified with no effects.\n");
1836 return DSERR_INVALIDPARAM;
1839 /* No effects; we can handle that */
1840 return DS_OK;
1843 if(!desc || !rescodes)
1845 WARN("NULL desc and/or result pointer specified.\n");
1846 return DSERR_INVALIDPARAM;
1849 /* We don't (currently) handle DSound effects */
1850 for(i = 0;i < fxcount;++i)
1852 FIXME("Cannot handle effect: %s\n", debugstr_guid(&desc[i].guidDSFXClass));
1853 rescodes[i] = DSFXR_FAILED;
1856 return DS_INCOMPLETE;
1859 static HRESULT WINAPI DS8Buffer_AcquireResources(IDirectSoundBuffer8 *iface, DWORD flags, DWORD fxcount, DWORD *rescodes)
1861 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1863 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %p)\n", This, flags, fxcount, rescodes);
1865 /* effects aren't supported at the moment.. */
1866 if(fxcount != 0 || rescodes)
1868 WARN("Non-zero effect count and/or result pointer specified with no effects.\n");
1869 return DSERR_INVALIDPARAM;
1872 EnterCriticalSection(This->crst);
1873 if((This->buffer->dsbflags&DSBCAPS_LOCDEFER))
1875 This->buffer->dsbflags &= ~(DSBCAPS_LOCSOFTWARE|DSBCAPS_LOCHARDWARE);
1876 if((flags&DSBPLAY_LOCSOFTWARE))
1877 This->buffer->dsbflags |= DSBCAPS_LOCSOFTWARE;
1878 else
1879 This->buffer->dsbflags |= DSBCAPS_LOCHARDWARE;
1881 LeaveCriticalSection(This->crst);
1883 return S_OK;
1886 static HRESULT WINAPI DS8Buffer_GetObjectInPath(IDirectSoundBuffer8 *iface, REFGUID guid, DWORD idx, REFGUID rguidiface, void **ppv)
1888 FIXME("(%p)->(%s, %"LONGFMT"u, %s, %p) : stub!\n", iface, debugstr_guid(guid), idx, debugstr_guid(rguidiface), ppv);
1889 return E_NOTIMPL;
1892 static const IDirectSoundBuffer8Vtbl DS8Buffer_Vtbl = {
1893 DS8Buffer_QueryInterface,
1894 DS8Buffer_AddRef,
1895 DS8Buffer_Release,
1896 DS8Buffer_GetCaps,
1897 DS8Buffer_GetCurrentPosition,
1898 DS8Buffer_GetFormat,
1899 DS8Buffer_GetVolume,
1900 DS8Buffer_GetPan,
1901 DS8Buffer_GetFrequency,
1902 DS8Buffer_GetStatus,
1903 DS8Buffer_Initialize,
1904 DS8Buffer_Lock,
1905 DS8Buffer_Play,
1906 DS8Buffer_SetCurrentPosition,
1907 DS8Buffer_SetFormat,
1908 DS8Buffer_SetVolume,
1909 DS8Buffer_SetPan,
1910 DS8Buffer_SetFrequency,
1911 DS8Buffer_Stop,
1912 DS8Buffer_Unlock,
1913 DS8Buffer_Restore,
1914 DS8Buffer_SetFX,
1915 DS8Buffer_AcquireResources,
1916 DS8Buffer_GetObjectInPath
1920 static HRESULT WINAPI DSBuffer_QueryInterface(IDirectSoundBuffer *iface, REFIID riid, void **ppv)
1922 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1923 return DS8Buffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
1926 static ULONG WINAPI DSBuffer_AddRef(IDirectSoundBuffer *iface)
1928 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1929 return DS8Buffer_AddRef(&This->IDirectSoundBuffer8_iface);
1932 static ULONG WINAPI DSBuffer_Release(IDirectSoundBuffer *iface)
1934 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1935 return DS8Buffer_Release(&This->IDirectSoundBuffer8_iface);
1938 static HRESULT WINAPI DSBuffer_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1940 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1941 return DS8Buffer_GetCaps(&This->IDirectSoundBuffer8_iface, caps);
1944 static HRESULT WINAPI DSBuffer_GetCurrentPosition(IDirectSoundBuffer *iface, DWORD *playpos, DWORD *curpos)
1946 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1947 return DS8Buffer_GetCurrentPosition(&This->IDirectSoundBuffer8_iface, playpos, curpos);
1950 static HRESULT WINAPI DSBuffer_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *wfx, DWORD allocated, DWORD *written)
1952 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1953 return DS8Buffer_GetFormat(&This->IDirectSoundBuffer8_iface, wfx, allocated, written);
1956 static HRESULT WINAPI DSBuffer_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
1958 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1959 return DS8Buffer_GetVolume(&This->IDirectSoundBuffer8_iface, vol);
1962 static HRESULT WINAPI DSBuffer_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1964 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1965 return DS8Buffer_GetPan(&This->IDirectSoundBuffer8_iface, pan);
1968 static HRESULT WINAPI DSBuffer_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1970 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1971 return DS8Buffer_GetFrequency(&This->IDirectSoundBuffer8_iface, freq);
1974 static HRESULT WINAPI DSBuffer_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
1976 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1977 return DS8Buffer_GetStatus(&This->IDirectSoundBuffer8_iface, status);
1980 static HRESULT WINAPI DSBuffer_Initialize(IDirectSoundBuffer *iface, IDirectSound *ds, const DSBUFFERDESC *desc)
1982 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1983 return DS8Buffer_Initialize(&This->IDirectSoundBuffer8_iface, ds, desc);
1986 static HRESULT WINAPI DSBuffer_Lock(IDirectSoundBuffer *iface, DWORD ofs, DWORD bytes, void **ptr1, DWORD *len1, void **ptr2, DWORD *len2, DWORD flags)
1988 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1989 return DS8Buffer_Lock(&This->IDirectSoundBuffer8_iface, ofs, bytes, ptr1, len1, ptr2, len2, flags);
1992 static HRESULT WINAPI DSBuffer_Play(IDirectSoundBuffer *iface, DWORD res1, DWORD prio, DWORD flags)
1994 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1995 return DS8Buffer_Play(&This->IDirectSoundBuffer8_iface, res1, prio, flags);
1998 static HRESULT WINAPI DSBuffer_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD pos)
2000 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2001 return DS8Buffer_SetCurrentPosition(&This->IDirectSoundBuffer8_iface, pos);
2004 static HRESULT WINAPI DSBuffer_SetFormat(IDirectSoundBuffer *iface, const WAVEFORMATEX *wfx)
2006 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2007 return DS8Buffer_SetFormat(&This->IDirectSoundBuffer8_iface, wfx);
2010 static HRESULT WINAPI DSBuffer_SetVolume(IDirectSoundBuffer *iface, LONG vol)
2012 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2013 return DS8Buffer_SetVolume(&This->IDirectSoundBuffer8_iface, vol);
2016 static HRESULT WINAPI DSBuffer_SetPan(IDirectSoundBuffer *iface, LONG pan)
2018 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2019 return DS8Buffer_SetPan(&This->IDirectSoundBuffer8_iface, pan);
2022 static HRESULT WINAPI DSBuffer_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
2024 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2025 return DS8Buffer_SetFrequency(&This->IDirectSoundBuffer8_iface, freq);
2028 static HRESULT WINAPI DSBuffer_Stop(IDirectSoundBuffer *iface)
2030 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2031 return DS8Buffer_Stop(&This->IDirectSoundBuffer8_iface);
2034 static HRESULT WINAPI DSBuffer_Unlock(IDirectSoundBuffer *iface, void *ptr1, DWORD len1, void *ptr2, DWORD len2)
2036 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2037 return DS8Buffer_Unlock(&This->IDirectSoundBuffer8_iface, ptr1, len1, ptr2, len2);
2040 static HRESULT WINAPI DSBuffer_Restore(IDirectSoundBuffer *iface)
2042 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2043 return DS8Buffer_Restore(&This->IDirectSoundBuffer8_iface);
2046 static const IDirectSoundBufferVtbl DSBuffer_Vtbl = {
2047 DSBuffer_QueryInterface,
2048 DSBuffer_AddRef,
2049 DSBuffer_Release,
2050 DSBuffer_GetCaps,
2051 DSBuffer_GetCurrentPosition,
2052 DSBuffer_GetFormat,
2053 DSBuffer_GetVolume,
2054 DSBuffer_GetPan,
2055 DSBuffer_GetFrequency,
2056 DSBuffer_GetStatus,
2057 DSBuffer_Initialize,
2058 DSBuffer_Lock,
2059 DSBuffer_Play,
2060 DSBuffer_SetCurrentPosition,
2061 DSBuffer_SetFormat,
2062 DSBuffer_SetVolume,
2063 DSBuffer_SetPan,
2064 DSBuffer_SetFrequency,
2065 DSBuffer_Stop,
2066 DSBuffer_Unlock,
2067 DSBuffer_Restore
2071 void DS8Buffer_SetParams(DS8Buffer *This, const DS3DBUFFER *params, LONG flags)
2073 const ALuint source = This->source;
2074 union BufferParamFlags dirty = { flags };
2076 if(dirty.bit.pos)
2077 alSource3f(source, AL_POSITION, params->vPosition.x, params->vPosition.y,
2078 -params->vPosition.z);
2079 if(dirty.bit.vel)
2080 alSource3f(source, AL_VELOCITY, params->vVelocity.x, params->vVelocity.y,
2081 -params->vVelocity.z);
2082 if(dirty.bit.cone_angles)
2084 alSourcei(source, AL_CONE_INNER_ANGLE, params->dwInsideConeAngle);
2085 alSourcei(source, AL_CONE_OUTER_ANGLE, params->dwOutsideConeAngle);
2087 if(dirty.bit.cone_orient)
2088 alSource3f(source, AL_DIRECTION, params->vConeOrientation.x,
2089 params->vConeOrientation.y,
2090 -params->vConeOrientation.z);
2091 if(dirty.bit.cone_outsidevolume)
2092 alSourcef(source, AL_CONE_OUTER_GAIN, mB_to_gain(params->lConeOutsideVolume));
2093 if(dirty.bit.min_distance)
2094 alSourcef(source, AL_REFERENCE_DISTANCE, params->flMinDistance);
2095 if(dirty.bit.max_distance)
2096 alSourcef(source, AL_MAX_DISTANCE, params->flMaxDistance);
2097 if(dirty.bit.mode)
2099 This->ds3dmode = params->dwMode;
2100 alSourcei(source, AL_SOURCE_RELATIVE, (params->dwMode!=DS3DMODE_NORMAL) ?
2101 AL_TRUE : AL_FALSE);
2102 alSourcef(source, AL_ROLLOFF_FACTOR, (params->dwMode==DS3DMODE_DISABLE) ?
2103 0.0f : This->primary->rollofffactor);
2107 static HRESULT WINAPI DS8Buffer3D_QueryInterface(IDirectSound3DBuffer *iface, REFIID riid, void **ppv)
2109 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2110 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
2113 static ULONG WINAPI DS8Buffer3D_AddRef(IDirectSound3DBuffer *iface)
2115 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2116 LONG ret;
2118 InterlockedIncrement(&This->all_ref);
2119 ret = InterlockedIncrement(&This->ds3d_ref);
2120 TRACE("new refcount %"LONGFMT"d\n", ret);
2122 return ret;
2125 static ULONG WINAPI DS8Buffer3D_Release(IDirectSound3DBuffer *iface)
2127 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2128 LONG ret;
2130 ret = InterlockedDecrement(&This->ds3d_ref);
2131 TRACE("new refcount %"LONGFMT"d\n", ret);
2132 if(InterlockedDecrement(&This->all_ref) == 0)
2133 DS8Buffer_Destroy(This);
2135 return ret;
2138 static HRESULT WINAPI DS8Buffer3D_GetAllParameters(IDirectSound3DBuffer *iface, DS3DBUFFER *ds3dbuffer)
2140 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2142 TRACE("(%p)->(%p)\n", iface, ds3dbuffer);
2144 if(!ds3dbuffer || ds3dbuffer->dwSize < sizeof(*ds3dbuffer))
2146 WARN("Invalid parameters %p %"LONGFMT"u\n", ds3dbuffer, ds3dbuffer ? ds3dbuffer->dwSize : 0);
2147 return DSERR_INVALIDPARAM;
2150 EnterCriticalSection(This->crst);
2151 setALContext(This->ctx);
2153 IDirectSound3DBuffer_GetPosition(iface, &ds3dbuffer->vPosition);
2154 IDirectSound3DBuffer_GetVelocity(iface, &ds3dbuffer->vVelocity);
2155 IDirectSound3DBuffer_GetConeAngles(iface, &ds3dbuffer->dwInsideConeAngle, &ds3dbuffer->dwOutsideConeAngle);
2156 IDirectSound3DBuffer_GetConeOrientation(iface, &ds3dbuffer->vConeOrientation);
2157 IDirectSound3DBuffer_GetConeOutsideVolume(iface, &ds3dbuffer->lConeOutsideVolume);
2158 IDirectSound3DBuffer_GetMinDistance(iface, &ds3dbuffer->flMinDistance);
2159 IDirectSound3DBuffer_GetMaxDistance(iface, &ds3dbuffer->flMaxDistance);
2160 IDirectSound3DBuffer_GetMode(iface, &ds3dbuffer->dwMode);
2162 popALContext();
2163 LeaveCriticalSection(This->crst);
2165 return DS_OK;
2168 static HRESULT WINAPI DS8Buffer3D_GetConeAngles(IDirectSound3DBuffer *iface, DWORD *pdwInsideConeAngle, DWORD *pdwOutsideConeAngle)
2170 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2171 ALint inangle, outangle;
2173 TRACE("(%p)->(%p, %p)\n", This, pdwInsideConeAngle, pdwOutsideConeAngle);
2174 if(!pdwInsideConeAngle || !pdwOutsideConeAngle)
2176 WARN("Invalid pointers (%p, %p)\n", pdwInsideConeAngle, pdwOutsideConeAngle);
2177 return DSERR_INVALIDPARAM;
2180 EnterCriticalSection(This->crst);
2181 setALContext(This->ctx);
2183 alGetSourcei(This->source, AL_CONE_INNER_ANGLE, &inangle);
2184 alGetSourcei(This->source, AL_CONE_OUTER_ANGLE, &outangle);
2185 checkALError();
2187 popALContext();
2188 LeaveCriticalSection(This->crst);
2190 *pdwInsideConeAngle = inangle;
2191 *pdwOutsideConeAngle = outangle;
2192 return S_OK;
2195 static HRESULT WINAPI DS8Buffer3D_GetConeOrientation(IDirectSound3DBuffer *iface, D3DVECTOR *orient)
2197 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2198 ALfloat dir[3];
2200 TRACE("(%p)->(%p)\n", This, orient);
2201 if(!orient)
2203 WARN("Invalid pointer\n");
2204 return DSERR_INVALIDPARAM;
2207 setALContext(This->ctx);
2208 alGetSourcefv(This->source, AL_DIRECTION, dir);
2209 checkALError();
2210 popALContext();
2212 orient->x = dir[0];
2213 orient->y = dir[1];
2214 orient->z = -dir[2];
2215 return S_OK;
2218 static HRESULT WINAPI DS8Buffer3D_GetConeOutsideVolume(IDirectSound3DBuffer *iface, LONG *vol)
2220 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2221 ALfloat gain;
2223 TRACE("(%p)->(%p)\n", This, vol);
2224 if(!vol)
2226 WARN("Invalid pointer\n");
2227 return DSERR_INVALIDPARAM;
2230 setALContext(This->ctx);
2231 alGetSourcef(This->source, AL_CONE_OUTER_GAIN, &gain);
2232 checkALError();
2233 popALContext();
2235 *vol = clampI(gain_to_mB(gain), DSBVOLUME_MIN, DSBVOLUME_MAX);
2236 return S_OK;
2239 static HRESULT WINAPI DS8Buffer3D_GetMaxDistance(IDirectSound3DBuffer *iface, D3DVALUE *maxdist)
2241 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2242 ALfloat dist;
2244 TRACE("(%p)->(%p)\n", This, maxdist);
2245 if(!maxdist)
2247 WARN("Invalid pointer\n");
2248 return DSERR_INVALIDPARAM;
2251 setALContext(This->ctx);
2252 alGetSourcef(This->source, AL_MAX_DISTANCE, &dist);
2253 checkALError();
2254 popALContext();
2256 *maxdist = dist;
2257 return S_OK;
2260 static HRESULT WINAPI DS8Buffer3D_GetMinDistance(IDirectSound3DBuffer *iface, D3DVALUE *mindist)
2262 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2263 ALfloat dist;
2265 TRACE("(%p)->(%p)\n", This, mindist);
2266 if(!mindist)
2268 WARN("Invalid pointer\n");
2269 return DSERR_INVALIDPARAM;
2272 setALContext(This->ctx);
2273 alGetSourcef(This->source, AL_REFERENCE_DISTANCE, &dist);
2274 checkALError();
2275 popALContext();
2277 *mindist = dist;
2278 return S_OK;
2281 static HRESULT WINAPI DS8Buffer3D_GetMode(IDirectSound3DBuffer *iface, DWORD *mode)
2283 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2285 TRACE("(%p)->(%p)\n", This, mode);
2286 if(!mode)
2288 WARN("Invalid pointer\n");
2289 return DSERR_INVALIDPARAM;
2292 EnterCriticalSection(This->crst);
2293 *mode = This->ds3dmode;
2294 LeaveCriticalSection(This->crst);
2296 return S_OK;
2299 static HRESULT WINAPI DS8Buffer3D_GetPosition(IDirectSound3DBuffer *iface, D3DVECTOR *pos)
2301 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2302 ALfloat alpos[3];
2304 TRACE("(%p)->(%p)\n", This, pos);
2305 if(!pos)
2307 WARN("Invalid pointer\n");
2308 return DSERR_INVALIDPARAM;
2311 setALContext(This->ctx);
2312 alGetSourcefv(This->source, AL_POSITION, alpos);
2313 checkALError();
2314 popALContext();
2316 pos->x = alpos[0];
2317 pos->y = alpos[1];
2318 pos->z = -alpos[2];
2319 return S_OK;
2322 static HRESULT WINAPI DS8Buffer3D_GetVelocity(IDirectSound3DBuffer *iface, D3DVECTOR *vel)
2324 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2325 ALfloat alvel[3];
2327 TRACE("(%p)->(%p)\n", This, vel);
2328 if(!vel)
2330 WARN("Invalid pointer\n");
2331 return DSERR_INVALIDPARAM;
2334 setALContext(This->ctx);
2335 alGetSourcefv(This->source, AL_VELOCITY, alvel);
2336 checkALError();
2337 popALContext();
2339 vel->x = alvel[0];
2340 vel->y = alvel[1];
2341 vel->z = -alvel[2];
2342 return S_OK;
2345 static HRESULT WINAPI DS8Buffer3D_SetAllParameters(IDirectSound3DBuffer *iface, const DS3DBUFFER *ds3dbuffer, DWORD apply)
2347 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2348 TRACE("(%p)->(%p, %"LONGFMT"u)\n", This, ds3dbuffer, apply);
2350 if(!ds3dbuffer || ds3dbuffer->dwSize < sizeof(*ds3dbuffer))
2352 WARN("Invalid DS3DBUFFER (%p, %"LONGFMT"u)\n", ds3dbuffer, ds3dbuffer ? ds3dbuffer->dwSize : 0);
2353 return DSERR_INVALIDPARAM;
2356 if(ds3dbuffer->dwInsideConeAngle > DS3D_MAXCONEANGLE ||
2357 ds3dbuffer->dwOutsideConeAngle > DS3D_MAXCONEANGLE)
2359 WARN("Invalid cone angles (%"LONGFMT"u, %"LONGFMT"u)\n",
2360 ds3dbuffer->dwInsideConeAngle, ds3dbuffer->dwOutsideConeAngle);
2361 return DSERR_INVALIDPARAM;
2364 if(ds3dbuffer->lConeOutsideVolume > DSBVOLUME_MAX ||
2365 ds3dbuffer->lConeOutsideVolume < DSBVOLUME_MIN)
2367 WARN("Invalid cone outside volume (%"LONGFMT"d)\n", ds3dbuffer->lConeOutsideVolume);
2368 return DSERR_INVALIDPARAM;
2371 if(ds3dbuffer->flMaxDistance < 0.0f)
2373 WARN("Invalid max distance (%f)\n", ds3dbuffer->flMaxDistance);
2374 return DSERR_INVALIDPARAM;
2377 if(ds3dbuffer->flMinDistance < 0.0f)
2379 WARN("Invalid min distance (%f)\n", ds3dbuffer->flMinDistance);
2380 return DSERR_INVALIDPARAM;
2383 if(ds3dbuffer->dwMode != DS3DMODE_NORMAL &&
2384 ds3dbuffer->dwMode != DS3DMODE_HEADRELATIVE &&
2385 ds3dbuffer->dwMode != DS3DMODE_DISABLE)
2387 WARN("Invalid mode (%"LONGFMT"u)\n", ds3dbuffer->dwMode);
2388 return DSERR_INVALIDPARAM;
2391 if(apply == DS3D_DEFERRED)
2393 EnterCriticalSection(This->crst);
2394 This->params = *ds3dbuffer;
2395 This->params.dwSize = sizeof(This->params);
2396 This->dirty.bit.pos = 1;
2397 This->dirty.bit.vel = 1;
2398 This->dirty.bit.cone_angles = 1;
2399 This->dirty.bit.cone_orient = 1;
2400 This->dirty.bit.cone_outsidevolume = 1;
2401 This->dirty.bit.min_distance = 1;
2402 This->dirty.bit.max_distance = 1;
2403 This->dirty.bit.mode = 1;
2404 LeaveCriticalSection(This->crst);
2406 else
2408 union BufferParamFlags dirty = { 0 };
2409 dirty.bit.pos = 1;
2410 dirty.bit.vel = 1;
2411 dirty.bit.cone_angles = 1;
2412 dirty.bit.cone_orient = 1;
2413 dirty.bit.cone_outsidevolume = 1;
2414 dirty.bit.min_distance = 1;
2415 dirty.bit.max_distance = 1;
2416 dirty.bit.mode = 1;
2418 EnterCriticalSection(This->crst);
2419 setALContext(This->ctx);
2420 DS8Buffer_SetParams(This, ds3dbuffer, dirty.flags);
2421 checkALError();
2422 popALContext();
2423 LeaveCriticalSection(This->crst);
2426 return S_OK;
2429 static HRESULT WINAPI DS8Buffer3D_SetConeAngles(IDirectSound3DBuffer *iface, DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD apply)
2431 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2433 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %"LONGFMT"u)\n", This, dwInsideConeAngle, dwOutsideConeAngle, apply);
2434 if(dwInsideConeAngle > DS3D_MAXCONEANGLE ||
2435 dwOutsideConeAngle > DS3D_MAXCONEANGLE)
2437 WARN("Invalid cone angles (%"LONGFMT"u, %"LONGFMT"u)\n", dwInsideConeAngle, dwOutsideConeAngle);
2438 return DSERR_INVALIDPARAM;
2441 EnterCriticalSection(This->crst);
2442 if(apply == DS3D_DEFERRED)
2444 This->params.dwInsideConeAngle = dwInsideConeAngle;
2445 This->params.dwOutsideConeAngle = dwOutsideConeAngle;
2446 This->dirty.bit.cone_angles = 1;
2448 else
2450 setALContext(This->ctx);
2451 alSourcei(This->source, AL_CONE_INNER_ANGLE, dwInsideConeAngle);
2452 alSourcei(This->source, AL_CONE_OUTER_ANGLE, dwOutsideConeAngle);
2453 checkALError();
2454 popALContext();
2456 LeaveCriticalSection(This->crst);
2458 return S_OK;
2461 static HRESULT WINAPI DS8Buffer3D_SetConeOrientation(IDirectSound3DBuffer *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
2463 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2465 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", This, x, y, z, apply);
2467 if(apply == DS3D_DEFERRED)
2469 EnterCriticalSection(This->crst);
2470 This->params.vConeOrientation.x = x;
2471 This->params.vConeOrientation.y = y;
2472 This->params.vConeOrientation.z = z;
2473 This->dirty.bit.cone_orient = 1;
2474 LeaveCriticalSection(This->crst);
2476 else
2478 setALContext(This->ctx);
2479 alSource3f(This->source, AL_DIRECTION, x, y, -z);
2480 checkALError();
2481 popALContext();
2484 return S_OK;
2487 static HRESULT WINAPI DS8Buffer3D_SetConeOutsideVolume(IDirectSound3DBuffer *iface, LONG vol, DWORD apply)
2489 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2491 TRACE("(%p)->(%"LONGFMT"d, %"LONGFMT"u)\n", This, vol, apply);
2492 if(vol < DSBVOLUME_MIN || vol > DSBVOLUME_MAX)
2494 WARN("Invalid volume (%"LONGFMT"d)\n", vol);
2495 return DSERR_INVALIDPARAM;
2498 if(apply == DS3D_DEFERRED)
2500 EnterCriticalSection(This->crst);
2501 This->params.lConeOutsideVolume = vol;
2502 This->dirty.bit.cone_outsidevolume = 1;
2503 LeaveCriticalSection(This->crst);
2505 else
2507 setALContext(This->ctx);
2508 alSourcef(This->source, AL_CONE_OUTER_GAIN, mB_to_gain(vol));
2509 checkALError();
2510 popALContext();
2513 return S_OK;
2516 static HRESULT WINAPI DS8Buffer3D_SetMaxDistance(IDirectSound3DBuffer *iface, D3DVALUE maxdist, DWORD apply)
2518 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2520 TRACE("(%p)->(%f, %"LONGFMT"u)\n", This, maxdist, apply);
2521 if(maxdist < 0.0f)
2523 WARN("Invalid max distance (%f)\n", maxdist);
2524 return DSERR_INVALIDPARAM;
2527 if(apply == DS3D_DEFERRED)
2529 EnterCriticalSection(This->crst);
2530 This->params.flMaxDistance = maxdist;
2531 This->dirty.bit.max_distance = 1;
2532 LeaveCriticalSection(This->crst);
2534 else
2536 setALContext(This->ctx);
2537 alSourcef(This->source, AL_MAX_DISTANCE, maxdist);
2538 checkALError();
2539 popALContext();
2542 return S_OK;
2545 static HRESULT WINAPI DS8Buffer3D_SetMinDistance(IDirectSound3DBuffer *iface, D3DVALUE mindist, DWORD apply)
2547 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2549 TRACE("(%p)->(%f, %"LONGFMT"u)\n", This, mindist, apply);
2550 if(mindist < 0.0f)
2552 WARN("Invalid min distance (%f)\n", mindist);
2553 return DSERR_INVALIDPARAM;
2556 if(apply == DS3D_DEFERRED)
2558 EnterCriticalSection(This->crst);
2559 This->params.flMinDistance = mindist;
2560 This->dirty.bit.min_distance = 1;
2561 LeaveCriticalSection(This->crst);
2563 else
2565 setALContext(This->ctx);
2566 alSourcef(This->source, AL_REFERENCE_DISTANCE, mindist);
2567 checkALError();
2568 popALContext();
2571 return S_OK;
2574 static HRESULT WINAPI DS8Buffer3D_SetMode(IDirectSound3DBuffer *iface, DWORD mode, DWORD apply)
2576 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2578 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u)\n", This, mode, apply);
2579 if(mode != DS3DMODE_NORMAL && mode != DS3DMODE_HEADRELATIVE &&
2580 mode != DS3DMODE_DISABLE)
2582 WARN("Invalid mode (%"LONGFMT"u)\n", mode);
2583 return DSERR_INVALIDPARAM;
2586 EnterCriticalSection(This->crst);
2587 if(apply == DS3D_DEFERRED)
2589 This->params.dwMode = mode;
2590 This->dirty.bit.mode = 1;
2592 else
2594 setALContext(This->ctx);
2595 alSourcei(This->source, AL_SOURCE_RELATIVE,
2596 (mode != DS3DMODE_NORMAL) ? AL_TRUE : AL_FALSE);
2597 alSourcef(This->source, AL_ROLLOFF_FACTOR,
2598 (mode == DS3DMODE_DISABLE) ? 0.0f : This->primary->rollofffactor);
2599 This->ds3dmode = mode;
2600 checkALError();
2601 popALContext();
2603 LeaveCriticalSection(This->crst);
2605 return S_OK;
2608 static HRESULT WINAPI DS8Buffer3D_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
2610 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2612 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", This, x, y, z, apply);
2614 if(apply == DS3D_DEFERRED)
2616 EnterCriticalSection(This->crst);
2617 This->params.vPosition.x = x;
2618 This->params.vPosition.y = y;
2619 This->params.vPosition.z = z;
2620 This->dirty.bit.pos = 1;
2621 LeaveCriticalSection(This->crst);
2623 else
2625 setALContext(This->ctx);
2626 alSource3f(This->source, AL_POSITION, x, y, -z);
2627 checkALError();
2628 popALContext();
2631 return S_OK;
2634 static HRESULT WINAPI DS8Buffer3D_SetVelocity(IDirectSound3DBuffer *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
2636 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2638 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", This, x, y, z, apply);
2640 if(apply == DS3D_DEFERRED)
2642 EnterCriticalSection(This->crst);
2643 This->params.vVelocity.x = x;
2644 This->params.vVelocity.y = y;
2645 This->params.vVelocity.z = z;
2646 This->dirty.bit.vel = 1;
2647 LeaveCriticalSection(This->crst);
2649 else
2651 setALContext(This->ctx);
2652 alSource3f(This->source, AL_VELOCITY, x, y, -z);
2653 checkALError();
2654 popALContext();
2657 return S_OK;
2660 static const IDirectSound3DBufferVtbl DS8Buffer3d_Vtbl =
2662 DS8Buffer3D_QueryInterface,
2663 DS8Buffer3D_AddRef,
2664 DS8Buffer3D_Release,
2665 DS8Buffer3D_GetAllParameters,
2666 DS8Buffer3D_GetConeAngles,
2667 DS8Buffer3D_GetConeOrientation,
2668 DS8Buffer3D_GetConeOutsideVolume,
2669 DS8Buffer3D_GetMaxDistance,
2670 DS8Buffer3D_GetMinDistance,
2671 DS8Buffer3D_GetMode,
2672 DS8Buffer3D_GetPosition,
2673 DS8Buffer3D_GetVelocity,
2674 DS8Buffer3D_SetAllParameters,
2675 DS8Buffer3D_SetConeAngles,
2676 DS8Buffer3D_SetConeOrientation,
2677 DS8Buffer3D_SetConeOutsideVolume,
2678 DS8Buffer3D_SetMaxDistance,
2679 DS8Buffer3D_SetMinDistance,
2680 DS8Buffer3D_SetMode,
2681 DS8Buffer3D_SetPosition,
2682 DS8Buffer3D_SetVelocity
2686 static HRESULT WINAPI DS8BufferNot_QueryInterface(IDirectSoundNotify *iface, REFIID riid, void **ppv)
2688 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2689 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
2692 static ULONG WINAPI DS8BufferNot_AddRef(IDirectSoundNotify *iface)
2694 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2695 LONG ret;
2697 InterlockedIncrement(&This->all_ref);
2698 ret = InterlockedIncrement(&This->not_ref);
2699 TRACE("new refcount %"LONGFMT"d\n", ret);
2701 return ret;
2704 static ULONG WINAPI DS8BufferNot_Release(IDirectSoundNotify *iface)
2706 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2707 LONG ret;
2709 ret = InterlockedDecrement(&This->not_ref);
2710 TRACE("new refcount %"LONGFMT"d\n", ret);
2711 if(InterlockedDecrement(&This->all_ref) == 0)
2712 DS8Buffer_Destroy(This);
2714 return ret;
2717 static HRESULT WINAPI DS8BufferNot_SetNotificationPositions(IDirectSoundNotify *iface, DWORD count, const DSBPOSITIONNOTIFY *notifications)
2719 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2720 DSBPOSITIONNOTIFY *nots;
2721 DWORD state;
2722 HRESULT hr;
2724 EnterCriticalSection(This->crst);
2725 hr = DSERR_INVALIDPARAM;
2726 if(count && !notifications)
2727 goto out;
2729 hr = IDirectSoundBuffer8_GetStatus(&This->IDirectSoundBuffer8_iface, &state);
2730 if(FAILED(hr))
2731 goto out;
2733 hr = DSERR_INVALIDCALL;
2734 if((state&DSBSTATUS_PLAYING))
2735 goto out;
2737 if(!count)
2739 HeapFree(GetProcessHeap(), 0, This->notify);
2740 This->notify = 0;
2741 This->nnotify = 0;
2742 hr = S_OK;
2744 else
2746 DWORD i;
2748 hr = DSERR_INVALIDPARAM;
2749 for(i = 0;i < count;++i)
2751 if(notifications[i].dwOffset >= This->buffer->buf_size &&
2752 notifications[i].dwOffset != (DWORD)DSBPN_OFFSETSTOP)
2753 goto out;
2756 hr = E_OUTOFMEMORY;
2757 nots = HeapAlloc(GetProcessHeap(), 0, count*sizeof(*nots));
2758 if(!nots)
2759 goto out;
2760 memcpy(nots, notifications, count*sizeof(*nots));
2762 HeapFree(GetProcessHeap(), 0, This->notify);
2763 This->notify = nots;
2764 This->nnotify = count;
2766 hr = S_OK;
2769 out:
2770 LeaveCriticalSection(This->crst);
2771 return hr;
2774 static const IDirectSoundNotifyVtbl DS8BufferNot_Vtbl =
2776 DS8BufferNot_QueryInterface,
2777 DS8BufferNot_AddRef,
2778 DS8BufferNot_Release,
2779 DS8BufferNot_SetNotificationPositions
2783 static HRESULT WINAPI DS8BufferProp_QueryInterface(IKsPropertySet *iface, REFIID riid, void **ppv)
2785 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2786 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
2789 static ULONG WINAPI DS8BufferProp_AddRef(IKsPropertySet *iface)
2791 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2792 LONG ret;
2794 InterlockedIncrement(&This->all_ref);
2795 ret = InterlockedIncrement(&This->prop_ref);
2796 TRACE("new refcount %"LONGFMT"d\n", ret);
2798 return ret;
2801 static ULONG WINAPI DS8BufferProp_Release(IKsPropertySet *iface)
2803 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2804 LONG ret;
2806 ret = InterlockedDecrement(&This->prop_ref);
2807 TRACE("new refcount %"LONGFMT"d\n", ret);
2808 if(InterlockedDecrement(&This->all_ref) == 0)
2809 DS8Buffer_Destroy(This);
2811 return ret;
2814 /* NOTE: Due to some apparent quirks in DSound, the listener properties are
2815 handled through secondary buffers. */
2816 static HRESULT WINAPI DS8BufferProp_Get(IKsPropertySet *iface,
2817 REFGUID guidPropSet, ULONG dwPropID,
2818 LPVOID pInstanceData, ULONG cbInstanceData,
2819 LPVOID pPropData, ULONG cbPropData,
2820 PULONG pcbReturned)
2822 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2823 HRESULT hr = E_PROP_ID_UNSUPPORTED;
2825 TRACE("(%p)->(%s, %"LONGFMT"u, %p, %"LONGFMT"u, %p, %"LONGFMT"u, %p)\n", iface, debugstr_guid(guidPropSet),
2826 dwPropID, pInstanceData, cbInstanceData, pPropData, cbPropData, pcbReturned);
2828 if(!pcbReturned)
2829 return E_POINTER;
2830 *pcbReturned = 0;
2832 #if 0
2833 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_BufferProperties))
2836 else
2837 #endif
2839 /* Not a known buffer/source property. Pass it to the listener */
2840 hr = IKsPropertySet_Get(&This->primary->IKsPropertySet_iface, guidPropSet,
2841 dwPropID, pInstanceData, cbInstanceData, pPropData, cbPropData,
2842 pcbReturned);
2845 return hr;
2848 static HRESULT WINAPI DS8BufferProp_Set(IKsPropertySet *iface,
2849 REFGUID guidPropSet, ULONG dwPropID,
2850 LPVOID pInstanceData, ULONG cbInstanceData,
2851 LPVOID pPropData, ULONG cbPropData)
2853 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2854 HRESULT hr = E_PROP_ID_UNSUPPORTED;
2856 TRACE("(%p)->(%s, %"LONGFMT"u, %p, %"LONGFMT"u, %p, %"LONGFMT"u)\n", iface, debugstr_guid(guidPropSet),
2857 dwPropID, pInstanceData, cbInstanceData, pPropData, cbPropData);
2859 #if 0
2860 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_BufferProperties))
2863 else
2864 #endif
2866 /* Not a known buffer/source property. Pass it to the listener */
2867 hr = IKsPropertySet_Set(&This->primary->IKsPropertySet_iface, guidPropSet,
2868 dwPropID, pInstanceData, cbInstanceData, pPropData,
2869 cbPropData);
2872 return hr;
2875 static HRESULT WINAPI DS8BufferProp_QuerySupport(IKsPropertySet *iface,
2876 REFGUID guidPropSet, ULONG dwPropID,
2877 PULONG pTypeSupport)
2879 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2880 HRESULT hr = E_PROP_ID_UNSUPPORTED;
2882 TRACE("(%p)->(%s, %"LONGFMT"u, %p)\n", iface, debugstr_guid(guidPropSet), dwPropID, pTypeSupport);
2884 if(!pTypeSupport)
2885 return E_POINTER;
2886 *pTypeSupport = 0;
2888 #if 0
2889 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_BufferProperties))
2892 else
2893 #endif
2895 /* Not a known buffer/source property. Pass it to the listener */
2896 hr = IKsPropertySet_QuerySupport(&This->primary->IKsPropertySet_iface,
2897 guidPropSet, dwPropID, pTypeSupport);
2900 return hr;
2903 static const IKsPropertySetVtbl DS8BufferProp_Vtbl =
2905 DS8BufferProp_QueryInterface,
2906 DS8BufferProp_AddRef,
2907 DS8BufferProp_Release,
2908 DS8BufferProp_Get,
2909 DS8BufferProp_Set,
2910 DS8BufferProp_QuerySupport