Rename the primary buffer's ThreadProc
[dsound-openal.git] / buffer.c
blobbfff44e6467f91228dda651dce9bf76dc218dd35
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 EnterCriticalSection(This->crst);
1004 setALContext(This->ctx);
1006 if(This->buffer->numsegs > 1)
1008 ALint queued = QBUFFERS;
1009 alGetSourcei(This->source, AL_BUFFERS_QUEUED, &queued);
1010 checkALError();
1012 pos = (This->curidx+This->buffer->numsegs-queued)%This->buffer->numsegs;
1013 pos *= This->buffer->segsize;
1014 writecursor = This->curidx * This->buffer->segsize;
1016 else if(This->primary->SupportedExt[SOFT_BUFFER_SUB_DATA] ||
1017 This->primary->SupportedExt[SOFT_BUFFER_SAMPLES])
1019 ALint rwpos[2] = { 0, 0 };
1021 alGetSourceiv(This->source, AL_BYTE_RW_OFFSETS_SOFT, rwpos);
1022 checkALError();
1024 pos = rwpos[0];
1025 writecursor = rwpos[1];
1027 else
1029 ALint status = 0;
1030 ALint ofs = 0;
1032 alGetSourcei(This->source, AL_BYTE_OFFSET, &ofs);
1033 alGetSourcei(This->source, AL_SOURCE_STATE, &status);
1034 checkALError();
1036 pos = ofs;
1037 if(status == AL_PLAYING)
1039 writecursor = format->nSamplesPerSec / 100;
1040 writecursor *= format->nBlockAlign;
1042 else
1043 writecursor = 0;
1044 writecursor = (writecursor + pos) % This->buffer->buf_size;
1046 TRACE("%p Play pos = %u, write pos = %u\n", This, pos, writecursor);
1047 if(pos >= This->buffer->buf_size)
1049 ERR("playpos >= buf_size\n");
1050 pos %= This->buffer->buf_size;
1052 if(writecursor >= This->buffer->buf_size)
1054 ERR("writepos >= buf_size\n");
1055 writecursor %= This->buffer->buf_size;
1058 if(playpos) *playpos = pos;
1059 if(curpos) *curpos = writecursor;
1061 popALContext();
1062 LeaveCriticalSection(This->crst);
1064 return S_OK;
1067 static HRESULT WINAPI DS8Buffer_GetFormat(IDirectSoundBuffer8 *iface, WAVEFORMATEX *wfx, DWORD allocated, DWORD *written)
1069 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1070 HRESULT hr = S_OK;
1071 UINT size;
1073 TRACE("(%p)->(%p, %"LONGFMT"u, %p)\n", iface, wfx, allocated, written);
1075 if(!wfx && !written)
1077 WARN("Cannot report format or format size\n");
1078 return DSERR_INVALIDPARAM;
1081 size = sizeof(This->buffer->format.Format) + This->buffer->format.Format.cbSize;
1082 if(wfx)
1084 if(allocated < size)
1085 hr = DSERR_INVALIDPARAM;
1086 else
1087 memcpy(wfx, &This->buffer->format.Format, size);
1089 if(written)
1090 *written = size;
1092 return hr;
1095 static HRESULT WINAPI DS8Buffer_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
1097 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1098 HRESULT hr;
1100 TRACE("(%p)->(%p)\n", iface, vol);
1102 if(!vol)
1104 WARN("Invalid pointer\n");
1105 return DSERR_INVALIDPARAM;
1108 hr = DSERR_CONTROLUNAVAIL;
1109 if(!(This->buffer->dsbflags&DSBCAPS_CTRLVOLUME))
1110 WARN("Volume control not set\n");
1111 else
1113 ALfloat gain = 1.0f;
1115 setALContext(This->ctx);
1116 alGetSourcef(This->source, AL_GAIN, &gain);
1117 checkALError();
1118 popALContext();
1120 *vol = clampI(gain_to_mB(gain), DSBVOLUME_MIN, DSBVOLUME_MAX);
1121 hr = DS_OK;
1124 return hr;
1127 static HRESULT WINAPI DS8Buffer_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
1129 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1130 HRESULT hr;
1132 TRACE("(%p)->(%p)\n", iface, pan);
1134 if(!pan)
1136 WARN("Invalid pointer\n");
1137 return DSERR_INVALIDPARAM;
1140 hr = DSERR_CONTROLUNAVAIL;
1141 if(!(This->buffer->dsbflags&DSBCAPS_CTRLPAN))
1142 WARN("Panning control not set\n");
1143 else
1145 ALfloat pos[3];
1147 setALContext(This->ctx);
1148 alGetSourcefv(This->source, AL_POSITION, pos);
1149 checkALError();
1150 popALContext();
1152 *pan = clampI(((pos[0]+1.0) * (DSBPAN_RIGHT-DSBPAN_LEFT) / 2.0 + 0.5) + DSBPAN_LEFT, DSBPAN_LEFT, DSBPAN_RIGHT);
1153 hr = DS_OK;
1156 return hr;
1159 static HRESULT WINAPI DS8Buffer_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
1161 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1162 HRESULT hr;
1164 TRACE("(%p)->(%p)\n", iface, freq);
1166 if(!freq)
1168 WARN("Invalid pointer\n");
1169 return DSERR_INVALIDPARAM;
1172 hr = DSERR_CONTROLUNAVAIL;
1173 if(!(This->buffer->dsbflags&DSBCAPS_CTRLFREQUENCY))
1174 WARN("Frequency control not set\n");
1175 else
1177 ALfloat pitch = 1.0f;
1179 setALContext(This->ctx);
1180 alGetSourcefv(This->source, AL_PITCH, &pitch);
1181 checkALError();
1182 popALContext();
1184 *freq = (DWORD)(This->buffer->format.Format.nSamplesPerSec * pitch);
1185 hr = DS_OK;
1188 return hr;
1191 static HRESULT WINAPI DS8Buffer_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
1193 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1194 ALint state, looping;
1196 TRACE("(%p)->(%p)\n", iface, status);
1198 if(!status)
1200 WARN("Invalid pointer\n");
1201 return DSERR_INVALIDPARAM;
1204 EnterCriticalSection(This->crst);
1206 setALContext(This->ctx);
1207 alGetSourcei(This->source, AL_SOURCE_STATE, &state);
1208 looping = This->islooping;
1209 if(This->buffer->numsegs == 1)
1210 alGetSourcei(This->source, AL_LOOPING, &looping);
1211 else if(state != AL_PLAYING)
1212 state = This->isplaying ? AL_PLAYING : AL_PAUSED;
1213 checkALError();
1214 popALContext();
1216 LeaveCriticalSection(This->crst);
1218 *status = 0;
1219 if((This->buffer->dsbflags&DSBCAPS_LOCDEFER))
1221 if((This->buffer->dsbflags&DSBCAPS_LOCSOFTWARE))
1222 *status |= DSBSTATUS_LOCSOFTWARE;
1223 else if((This->buffer->dsbflags&DSBCAPS_LOCHARDWARE))
1224 *status |= DSBSTATUS_LOCHARDWARE;
1226 if(state == AL_PLAYING)
1227 *status |= DSBSTATUS_PLAYING | (looping ? DSBSTATUS_LOOPING : 0);
1229 return S_OK;
1232 static HRESULT WINAPI DS8Buffer_Initialize(IDirectSoundBuffer8 *iface, IDirectSound *ds, const DSBUFFERDESC *desc)
1234 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1235 DS3DBUFFER *ds3dbuffer;
1236 HRESULT hr;
1238 TRACE("(%p)->(%p, %p)\n", iface, ds, desc);
1240 EnterCriticalSection(This->crst);
1241 setALContext(This->ctx);
1243 hr = DSERR_ALREADYINITIALIZED;
1244 if(This->source)
1245 goto out;
1247 if(!This->buffer)
1249 hr = DSERR_INVALIDPARAM;
1250 if(!desc)
1252 WARN("Missing DSound buffer description\n");
1253 goto out;
1255 if(!desc->lpwfxFormat)
1257 WARN("Missing buffer format (%p)\n", This);
1258 goto out;
1260 if((desc->dwFlags&DSBCAPS_CTRL3D) && desc->lpwfxFormat->nChannels != 1)
1262 if(This->primary->parent->is_8)
1264 /* DirectSoundBuffer8 objects aren't allowed non-mono 3D
1265 * buffers */
1266 WARN("Can't create multi-channel 3D buffers\n");
1267 goto out;
1269 ERR("Multi-channel 3D sounds are not spatialized\n");
1272 hr = DS8Data_Create(&This->buffer, desc, This->primary);
1273 if(FAILED(hr))
1274 goto out;
1275 else
1277 DS8Data *buf = This->buffer;
1279 if(buf->format.Format.wBitsPerSample == 8)
1280 memset(buf->data, 0x80, buf->buf_size);
1281 else
1282 memset(buf->data, 0x00, buf->buf_size);
1284 if(This->primary->SupportedExt[EXT_STATIC_BUFFER])
1285 This->ExtAL->BufferDataStatic(buf->buffers[0], buf->buf_format,
1286 buf->data, buf->buf_size,
1287 buf->format.Format.nSamplesPerSec);
1288 else if(This->primary->SupportedExt[SOFT_BUFFER_SAMPLES])
1289 This->ExtAL->BufferSamplesSOFT(buf->buffers[0],
1290 buf->format.Format.nSamplesPerSec, buf->buf_format,
1291 buf->buf_size/buf->format.Format.nBlockAlign,
1292 buf->in_chans, buf->in_type, buf->data);
1293 else if(This->primary->SupportedExt[SOFT_BUFFER_SUB_DATA])
1294 alBufferData(buf->buffers[0], buf->buf_format,
1295 buf->data, buf->buf_size,
1296 buf->format.Format.nSamplesPerSec);
1298 checkALError();
1301 hr = DSERR_GENERIC;
1302 if(This->primary->parent->share->nsources)
1304 This->source = This->primary->sources[--(This->primary->parent->share->nsources)];
1305 alSourcef(This->source, AL_GAIN, 1.0f);
1306 alSourcef(This->source, AL_PITCH, 1.0f);
1307 checkALError();
1309 else
1310 goto out;
1312 ds3dbuffer = &This->ds3dbuffer;
1313 ds3dbuffer->dwSize = sizeof(*ds3dbuffer);
1314 ds3dbuffer->vPosition.x = 0.0;
1315 ds3dbuffer->vPosition.y = 0.0;
1316 ds3dbuffer->vPosition.z = 0.0;
1317 ds3dbuffer->vVelocity.x = 0.0;
1318 ds3dbuffer->vVelocity.y = 0.0;
1319 ds3dbuffer->vVelocity.z = 0.0;
1320 ds3dbuffer->dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1321 ds3dbuffer->dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1322 ds3dbuffer->vConeOrientation.x = 0.0;
1323 ds3dbuffer->vConeOrientation.y = 0.0;
1324 ds3dbuffer->vConeOrientation.z = 1.0;
1325 ds3dbuffer->lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1326 ds3dbuffer->flMinDistance = DS3D_DEFAULTMINDISTANCE;
1327 ds3dbuffer->flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1328 ds3dbuffer->dwMode = DS3DMODE_NORMAL;
1330 if((This->buffer->dsbflags&DSBCAPS_CTRL3D))
1332 if(This->primary->auxslot != 0)
1334 alSource3i(This->source, AL_AUXILIARY_SEND_FILTER, This->primary->auxslot, 0, AL_FILTER_NULL);
1335 checkALError();
1338 hr = IDirectSound3DBuffer_SetAllParameters(&This->IDirectSound3DBuffer_iface, ds3dbuffer, DS3D_IMMEDIATE);
1339 if(FAILED(hr))
1341 ERR("SetAllParameters failed\n");
1342 goto out;
1345 else
1347 ALuint source = This->source;
1349 if(This->primary->auxslot != 0)
1351 /* Simple hack to make reverb affect non-3D sounds too */
1352 alSource3i(source, AL_AUXILIARY_SEND_FILTER, This->primary->auxslot, 0, AL_FILTER_NULL);
1353 /*alSource3i(source, AL_AUXILIARY_SEND_FILTER, 0, 0, AL_FILTER_NULL);*/
1356 /* Non-3D sources aren't distance attenuated */
1357 This->ds3dmode = DS3DMODE_DISABLE;
1358 alSource3f(source, AL_POSITION, 0.0f, 1.0f, 0.0f);
1359 alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
1360 alSource3f(source, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
1361 alSourcef(source, AL_CONE_OUTER_GAIN, 1.0f);
1362 alSourcef(source, AL_REFERENCE_DISTANCE, 1.0f);
1363 alSourcef(source, AL_MAX_DISTANCE, 1000.0f);
1364 alSourcef(source, AL_ROLLOFF_FACTOR, 0.0f);
1365 alSourcei(source, AL_CONE_INNER_ANGLE, 360);
1366 alSourcei(source, AL_CONE_OUTER_ANGLE, 360);
1367 alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
1368 checkALError();
1370 hr = S_OK;
1372 out:
1373 popALContext();
1374 LeaveCriticalSection(This->crst);
1376 return hr;
1379 static HRESULT WINAPI DS8Buffer_Lock(IDirectSoundBuffer8 *iface, DWORD ofs, DWORD bytes, void **ptr1, DWORD *len1, void **ptr2, DWORD *len2, DWORD flags)
1381 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1382 DWORD remain;
1384 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %p, %p, %p, %p, 0x%"LONGFMT"x)\n", This, ofs, bytes, ptr1, len1, ptr2, len2, flags);
1386 if(!ptr1 || !len1)
1388 WARN("Invalid pointer/len %p %p\n", ptr1, len1);
1389 return DSERR_INVALIDPARAM;
1392 *ptr1 = NULL;
1393 *len1 = 0;
1394 if(ptr2) *ptr2 = NULL;
1395 if(len2) *len2 = 0;
1397 if((flags&DSBLOCK_FROMWRITECURSOR))
1398 DS8Buffer_GetCurrentPosition(iface, NULL, &ofs);
1399 else if(ofs >= This->buffer->buf_size)
1401 WARN("Invalid ofs %"LONGFMT"u\n", ofs);
1402 return DSERR_INVALIDPARAM;
1404 if((flags&DSBLOCK_ENTIREBUFFER))
1405 bytes = This->buffer->buf_size;
1406 else if(bytes > This->buffer->buf_size)
1408 WARN("Invalid size %"LONGFMT"u\n", bytes);
1409 return DSERR_INVALIDPARAM;
1412 if(InterlockedExchange(&This->buffer->locked, TRUE) == TRUE)
1414 WARN("Already locked\n");
1415 return DSERR_INVALIDPARAM;
1418 *ptr1 = This->buffer->data + ofs;
1419 if(ofs+bytes >= This->buffer->buf_size)
1421 *len1 = This->buffer->buf_size - ofs;
1422 remain = bytes - *len1;
1424 else
1426 *len1 = bytes;
1427 remain = 0;
1430 if(ptr2 && len2 && remain)
1432 *ptr2 = This->buffer->data;
1433 *len2 = remain;
1436 return DS_OK;
1439 static HRESULT WINAPI DS8Buffer_Play(IDirectSoundBuffer8 *iface, DWORD res1, DWORD prio, DWORD flags)
1441 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1442 ALint type, state = AL_STOPPED;
1443 HRESULT hr;
1445 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %"LONGFMT"u\n", iface, res1, prio, flags);
1447 EnterCriticalSection(This->crst);
1448 setALContext(This->ctx);
1450 hr = DSERR_BUFFERLOST;
1451 if(This->bufferlost)
1453 WARN("Buffer %p lost\n", This);
1454 goto out;
1457 if((This->buffer->dsbflags&DSBCAPS_LOCDEFER))
1459 if(!(This->buffer->dsbflags&(DSBCAPS_LOCHARDWARE|DSBCAPS_LOCSOFTWARE)))
1461 if(flags & DSBPLAY_LOCSOFTWARE)
1462 This->buffer->dsbflags |= DSBCAPS_LOCSOFTWARE;
1463 else
1464 This->buffer->dsbflags |= DSBCAPS_LOCHARDWARE;
1467 else if(prio)
1469 ERR("Invalid priority set for non-deferred buffer %p, %"LONGFMT"u!\n", This->buffer, prio);
1470 hr = DSERR_INVALIDPARAM;
1471 goto out;
1474 alGetSourcei(This->source, AL_SOURCE_STATE, &state);
1475 if(This->buffer->numsegs > 1)
1477 This->islooping = !!(flags&DSBPLAY_LOOPING);
1478 if(state != AL_PLAYING && This->isplaying)
1479 state = AL_PLAYING;
1481 else
1483 alGetSourcei(This->source, AL_SOURCE_TYPE, &type);
1484 alSourcei(This->source, AL_LOOPING, (flags&DSBPLAY_LOOPING) ? AL_TRUE : AL_FALSE);
1486 checkALError();
1488 hr = S_OK;
1489 if(state == AL_PLAYING)
1490 goto out;
1492 /* alSourceQueueBuffers will implicitly set type to streaming */
1493 if(This->buffer->numsegs == 1)
1495 if(type != AL_STATIC)
1496 alSourcei(This->source, AL_BUFFER, This->buffer->buffers[0]);
1497 alSourcePlay(This->source);
1499 if(alGetError() != AL_NO_ERROR)
1501 ERR("Couldn't start source\n");
1502 This->curidx = (This->buffer->numsegs-1+This->curidx)%This->buffer->numsegs;
1503 alSourcei(This->source, AL_BUFFER, 0);
1504 checkALError();
1505 hr = DSERR_GENERIC;
1506 goto out;
1508 This->isplaying = TRUE;
1510 if(This->nnotify)
1512 DS8Buffer_addnotify(This);
1513 DS8Primary_starttimer(This->primary);
1515 else if(This->buffer->numsegs > 1)
1516 DS8Primary_starttimer(This->primary);
1518 out:
1519 popALContext();
1520 LeaveCriticalSection(This->crst);
1521 return hr;
1524 static HRESULT WINAPI DS8Buffer_SetCurrentPosition(IDirectSoundBuffer8 *iface, DWORD pos)
1526 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1528 TRACE("(%p)->(%"LONGFMT"u)\n", iface, pos);
1530 if(pos >= This->buffer->buf_size)
1531 return DSERR_INVALIDPARAM;
1533 EnterCriticalSection(This->crst);
1535 if(This->buffer->numsegs > 1)
1537 DS8Data *buf = This->buffer;
1538 This->curidx = pos/buf->segsize;
1539 if(This->curidx >= buf->numsegs)
1540 This->curidx = buf->numsegs - 1;
1541 if(This->isplaying)
1543 setALContext(This->ctx);
1544 /* Perform a flush, so the next timer update will restart at the
1545 * proper position */
1546 alSourceStop(This->source);
1547 alSourcei(This->source, AL_BUFFER, 0);
1548 checkALError();
1549 popALContext();
1552 else
1554 setALContext(This->ctx);
1555 alSourcei(This->source, AL_BYTE_OFFSET, pos);
1556 popALContext();
1558 This->lastpos = pos;
1560 LeaveCriticalSection(This->crst);
1561 return DS_OK;
1564 static HRESULT WINAPI DS8Buffer_SetFormat(IDirectSoundBuffer8 *iface, const WAVEFORMATEX *wfx)
1566 /* This call only works on primary buffers */
1567 WARN("(%p)->(%p)\n", iface, wfx);
1568 return DSERR_INVALIDCALL;
1571 static HRESULT WINAPI DS8Buffer_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
1573 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1574 HRESULT hr = S_OK;
1576 TRACE("(%p)->(%"LONGFMT"d)\n", iface, vol);
1578 if(vol > DSBVOLUME_MAX || vol < DSBVOLUME_MIN)
1580 WARN("Invalid volume (%"LONGFMT"d)\n", vol);
1581 return DSERR_INVALIDPARAM;
1584 if(!(This->buffer->dsbflags&DSBCAPS_CTRLVOLUME))
1585 hr = DSERR_CONTROLUNAVAIL;
1586 if(SUCCEEDED(hr))
1588 ALfloat fvol = mB_to_gain(vol);
1589 setALContext(This->ctx);
1590 alSourcef(This->source, AL_GAIN, fvol);
1591 popALContext();
1594 return hr;
1597 static HRESULT WINAPI DS8Buffer_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
1599 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1600 HRESULT hr = S_OK;
1602 TRACE("(%p)->(%"LONGFMT"d)\n", iface, pan);
1604 if(pan > DSBPAN_RIGHT || pan < DSBPAN_LEFT)
1606 WARN("invalid parameter: pan = %"LONGFMT"d\n", pan);
1607 return DSERR_INVALIDPARAM;
1610 if(!(This->buffer->dsbflags&DSBCAPS_CTRLPAN))
1611 hr = DSERR_CONTROLUNAVAIL;
1612 else
1614 ALfloat pos[3];
1615 pos[0] = (pan-DSBPAN_LEFT) * 2.0 / (ALfloat)(DSBPAN_RIGHT-DSBPAN_LEFT) - 1.0;
1616 /* NOTE: Strict movement along the X plane can cause the sound to jump
1617 * between left and right sharply. Using a curved path helps smooth it
1618 * out */
1619 pos[1] = sqrt(1.0 - pos[0]*pos[0]);
1620 pos[2] = 0.0;
1622 setALContext(This->ctx);
1623 alSourcefv(This->source, AL_POSITION, pos);
1624 checkALError();
1625 popALContext();
1627 if(pan != 0 && This->buffer->format.Format.nChannels > 1)
1628 FIXME("Panning for multi-channel buffers is not supported\n");
1631 return hr;
1634 static HRESULT WINAPI DS8Buffer_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
1636 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1637 HRESULT hr = S_OK;
1639 TRACE("(%p)->(%"LONGFMT"u)\n", iface, freq);
1641 if(freq < DSBFREQUENCY_MIN || freq > DSBFREQUENCY_MAX)
1643 WARN("invalid parameter: freq = %"LONGFMT"u\n", freq);
1644 return DSERR_INVALIDPARAM;
1647 if(!(This->buffer->dsbflags&DSBCAPS_CTRLFREQUENCY))
1648 hr = DSERR_CONTROLUNAVAIL;
1649 else
1651 ALfloat pitch = 1.0f;
1652 if(freq != DSBFREQUENCY_ORIGINAL)
1653 pitch = freq / (ALfloat)This->buffer->format.Format.nSamplesPerSec;
1655 setALContext(This->ctx);
1656 alSourcef(This->source, AL_PITCH, pitch);
1657 checkALError();
1658 popALContext();
1661 return hr;
1664 static HRESULT WINAPI DS8Buffer_Stop(IDirectSoundBuffer8 *iface)
1666 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1667 ALint state;
1669 TRACE("(%p)->()\n", iface);
1671 EnterCriticalSection(This->crst);
1672 setALContext(This->ctx);
1674 alSourcePause(This->source);
1675 checkALError();
1676 /* Mac OS X doesn't immediately report state change
1677 * if Play() is immediately called after Stop, this can be fatal,
1678 * the buffer would never be restarted
1680 do {
1681 state = AL_PAUSED;
1682 alGetSourcei(This->source, AL_SOURCE_STATE, &state);
1683 if(state != AL_PLAYING)
1684 break;
1685 Sleep(1);
1686 } while(1);
1688 This->isplaying = FALSE;
1690 popALContext();
1691 LeaveCriticalSection(This->crst);
1693 return S_OK;
1696 static HRESULT WINAPI DS8Buffer_Unlock(IDirectSoundBuffer8 *iface, void *ptr1, DWORD len1, void *ptr2, DWORD len2)
1698 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1699 DS8Data *buf = This->buffer;
1700 DWORD bufsize = buf->buf_size;
1701 DWORD_PTR ofs1, ofs2;
1702 DWORD_PTR boundary = (DWORD_PTR)buf->data;
1703 HRESULT hr;
1705 TRACE("(%p)->(%p, %"LONGFMT"u, %p, %"LONGFMT"u)\n", iface, ptr1, len1, ptr2, len2);
1707 if(InterlockedExchange(&This->buffer->locked, FALSE) == FALSE)
1709 WARN("Not locked\n");
1710 return DSERR_INVALIDPARAM;
1713 hr = DSERR_INVALIDPARAM;
1714 /* Make sure offset is between boundary and boundary + bufsize */
1715 ofs1 = (DWORD_PTR)ptr1;
1716 ofs2 = (DWORD_PTR)ptr2;
1717 if(ofs1 < boundary)
1718 goto out;
1719 if(ofs2 && ofs2 != boundary)
1720 goto out;
1721 ofs1 -= boundary;
1722 ofs2 = 0;
1723 if(bufsize-ofs1 < len1 || len2 > ofs1)
1724 goto out;
1725 if(!ptr2)
1726 len2 = 0;
1728 hr = DS_OK;
1729 if(!len1 && !len2)
1730 goto out;
1731 if(This->primary->SupportedExt[EXT_STATIC_BUFFER])
1732 goto out;
1734 setALContext(This->ctx);
1735 if(This->primary->SupportedExt[SOFT_BUFFER_SAMPLES])
1737 const WAVEFORMATEX *format = &buf->format.Format;
1739 ptr1 = (BYTE*)ptr1 - (ofs1%format->nBlockAlign);
1740 ofs1 /= format->nBlockAlign;
1741 len1 /= format->nBlockAlign;
1742 if(len1 > 0)
1743 This->ExtAL->BufferSubSamplesSOFT(buf->buffers[0], ofs1, len1,
1744 buf->in_chans, buf->in_type, ptr1);
1745 ptr2 = (BYTE*)ptr2 - (ofs2%format->nBlockAlign);
1746 ofs2 /= format->nBlockAlign;
1747 len2 /= format->nBlockAlign;
1748 if(len2 > 0)
1749 This->ExtAL->BufferSubSamplesSOFT(buf->buffers[0], ofs2, len2,
1750 buf->in_chans, buf->in_type, ptr2);
1751 checkALError();
1753 else if(This->primary->SupportedExt[SOFT_BUFFER_SUB_DATA])
1755 const WAVEFORMATEX *format = &buf->format.Format;
1757 len1 -= len1%format->nBlockAlign;
1758 if(len1 > 0)
1759 This->ExtAL->BufferSubData(buf->buffers[0], buf->buf_format, ptr1,
1760 ofs1, len1);
1761 len2 -= len2%format->nBlockAlign;
1762 if(len2 > 0)
1763 This->ExtAL->BufferSubData(buf->buffers[0], buf->buf_format, ptr2,
1764 ofs2, len2);
1765 checkALError();
1767 else
1769 alBufferData(buf->buffers[0], buf->buf_format,
1770 buf->data, buf->buf_size,
1771 buf->format.Format.nSamplesPerSec);
1772 checkALError();
1774 popALContext();
1776 out:
1777 if(hr != S_OK)
1778 WARN("Invalid parameters (0x%lx,%"LONGFMT"u) (%p,%"LONGFMT"u,%p,%"LONGFMT"u)\n", boundary, bufsize, ptr1, len1, ptr2, len2);
1779 return hr;
1782 static HRESULT WINAPI DS8Buffer_Restore(IDirectSoundBuffer8 *iface)
1784 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1785 HRESULT hr;
1787 TRACE("(%p)->()\n", iface);
1789 EnterCriticalSection(This->crst);
1790 if(This->primary->parent->prio_level < DSSCL_WRITEPRIMARY ||
1791 iface == This->primary->write_emu)
1793 This->bufferlost = 0;
1794 hr = S_OK;
1796 else
1797 hr = DSERR_BUFFERLOST;
1798 LeaveCriticalSection(This->crst);
1800 return hr;
1803 static HRESULT WINAPI DS8Buffer_SetFX(IDirectSoundBuffer8 *iface, DWORD fxcount, DSEFFECTDESC *desc, DWORD *rescodes)
1805 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1806 DWORD i;
1808 TRACE("(%p)->(%"LONGFMT"u, %p, %p)\n", This, fxcount, desc, rescodes);
1810 if(!(This->buffer->dsbflags&DSBCAPS_CTRLFX))
1812 WARN("FX control not set\n");
1813 return DSERR_CONTROLUNAVAIL;
1816 if(fxcount == 0)
1818 if(desc || rescodes)
1820 WARN("Non-NULL desc and/or result pointer specified with no effects.\n");
1821 return DSERR_INVALIDPARAM;
1824 /* No effects; we can handle that */
1825 return DS_OK;
1828 if(!desc || !rescodes)
1830 WARN("NULL desc and/or result pointer specified.\n");
1831 return DSERR_INVALIDPARAM;
1834 /* We don't (currently) handle DSound effects */
1835 for(i = 0;i < fxcount;++i)
1837 FIXME("Cannot handle effect: %s\n", debugstr_guid(&desc[i].guidDSFXClass));
1838 rescodes[i] = DSFXR_FAILED;
1841 return DS_INCOMPLETE;
1844 static HRESULT WINAPI DS8Buffer_AcquireResources(IDirectSoundBuffer8 *iface, DWORD flags, DWORD fxcount, DWORD *rescodes)
1846 DS8Buffer *This = impl_from_IDirectSoundBuffer8(iface);
1848 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %p)\n", This, flags, fxcount, rescodes);
1850 /* effects aren't supported at the moment.. */
1851 if(fxcount != 0 || rescodes)
1853 WARN("Non-zero effect count and/or result pointer specified with no effects.\n");
1854 return DSERR_INVALIDPARAM;
1857 EnterCriticalSection(This->crst);
1858 if((This->buffer->dsbflags&DSBCAPS_LOCDEFER))
1860 This->buffer->dsbflags &= ~(DSBCAPS_LOCSOFTWARE|DSBCAPS_LOCHARDWARE);
1861 if((flags&DSBPLAY_LOCSOFTWARE))
1862 This->buffer->dsbflags |= DSBCAPS_LOCSOFTWARE;
1863 else
1864 This->buffer->dsbflags |= DSBCAPS_LOCHARDWARE;
1866 LeaveCriticalSection(This->crst);
1868 return S_OK;
1871 static HRESULT WINAPI DS8Buffer_GetObjectInPath(IDirectSoundBuffer8 *iface, REFGUID guid, DWORD idx, REFGUID rguidiface, void **ppv)
1873 FIXME("(%p)->(%s, %"LONGFMT"u, %s, %p) : stub!\n", iface, debugstr_guid(guid), idx, debugstr_guid(rguidiface), ppv);
1874 return E_NOTIMPL;
1877 static const IDirectSoundBuffer8Vtbl DS8Buffer_Vtbl = {
1878 DS8Buffer_QueryInterface,
1879 DS8Buffer_AddRef,
1880 DS8Buffer_Release,
1881 DS8Buffer_GetCaps,
1882 DS8Buffer_GetCurrentPosition,
1883 DS8Buffer_GetFormat,
1884 DS8Buffer_GetVolume,
1885 DS8Buffer_GetPan,
1886 DS8Buffer_GetFrequency,
1887 DS8Buffer_GetStatus,
1888 DS8Buffer_Initialize,
1889 DS8Buffer_Lock,
1890 DS8Buffer_Play,
1891 DS8Buffer_SetCurrentPosition,
1892 DS8Buffer_SetFormat,
1893 DS8Buffer_SetVolume,
1894 DS8Buffer_SetPan,
1895 DS8Buffer_SetFrequency,
1896 DS8Buffer_Stop,
1897 DS8Buffer_Unlock,
1898 DS8Buffer_Restore,
1899 DS8Buffer_SetFX,
1900 DS8Buffer_AcquireResources,
1901 DS8Buffer_GetObjectInPath
1905 static HRESULT WINAPI DSBuffer_QueryInterface(IDirectSoundBuffer *iface, REFIID riid, void **ppv)
1907 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1908 return DS8Buffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
1911 static ULONG WINAPI DSBuffer_AddRef(IDirectSoundBuffer *iface)
1913 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1914 return DS8Buffer_AddRef(&This->IDirectSoundBuffer8_iface);
1917 static ULONG WINAPI DSBuffer_Release(IDirectSoundBuffer *iface)
1919 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1920 return DS8Buffer_Release(&This->IDirectSoundBuffer8_iface);
1923 static HRESULT WINAPI DSBuffer_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1925 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1926 return DS8Buffer_GetCaps(&This->IDirectSoundBuffer8_iface, caps);
1929 static HRESULT WINAPI DSBuffer_GetCurrentPosition(IDirectSoundBuffer *iface, DWORD *playpos, DWORD *curpos)
1931 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1932 return DS8Buffer_GetCurrentPosition(&This->IDirectSoundBuffer8_iface, playpos, curpos);
1935 static HRESULT WINAPI DSBuffer_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *wfx, DWORD allocated, DWORD *written)
1937 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1938 return DS8Buffer_GetFormat(&This->IDirectSoundBuffer8_iface, wfx, allocated, written);
1941 static HRESULT WINAPI DSBuffer_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
1943 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1944 return DS8Buffer_GetVolume(&This->IDirectSoundBuffer8_iface, vol);
1947 static HRESULT WINAPI DSBuffer_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1949 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1950 return DS8Buffer_GetPan(&This->IDirectSoundBuffer8_iface, pan);
1953 static HRESULT WINAPI DSBuffer_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1955 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1956 return DS8Buffer_GetFrequency(&This->IDirectSoundBuffer8_iface, freq);
1959 static HRESULT WINAPI DSBuffer_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
1961 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1962 return DS8Buffer_GetStatus(&This->IDirectSoundBuffer8_iface, status);
1965 static HRESULT WINAPI DSBuffer_Initialize(IDirectSoundBuffer *iface, IDirectSound *ds, const DSBUFFERDESC *desc)
1967 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1968 return DS8Buffer_Initialize(&This->IDirectSoundBuffer8_iface, ds, desc);
1971 static HRESULT WINAPI DSBuffer_Lock(IDirectSoundBuffer *iface, DWORD ofs, DWORD bytes, void **ptr1, DWORD *len1, void **ptr2, DWORD *len2, DWORD flags)
1973 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1974 return DS8Buffer_Lock(&This->IDirectSoundBuffer8_iface, ofs, bytes, ptr1, len1, ptr2, len2, flags);
1977 static HRESULT WINAPI DSBuffer_Play(IDirectSoundBuffer *iface, DWORD res1, DWORD prio, DWORD flags)
1979 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1980 return DS8Buffer_Play(&This->IDirectSoundBuffer8_iface, res1, prio, flags);
1983 static HRESULT WINAPI DSBuffer_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD pos)
1985 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1986 return DS8Buffer_SetCurrentPosition(&This->IDirectSoundBuffer8_iface, pos);
1989 static HRESULT WINAPI DSBuffer_SetFormat(IDirectSoundBuffer *iface, const WAVEFORMATEX *wfx)
1991 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1992 return DS8Buffer_SetFormat(&This->IDirectSoundBuffer8_iface, wfx);
1995 static HRESULT WINAPI DSBuffer_SetVolume(IDirectSoundBuffer *iface, LONG vol)
1997 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
1998 return DS8Buffer_SetVolume(&This->IDirectSoundBuffer8_iface, vol);
2001 static HRESULT WINAPI DSBuffer_SetPan(IDirectSoundBuffer *iface, LONG pan)
2003 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2004 return DS8Buffer_SetPan(&This->IDirectSoundBuffer8_iface, pan);
2007 static HRESULT WINAPI DSBuffer_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
2009 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2010 return DS8Buffer_SetFrequency(&This->IDirectSoundBuffer8_iface, freq);
2013 static HRESULT WINAPI DSBuffer_Stop(IDirectSoundBuffer *iface)
2015 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2016 return DS8Buffer_Stop(&This->IDirectSoundBuffer8_iface);
2019 static HRESULT WINAPI DSBuffer_Unlock(IDirectSoundBuffer *iface, void *ptr1, DWORD len1, void *ptr2, DWORD len2)
2021 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2022 return DS8Buffer_Unlock(&This->IDirectSoundBuffer8_iface, ptr1, len1, ptr2, len2);
2025 static HRESULT WINAPI DSBuffer_Restore(IDirectSoundBuffer *iface)
2027 DS8Buffer *This = impl_from_IDirectSoundBuffer(iface);
2028 return DS8Buffer_Restore(&This->IDirectSoundBuffer8_iface);
2031 static const IDirectSoundBufferVtbl DSBuffer_Vtbl = {
2032 DSBuffer_QueryInterface,
2033 DSBuffer_AddRef,
2034 DSBuffer_Release,
2035 DSBuffer_GetCaps,
2036 DSBuffer_GetCurrentPosition,
2037 DSBuffer_GetFormat,
2038 DSBuffer_GetVolume,
2039 DSBuffer_GetPan,
2040 DSBuffer_GetFrequency,
2041 DSBuffer_GetStatus,
2042 DSBuffer_Initialize,
2043 DSBuffer_Lock,
2044 DSBuffer_Play,
2045 DSBuffer_SetCurrentPosition,
2046 DSBuffer_SetFormat,
2047 DSBuffer_SetVolume,
2048 DSBuffer_SetPan,
2049 DSBuffer_SetFrequency,
2050 DSBuffer_Stop,
2051 DSBuffer_Unlock,
2052 DSBuffer_Restore
2056 static HRESULT WINAPI DS8Buffer3D_QueryInterface(IDirectSound3DBuffer *iface, REFIID riid, void **ppv)
2058 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2059 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
2062 static ULONG WINAPI DS8Buffer3D_AddRef(IDirectSound3DBuffer *iface)
2064 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2065 LONG ret;
2067 InterlockedIncrement(&This->all_ref);
2068 ret = InterlockedIncrement(&This->ds3d_ref);
2069 TRACE("new refcount %"LONGFMT"d\n", ret);
2071 return ret;
2074 static ULONG WINAPI DS8Buffer3D_Release(IDirectSound3DBuffer *iface)
2076 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2077 LONG ret;
2079 ret = InterlockedDecrement(&This->ds3d_ref);
2080 TRACE("new refcount %"LONGFMT"d\n", ret);
2081 if(InterlockedDecrement(&This->all_ref) == 0)
2082 DS8Buffer_Destroy(This);
2084 return ret;
2087 static HRESULT WINAPI DS8Buffer3D_GetAllParameters(IDirectSound3DBuffer *iface, DS3DBUFFER *ds3dbuffer)
2089 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2090 DS3DBUFFER ds3dbuf;
2091 HRESULT hr;
2093 TRACE("%p\n", This);
2095 if(!ds3dbuffer || ds3dbuffer->dwSize < sizeof(*ds3dbuffer))
2097 WARN("Invalid parameters %p %"LONGFMT"u\n", ds3dbuffer, ds3dbuffer ? ds3dbuffer->dwSize : 0);
2098 return DSERR_INVALIDPARAM;
2100 ds3dbuf.dwSize = sizeof(ds3dbuf);
2102 EnterCriticalSection(This->crst);
2103 setALContext(This->ctx);
2105 hr = IDirectSound3DBuffer_GetPosition(iface, &ds3dbuf.vPosition);
2106 if(SUCCEEDED(hr))
2107 hr = IDirectSound3DBuffer_GetVelocity(iface, &ds3dbuf.vVelocity);
2108 if(SUCCEEDED(hr))
2109 hr = IDirectSound3DBuffer_GetConeAngles(iface, &ds3dbuf.dwInsideConeAngle, &ds3dbuf.dwOutsideConeAngle);
2110 if(SUCCEEDED(hr))
2111 hr = IDirectSound3DBuffer_GetConeOrientation(iface, &ds3dbuf.vConeOrientation);
2112 if(SUCCEEDED(hr))
2113 hr = IDirectSound3DBuffer_GetConeOutsideVolume(iface, &ds3dbuf.lConeOutsideVolume);
2114 if(SUCCEEDED(hr))
2115 hr = IDirectSound3DBuffer_GetMinDistance(iface, &ds3dbuf.flMinDistance);
2116 if(SUCCEEDED(hr))
2117 hr = IDirectSound3DBuffer_GetMaxDistance(iface, &ds3dbuf.flMaxDistance);
2118 if(SUCCEEDED(hr))
2119 hr = IDirectSound3DBuffer_GetMode(iface, &ds3dbuf.dwMode);
2120 if(SUCCEEDED(hr))
2121 memcpy(ds3dbuffer, &ds3dbuf, sizeof(ds3dbuf));
2123 popALContext();
2124 LeaveCriticalSection(This->crst);
2126 return hr;
2129 static HRESULT WINAPI DS8Buffer3D_GetConeAngles(IDirectSound3DBuffer *iface, DWORD *pdwInsideConeAngle, DWORD *pdwOutsideConeAngle)
2131 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2132 ALint inangle, outangle;
2134 TRACE("(%p)->(%p, %p)\n", This, pdwInsideConeAngle, pdwOutsideConeAngle);
2135 if(!pdwInsideConeAngle || !pdwOutsideConeAngle)
2137 WARN("Invalid pointers (%p, %p)\n", pdwInsideConeAngle, pdwOutsideConeAngle);
2138 return DSERR_INVALIDPARAM;
2141 EnterCriticalSection(This->crst);
2142 setALContext(This->ctx);
2144 alGetSourcei(This->source, AL_CONE_INNER_ANGLE, &inangle);
2145 alGetSourcei(This->source, AL_CONE_OUTER_ANGLE, &outangle);
2146 checkALError();
2148 popALContext();
2149 LeaveCriticalSection(This->crst);
2151 *pdwInsideConeAngle = inangle;
2152 *pdwOutsideConeAngle = outangle;
2153 return S_OK;
2156 static HRESULT WINAPI DS8Buffer3D_GetConeOrientation(IDirectSound3DBuffer *iface, D3DVECTOR *orient)
2158 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2159 ALfloat dir[3];
2161 TRACE("(%p)->(%p)\n", This, orient);
2162 if(!orient)
2164 WARN("Invalid pointer\n");
2165 return DSERR_INVALIDPARAM;
2168 setALContext(This->ctx);
2169 alGetSourcefv(This->source, AL_DIRECTION, dir);
2170 checkALError();
2171 popALContext();
2173 orient->x = dir[0];
2174 orient->y = dir[1];
2175 orient->z = -dir[2];
2176 return S_OK;
2179 static HRESULT WINAPI DS8Buffer3D_GetConeOutsideVolume(IDirectSound3DBuffer *iface, LONG *vol)
2181 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2182 ALfloat gain;
2184 TRACE("(%p)->(%p)\n", This, vol);
2185 if(!vol)
2187 WARN("Invalid pointer\n");
2188 return DSERR_INVALIDPARAM;
2191 setALContext(This->ctx);
2192 alGetSourcef(This->source, AL_CONE_OUTER_GAIN, &gain);
2193 checkALError();
2194 popALContext();
2196 *vol = clampI(gain_to_mB(gain), DSBVOLUME_MIN, DSBVOLUME_MAX);
2197 return S_OK;
2200 static HRESULT WINAPI DS8Buffer3D_GetMaxDistance(IDirectSound3DBuffer *iface, D3DVALUE *maxdist)
2202 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2203 ALfloat dist;
2205 TRACE("(%p)->(%p)\n", This, maxdist);
2206 if(!maxdist)
2208 WARN("Invalid pointer\n");
2209 return DSERR_INVALIDPARAM;
2212 setALContext(This->ctx);
2213 alGetSourcef(This->source, AL_MAX_DISTANCE, &dist);
2214 checkALError();
2215 popALContext();
2217 *maxdist = dist;
2218 return S_OK;
2221 static HRESULT WINAPI DS8Buffer3D_GetMinDistance(IDirectSound3DBuffer *iface, D3DVALUE *mindist)
2223 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2224 ALfloat dist;
2226 TRACE("(%p)->(%p)\n", This, mindist);
2227 if(!mindist)
2229 WARN("Invalid pointer\n");
2230 return DSERR_INVALIDPARAM;
2233 setALContext(This->ctx);
2234 alGetSourcef(This->source, AL_REFERENCE_DISTANCE, &dist);
2235 checkALError();
2236 popALContext();
2238 *mindist = dist;
2239 return S_OK;
2242 static HRESULT WINAPI DS8Buffer3D_GetMode(IDirectSound3DBuffer *iface, DWORD *mode)
2244 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2246 TRACE("(%p)->(%p)\n", This, mode);
2247 if(!mode)
2249 WARN("Invalid pointer\n");
2250 return DSERR_INVALIDPARAM;
2253 EnterCriticalSection(This->crst);
2254 *mode = This->ds3dmode;
2255 LeaveCriticalSection(This->crst);
2257 return S_OK;
2260 static HRESULT WINAPI DS8Buffer3D_GetPosition(IDirectSound3DBuffer *iface, D3DVECTOR *pos)
2262 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2263 ALfloat alpos[3];
2265 TRACE("(%p)->(%p)\n", This, pos);
2266 if(!pos)
2268 WARN("Invalid pointer\n");
2269 return DSERR_INVALIDPARAM;
2272 setALContext(This->ctx);
2273 alGetSourcefv(This->source, AL_POSITION, alpos);
2274 checkALError();
2275 popALContext();
2277 pos->x = alpos[0];
2278 pos->y = alpos[1];
2279 pos->z = -alpos[2];
2280 return S_OK;
2283 static HRESULT WINAPI DS8Buffer3D_GetVelocity(IDirectSound3DBuffer *iface, D3DVECTOR *vel)
2285 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2286 ALfloat alvel[3];
2288 TRACE("(%p)->(%p)\n", This, vel);
2289 if(!vel)
2291 WARN("Invalid pointer\n");
2292 return DSERR_INVALIDPARAM;
2295 setALContext(This->ctx);
2296 alGetSourcefv(This->source, AL_VELOCITY, alvel);
2297 checkALError();
2298 popALContext();
2300 vel->x = alvel[0];
2301 vel->y = alvel[1];
2302 vel->z = -alvel[2];
2303 return S_OK;
2306 static HRESULT WINAPI DS8Buffer3D_SetAllParameters(IDirectSound3DBuffer *iface, const DS3DBUFFER *ds3dbuffer, DWORD apply)
2308 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2309 TRACE("(%p)->(%p, %"LONGFMT"u)\n", This, ds3dbuffer, apply);
2311 if(!ds3dbuffer || ds3dbuffer->dwSize < sizeof(*ds3dbuffer))
2313 WARN("Invalid DS3DBUFFER (%p, %"LONGFMT"u)\n", ds3dbuffer, ds3dbuffer ? ds3dbuffer->dwSize : 0);
2314 return DSERR_INVALIDPARAM;
2317 if(ds3dbuffer->dwInsideConeAngle > DS3D_MAXCONEANGLE ||
2318 ds3dbuffer->dwOutsideConeAngle > DS3D_MAXCONEANGLE)
2320 WARN("Invalid cone angles (%"LONGFMT"u, %"LONGFMT"u)\n", ds3dbuffer->dwInsideConeAngle,
2321 ds3dbuffer->dwOutsideConeAngle);
2322 return DSERR_INVALIDPARAM;
2325 if(ds3dbuffer->lConeOutsideVolume > DSBVOLUME_MAX ||
2326 ds3dbuffer->lConeOutsideVolume < DSBVOLUME_MIN)
2328 WARN("Invalid cone outside volume (%"LONGFMT"d)\n", ds3dbuffer->lConeOutsideVolume);
2329 return DSERR_INVALIDPARAM;
2332 if(ds3dbuffer->flMaxDistance < 0.0f)
2334 WARN("Invalid max distance (%f)\n", ds3dbuffer->flMaxDistance);
2335 return DSERR_INVALIDPARAM;
2338 if(ds3dbuffer->flMinDistance < 0.0f)
2340 WARN("Invalid min distance (%f)\n", ds3dbuffer->flMinDistance);
2341 return DSERR_INVALIDPARAM;
2344 if(ds3dbuffer->dwMode != DS3DMODE_NORMAL &&
2345 ds3dbuffer->dwMode != DS3DMODE_HEADRELATIVE &&
2346 ds3dbuffer->dwMode != DS3DMODE_DISABLE)
2348 WARN("Invalid mode (%"LONGFMT"u)\n", ds3dbuffer->dwMode);
2349 return DSERR_INVALIDPARAM;
2352 EnterCriticalSection(This->crst);
2353 setALContext(This->ctx);
2354 IDirectSound3DBuffer_SetPosition(iface, ds3dbuffer->vPosition.x, ds3dbuffer->vPosition.y, ds3dbuffer->vPosition.z, apply);
2355 IDirectSound3DBuffer_SetVelocity(iface, ds3dbuffer->vVelocity.x, ds3dbuffer->vVelocity.y, ds3dbuffer->vVelocity.z, apply);
2356 IDirectSound3DBuffer_SetConeAngles(iface, ds3dbuffer->dwInsideConeAngle, ds3dbuffer->dwOutsideConeAngle, apply);
2357 IDirectSound3DBuffer_SetConeOrientation(iface, ds3dbuffer->vConeOrientation.x, ds3dbuffer->vConeOrientation.y, ds3dbuffer->vConeOrientation.z, apply);
2358 IDirectSound3DBuffer_SetConeOutsideVolume(iface, ds3dbuffer->lConeOutsideVolume, apply);
2359 IDirectSound3DBuffer_SetMinDistance(iface, ds3dbuffer->flMinDistance, apply);
2360 IDirectSound3DBuffer_SetMaxDistance(iface, ds3dbuffer->flMaxDistance, apply);
2361 IDirectSound3DBuffer_SetMode(iface, ds3dbuffer->dwMode, apply);
2362 popALContext();
2363 LeaveCriticalSection(This->crst);
2365 return S_OK;
2368 static HRESULT WINAPI DS8Buffer3D_SetConeAngles(IDirectSound3DBuffer *iface, DWORD dwInsideConeAngle, DWORD dwOutsideConeAngle, DWORD apply)
2370 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2372 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u, %"LONGFMT"u)\n", This, dwInsideConeAngle, dwOutsideConeAngle, apply);
2373 if(dwInsideConeAngle > DS3D_MAXCONEANGLE ||
2374 dwOutsideConeAngle > DS3D_MAXCONEANGLE)
2376 WARN("Invalid cone angles (%"LONGFMT"u, %"LONGFMT"u)\n", dwInsideConeAngle, dwOutsideConeAngle);
2377 return DSERR_INVALIDPARAM;
2380 EnterCriticalSection(This->crst);
2381 if(apply == DS3D_DEFERRED)
2383 This->ds3dbuffer.dwInsideConeAngle = dwInsideConeAngle;
2384 This->ds3dbuffer.dwOutsideConeAngle = dwOutsideConeAngle;
2385 This->dirty.bit.cone_angles = 1;
2387 else
2389 setALContext(This->ctx);
2390 alSourcei(This->source, AL_CONE_INNER_ANGLE, dwInsideConeAngle);
2391 alSourcei(This->source, AL_CONE_OUTER_ANGLE, dwOutsideConeAngle);
2392 checkALError();
2393 popALContext();
2395 LeaveCriticalSection(This->crst);
2397 return S_OK;
2400 static HRESULT WINAPI DS8Buffer3D_SetConeOrientation(IDirectSound3DBuffer *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
2402 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2404 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", This, x, y, z, apply);
2406 if(apply == DS3D_DEFERRED)
2408 EnterCriticalSection(This->crst);
2409 This->ds3dbuffer.vConeOrientation.x = x;
2410 This->ds3dbuffer.vConeOrientation.y = y;
2411 This->ds3dbuffer.vConeOrientation.z = z;
2412 This->dirty.bit.cone_orient = 1;
2413 LeaveCriticalSection(This->crst);
2415 else
2417 setALContext(This->ctx);
2418 alSource3f(This->source, AL_DIRECTION, x, y, -z);
2419 checkALError();
2420 popALContext();
2423 return S_OK;
2426 static HRESULT WINAPI DS8Buffer3D_SetConeOutsideVolume(IDirectSound3DBuffer *iface, LONG vol, DWORD apply)
2428 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2430 TRACE("(%p)->(%"LONGFMT"d, %"LONGFMT"u)\n", This, vol, apply);
2431 if(vol < DSBVOLUME_MIN || vol > DSBVOLUME_MAX)
2433 WARN("Invalid volume (%"LONGFMT"d)\n", vol);
2434 return DSERR_INVALIDPARAM;
2437 if(apply == DS3D_DEFERRED)
2439 EnterCriticalSection(This->crst);
2440 This->ds3dbuffer.lConeOutsideVolume = vol;
2441 This->dirty.bit.cone_outsidevolume = 1;
2442 LeaveCriticalSection(This->crst);
2444 else
2446 setALContext(This->ctx);
2447 alSourcef(This->source, AL_CONE_OUTER_GAIN, mB_to_gain(vol));
2448 checkALError();
2449 popALContext();
2452 return S_OK;
2455 static HRESULT WINAPI DS8Buffer3D_SetMaxDistance(IDirectSound3DBuffer *iface, D3DVALUE maxdist, DWORD apply)
2457 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2459 TRACE("(%p)->(%f, %"LONGFMT"u)\n", This, maxdist, apply);
2460 if(maxdist < 0.0f)
2462 WARN("Invalid max distance (%f)\n", maxdist);
2463 return DSERR_INVALIDPARAM;
2466 if(apply == DS3D_DEFERRED)
2468 EnterCriticalSection(This->crst);
2469 This->ds3dbuffer.flMaxDistance = maxdist;
2470 This->dirty.bit.max_distance = 1;
2471 LeaveCriticalSection(This->crst);
2473 else
2475 setALContext(This->ctx);
2476 alSourcef(This->source, AL_MAX_DISTANCE, maxdist);
2477 checkALError();
2478 popALContext();
2481 return S_OK;
2484 static HRESULT WINAPI DS8Buffer3D_SetMinDistance(IDirectSound3DBuffer *iface, D3DVALUE mindist, DWORD apply)
2486 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2488 TRACE("(%p)->(%f, %"LONGFMT"u)\n", This, mindist, apply);
2489 if(mindist < 0.0f)
2491 WARN("Invalid min distance (%f)\n", mindist);
2492 return DSERR_INVALIDPARAM;
2495 if(apply == DS3D_DEFERRED)
2497 EnterCriticalSection(This->crst);
2498 This->ds3dbuffer.flMinDistance = mindist;
2499 This->dirty.bit.min_distance = 1;
2500 LeaveCriticalSection(This->crst);
2502 else
2504 setALContext(This->ctx);
2505 alSourcef(This->source, AL_REFERENCE_DISTANCE, mindist);
2506 checkALError();
2507 popALContext();
2510 return S_OK;
2513 static HRESULT WINAPI DS8Buffer3D_SetMode(IDirectSound3DBuffer *iface, DWORD mode, DWORD apply)
2515 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2517 TRACE("(%p)->(%"LONGFMT"u, %"LONGFMT"u)\n", This, mode, apply);
2518 if(mode != DS3DMODE_NORMAL && mode != DS3DMODE_HEADRELATIVE &&
2519 mode != DS3DMODE_DISABLE)
2521 WARN("Invalid mode (%"LONGFMT"u)\n", mode);
2522 return DSERR_INVALIDPARAM;
2525 EnterCriticalSection(This->crst);
2526 if(apply == DS3D_DEFERRED)
2528 This->ds3dbuffer.dwMode = mode;
2529 This->dirty.bit.mode = 1;
2531 else
2533 setALContext(This->ctx);
2534 alSourcei(This->source, AL_SOURCE_RELATIVE,
2535 (mode != DS3DMODE_NORMAL) ? AL_TRUE : AL_FALSE);
2536 alSourcef(This->source, AL_ROLLOFF_FACTOR,
2537 (mode == DS3DMODE_DISABLE) ? 0.0f : This->primary->rollofffactor);
2538 This->ds3dmode = mode;
2539 checkALError();
2540 popALContext();
2542 LeaveCriticalSection(This->crst);
2544 return S_OK;
2547 static HRESULT WINAPI DS8Buffer3D_SetPosition(IDirectSound3DBuffer *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
2549 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2551 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", This, x, y, z, apply);
2553 if(apply == DS3D_DEFERRED)
2555 EnterCriticalSection(This->crst);
2556 This->ds3dbuffer.vPosition.x = x;
2557 This->ds3dbuffer.vPosition.y = y;
2558 This->ds3dbuffer.vPosition.z = z;
2559 This->dirty.bit.pos = 1;
2560 LeaveCriticalSection(This->crst);
2562 else
2564 setALContext(This->ctx);
2565 alSource3f(This->source, AL_POSITION, x, y, -z);
2566 checkALError();
2567 popALContext();
2570 return S_OK;
2573 static HRESULT WINAPI DS8Buffer3D_SetVelocity(IDirectSound3DBuffer *iface, D3DVALUE x, D3DVALUE y, D3DVALUE z, DWORD apply)
2575 DS8Buffer *This = impl_from_IDirectSound3DBuffer(iface);
2577 TRACE("(%p)->(%f, %f, %f, %"LONGFMT"u)\n", This, x, y, z, apply);
2579 if(apply == DS3D_DEFERRED)
2581 EnterCriticalSection(This->crst);
2582 This->ds3dbuffer.vVelocity.x = x;
2583 This->ds3dbuffer.vVelocity.y = y;
2584 This->ds3dbuffer.vVelocity.z = z;
2585 This->dirty.bit.vel = 1;
2586 LeaveCriticalSection(This->crst);
2588 else
2590 setALContext(This->ctx);
2591 alSource3f(This->source, AL_VELOCITY, x, y, -z);
2592 checkALError();
2593 popALContext();
2596 return S_OK;
2599 static const IDirectSound3DBufferVtbl DS8Buffer3d_Vtbl =
2601 DS8Buffer3D_QueryInterface,
2602 DS8Buffer3D_AddRef,
2603 DS8Buffer3D_Release,
2604 DS8Buffer3D_GetAllParameters,
2605 DS8Buffer3D_GetConeAngles,
2606 DS8Buffer3D_GetConeOrientation,
2607 DS8Buffer3D_GetConeOutsideVolume,
2608 DS8Buffer3D_GetMaxDistance,
2609 DS8Buffer3D_GetMinDistance,
2610 DS8Buffer3D_GetMode,
2611 DS8Buffer3D_GetPosition,
2612 DS8Buffer3D_GetVelocity,
2613 DS8Buffer3D_SetAllParameters,
2614 DS8Buffer3D_SetConeAngles,
2615 DS8Buffer3D_SetConeOrientation,
2616 DS8Buffer3D_SetConeOutsideVolume,
2617 DS8Buffer3D_SetMaxDistance,
2618 DS8Buffer3D_SetMinDistance,
2619 DS8Buffer3D_SetMode,
2620 DS8Buffer3D_SetPosition,
2621 DS8Buffer3D_SetVelocity
2625 static HRESULT WINAPI DS8BufferNot_QueryInterface(IDirectSoundNotify *iface, REFIID riid, void **ppv)
2627 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2628 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
2631 static ULONG WINAPI DS8BufferNot_AddRef(IDirectSoundNotify *iface)
2633 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2634 LONG ret;
2636 InterlockedIncrement(&This->all_ref);
2637 ret = InterlockedIncrement(&This->not_ref);
2638 TRACE("new refcount %"LONGFMT"d\n", ret);
2640 return ret;
2643 static ULONG WINAPI DS8BufferNot_Release(IDirectSoundNotify *iface)
2645 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2646 LONG ret;
2648 ret = InterlockedDecrement(&This->not_ref);
2649 TRACE("new refcount %"LONGFMT"d\n", ret);
2650 if(InterlockedDecrement(&This->all_ref) == 0)
2651 DS8Buffer_Destroy(This);
2653 return ret;
2656 static HRESULT WINAPI DS8BufferNot_SetNotificationPositions(IDirectSoundNotify *iface, DWORD count, const DSBPOSITIONNOTIFY *notifications)
2658 DS8Buffer *This = impl_from_IDirectSoundNotify(iface);
2659 DSBPOSITIONNOTIFY *nots;
2660 DWORD state;
2661 HRESULT hr;
2663 EnterCriticalSection(This->crst);
2664 hr = DSERR_INVALIDPARAM;
2665 if(count && !notifications)
2666 goto out;
2668 hr = IDirectSoundBuffer8_GetStatus(&This->IDirectSoundBuffer8_iface, &state);
2669 if(FAILED(hr))
2670 goto out;
2672 hr = DSERR_INVALIDCALL;
2673 if((state&DSBSTATUS_PLAYING))
2674 goto out;
2676 if(!count)
2678 HeapFree(GetProcessHeap(), 0, This->notify);
2679 This->notify = 0;
2680 This->nnotify = 0;
2681 hr = S_OK;
2683 else
2685 DWORD i;
2687 hr = DSERR_INVALIDPARAM;
2688 for(i = 0;i < count;++i)
2690 if(notifications[i].dwOffset >= This->buffer->buf_size &&
2691 notifications[i].dwOffset != (DWORD)DSBPN_OFFSETSTOP)
2692 goto out;
2695 hr = E_OUTOFMEMORY;
2696 nots = HeapAlloc(GetProcessHeap(), 0, count*sizeof(*nots));
2697 if(!nots)
2698 goto out;
2699 memcpy(nots, notifications, count*sizeof(*nots));
2701 HeapFree(GetProcessHeap(), 0, This->notify);
2702 This->notify = nots;
2703 This->nnotify = count;
2705 hr = S_OK;
2708 out:
2709 LeaveCriticalSection(This->crst);
2710 return hr;
2713 static const IDirectSoundNotifyVtbl DS8BufferNot_Vtbl =
2715 DS8BufferNot_QueryInterface,
2716 DS8BufferNot_AddRef,
2717 DS8BufferNot_Release,
2718 DS8BufferNot_SetNotificationPositions
2722 static HRESULT WINAPI DS8BufferProp_QueryInterface(IKsPropertySet *iface, REFIID riid, void **ppv)
2724 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2725 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppv);
2728 static ULONG WINAPI DS8BufferProp_AddRef(IKsPropertySet *iface)
2730 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2731 LONG ret;
2733 InterlockedIncrement(&This->all_ref);
2734 ret = InterlockedIncrement(&This->prop_ref);
2735 TRACE("new refcount %"LONGFMT"d\n", ret);
2737 return ret;
2740 static ULONG WINAPI DS8BufferProp_Release(IKsPropertySet *iface)
2742 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2743 LONG ret;
2745 ret = InterlockedDecrement(&This->prop_ref);
2746 TRACE("new refcount %"LONGFMT"d\n", ret);
2747 if(InterlockedDecrement(&This->all_ref) == 0)
2748 DS8Buffer_Destroy(This);
2750 return ret;
2753 /* NOTE: Due to some apparent quirks in DSound, the listener properties are
2754 handled through secondary buffers. */
2755 static HRESULT WINAPI DS8BufferProp_Get(IKsPropertySet *iface,
2756 REFGUID guidPropSet, ULONG dwPropID,
2757 LPVOID pInstanceData, ULONG cbInstanceData,
2758 LPVOID pPropData, ULONG cbPropData,
2759 PULONG pcbReturned)
2761 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2762 HRESULT hr = E_PROP_ID_UNSUPPORTED;
2764 TRACE("(%p)->(%s, %"LONGFMT"u, %p, %"LONGFMT"u, %p, %"LONGFMT"u, %p)\n", iface, debugstr_guid(guidPropSet),
2765 dwPropID, pInstanceData, cbInstanceData, pPropData, cbPropData, pcbReturned);
2767 if(!pcbReturned)
2768 return E_POINTER;
2769 *pcbReturned = 0;
2771 #if 0
2772 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_BufferProperties))
2775 else
2776 #endif
2778 /* Not a known buffer/source property. Pass it to the listener */
2779 hr = IKsPropertySet_Get(&This->primary->IKsPropertySet_iface, guidPropSet,
2780 dwPropID, pInstanceData, cbInstanceData, pPropData, cbPropData,
2781 pcbReturned);
2784 return hr;
2787 static HRESULT WINAPI DS8BufferProp_Set(IKsPropertySet *iface,
2788 REFGUID guidPropSet, ULONG dwPropID,
2789 LPVOID pInstanceData, ULONG cbInstanceData,
2790 LPVOID pPropData, ULONG cbPropData)
2792 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2793 HRESULT hr = E_PROP_ID_UNSUPPORTED;
2795 TRACE("(%p)->(%s, %"LONGFMT"u, %p, %"LONGFMT"u, %p, %"LONGFMT"u)\n", iface, debugstr_guid(guidPropSet),
2796 dwPropID, pInstanceData, cbInstanceData, pPropData, cbPropData);
2798 #if 0
2799 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_BufferProperties))
2802 else
2803 #endif
2805 /* Not a known buffer/source property. Pass it to the listener */
2806 hr = IKsPropertySet_Set(&This->primary->IKsPropertySet_iface, guidPropSet,
2807 dwPropID, pInstanceData, cbInstanceData, pPropData,
2808 cbPropData);
2811 return hr;
2814 static HRESULT WINAPI DS8BufferProp_QuerySupport(IKsPropertySet *iface,
2815 REFGUID guidPropSet, ULONG dwPropID,
2816 PULONG pTypeSupport)
2818 DS8Buffer *This = impl_from_IKsPropertySet(iface);
2819 HRESULT hr = E_PROP_ID_UNSUPPORTED;
2821 TRACE("(%p)->(%s, %"LONGFMT"u, %p)\n", iface, debugstr_guid(guidPropSet), dwPropID, pTypeSupport);
2823 if(!pTypeSupport)
2824 return E_POINTER;
2825 *pTypeSupport = 0;
2827 #if 0
2828 if(IsEqualIID(guidPropSet, &DSPROPSETID_EAX20_BufferProperties))
2831 else
2832 #endif
2834 /* Not a known buffer/source property. Pass it to the listener */
2835 hr = IKsPropertySet_QuerySupport(&This->primary->IKsPropertySet_iface,
2836 guidPropSet, dwPropID, pTypeSupport);
2839 return hr;
2842 static const IKsPropertySetVtbl DS8BufferProp_Vtbl =
2844 DS8BufferProp_QueryInterface,
2845 DS8BufferProp_AddRef,
2846 DS8BufferProp_Release,
2847 DS8BufferProp_Get,
2848 DS8BufferProp_Set,
2849 DS8BufferProp_QuerySupport