2 * Copyright 1993 Martin Ayotte
3 * 1998-2002 Eric Pouech
4 * 2011 Andrew Eikum for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
43 #include "mmdeviceapi.h"
44 #include "audioclient.h"
45 #include "audiopolicy.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(winmm
);
51 /* FIXME: Should be localized */
52 static const WCHAR volumeW
[] = {'V','o','l','u','m','e',0};
53 static const WCHAR mastervolumeW
[] = {'M','a','s','t','e','r',' ','V','o','l',
55 static const WCHAR muteW
[] = {'M','u','t','e',0};
57 /* HWAVE (and HMIXER) format:
59 * XXXX... 1FDD DDDD IIII IIII
60 * X = unused (must be 0)
61 * 1 = the bit is set to 1, to avoid all-zero HWAVEs
62 * F = flow direction (0 = IN, 1 = OUT)
63 * D = index into g_out_mmdevices
64 * I = index in the mmdevice's devices array
66 * Two reasons that we don't just use pointers:
67 * - HWAVEs must fit into 16 bits for compatibility with old applications.
68 * - We must be able to identify bad devices without crashing.
71 #define MAX_DEVICES 256
73 typedef struct _WINMM_CBInfo
{
80 struct _WINMM_MMDevice
;
81 typedef struct _WINMM_MMDevice WINMM_MMDevice
;
83 typedef struct _WINMM_Device
{
92 IAudioRenderClient
*render
;
93 IAudioCaptureClient
*capture
;
95 IAudioStreamVolume
*volume
;
97 HACMSTREAM acm_handle
;
98 ACMSTREAMHEADER acm_hdr
;
101 WAVEHDR
*first
, *last
, *playing
, *loop_start
;
105 UINT32 bytes_per_frame
, samples_per_sec
, ofs_bytes
, played_frames
;
107 /* stored in frames of sample rate, *not* AC::GetFrequency */
108 UINT64 last_clock_pos
;
111 CRITICAL_SECTION lock
;
113 WINMM_MMDevice
*parent
;
116 struct _WINMM_MMDevice
{
117 WAVEOUTCAPSW out_caps
; /* must not be modified outside of WINMM_InitMMDevices*/
118 WAVEINCAPSW in_caps
; /* must not be modified outside of WINMM_InitMMDevices*/
121 ISimpleAudioVolume
*volume
;
125 /* HMIXER format is the same as the HWAVE format, but the I bits are
126 * replaced by the value of this counter, to keep each HMIXER unique */
129 CRITICAL_SECTION lock
;
131 WINMM_Device
*devices
[MAX_DEVICES
];
134 static WINMM_MMDevice
*g_out_mmdevices
;
135 static UINT g_outmmdevices_count
;
137 static WINMM_MMDevice
*g_in_mmdevices
;
138 static UINT g_inmmdevices_count
;
140 static IMMDeviceEnumerator
*g_devenum
;
142 static CRITICAL_SECTION g_devthread_lock
;
143 static CRITICAL_SECTION_DEBUG g_devthread_lock_debug
=
145 0, 0, &g_devthread_lock
,
146 { &g_devthread_lock_debug
.ProcessLocksList
, &g_devthread_lock_debug
.ProcessLocksList
},
147 0, 0, { (DWORD_PTR
)(__FILE__
": g_devthread_lock") }
149 static CRITICAL_SECTION g_devthread_lock
= { &g_devthread_lock_debug
, -1, 0, 0, 0, 0 };
150 static HANDLE g_devices_thread
;
151 static HWND g_devices_hwnd
;
153 static UINT g_devhandle_count
;
154 static HANDLE
*g_device_handles
;
155 static WINMM_Device
**g_handle_devices
;
157 typedef struct _WINMM_OpenInfo
{
160 WAVEFORMATEX
*format
;
166 typedef struct _WINMM_ControlDetails
{
168 MIXERCONTROLDETAILS
*details
;
170 } WINMM_ControlDetails
;
172 static LRESULT
WOD_Open(WINMM_OpenInfo
*info
);
173 static LRESULT
WOD_Close(HWAVEOUT hwave
);
174 static LRESULT
WID_Open(WINMM_OpenInfo
*info
);
175 static LRESULT
WID_Close(HWAVEIN hwave
);
177 void WINMM_DeleteWaveform(void)
179 /* FIXME: Free g_(in,out)_mmdevices? */
180 DeleteCriticalSection(&g_devthread_lock
);
183 static inline HWAVE
WINMM_MakeHWAVE(UINT mmdevice
, BOOL is_out
, UINT device
)
185 return ULongToHandle((1 << 15) | ((!!is_out
) << 14) |
186 (mmdevice
<< 8) | device
);
189 static inline void WINMM_DecomposeHWAVE(HWAVE hwave
, UINT
*mmdevice_index
,
190 BOOL
*is_out
, UINT
*device_index
, UINT
*junk
)
192 ULONG32 l
= HandleToULong(hwave
);
193 *device_index
= l
& 0xFF;
194 *mmdevice_index
= (l
>> 8) & 0x3F;
195 *is_out
= (l
>> 14) & 0x1;
199 static void WINMM_InitDevice(WINMM_Device
*device
,
200 WINMM_MMDevice
*parent
, HWAVE hwave
)
202 InitializeCriticalSection(&device
->lock
);
203 device
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
205 device
->handle
= hwave
;
206 device
->parent
= parent
;
209 /* finds the first unused Device, marks it as "open", and returns
210 * a pointer to the device
212 * IMPORTANT: it is the caller's responsibility to release the device's lock
215 static WINMM_Device
*WINMM_FindUnusedDevice(BOOL is_out
, UINT mmdevice_index
)
217 WINMM_MMDevice
*mmdevice
;
221 mmdevice
= &g_out_mmdevices
[mmdevice_index
];
223 mmdevice
= &g_in_mmdevices
[mmdevice_index
];
225 EnterCriticalSection(&mmdevice
->lock
);
226 for(i
= 0; i
< MAX_DEVICES
; ++i
){
227 WINMM_Device
*device
= mmdevice
->devices
[i
];
230 device
= mmdevice
->devices
[i
] = HeapAlloc(GetProcessHeap(),
231 HEAP_ZERO_MEMORY
, sizeof(WINMM_Device
));
233 LeaveCriticalSection(&mmdevice
->lock
);
237 WINMM_InitDevice(device
, mmdevice
,
238 WINMM_MakeHWAVE(mmdevice_index
, is_out
, i
));
239 EnterCriticalSection(&device
->lock
);
241 EnterCriticalSection(&device
->lock
);
244 LeaveCriticalSection(&mmdevice
->lock
);
246 TRACE("Found free device: mmdevice: %u, device id: %u\n",
251 LeaveCriticalSection(&device
->lock
);
254 LeaveCriticalSection(&mmdevice
->lock
);
256 TRACE("All devices in use: mmdevice: %u\n", mmdevice_index
);
261 static inline BOOL
WINMM_ValidateAndLock(WINMM_Device
*device
)
266 EnterCriticalSection(&device
->lock
);
269 LeaveCriticalSection(&device
->lock
);
276 static WINMM_Device
*WINMM_GetDeviceFromHWAVE(HWAVE hwave
)
278 WINMM_MMDevice
*mmdevice
;
279 WINMM_Device
*device
;
280 UINT mmdevice_index
, device_index
, junk
;
283 WINMM_DecomposeHWAVE(hwave
, &mmdevice_index
, &is_out
, &device_index
, &junk
);
288 if(mmdevice_index
>= (is_out
? g_outmmdevices_count
: g_inmmdevices_count
))
292 mmdevice
= &g_out_mmdevices
[mmdevice_index
];
294 mmdevice
= &g_in_mmdevices
[mmdevice_index
];
296 EnterCriticalSection(&mmdevice
->lock
);
298 device
= mmdevice
->devices
[device_index
];
300 LeaveCriticalSection(&mmdevice
->lock
);
305 /* Note: NotifyClient should never be called while holding the device lock
306 * since the client may call wave* functions from within the callback. */
307 static inline void WINMM_NotifyClient(WINMM_CBInfo
*info
, WORD msg
, DWORD_PTR param1
,
310 DriverCallback(info
->callback
, info
->flags
, (HDRVR
)info
->hwave
,
311 msg
, info
->user
, param1
, param2
);
314 static HRESULT
WINMM_GetFriendlyName(IMMDevice
*device
, WCHAR
*out
,
321 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
325 PropVariantInit(&var
);
327 hr
= IPropertyStore_GetValue(ps
,
328 (PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &var
);
330 IPropertyStore_Release(ps
);
334 lstrcpynW(out
, var
.u
.pwszVal
, outlen
);
336 PropVariantClear(&var
);
338 IPropertyStore_Release(ps
);
343 static HRESULT
WINMM_TestFormat(IAudioClient
*client
, DWORD rate
, DWORD depth
,
346 WAVEFORMATEX fmt
, *junk
;
349 fmt
.wFormatTag
= WAVE_FORMAT_PCM
;
350 fmt
.nChannels
= channels
;
351 fmt
.nSamplesPerSec
= rate
;
352 fmt
.wBitsPerSample
= depth
;
353 fmt
.nBlockAlign
= (channels
* depth
) / 8;
354 fmt
.nAvgBytesPerSec
= rate
* fmt
.nBlockAlign
;
357 hr
= IAudioClient_IsFormatSupported(client
, AUDCLNT_SHAREMODE_SHARED
,
365 static struct _TestFormat
{
370 } formats_to_test
[] = {
371 { WAVE_FORMAT_1M08
, 11025, 8, 1 },
372 { WAVE_FORMAT_1M16
, 11025, 16, 1 },
373 { WAVE_FORMAT_1S08
, 11025, 8, 2 },
374 { WAVE_FORMAT_1S16
, 11025, 16, 2 },
375 { WAVE_FORMAT_2M08
, 22050, 8, 1 },
376 { WAVE_FORMAT_2M16
, 22050, 16, 1 },
377 { WAVE_FORMAT_2S08
, 22050, 8, 2 },
378 { WAVE_FORMAT_2S16
, 22050, 16, 2 },
379 { WAVE_FORMAT_4M08
, 44100, 8, 1 },
380 { WAVE_FORMAT_4M16
, 44100, 16, 1 },
381 { WAVE_FORMAT_4S08
, 44100, 8, 2 },
382 { WAVE_FORMAT_4S16
, 44100, 16, 2 },
383 { WAVE_FORMAT_48M08
, 48000, 8, 1 },
384 { WAVE_FORMAT_48M16
, 48000, 16, 1 },
385 { WAVE_FORMAT_48S08
, 48000, 8, 2 },
386 { WAVE_FORMAT_48S16
, 48000, 16, 2 },
387 { WAVE_FORMAT_96M08
, 96000, 8, 1 },
388 { WAVE_FORMAT_96M16
, 96000, 16, 1 },
389 { WAVE_FORMAT_96S08
, 96000, 8, 2 },
390 { WAVE_FORMAT_96S16
, 96000, 16, 2 },
394 static DWORD
WINMM_GetSupportedFormats(IMMDevice
*device
)
398 struct _TestFormat
*fmt
;
399 IAudioClient
*client
;
401 hr
= IMMDevice_Activate(device
, &IID_IAudioClient
,
402 CLSCTX_INPROC_SERVER
, NULL
, (void**)&client
);
406 for(fmt
= formats_to_test
; fmt
->flag
; ++fmt
){
407 hr
= WINMM_TestFormat(client
, fmt
->rate
, fmt
->depth
, fmt
->channels
);
412 IAudioClient_Release(client
);
417 static HRESULT
WINMM_InitMMDevice(EDataFlow flow
, IMMDevice
*device
,
418 WINMM_MMDevice
*dev
, UINT index
)
423 dev
->out_caps
.wMid
= 0xFF;
424 dev
->out_caps
.wPid
= 0xFF;
425 dev
->out_caps
.vDriverVersion
= 0x00010001;
426 dev
->out_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
427 dev
->out_caps
.wReserved1
= 0;
428 dev
->out_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
429 WAVECAPS_SAMPLEACCURATE
;
430 dev
->out_caps
.wChannels
= 2;
431 dev
->out_caps
.szPname
[0] = '\0';
433 hr
= WINMM_GetFriendlyName(device
, dev
->out_caps
.szPname
,
434 sizeof(dev
->out_caps
.szPname
) /
435 sizeof(*dev
->out_caps
.szPname
));
439 dev
->in_caps
.wMid
= 0xFF;
440 dev
->in_caps
.wPid
= 0xFF;
441 dev
->in_caps
.vDriverVersion
= 0x00010001;
442 dev
->in_caps
.dwFormats
= WINMM_GetSupportedFormats(device
);
443 dev
->in_caps
.wReserved1
= 0;
444 dev
->in_caps
.wChannels
= 2;
445 dev
->in_caps
.szPname
[0] = '\0';
447 hr
= WINMM_GetFriendlyName(device
, dev
->in_caps
.szPname
,
448 sizeof(dev
->in_caps
.szPname
) /
449 sizeof(*dev
->in_caps
.szPname
));
454 hr
= IMMDevice_GetId(device
, &dev
->dev_id
);
458 CoCreateGuid(&dev
->session
);
460 InitializeCriticalSection(&dev
->lock
);
461 dev
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_Device.lock");
466 static HRESULT
WINMM_EnumDevices(WINMM_MMDevice
**devices
, UINT
*devcount
,
467 EDataFlow flow
, IMMDeviceEnumerator
*devenum
)
469 IMMDeviceCollection
*devcoll
;
472 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, flow
,
473 DEVICE_STATE_ACTIVE
, &devcoll
);
477 hr
= IMMDeviceCollection_GetCount(devcoll
, devcount
);
479 IMMDeviceCollection_Release(devcoll
);
485 IMMDevice
*def_dev
= NULL
;
487 *devices
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
488 sizeof(WINMM_MMDevice
) * (*devcount
));
490 IMMDeviceCollection_Release(devcoll
);
491 return E_OUTOFMEMORY
;
496 /* make sure that device 0 is the default device */
497 hr
= IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum
,
498 flow
, eConsole
, &def_dev
);
500 WINMM_InitMMDevice(flow
, def_dev
, &(*devices
)[0], 0);
504 for(n
= 0; n
< *devcount
; ++n
){
507 hr
= IMMDeviceCollection_Item(devcoll
, n
, &device
);
509 if(device
!= def_dev
){
510 WINMM_InitMMDevice(flow
, device
, &(*devices
)[count
], count
);
514 IMMDevice_Release(device
);
519 IMMDevice_Release(def_dev
);
524 IMMDeviceCollection_Release(devcoll
);
529 static HRESULT
WINMM_InitMMDevices(void)
532 IMMDeviceEnumerator
*devenum
= NULL
;
534 if(g_outmmdevices_count
|| g_inmmdevices_count
)
537 init_hr
= CoInitialize(NULL
);
539 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
540 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
544 hr
= WINMM_EnumDevices(&g_out_mmdevices
, &g_outmmdevices_count
, eRender
, devenum
);
546 g_outmmdevices_count
= 0;
547 g_inmmdevices_count
= 0;
551 hr
= WINMM_EnumDevices(&g_in_mmdevices
, &g_inmmdevices_count
, eCapture
, devenum
);
553 g_inmmdevices_count
= 0;
559 IMMDeviceEnumerator_Release(devenum
);
560 if(SUCCEEDED(init_hr
))
566 static inline BOOL
WINMM_IsMapper(UINT device
)
568 return (device
== WAVE_MAPPER
|| device
== (UINT16
)WAVE_MAPPER
);
571 static MMRESULT
WINMM_TryDeviceMapping(WINMM_OpenInfo
*info
, WORD channels
,
572 DWORD freq
, DWORD bits_per_samp
, BOOL is_out
)
574 WINMM_Device
*device
;
579 TRACE("format: %u, channels: %u, sample rate: %u, bit depth: %u\n",
580 WAVE_FORMAT_PCM
, channels
, freq
, bits_per_samp
);
582 target
.wFormatTag
= WAVE_FORMAT_PCM
;
583 target
.nChannels
= channels
;
584 target
.nSamplesPerSec
= freq
;
585 target
.wBitsPerSample
= bits_per_samp
;
586 target
.nBlockAlign
= (target
.nChannels
* target
.wBitsPerSample
) / 8;
587 target
.nAvgBytesPerSec
= target
.nSamplesPerSec
* target
.nBlockAlign
;
591 mr
= acmStreamOpen(NULL
, NULL
, info
->format
, &target
, NULL
, 0,
592 0, ACM_STREAMOPENF_QUERY
);
594 mr
= acmStreamOpen(NULL
, NULL
, &target
, info
->format
, NULL
, 0,
595 0, ACM_STREAMOPENF_QUERY
);
596 if(mr
!= MMSYSERR_NOERROR
)
599 /* ACM can convert from src->dst, so try to find a device
600 * that supports dst */
602 if(WINMM_IsMapper(info
->req_device
)){
603 for(i
= 0; i
< g_outmmdevices_count
; ++i
){
604 WINMM_OpenInfo l_info
= *info
;
605 l_info
.req_device
= i
;
606 l_info
.format
= &target
;
607 mr
= WOD_Open(&l_info
);
608 if(mr
== MMSYSERR_NOERROR
){
609 info
->handle
= l_info
.handle
;
614 WINMM_OpenInfo l_info
= *info
;
615 l_info
.flags
&= ~WAVE_MAPPED
;
616 l_info
.format
= &target
;
617 mr
= WOD_Open(&l_info
);
618 if(mr
== MMSYSERR_NOERROR
)
619 info
->handle
= l_info
.handle
;
622 if(WINMM_IsMapper(info
->req_device
)){
623 for(i
= 0; i
< g_inmmdevices_count
; ++i
){
624 WINMM_OpenInfo l_info
= *info
;
625 l_info
.req_device
= i
;
626 l_info
.format
= &target
;
627 mr
= WID_Open(&l_info
);
628 if(mr
== MMSYSERR_NOERROR
){
629 info
->handle
= l_info
.handle
;
634 WINMM_OpenInfo l_info
= *info
;
635 l_info
.flags
&= ~WAVE_MAPPED
;
636 l_info
.format
= &target
;
637 mr
= WID_Open(&l_info
);
638 if(mr
== MMSYSERR_NOERROR
)
639 info
->handle
= l_info
.handle
;
642 if(mr
!= MMSYSERR_NOERROR
)
643 return WAVERR_BADFORMAT
;
645 device
= WINMM_GetDeviceFromHWAVE(info
->handle
);
647 return MMSYSERR_INVALHANDLE
;
649 /* set up the ACM stream */
651 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, info
->format
, &target
,
654 mr
= acmStreamOpen(&device
->acm_handle
, NULL
, &target
, info
->format
,
656 if(mr
!= MMSYSERR_NOERROR
){
658 WOD_Close((HWAVEOUT
)info
->handle
);
660 WID_Close((HWAVEIN
)info
->handle
);
665 return MMSYSERR_NOERROR
;
668 static MMRESULT
WINMM_MapDevice(WINMM_OpenInfo
*info
, BOOL is_out
)
672 WAVEFORMATEXTENSIBLE
*fmtex
= (WAVEFORMATEXTENSIBLE
*)info
->format
;
674 TRACE("(%p, %d)\n", info
, is_out
);
676 /* try to find a direct match */
678 WINMM_OpenInfo l_info
= *info
;
679 if(WINMM_IsMapper(info
->req_device
)){
680 for(i
= 0; i
< g_outmmdevices_count
; ++i
){
681 l_info
.req_device
= i
;
682 mr
= WOD_Open(&l_info
);
683 if(mr
== MMSYSERR_NOERROR
){
684 info
->handle
= l_info
.handle
;
689 l_info
.flags
&= ~WAVE_MAPPED
;
690 mr
= WOD_Open(&l_info
);
691 if(mr
== MMSYSERR_NOERROR
){
692 info
->handle
= l_info
.handle
;
697 WINMM_OpenInfo l_info
= *info
;
698 if(WINMM_IsMapper(info
->req_device
)){
699 for(i
= 0; i
< g_inmmdevices_count
; ++i
){
700 l_info
.req_device
= i
;
701 mr
= WID_Open(&l_info
);
702 if(mr
== MMSYSERR_NOERROR
){
703 info
->handle
= l_info
.handle
;
708 l_info
.flags
&= ~WAVE_MAPPED
;
709 mr
= WID_Open(&l_info
);
710 if(mr
== MMSYSERR_NOERROR
){
711 info
->handle
= l_info
.handle
;
717 /* no direct match, so set up the ACM stream */
718 if(info
->format
->wFormatTag
!= WAVE_FORMAT_PCM
&&
719 !(info
->format
->wFormatTag
== WAVE_FORMAT_EXTENSIBLE
&&
720 IsEqualGUID(&fmtex
->SubFormat
, &KSDATAFORMAT_SUBTYPE_PCM
))){
721 /* convert to PCM format if it's not already */
722 mr
= WINMM_TryDeviceMapping(info
, info
->format
->nChannels
,
723 info
->format
->nSamplesPerSec
, 16, is_out
);
724 if(mr
== MMSYSERR_NOERROR
)
727 mr
= WINMM_TryDeviceMapping(info
, info
->format
->nChannels
,
728 info
->format
->nSamplesPerSec
, 8, is_out
);
729 if(mr
== MMSYSERR_NOERROR
)
734 /* first try just changing bit depth and channels */
735 channels
= info
->format
->nChannels
;
736 mr
= WINMM_TryDeviceMapping(info
, channels
,
737 info
->format
->nSamplesPerSec
, 16, is_out
);
738 if(mr
== MMSYSERR_NOERROR
)
740 mr
= WINMM_TryDeviceMapping(info
, channels
,
741 info
->format
->nSamplesPerSec
, 8, is_out
);
742 if(mr
== MMSYSERR_NOERROR
)
745 channels
= (channels
== 2) ? 1 : 2;
746 mr
= WINMM_TryDeviceMapping(info
, channels
,
747 info
->format
->nSamplesPerSec
, 16, is_out
);
748 if(mr
== MMSYSERR_NOERROR
)
750 mr
= WINMM_TryDeviceMapping(info
, channels
,
751 info
->format
->nSamplesPerSec
, 8, is_out
);
752 if(mr
== MMSYSERR_NOERROR
)
755 /* that didn't work, so now try different sample rates */
756 channels
= info
->format
->nChannels
;
757 mr
= WINMM_TryDeviceMapping(info
, channels
, 96000, 16, is_out
);
758 if(mr
== MMSYSERR_NOERROR
)
760 mr
= WINMM_TryDeviceMapping(info
, channels
, 48000, 16, is_out
);
761 if(mr
== MMSYSERR_NOERROR
)
763 mr
= WINMM_TryDeviceMapping(info
, channels
, 44100, 16, is_out
);
764 if(mr
== MMSYSERR_NOERROR
)
766 mr
= WINMM_TryDeviceMapping(info
, channels
, 22050, 16, is_out
);
767 if(mr
== MMSYSERR_NOERROR
)
769 mr
= WINMM_TryDeviceMapping(info
, channels
, 11025, 16, is_out
);
770 if(mr
== MMSYSERR_NOERROR
)
773 channels
= (channels
== 2) ? 1 : 2;
774 mr
= WINMM_TryDeviceMapping(info
, channels
, 96000, 16, is_out
);
775 if(mr
== MMSYSERR_NOERROR
)
777 mr
= WINMM_TryDeviceMapping(info
, channels
, 48000, 16, is_out
);
778 if(mr
== MMSYSERR_NOERROR
)
780 mr
= WINMM_TryDeviceMapping(info
, channels
, 44100, 16, is_out
);
781 if(mr
== MMSYSERR_NOERROR
)
783 mr
= WINMM_TryDeviceMapping(info
, channels
, 22050, 16, is_out
);
784 if(mr
== MMSYSERR_NOERROR
)
786 mr
= WINMM_TryDeviceMapping(info
, channels
, 11025, 16, is_out
);
787 if(mr
== MMSYSERR_NOERROR
)
790 channels
= info
->format
->nChannels
;
791 mr
= WINMM_TryDeviceMapping(info
, channels
, 96000, 8, is_out
);
792 if(mr
== MMSYSERR_NOERROR
)
794 mr
= WINMM_TryDeviceMapping(info
, channels
, 48000, 8, is_out
);
795 if(mr
== MMSYSERR_NOERROR
)
797 mr
= WINMM_TryDeviceMapping(info
, channels
, 44100, 8, is_out
);
798 if(mr
== MMSYSERR_NOERROR
)
800 mr
= WINMM_TryDeviceMapping(info
, channels
, 22050, 8, is_out
);
801 if(mr
== MMSYSERR_NOERROR
)
803 mr
= WINMM_TryDeviceMapping(info
, channels
, 11025, 8, is_out
);
804 if(mr
== MMSYSERR_NOERROR
)
807 channels
= (channels
== 2) ? 1 : 2;
808 mr
= WINMM_TryDeviceMapping(info
, channels
, 96000, 8, is_out
);
809 if(mr
== MMSYSERR_NOERROR
)
811 mr
= WINMM_TryDeviceMapping(info
, channels
, 48000, 8, is_out
);
812 if(mr
== MMSYSERR_NOERROR
)
814 mr
= WINMM_TryDeviceMapping(info
, channels
, 44100, 8, is_out
);
815 if(mr
== MMSYSERR_NOERROR
)
817 mr
= WINMM_TryDeviceMapping(info
, channels
, 22050, 8, is_out
);
818 if(mr
== MMSYSERR_NOERROR
)
820 mr
= WINMM_TryDeviceMapping(info
, channels
, 11025, 8, is_out
);
821 if(mr
== MMSYSERR_NOERROR
)
825 WARN("Unable to find compatible device!\n");
826 return WAVERR_BADFORMAT
;
829 static LRESULT
WINMM_OpenDevice(WINMM_Device
*device
, WINMM_MMDevice
*mmdevice
,
830 WINMM_OpenInfo
*info
)
832 WAVEFORMATEX
*closer_fmt
= NULL
, fmt
, *passed_fmt
;
833 LRESULT ret
= MMSYSERR_ERROR
;
836 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
,
839 WARN("Device %s (%s) unavailable: %08x\n",
840 wine_dbgstr_w(mmdevice
->dev_id
),
841 wine_dbgstr_w(mmdevice
->out_caps
.szPname
), hr
);
845 hr
= IMMDevice_Activate(device
->device
, &IID_IAudioClient
,
846 CLSCTX_INPROC_SERVER
, NULL
, (void**)&device
->client
);
848 WARN("Activate failed: %08x\n", hr
);
852 if(info
->format
->wFormatTag
== WAVE_FORMAT_PCM
){
853 /* we aren't guaranteed that the struct in lpFormat is a full
854 * WAVEFORMATEX struct, which IAC::IsFormatSupported requires */
856 memcpy(passed_fmt
, info
->format
, sizeof(PCMWAVEFORMAT
));
857 passed_fmt
->cbSize
= 0;
858 if(fmt
.wBitsPerSample
% 8 != 0){
859 WARN("Fixing bad wBitsPerSample (%u)\n", fmt
.wBitsPerSample
);
860 fmt
.wBitsPerSample
= (fmt
.wBitsPerSample
+ 7) & ~7;
862 /* winmm ignores broken blockalign and avgbytes */
863 if(fmt
.nBlockAlign
!= fmt
.nChannels
* fmt
.wBitsPerSample
/8){
864 WARN("Fixing bad nBlockAlign (%u)\n", fmt
.nBlockAlign
);
865 fmt
.nBlockAlign
= fmt
.nChannels
* fmt
.wBitsPerSample
/8;
867 if (fmt
.nAvgBytesPerSec
!= fmt
.nSamplesPerSec
* fmt
.nBlockAlign
) {
868 WARN("Fixing bad nAvgBytesPerSec (%u)\n", fmt
.nAvgBytesPerSec
);
869 fmt
.nAvgBytesPerSec
= fmt
.nSamplesPerSec
* fmt
.nBlockAlign
;
872 passed_fmt
= info
->format
;
874 hr
= IAudioClient_IsFormatSupported(device
->client
,
875 AUDCLNT_SHAREMODE_SHARED
, passed_fmt
, &closer_fmt
);
877 CoTaskMemFree(closer_fmt
);
878 if(FAILED(hr
) && hr
!= AUDCLNT_E_UNSUPPORTED_FORMAT
){
879 WARN("IsFormatSupported failed: %08x\n", hr
);
882 if(hr
== S_FALSE
|| hr
== AUDCLNT_E_UNSUPPORTED_FORMAT
){
883 ret
= WAVERR_BADFORMAT
;
886 if(info
->flags
& WAVE_FORMAT_QUERY
){
887 ret
= MMSYSERR_NOERROR
;
891 /* buffer size = 10 * 100000 (100 ns) = 0.1 seconds */
892 hr
= IAudioClient_Initialize(device
->client
, AUDCLNT_SHAREMODE_SHARED
,
893 AUDCLNT_STREAMFLAGS_EVENTCALLBACK
| AUDCLNT_STREAMFLAGS_NOPERSIST
,
894 10 * 100000, 50000, passed_fmt
, &device
->parent
->session
);
896 WARN("Initialize failed: %08x\n", hr
);
900 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioClock
,
901 (void**)&device
->clock
);
903 WARN("GetService failed: %08x\n", hr
);
908 device
->event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
910 WARN("CreateEvent failed: %08x\n", hr
);
914 /* As the devices thread is waiting on g_device_handles, it can
915 * only be modified from within this same thread. */
916 if(g_device_handles
){
917 g_device_handles
= HeapReAlloc(GetProcessHeap(), 0, g_device_handles
,
918 sizeof(HANDLE
) * (g_devhandle_count
+ 1));
919 g_handle_devices
= HeapReAlloc(GetProcessHeap(), 0, g_handle_devices
,
920 sizeof(WINMM_Device
*) * (g_devhandle_count
+ 1));
922 g_device_handles
= HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLE
));
923 g_handle_devices
= HeapAlloc(GetProcessHeap(), 0,
924 sizeof(WINMM_Device
*));
926 g_device_handles
[g_devhandle_count
] = device
->event
;
927 g_handle_devices
[g_devhandle_count
] = device
;
931 hr
= IAudioClient_SetEventHandle(device
->client
, device
->event
);
933 WARN("SetEventHandle failed: %08x\n", hr
);
937 device
->bytes_per_frame
= passed_fmt
->nBlockAlign
;
938 device
->samples_per_sec
= passed_fmt
->nSamplesPerSec
;
940 device
->played_frames
= 0;
941 device
->last_clock_pos
= 0;
942 device
->ofs_bytes
= 0;
943 device
->loop_counter
= 0;
944 device
->stopped
= TRUE
;
945 device
->first
= device
->last
= device
->playing
= device
->loop_start
= NULL
;
947 device
->cb_info
.flags
= HIWORD(info
->flags
& CALLBACK_TYPEMASK
);
948 device
->cb_info
.callback
= info
->callback
;
949 device
->cb_info
.user
= info
->cb_user
;
950 device
->cb_info
.hwave
= device
->handle
;
952 info
->handle
= device
->handle
;
954 return MMSYSERR_NOERROR
;
958 IAudioClient_Release(device
->client
);
959 device
->client
= NULL
;
962 IMMDevice_Release(device
->device
);
963 device
->device
= NULL
;
969 static LRESULT
WOD_Open(WINMM_OpenInfo
*info
)
971 WINMM_MMDevice
*mmdevice
;
972 WINMM_Device
*device
= NULL
;
973 LRESULT ret
= MMSYSERR_ERROR
;
976 TRACE("(%u, %p, %08x)\n", info
->req_device
, info
, info
->flags
);
978 if(WINMM_IsMapper(info
->req_device
) || (info
->flags
& WAVE_MAPPED
))
979 return WINMM_MapDevice(info
, TRUE
);
981 if(info
->req_device
>= g_outmmdevices_count
)
982 return MMSYSERR_BADDEVICEID
;
984 mmdevice
= &g_out_mmdevices
[info
->req_device
];
986 if(!mmdevice
->out_caps
.szPname
[0])
987 return MMSYSERR_NOTENABLED
;
989 device
= WINMM_FindUnusedDevice(TRUE
, info
->req_device
);
991 return MMSYSERR_ALLOCATED
;
993 ret
= WINMM_OpenDevice(device
, mmdevice
, info
);
994 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
996 ret
= MMSYSERR_ERROR
;
998 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioRenderClient
,
999 (void**)&device
->render
);
1001 WARN("GetService(RenderClient) failed: %08x\n", hr
);
1005 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioStreamVolume
,
1006 (void**)&device
->volume
);
1008 WARN("GetService(StreamVolume) failed: %08x\n", hr
);
1012 LeaveCriticalSection(&device
->lock
);
1014 return MMSYSERR_NOERROR
;
1018 IMMDevice_Release(device
->device
);
1019 device
->device
= NULL
;
1022 IAudioClient_Release(device
->client
);
1023 device
->client
= NULL
;
1026 IAudioRenderClient_Release(device
->render
);
1027 device
->render
= NULL
;
1030 IAudioStreamVolume_Release(device
->volume
);
1031 device
->volume
= NULL
;
1034 IAudioClock_Release(device
->clock
);
1035 device
->clock
= NULL
;
1037 device
->open
= FALSE
;
1038 LeaveCriticalSection(&device
->lock
);
1042 static LRESULT
WID_Open(WINMM_OpenInfo
*info
)
1044 WINMM_MMDevice
*mmdevice
;
1045 WINMM_Device
*device
= NULL
;
1046 LRESULT ret
= MMSYSERR_ERROR
;
1049 TRACE("(%u, %p, %08x)\n", info
->req_device
, info
, info
->flags
);
1051 if(WINMM_IsMapper(info
->req_device
) || info
->flags
& WAVE_MAPPED
)
1052 return WINMM_MapDevice(info
, FALSE
);
1054 if(info
->req_device
>= g_inmmdevices_count
)
1055 return MMSYSERR_BADDEVICEID
;
1057 mmdevice
= &g_in_mmdevices
[info
->req_device
];
1059 if(!mmdevice
->in_caps
.szPname
[0])
1060 return MMSYSERR_NOTENABLED
;
1062 device
= WINMM_FindUnusedDevice(FALSE
, info
->req_device
);
1064 return MMSYSERR_ALLOCATED
;
1066 ret
= WINMM_OpenDevice(device
, mmdevice
, info
);
1067 if((info
->flags
& WAVE_FORMAT_QUERY
) || ret
!= MMSYSERR_NOERROR
)
1069 ret
= MMSYSERR_ERROR
;
1071 hr
= IAudioClient_GetService(device
->client
, &IID_IAudioCaptureClient
,
1072 (void**)&device
->capture
);
1074 WARN("GetService failed: %08x\n", hr
);
1078 LeaveCriticalSection(&device
->lock
);
1080 return MMSYSERR_NOERROR
;
1084 IMMDevice_Release(device
->device
);
1085 device
->device
= NULL
;
1088 IAudioClient_Release(device
->client
);
1089 device
->client
= NULL
;
1091 if(device
->capture
){
1092 IAudioCaptureClient_Release(device
->capture
);
1093 device
->capture
= NULL
;
1096 IAudioClock_Release(device
->clock
);
1097 device
->clock
= NULL
;
1099 device
->open
= FALSE
;
1100 LeaveCriticalSection(&device
->lock
);
1104 static HRESULT
WINMM_CloseDevice(WINMM_Device
*device
)
1106 device
->open
= FALSE
;
1108 if(!device
->stopped
){
1109 IAudioClient_Stop(device
->client
);
1110 device
->stopped
= TRUE
;
1113 if(device
->acm_handle
){
1114 acmStreamClose(device
->acm_handle
, 0);
1115 device
->acm_handle
= NULL
;
1118 IMMDevice_Release(device
->device
);
1119 device
->device
= NULL
;
1121 IAudioClient_Release(device
->client
);
1122 device
->client
= NULL
;
1124 IAudioClock_Release(device
->clock
);
1125 device
->clock
= NULL
;
1130 static LRESULT
WOD_Close(HWAVEOUT hwave
)
1132 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1134 TRACE("(%p)\n", hwave
);
1136 if(!WINMM_ValidateAndLock(device
))
1137 return MMSYSERR_INVALHANDLE
;
1139 WINMM_CloseDevice(device
);
1141 IAudioRenderClient_Release(device
->render
);
1142 device
->render
= NULL
;
1144 IAudioStreamVolume_Release(device
->volume
);
1145 device
->volume
= NULL
;
1147 LeaveCriticalSection(&device
->lock
);
1149 return MMSYSERR_NOERROR
;
1152 static LRESULT
WID_Close(HWAVEIN hwave
)
1154 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hwave
);
1156 TRACE("(%p)\n", hwave
);
1158 if(!WINMM_ValidateAndLock(device
))
1159 return MMSYSERR_INVALHANDLE
;
1161 WINMM_CloseDevice(device
);
1163 IAudioCaptureClient_Release(device
->capture
);
1164 device
->capture
= NULL
;
1166 LeaveCriticalSection(&device
->lock
);
1168 return MMSYSERR_NOERROR
;
1171 static LRESULT
WINMM_PrepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1173 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1175 TRACE("(%p, %p)\n", hwave
, header
);
1177 if(!WINMM_ValidateAndLock(device
))
1178 return MMSYSERR_INVALHANDLE
;
1180 if(device
->render
&& device
->acm_handle
){
1181 ACMSTREAMHEADER
*ash
;
1185 mr
= acmStreamSize(device
->acm_handle
, header
->dwBufferLength
, &size
,
1186 ACM_STREAMSIZEF_SOURCE
);
1187 if(mr
!= MMSYSERR_NOERROR
){
1188 LeaveCriticalSection(&device
->lock
);
1192 ash
= HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER
) + size
);
1194 LeaveCriticalSection(&device
->lock
);
1195 return MMSYSERR_NOMEM
;
1198 ash
->cbStruct
= sizeof(*ash
);
1200 ash
->dwUser
= (DWORD_PTR
)header
;
1201 ash
->pbSrc
= (BYTE
*)header
->lpData
;
1202 ash
->cbSrcLength
= header
->dwBufferLength
;
1203 ash
->dwSrcUser
= header
->dwUser
;
1204 ash
->pbDst
= (BYTE
*)ash
+ sizeof(ACMSTREAMHEADER
);
1205 ash
->cbDstLength
= size
;
1208 mr
= acmStreamPrepareHeader(device
->acm_handle
, ash
, 0);
1209 if(mr
!= MMSYSERR_NOERROR
){
1210 HeapFree(GetProcessHeap(), 0, ash
);
1211 LeaveCriticalSection(&device
->lock
);
1215 header
->reserved
= (DWORD_PTR
)ash
;
1218 LeaveCriticalSection(&device
->lock
);
1220 header
->dwFlags
|= WHDR_PREPARED
;
1221 header
->dwFlags
&= ~WHDR_DONE
;
1223 return MMSYSERR_NOERROR
;
1226 static LRESULT
WINMM_UnprepareHeader(HWAVE hwave
, WAVEHDR
*header
)
1228 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1230 TRACE("(%p, %p)\n", hwave
, header
);
1232 if(!WINMM_ValidateAndLock(device
))
1233 return MMSYSERR_INVALHANDLE
;
1235 if(device
->render
&& device
->acm_handle
){
1236 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1238 acmStreamUnprepareHeader(device
->acm_handle
, ash
, 0);
1240 HeapFree(GetProcessHeap(), 0, ash
);
1243 LeaveCriticalSection(&device
->lock
);
1245 header
->dwFlags
&= ~WHDR_PREPARED
;
1246 header
->dwFlags
|= WHDR_DONE
;
1248 return MMSYSERR_NOERROR
;
1251 static UINT32
WINMM_HeaderLenBytes(WINMM_Device
*device
, WAVEHDR
*header
)
1253 if(device
->acm_handle
){
1254 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
1255 return ash
->cbDstLengthUsed
;
1258 return header
->dwBufferLength
;
1261 static UINT32
WINMM_HeaderLenFrames(WINMM_Device
*device
, WAVEHDR
*header
)
1263 return WINMM_HeaderLenBytes(device
, header
) / device
->bytes_per_frame
;
1266 static WAVEHDR
*WOD_MarkDoneHeaders(WINMM_Device
*device
)
1269 WAVEHDR
*first
= device
->first
, *queue
= first
, *last
= NULL
;
1270 UINT64 clock_freq
, clock_pos
, clock_frames
;
1271 UINT32 nloops
, queue_frames
= 0;
1273 hr
= IAudioClock_GetFrequency(device
->clock
, &clock_freq
);
1275 WARN("GetFrequency failed: %08x\n", hr
);
1279 hr
= IAudioClock_GetPosition(device
->clock
, &clock_pos
, NULL
);
1281 WARN("GetPosition failed: %08x\n", hr
);
1285 clock_frames
= (clock_pos
* device
->samples_per_sec
) / clock_freq
;
1287 nloops
= device
->loop_counter
;
1289 (queue_frames
+= WINMM_HeaderLenFrames(device
, queue
)) <=
1290 clock_frames
- device
->last_clock_pos
){
1293 device
->last_clock_pos
+= queue_frames
;
1295 queue
= device
->first
= queue
->lpNext
;
1297 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1298 if(queue
->dwFlags
& WHDR_ENDLOOP
)
1301 queue
= queue
->lpNext
;
1302 }else if(queue
->dwFlags
& WHDR_ENDLOOP
){
1303 queue
= device
->loop_start
;
1310 last
->lpNext
= NULL
;
1316 static void WOD_PushData(WINMM_Device
*device
)
1318 WINMM_CBInfo cb_info
;
1320 UINT32 pad
, bufsize
, avail_frames
, queue_frames
, written
, ofs
;
1321 UINT32 queue_bytes
, nloops
;
1323 WAVEHDR
*queue
, *first
= NULL
;
1325 TRACE("(%p)\n", device
->handle
);
1327 EnterCriticalSection(&device
->lock
);
1333 device
->stopped
= TRUE
;
1334 device
->last_clock_pos
= 0;
1335 IAudioClient_Stop(device
->client
);
1336 IAudioClient_Reset(device
->client
);
1340 hr
= IAudioClient_GetBufferSize(device
->client
, &bufsize
);
1342 WARN("GetBufferSize failed: %08x\n", hr
);
1346 hr
= IAudioClient_GetCurrentPadding(device
->client
, &pad
);
1348 WARN("GetCurrentPadding failed: %08x\n", hr
);
1352 first
= WOD_MarkDoneHeaders(device
);
1354 /* determine which is larger between the available buffer size and
1355 * the amount of data left in the queue */
1356 avail_frames
= bufsize
- pad
;
1358 queue
= device
->playing
;
1359 ofs
= device
->ofs_bytes
;
1362 while(queue
&& queue_frames
< avail_frames
){
1363 queue_bytes
= WINMM_HeaderLenBytes(device
, queue
);
1364 queue_frames
+= (queue_bytes
- ofs
) / device
->bytes_per_frame
;
1367 if(queue
->dwFlags
& WHDR_ENDLOOP
&& nloops
< device
->loop_counter
){
1368 queue
= device
->loop_start
;
1371 queue
= queue
->lpNext
;
1374 if(queue_frames
< avail_frames
)
1375 avail_frames
= queue_frames
;
1376 if(avail_frames
== 0)
1379 hr
= IAudioRenderClient_GetBuffer(device
->render
, avail_frames
, &data
);
1381 WARN("GetBuffer failed: %08x\n", hr
);
1386 while(device
->playing
&& written
< avail_frames
){
1387 UINT32 copy_frames
, copy_bytes
;
1390 queue
= device
->playing
;
1392 if(device
->acm_handle
){
1393 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)queue
->reserved
;
1394 queue_bytes
= ash
->cbDstLengthUsed
;
1395 queue_data
= ash
->pbDst
;
1397 queue_bytes
= queue
->dwBufferLength
;
1398 queue_data
= (BYTE
*)queue
->lpData
;
1401 queue_frames
= (queue_bytes
- device
->ofs_bytes
) /
1402 device
->bytes_per_frame
;
1404 copy_frames
= queue_frames
< (avail_frames
- written
) ?
1405 queue_frames
: avail_frames
- written
;
1406 copy_bytes
= copy_frames
* device
->bytes_per_frame
;
1408 memcpy(data
, queue_data
+ device
->ofs_bytes
, copy_bytes
);
1411 written
+= copy_frames
;
1412 device
->ofs_bytes
+= copy_bytes
;
1414 if(device
->ofs_bytes
>= queue_bytes
){
1415 device
->ofs_bytes
= 0;
1417 if(!(queue
->dwFlags
& (WHDR_BEGINLOOP
| WHDR_ENDLOOP
)))
1418 device
->playing
= queue
->lpNext
;
1420 if(queue
->dwFlags
& WHDR_BEGINLOOP
){
1421 device
->loop_start
= device
->playing
;
1422 device
->playing
= queue
->lpNext
;
1423 device
->loop_counter
= queue
->dwLoops
;
1425 if(queue
->dwFlags
& WHDR_ENDLOOP
){
1426 --device
->loop_counter
;
1427 if(device
->loop_counter
)
1428 device
->playing
= device
->loop_start
;
1430 device
->loop_start
= device
->playing
= queue
->lpNext
;
1436 hr
= IAudioRenderClient_ReleaseBuffer(device
->render
, avail_frames
, 0);
1438 WARN("ReleaseBuffer failed: %08x\n", hr
);
1442 device
->played_frames
+= avail_frames
;
1445 cb_info
= device
->cb_info
;
1447 LeaveCriticalSection(&device
->lock
);
1450 WAVEHDR
*next
= first
->lpNext
;
1451 first
->dwFlags
&= ~WHDR_INQUEUE
;
1452 first
->dwFlags
|= WHDR_DONE
;
1453 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
1458 static void WID_PullACMData(WINMM_Device
*device
)
1460 UINT32 packet
, packet_bytes
;
1467 if(device
->acm_hdr
.cbDstLength
== 0){
1468 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet
,
1469 &flags
, NULL
, NULL
);
1472 WARN("GetBuffer failed: %08x\n", hr
);
1476 acmStreamSize(device
->acm_handle
, packet
* device
->bytes_per_frame
,
1477 &packet_bytes
, ACM_STREAMSIZEF_SOURCE
);
1479 device
->acm_offs
= 0;
1481 device
->acm_hdr
.cbStruct
= sizeof(device
->acm_hdr
);
1482 device
->acm_hdr
.fdwStatus
= 0;
1483 device
->acm_hdr
.dwUser
= 0;
1484 device
->acm_hdr
.pbSrc
= data
;
1485 device
->acm_hdr
.cbSrcLength
= packet
* device
->bytes_per_frame
;
1486 device
->acm_hdr
.cbSrcLengthUsed
= 0;
1487 device
->acm_hdr
.dwSrcUser
= 0;
1488 device
->acm_hdr
.pbDst
= HeapAlloc(GetProcessHeap(), 0, packet_bytes
);
1489 device
->acm_hdr
.cbDstLength
= packet_bytes
;
1490 device
->acm_hdr
.cbDstLengthUsed
= 0;
1491 device
->acm_hdr
.dwDstUser
= 0;
1493 mr
= acmStreamPrepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1494 if(mr
!= MMSYSERR_NOERROR
){
1495 WARN("acmStreamPrepareHeader failed: %d\n", mr
);
1499 mr
= acmStreamConvert(device
->acm_handle
, &device
->acm_hdr
, 0);
1500 if(mr
!= MMSYSERR_NOERROR
){
1501 WARN("acmStreamConvert failed: %d\n", mr
);
1505 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet
);
1507 WARN("ReleaseBuffer failed: %08x\n", hr
);
1509 device
->played_frames
+= packet
;
1512 queue
= device
->first
;
1514 UINT32 to_copy_bytes
;
1516 to_copy_bytes
= min(queue
->dwBufferLength
- queue
->dwBytesRecorded
,
1517 device
->acm_hdr
.cbDstLengthUsed
- device
->acm_offs
);
1519 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1520 device
->acm_hdr
.pbDst
+ device
->acm_offs
, to_copy_bytes
);
1522 queue
->dwBytesRecorded
+= to_copy_bytes
;
1523 device
->acm_offs
+= to_copy_bytes
;
1525 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1526 device
->bytes_per_frame
){
1527 queue
->dwFlags
&= ~WHDR_INQUEUE
;
1528 queue
->dwFlags
|= WHDR_DONE
;
1529 device
->first
= queue
= queue
->lpNext
;
1532 if(device
->acm_offs
>= device
->acm_hdr
.cbDstLengthUsed
){
1533 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1534 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1535 device
->acm_hdr
.cbDstLength
= 0;
1536 device
->acm_hdr
.cbDstLengthUsed
= 0;
1538 /* done with this ACM Header, so try to pull more data */
1539 WID_PullACMData(device
);
1544 /* out of WAVEHDRs to write into, so toss the rest of this packet */
1545 acmStreamUnprepareHeader(device
->acm_handle
, &device
->acm_hdr
, 0);
1546 HeapFree(GetProcessHeap(), 0, device
->acm_hdr
.pbDst
);
1547 device
->acm_hdr
.cbDstLength
= 0;
1548 device
->acm_hdr
.cbDstLengthUsed
= 0;
1551 static void WID_PullData(WINMM_Device
*device
)
1553 WINMM_CBInfo cb_info
;
1554 WAVEHDR
*queue
, *first
= NULL
, *last
= NULL
;
1557 TRACE("(%p)\n", device
->handle
);
1559 EnterCriticalSection(&device
->lock
);
1561 if(!device
->device
|| !device
->first
)
1564 first
= device
->first
;
1566 if(device
->acm_handle
){
1567 WID_PullACMData(device
);
1571 while(device
->first
){
1573 UINT32 packet_len
, packet
;
1576 hr
= IAudioCaptureClient_GetBuffer(device
->capture
, &data
, &packet_len
,
1577 &flags
, NULL
, NULL
);
1580 WARN("GetBuffer failed: %08x\n", hr
);
1581 else /* AUDCLNT_S_BUFFER_EMPTY success code */
1582 IAudioCaptureClient_ReleaseBuffer(device
->capture
, 0);
1586 packet
= packet_len
;
1587 queue
= device
->first
;
1588 while(queue
&& packet
> 0){
1589 UINT32 to_copy_bytes
;
1591 to_copy_bytes
= min(packet
* device
->bytes_per_frame
,
1592 queue
->dwBufferLength
- queue
->dwBytesRecorded
);
1594 memcpy(queue
->lpData
+ queue
->dwBytesRecorded
,
1595 data
+ (packet_len
- packet
) * device
->bytes_per_frame
,
1598 queue
->dwBytesRecorded
+= to_copy_bytes
;
1600 if(queue
->dwBufferLength
- queue
->dwBytesRecorded
<
1601 device
->bytes_per_frame
){
1603 device
->first
= queue
= queue
->lpNext
;
1606 packet
-= to_copy_bytes
/ device
->bytes_per_frame
;
1609 hr
= IAudioCaptureClient_ReleaseBuffer(device
->capture
, packet_len
);
1611 WARN("ReleaseBuffer failed: %08x\n", hr
);
1614 WARN("losing %u frames\n", packet
);
1615 device
->played_frames
+= packet_len
;
1619 cb_info
= device
->cb_info
;
1621 LeaveCriticalSection(&device
->lock
);
1624 last
->lpNext
= NULL
;
1626 WAVEHDR
*next
= first
->lpNext
;
1627 first
->dwFlags
&= ~WHDR_INQUEUE
;
1628 first
->dwFlags
|= WHDR_DONE
;
1629 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
1635 static HRESULT
WINMM_BeginPlaying(WINMM_Device
*device
)
1639 TRACE("(%p)\n", device
->handle
);
1642 /* prebuffer data before starting */
1643 WOD_PushData(device
);
1645 if(device
->stopped
){
1646 device
->stopped
= FALSE
;
1648 hr
= IAudioClient_Start(device
->client
);
1649 if(FAILED(hr
) && hr
!= AUDCLNT_E_NOT_STOPPED
){
1650 device
->stopped
= TRUE
;
1651 WARN("Start failed: %08x\n", hr
);
1659 static LRESULT
WINMM_Pause(HWAVE hwave
)
1661 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1664 TRACE("(%p)\n", hwave
);
1666 if(!WINMM_ValidateAndLock(device
))
1667 return MMSYSERR_INVALHANDLE
;
1669 hr
= IAudioClient_Stop(device
->client
);
1671 LeaveCriticalSection(&device
->lock
);
1672 WARN("Stop failed: %08x\n", hr
);
1673 return MMSYSERR_ERROR
;
1676 device
->stopped
= FALSE
;
1678 LeaveCriticalSection(&device
->lock
);
1680 return MMSYSERR_NOERROR
;
1683 static LRESULT
WINMM_Reset(HWAVE hwave
)
1685 WINMM_CBInfo cb_info
;
1686 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1691 TRACE("(%p)\n", hwave
);
1693 if(!WINMM_ValidateAndLock(device
))
1694 return MMSYSERR_INVALHANDLE
;
1696 hr
= IAudioClient_Stop(device
->client
);
1698 LeaveCriticalSection(&device
->lock
);
1699 WARN("Stop failed: %08x\n", hr
);
1700 return MMSYSERR_ERROR
;
1702 device
->stopped
= TRUE
;
1704 first
= device
->first
;
1705 device
->first
= device
->last
= device
->playing
= NULL
;
1706 device
->ofs_bytes
= 0;
1707 device
->played_frames
= 0;
1708 device
->loop_counter
= 0;
1709 device
->last_clock_pos
= 0;
1710 IAudioClient_Reset(device
->client
);
1712 cb_info
= device
->cb_info
;
1713 is_out
= device
->render
? TRUE
: FALSE
;
1715 LeaveCriticalSection(&device
->lock
);
1718 WAVEHDR
*next
= first
->lpNext
;
1719 first
->dwFlags
&= ~WHDR_INQUEUE
;
1720 first
->dwFlags
|= WHDR_DONE
;
1722 WINMM_NotifyClient(&cb_info
, WOM_DONE
, (DWORD_PTR
)first
, 0);
1724 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)first
, 0);
1728 return MMSYSERR_NOERROR
;
1731 static MMRESULT
WINMM_FramesToMMTime(MMTIME
*time
, UINT32 played_frames
,
1732 UINT32 sample_rate
, UINT32 bytes_per_frame
)
1734 switch(time
->wType
){
1736 time
->u
.sample
= played_frames
;
1737 return MMSYSERR_NOERROR
;
1739 time
->u
.ms
= (UINT64
)played_frames
* 1000 / sample_rate
;
1740 return MMSYSERR_NOERROR
;
1742 time
->u
.smpte
.fps
= 30;
1743 played_frames
+= sample_rate
/ time
->u
.smpte
.fps
- 1; /* round up */
1744 time
->u
.smpte
.frame
= (played_frames
% sample_rate
) * time
->u
.smpte
.fps
/ sample_rate
;
1745 played_frames
/= sample_rate
; /* yields seconds */
1746 time
->u
.smpte
.sec
= played_frames
% 60;
1747 played_frames
/= 60;
1748 time
->u
.smpte
.min
= played_frames
% 60;
1749 time
->u
.smpte
.hour
= played_frames
/ 60;
1750 return MMSYSERR_NOERROR
;
1752 time
->wType
= TIME_BYTES
;
1755 time
->u
.cb
= played_frames
* bytes_per_frame
;
1756 return MMSYSERR_NOERROR
;
1760 static LRESULT
WINMM_GetPosition(HWAVE hwave
, MMTIME
*time
)
1762 WINMM_Device
*device
= WINMM_GetDeviceFromHWAVE(hwave
);
1763 UINT32 played_frames
, sample_rate
, bytes_per_frame
;
1765 TRACE("(%p, %p)\n", hwave
, time
);
1767 if(!WINMM_ValidateAndLock(device
))
1768 return MMSYSERR_INVALHANDLE
;
1770 played_frames
= device
->played_frames
;
1771 sample_rate
= device
->samples_per_sec
;
1772 bytes_per_frame
= device
->bytes_per_frame
;
1774 LeaveCriticalSection(&device
->lock
);
1776 return WINMM_FramesToMMTime(time
, played_frames
, sample_rate
,
1780 static WINMM_MMDevice
*WINMM_GetMixerMMDevice(HMIXEROBJ hmix
, DWORD flags
,
1783 UINT mmdev
, dev
, junk
, *out
;
1791 switch(flags
& 0xF0000000){
1792 case MIXER_OBJECTF_MIXER
: /* == 0 */
1793 *out
= HandleToULong(hmix
);
1794 if(*out
< g_outmmdevices_count
)
1795 return &g_out_mmdevices
[*out
];
1796 if(*out
- g_outmmdevices_count
< g_inmmdevices_count
){
1797 *out
-= g_outmmdevices_count
;
1798 return &g_in_mmdevices
[*out
];
1800 /* fall through -- if it's not a valid mixer device, then
1801 * it could be a valid mixer handle. windows seems to do
1803 case MIXER_OBJECTF_HMIXER
:
1804 case MIXER_OBJECTF_HWAVEOUT
:
1805 case MIXER_OBJECTF_HWAVEIN
:
1806 WINMM_DecomposeHWAVE((HWAVE
)hmix
, out
, &is_out
, &dev
, &junk
);
1807 if(junk
!= 0x1 || (is_out
&& *out
>= g_outmmdevices_count
) ||
1808 (!is_out
&& *out
>= g_inmmdevices_count
))
1811 return &g_out_mmdevices
[*out
];
1812 return &g_in_mmdevices
[*out
];
1813 case MIXER_OBJECTF_WAVEOUT
:
1814 *out
= HandleToULong(hmix
);
1815 if(*out
< g_outmmdevices_count
)
1816 return &g_out_mmdevices
[*out
];
1818 case MIXER_OBJECTF_WAVEIN
:
1819 *out
= HandleToULong(hmix
);
1820 if(*out
< g_inmmdevices_count
)
1821 return &g_in_mmdevices
[*out
];
1828 static MMRESULT
WINMM_SetupMMDeviceVolume(WINMM_MMDevice
*mmdevice
)
1830 IAudioSessionManager
*sesman
;
1834 hr
= IMMDeviceEnumerator_GetDevice(g_devenum
, mmdevice
->dev_id
, &device
);
1836 WARN("Device %s (%s) unavailable: %08x\n",
1837 wine_dbgstr_w(mmdevice
->dev_id
),
1838 wine_dbgstr_w(mmdevice
->out_caps
.szPname
), hr
);
1839 return MMSYSERR_ERROR
;
1842 hr
= IMMDevice_Activate(device
, &IID_IAudioSessionManager
,
1843 CLSCTX_INPROC_SERVER
, NULL
, (void**)&sesman
);
1845 WARN("Activate failed: %08x\n", hr
);
1846 IMMDevice_Release(device
);
1847 return MMSYSERR_ERROR
;
1850 IMMDevice_Release(device
);
1852 hr
= IAudioSessionManager_GetSimpleAudioVolume(sesman
, &mmdevice
->session
,
1853 FALSE
, &mmdevice
->volume
);
1854 IAudioSessionManager_Release(sesman
);
1856 WARN("GetSimpleAudioVolume failed: %08x\n", hr
);
1857 return MMSYSERR_ERROR
;
1860 return MMSYSERR_NOERROR
;
1863 static LRESULT
MXD_GetControlDetails(WINMM_ControlDetails
*details
)
1865 WINMM_MMDevice
*mmdevice
;
1866 MIXERCONTROLDETAILS
*control
= details
->details
;
1869 TRACE("(%p)\n", details
->hmix
);
1871 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
1873 return MMSYSERR_INVALHANDLE
;
1875 EnterCriticalSection(&mmdevice
->lock
);
1877 if(!mmdevice
->volume
){
1880 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
1881 if(mr
!= MMSYSERR_NOERROR
){
1882 LeaveCriticalSection(&mmdevice
->lock
);
1887 if(control
->dwControlID
== 0){
1889 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
1891 if(!control
->paDetails
||
1892 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
1893 LeaveCriticalSection(&mmdevice
->lock
);
1894 return MMSYSERR_INVALPARAM
;
1897 hr
= ISimpleAudioVolume_GetMasterVolume(mmdevice
->volume
, &vol
);
1899 WARN("GetMasterVolume failed: %08x\n", hr
);
1900 LeaveCriticalSection(&mmdevice
->lock
);
1901 return MMSYSERR_ERROR
;
1904 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
1905 udet
->dwValue
= vol
* ((unsigned int)0xFFFF);
1906 }else if(control
->dwControlID
== 1){
1908 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
1910 if(!control
->paDetails
||
1911 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
1912 LeaveCriticalSection(&mmdevice
->lock
);
1913 return MMSYSERR_INVALPARAM
;
1916 hr
= ISimpleAudioVolume_GetMute(mmdevice
->volume
, &mute
);
1918 WARN("GetMute failed: %08x\n", hr
);
1919 LeaveCriticalSection(&mmdevice
->lock
);
1920 return MMSYSERR_ERROR
;
1923 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
1924 bdet
->fValue
= mute
;
1925 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
1926 FIXME("What should the sw-side mixer controls map to?\n");
1928 LeaveCriticalSection(&mmdevice
->lock
);
1929 return MIXERR_INVALCONTROL
;
1932 LeaveCriticalSection(&mmdevice
->lock
);
1934 return MMSYSERR_NOERROR
;
1937 static LRESULT
MXD_SetControlDetails(WINMM_ControlDetails
*details
)
1939 WINMM_MMDevice
*mmdevice
;
1940 MIXERCONTROLDETAILS
*control
= details
->details
;
1943 TRACE("(%p)\n", details
->hmix
);
1945 mmdevice
= WINMM_GetMixerMMDevice(details
->hmix
, details
->flags
, NULL
);
1947 return MMSYSERR_INVALHANDLE
;
1949 EnterCriticalSection(&mmdevice
->lock
);
1951 if(!mmdevice
->volume
){
1954 mr
= WINMM_SetupMMDeviceVolume(mmdevice
);
1955 if(mr
!= MMSYSERR_NOERROR
){
1956 LeaveCriticalSection(&mmdevice
->lock
);
1961 if(control
->dwControlID
== 0){
1963 MIXERCONTROLDETAILS_UNSIGNED
*udet
;
1965 if(!control
->paDetails
||
1966 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_UNSIGNED
)){
1967 LeaveCriticalSection(&mmdevice
->lock
);
1968 return MMSYSERR_INVALPARAM
;
1971 udet
= (MIXERCONTROLDETAILS_UNSIGNED
*)control
->paDetails
;
1973 if(udet
->dwValue
> 65535){
1974 LeaveCriticalSection(&mmdevice
->lock
);
1975 return MMSYSERR_INVALPARAM
;
1978 vol
= udet
->dwValue
/ 65535.f
;
1980 hr
= ISimpleAudioVolume_SetMasterVolume(mmdevice
->volume
, vol
, NULL
);
1982 WARN("SetMasterVolume failed: %08x\n", hr
);
1983 LeaveCriticalSection(&mmdevice
->lock
);
1984 return MMSYSERR_ERROR
;
1986 }else if(control
->dwControlID
== 1){
1988 MIXERCONTROLDETAILS_BOOLEAN
*bdet
;
1990 if(!control
->paDetails
||
1991 control
->cbDetails
< sizeof(MIXERCONTROLDETAILS_BOOLEAN
)){
1992 LeaveCriticalSection(&mmdevice
->lock
);
1993 return MMSYSERR_INVALPARAM
;
1996 bdet
= (MIXERCONTROLDETAILS_BOOLEAN
*)control
->paDetails
;
1997 mute
= bdet
->fValue
;
1999 hr
= ISimpleAudioVolume_SetMute(mmdevice
->volume
, mute
, NULL
);
2001 WARN("SetMute failed: %08x\n", hr
);
2002 LeaveCriticalSection(&mmdevice
->lock
);
2003 return MMSYSERR_ERROR
;
2005 }else if(control
->dwControlID
== 2 || control
->dwControlID
== 3){
2006 FIXME("What should the sw-side mixer controls map to?\n");
2008 LeaveCriticalSection(&mmdevice
->lock
);
2009 return MIXERR_INVALCONTROL
;
2012 LeaveCriticalSection(&mmdevice
->lock
);
2014 return MMSYSERR_NOERROR
;
2017 static LRESULT CALLBACK
WINMM_DevicesMsgProc(HWND hwnd
, UINT msg
, WPARAM wparam
,
2022 return WOD_Open((WINMM_OpenInfo
*)wparam
);
2024 return WOD_Close((HWAVEOUT
)wparam
);
2026 return WID_Open((WINMM_OpenInfo
*)wparam
);
2028 return WID_Close((HWAVEIN
)wparam
);
2029 case MXDM_GETCONTROLDETAILS
:
2030 return MXD_GetControlDetails((WINMM_ControlDetails
*)wparam
);
2031 case MXDM_SETCONTROLDETAILS
:
2032 return MXD_SetControlDetails((WINMM_ControlDetails
*)wparam
);
2034 return DefWindowProcW(hwnd
, msg
, wparam
, lparam
);
2037 static DWORD WINAPI
WINMM_DevicesThreadProc(void *arg
)
2041 static const WCHAR messageW
[] = {'M','e','s','s','a','g','e',0};
2043 hr
= CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
2045 WARN("CoInitializeEx failed: %08x\n", hr
);
2049 hr
= WINMM_InitMMDevices();
2055 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
2056 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&g_devenum
);
2058 WARN("CoCreateInstance failed: %08x\n", hr
);
2063 g_devices_hwnd
= CreateWindowW(messageW
, NULL
, 0, 0, 0, 0, 0,
2064 HWND_MESSAGE
, NULL
, NULL
, NULL
);
2065 if(!g_devices_hwnd
){
2066 WARN("CreateWindow failed: %d\n", GetLastError());
2067 IMMDeviceEnumerator_Release(g_devenum
);
2072 SetWindowLongPtrW(g_devices_hwnd
, GWLP_WNDPROC
,
2073 (LONG_PTR
)WINMM_DevicesMsgProc
);
2075 /* inform caller that the thread is ready to process messages */
2077 evt
= NULL
; /* do not use after this point */
2081 wait
= MsgWaitForMultipleObjects(g_devhandle_count
, g_device_handles
,
2082 FALSE
, INFINITE
, QS_ALLINPUT
);
2083 if(wait
== g_devhandle_count
+ WAIT_OBJECT_0
){
2085 if(PeekMessageW(&msg
, g_devices_hwnd
, 0, 0, PM_REMOVE
))
2086 WARN("Unexpected message: 0x%x\n", msg
.message
);
2087 }else if(wait
< g_devhandle_count
+ WAIT_OBJECT_0
){
2088 WINMM_Device
*device
= g_handle_devices
[wait
- WAIT_OBJECT_0
];
2090 WOD_PushData(device
);
2092 WID_PullData(device
);
2094 WARN("Unexpected MsgWait result 0x%x, GLE: %d\n", wait
,
2098 DestroyWindow(g_devices_hwnd
);
2100 IMMDeviceEnumerator_Release(g_devenum
);
2107 static BOOL
WINMM_StartDevicesThread(void)
2112 EnterCriticalSection(&g_devthread_lock
);
2114 if(g_devices_thread
){
2117 wait
= WaitForSingleObject(g_devices_thread
, 0);
2118 if(wait
== WAIT_TIMEOUT
){
2119 LeaveCriticalSection(&g_devthread_lock
);
2122 if(wait
!= WAIT_OBJECT_0
){
2123 LeaveCriticalSection(&g_devthread_lock
);
2127 g_devices_thread
= NULL
;
2128 g_devices_hwnd
= NULL
;
2131 TRACE("Starting up devices thread\n");
2133 events
[0] = CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2135 g_devices_thread
= CreateThread(NULL
, 0, WINMM_DevicesThreadProc
,
2136 events
[0], 0, NULL
);
2137 if(!g_devices_thread
){
2138 LeaveCriticalSection(&g_devthread_lock
);
2139 CloseHandle(events
[0]);
2143 events
[1] = g_devices_thread
;
2144 wait
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
2145 CloseHandle(events
[0]);
2146 if(wait
!= WAIT_OBJECT_0
){
2147 if(wait
== 1 + WAIT_OBJECT_0
){
2148 CloseHandle(g_devices_thread
);
2149 g_devices_thread
= NULL
;
2150 g_devices_hwnd
= NULL
;
2152 LeaveCriticalSection(&g_devthread_lock
);
2156 LeaveCriticalSection(&g_devthread_lock
);
2161 /**************************************************************************
2162 * waveOutGetNumDevs [WINMM.@]
2164 UINT WINAPI
waveOutGetNumDevs(void)
2166 HRESULT hr
= WINMM_InitMMDevices();
2170 TRACE("count: %u\n", g_outmmdevices_count
);
2172 return g_outmmdevices_count
;
2175 /**************************************************************************
2176 * waveOutGetDevCapsA [WINMM.@]
2178 UINT WINAPI
waveOutGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEOUTCAPSA lpCaps
,
2184 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2187 return MMSYSERR_INVALPARAM
;
2189 ret
= waveOutGetDevCapsW(uDeviceID
, &wocW
, sizeof(wocW
));
2191 if (ret
== MMSYSERR_NOERROR
) {
2193 wocA
.wMid
= wocW
.wMid
;
2194 wocA
.wPid
= wocW
.wPid
;
2195 wocA
.vDriverVersion
= wocW
.vDriverVersion
;
2196 WideCharToMultiByte( CP_ACP
, 0, wocW
.szPname
, -1, wocA
.szPname
,
2197 sizeof(wocA
.szPname
), NULL
, NULL
);
2198 wocA
.dwFormats
= wocW
.dwFormats
;
2199 wocA
.wChannels
= wocW
.wChannels
;
2200 wocA
.dwSupport
= wocW
.dwSupport
;
2201 memcpy(lpCaps
, &wocA
, min(uSize
, sizeof(wocA
)));
2206 /**************************************************************************
2207 * waveOutGetDevCapsW [WINMM.@]
2209 UINT WINAPI
waveOutGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEOUTCAPSW lpCaps
,
2212 WAVEOUTCAPSW mapper_caps
, *caps
;
2215 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2217 hr
= WINMM_InitMMDevices();
2219 return MMSYSERR_ERROR
;
2221 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
2223 if(WINMM_IsMapper(uDeviceID
)){
2224 /* FIXME: Should be localized */
2225 static const WCHAR mapper_pnameW
[] = {'W','i','n','e',' ','S','o','u',
2226 'n','d',' ','M','a','p','p','e','r',0};
2228 mapper_caps
.wMid
= 0xFF;
2229 mapper_caps
.wPid
= 0xFF;
2230 mapper_caps
.vDriverVersion
= 0x00010001;
2231 mapper_caps
.dwFormats
= 0xFFFFFFFF;
2232 mapper_caps
.wReserved1
= 0;
2233 mapper_caps
.dwSupport
= WAVECAPS_LRVOLUME
| WAVECAPS_VOLUME
|
2234 WAVECAPS_SAMPLEACCURATE
;
2235 mapper_caps
.wChannels
= 2;
2236 lstrcpyW(mapper_caps
.szPname
, mapper_pnameW
);
2238 caps
= &mapper_caps
;
2240 if(uDeviceID
>= g_outmmdevices_count
)
2241 return MMSYSERR_BADDEVICEID
;
2243 caps
= &g_out_mmdevices
[uDeviceID
].out_caps
;
2246 memcpy(lpCaps
, caps
, min(uSize
, sizeof(*lpCaps
)));
2248 return MMSYSERR_NOERROR
;
2251 /**************************************************************************
2252 * waveOutGetErrorTextA [WINMM.@]
2253 * waveInGetErrorTextA [WINMM.@]
2255 UINT WINAPI
waveOutGetErrorTextA(UINT uError
, LPSTR lpText
, UINT uSize
)
2259 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
2260 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
2263 LPWSTR xstr
= HeapAlloc(GetProcessHeap(), 0, uSize
* sizeof(WCHAR
));
2264 if (!xstr
) ret
= MMSYSERR_NOMEM
;
2267 ret
= waveOutGetErrorTextW(uError
, xstr
, uSize
);
2268 if (ret
== MMSYSERR_NOERROR
)
2269 WideCharToMultiByte(CP_ACP
, 0, xstr
, -1, lpText
, uSize
, NULL
, NULL
);
2270 HeapFree(GetProcessHeap(), 0, xstr
);
2276 /**************************************************************************
2277 * waveOutGetErrorTextW [WINMM.@]
2278 * waveInGetErrorTextW [WINMM.@]
2280 UINT WINAPI
waveOutGetErrorTextW(UINT uError
, LPWSTR lpText
, UINT uSize
)
2282 UINT ret
= MMSYSERR_BADERRNUM
;
2284 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
2285 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
2287 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2288 * a warning for the test was always true */
2289 (/*uError >= MMSYSERR_BASE && */ uError
<= MMSYSERR_LASTERROR
) ||
2290 (uError
>= WAVERR_BASE
&& uError
<= WAVERR_LASTERROR
)) {
2291 if (LoadStringW(hWinMM32Instance
,
2292 uError
, lpText
, uSize
) > 0) {
2293 ret
= MMSYSERR_NOERROR
;
2299 /**************************************************************************
2300 * waveOutOpen [WINMM.@]
2301 * All the args/structs have the same layout as the win16 equivalents
2303 MMRESULT WINAPI
waveOutOpen(LPHWAVEOUT lphWaveOut
, UINT uDeviceID
,
2304 LPCWAVEFORMATEX lpFormat
, DWORD_PTR dwCallback
,
2305 DWORD_PTR dwInstance
, DWORD dwFlags
)
2308 WINMM_OpenInfo info
;
2309 WINMM_CBInfo cb_info
;
2311 TRACE("(%p, %u, %p, %lx, %lx, %08x)\n", lphWaveOut
, uDeviceID
, lpFormat
,
2312 dwCallback
, dwInstance
, dwFlags
);
2314 if(!WINMM_StartDevicesThread())
2315 return MMSYSERR_ERROR
;
2317 if(!lphWaveOut
&& !(dwFlags
& WAVE_FORMAT_QUERY
))
2318 return MMSYSERR_INVALPARAM
;
2320 res
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
2321 if(res
!= MMSYSERR_NOERROR
)
2324 info
.format
= (WAVEFORMATEX
*)lpFormat
;
2325 info
.callback
= dwCallback
;
2326 info
.cb_user
= dwInstance
;
2327 info
.req_device
= uDeviceID
;
2328 info
.flags
= dwFlags
;
2330 res
= SendMessageW(g_devices_hwnd
, WODM_OPEN
, (DWORD_PTR
)&info
, 0);
2331 if(res
!= MMSYSERR_NOERROR
)
2335 *lphWaveOut
= (HWAVEOUT
)info
.handle
;
2337 cb_info
.flags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2338 cb_info
.callback
= dwCallback
;
2339 cb_info
.user
= dwInstance
;
2340 cb_info
.hwave
= info
.handle
;
2342 WINMM_NotifyClient(&cb_info
, WOM_OPEN
, 0, 0);
2347 /**************************************************************************
2348 * waveOutClose [WINMM.@]
2350 UINT WINAPI
waveOutClose(HWAVEOUT hWaveOut
)
2353 WINMM_Device
*device
;
2354 WINMM_CBInfo cb_info
;
2356 TRACE("(%p)\n", hWaveOut
);
2358 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2360 if(!WINMM_ValidateAndLock(device
))
2361 return MMSYSERR_INVALHANDLE
;
2363 cb_info
= device
->cb_info
;
2365 LeaveCriticalSection(&device
->lock
);
2367 res
= SendMessageW(g_devices_hwnd
, WODM_CLOSE
, (WPARAM
)hWaveOut
, 0);
2369 if(res
== MMSYSERR_NOERROR
)
2370 WINMM_NotifyClient(&cb_info
, WOM_CLOSE
, 0, 0);
2375 /**************************************************************************
2376 * waveOutPrepareHeader [WINMM.@]
2378 UINT WINAPI
waveOutPrepareHeader(HWAVEOUT hWaveOut
,
2379 WAVEHDR
* lpWaveOutHdr
, UINT uSize
)
2381 TRACE("(%p, %p, %u)\n", hWaveOut
, lpWaveOutHdr
, uSize
);
2383 if(!lpWaveOutHdr
|| uSize
< sizeof(WAVEHDR
))
2384 return MMSYSERR_INVALPARAM
;
2386 if(lpWaveOutHdr
->dwFlags
& WHDR_INQUEUE
)
2387 return WAVERR_STILLPLAYING
;
2389 return WINMM_PrepareHeader((HWAVE
)hWaveOut
, lpWaveOutHdr
);
2392 /**************************************************************************
2393 * waveOutUnprepareHeader [WINMM.@]
2395 UINT WINAPI
waveOutUnprepareHeader(HWAVEOUT hWaveOut
,
2396 LPWAVEHDR lpWaveOutHdr
, UINT uSize
)
2398 TRACE("(%p, %p, %u)\n", hWaveOut
, lpWaveOutHdr
, uSize
);
2400 if(!lpWaveOutHdr
|| uSize
< sizeof(WAVEHDR
))
2401 return MMSYSERR_INVALPARAM
;
2403 if(!(lpWaveOutHdr
->dwFlags
& WHDR_PREPARED
))
2404 return MMSYSERR_NOERROR
;
2406 if(lpWaveOutHdr
->dwFlags
& WHDR_INQUEUE
)
2407 return WAVERR_STILLPLAYING
;
2409 return WINMM_UnprepareHeader((HWAVE
)hWaveOut
, lpWaveOutHdr
);
2412 /**************************************************************************
2413 * waveOutWrite [WINMM.@]
2415 UINT WINAPI
waveOutWrite(HWAVEOUT hWaveOut
, WAVEHDR
*header
, UINT uSize
)
2417 WINMM_Device
*device
;
2420 TRACE("(%p, %p, %u)\n", hWaveOut
, header
, uSize
);
2422 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2424 if(!WINMM_ValidateAndLock(device
))
2425 return MMSYSERR_INVALHANDLE
;
2427 if(!header
->lpData
|| !(header
->dwFlags
& WHDR_PREPARED
)){
2428 LeaveCriticalSection(&device
->lock
);
2429 return WAVERR_UNPREPARED
;
2432 if(header
->dwFlags
& WHDR_INQUEUE
){
2433 LeaveCriticalSection(&device
->lock
);
2434 return WAVERR_STILLPLAYING
;
2437 if(device
->acm_handle
){
2438 ACMSTREAMHEADER
*ash
= (ACMSTREAMHEADER
*)header
->reserved
;
2441 ash
->cbSrcLength
= header
->dwBufferLength
;
2442 mr
= acmStreamConvert(device
->acm_handle
, ash
, 0);
2443 if(mr
!= MMSYSERR_NOERROR
){
2444 LeaveCriticalSection(&device
->lock
);
2450 device
->last
->lpNext
= header
;
2451 device
->last
= header
;
2452 if(!device
->playing
)
2453 device
->playing
= header
;
2455 device
->playing
= device
->first
= device
->last
= header
;
2456 if(header
->dwFlags
& WHDR_BEGINLOOP
){
2457 device
->loop_counter
= header
->dwLoops
;
2458 device
->loop_start
= header
;
2462 header
->lpNext
= NULL
;
2463 header
->dwFlags
&= ~WHDR_DONE
;
2464 header
->dwFlags
|= WHDR_INQUEUE
;
2466 hr
= WINMM_BeginPlaying(device
);
2468 LeaveCriticalSection(&device
->lock
);
2469 return MMSYSERR_ERROR
;
2472 LeaveCriticalSection(&device
->lock
);
2474 return MMSYSERR_NOERROR
;
2477 /**************************************************************************
2478 * waveOutBreakLoop [WINMM.@]
2480 UINT WINAPI
waveOutBreakLoop(HWAVEOUT hWaveOut
)
2482 WINMM_Device
*device
;
2484 TRACE("(%p)\n", hWaveOut
);
2486 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2488 if(!WINMM_ValidateAndLock(device
))
2489 return MMSYSERR_INVALHANDLE
;
2491 device
->loop_counter
= 0;
2493 LeaveCriticalSection(&device
->lock
);
2495 return MMSYSERR_NOERROR
;
2498 /**************************************************************************
2499 * waveOutPause [WINMM.@]
2501 UINT WINAPI
waveOutPause(HWAVEOUT hWaveOut
)
2503 TRACE("(%p)\n", hWaveOut
);
2505 return WINMM_Pause((HWAVE
)hWaveOut
);
2508 /**************************************************************************
2509 * waveOutReset [WINMM.@]
2511 UINT WINAPI
waveOutReset(HWAVEOUT hWaveOut
)
2513 TRACE("(%p)\n", hWaveOut
);
2515 return WINMM_Reset((HWAVE
)hWaveOut
);
2518 /**************************************************************************
2519 * waveOutRestart [WINMM.@]
2521 UINT WINAPI
waveOutRestart(HWAVEOUT hWaveOut
)
2523 WINMM_Device
*device
;
2526 TRACE("(%p)\n", hWaveOut
);
2528 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2530 if(!WINMM_ValidateAndLock(device
))
2531 return MMSYSERR_INVALHANDLE
;
2533 device
->stopped
= TRUE
;
2535 hr
= WINMM_BeginPlaying(device
);
2537 LeaveCriticalSection(&device
->lock
);
2538 return MMSYSERR_ERROR
;
2541 LeaveCriticalSection(&device
->lock
);
2543 return MMSYSERR_NOERROR
;
2546 /**************************************************************************
2547 * waveOutGetPosition [WINMM.@]
2549 UINT WINAPI
waveOutGetPosition(HWAVEOUT hWaveOut
, LPMMTIME lpTime
,
2552 TRACE("(%p, %p, %u)\n", hWaveOut
, lpTime
, uSize
);
2554 if(!uSize
|| !lpTime
|| uSize
!= sizeof(MMTIME
))
2555 return MMSYSERR_INVALPARAM
;
2557 return WINMM_GetPosition((HWAVE
)hWaveOut
, lpTime
);
2560 /**************************************************************************
2561 * waveOutGetPitch [WINMM.@]
2563 UINT WINAPI
waveOutGetPitch(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
2565 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
2566 return MMSYSERR_NOTSUPPORTED
;
2569 /**************************************************************************
2570 * waveOutSetPitch [WINMM.@]
2572 UINT WINAPI
waveOutSetPitch(HWAVEOUT hWaveOut
, DWORD dw
)
2574 TRACE("(%p, %08x)\n", hWaveOut
, dw
);
2576 return MMSYSERR_NOTSUPPORTED
;
2579 /**************************************************************************
2580 * waveOutGetPlaybackRate [WINMM.@]
2582 UINT WINAPI
waveOutGetPlaybackRate(HWAVEOUT hWaveOut
, LPDWORD lpdw
)
2584 TRACE("(%p, %p)\n", hWaveOut
, lpdw
);
2586 return MMSYSERR_NOTSUPPORTED
;
2589 /**************************************************************************
2590 * waveOutSetPlaybackRate [WINMM.@]
2592 UINT WINAPI
waveOutSetPlaybackRate(HWAVEOUT hWaveOut
, DWORD dw
)
2594 TRACE("(%p, %08x)\n", hWaveOut
, dw
);
2596 return MMSYSERR_NOTSUPPORTED
;
2599 /**************************************************************************
2600 * waveOutGetVolume [WINMM.@]
2602 UINT WINAPI
waveOutGetVolume(HWAVEOUT hWaveOut
, DWORD
*out
)
2604 WINMM_Device
*device
;
2609 TRACE("(%p, %p)\n", hWaveOut
, out
);
2612 return MMSYSERR_INVALPARAM
;
2614 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2616 if(!WINMM_ValidateAndLock(device
))
2617 return MMSYSERR_INVALHANDLE
;
2619 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
2621 LeaveCriticalSection(&device
->lock
);
2622 WARN("GetChannelCount failed: %08x\n", hr
);
2623 return MMSYSERR_ERROR
;
2626 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
2628 LeaveCriticalSection(&device
->lock
);
2629 return MMSYSERR_NOMEM
;
2632 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
2634 LeaveCriticalSection(&device
->lock
);
2635 HeapFree(GetProcessHeap(), 0, vols
);
2636 WARN("GetAllVolumes failed: %08x\n", hr
);
2637 return MMSYSERR_ERROR
;
2640 LeaveCriticalSection(&device
->lock
);
2642 *out
= ((UINT16
)(vols
[0] * (DWORD
)0xFFFF));
2644 *out
|= ((UINT16
)(vols
[1] * (DWORD
)0xFFFF)) << 16;
2646 HeapFree(GetProcessHeap(), 0, vols
);
2648 return MMSYSERR_NOERROR
;
2651 /**************************************************************************
2652 * waveOutSetVolume [WINMM.@]
2654 UINT WINAPI
waveOutSetVolume(HWAVEOUT hWaveOut
, DWORD in
)
2656 WINMM_Device
*device
;
2661 TRACE("(%p, %08x)\n", hWaveOut
, in
);
2663 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2665 if(!WINMM_ValidateAndLock(device
))
2666 return MMSYSERR_INVALHANDLE
;
2668 hr
= IAudioStreamVolume_GetChannelCount(device
->volume
, &channels
);
2670 LeaveCriticalSection(&device
->lock
);
2671 WARN("GetChannelCount failed: %08x\n", hr
);
2672 return MMSYSERR_ERROR
;
2675 vols
= HeapAlloc(GetProcessHeap(), 0, sizeof(float) * channels
);
2677 LeaveCriticalSection(&device
->lock
);
2678 return MMSYSERR_NOMEM
;
2681 hr
= IAudioStreamVolume_GetAllVolumes(device
->volume
, channels
, vols
);
2683 LeaveCriticalSection(&device
->lock
);
2684 HeapFree(GetProcessHeap(), 0, vols
);
2685 WARN("GetAllVolumes failed: %08x\n", hr
);
2686 return MMSYSERR_ERROR
;
2689 vols
[0] = (float)((DWORD
)(in
& 0xFFFF) / (float)0xFFFF);
2691 vols
[1] = (float)((DWORD
)(in
>> 16) / (float)0xFFFF);
2693 hr
= IAudioStreamVolume_SetAllVolumes(device
->volume
, channels
, vols
);
2695 LeaveCriticalSection(&device
->lock
);
2696 HeapFree(GetProcessHeap(), 0, vols
);
2697 WARN("SetAllVolumes failed: %08x\n", hr
);
2698 return MMSYSERR_ERROR
;
2701 LeaveCriticalSection(&device
->lock
);
2703 HeapFree(GetProcessHeap(), 0, vols
);
2705 return MMSYSERR_NOERROR
;
2708 /**************************************************************************
2709 * waveOutGetID [WINMM.@]
2711 UINT WINAPI
waveOutGetID(HWAVEOUT hWaveOut
, UINT
* lpuDeviceID
)
2713 WINMM_Device
*device
;
2717 TRACE("(%p, %p)\n", hWaveOut
, lpuDeviceID
);
2720 return MMSYSERR_INVALPARAM
;
2722 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveOut
);
2723 if(!WINMM_ValidateAndLock(device
))
2724 return MMSYSERR_INVALHANDLE
;
2726 LeaveCriticalSection(&device
->lock
);
2728 WINMM_DecomposeHWAVE((HWAVE
)hWaveOut
, lpuDeviceID
, &is_out
, &dev
, &junk
);
2730 return MMSYSERR_NOERROR
;
2733 static UINT
WINMM_QueryInstanceIDSize(UINT device
, DWORD_PTR
*len
, BOOL is_out
)
2736 WINMM_MMDevice
*devices
;
2738 TRACE("(%u, %p, %d)\n", device
, len
, is_out
);
2741 count
= g_outmmdevices_count
;
2742 devices
= g_out_mmdevices
;
2744 count
= g_inmmdevices_count
;
2745 devices
= g_in_mmdevices
;
2749 return MMSYSERR_INVALHANDLE
;
2751 *len
= (lstrlenW(devices
[device
].dev_id
) + 1) * sizeof(WCHAR
);
2753 return MMSYSERR_NOERROR
;
2756 static UINT
WINMM_QueryInstanceID(UINT device
, WCHAR
*str
, DWORD_PTR len
,
2760 WINMM_MMDevice
*devices
;
2762 TRACE("(%u, %p, %d)\n", device
, str
, is_out
);
2765 count
= g_outmmdevices_count
;
2766 devices
= g_out_mmdevices
;
2768 count
= g_inmmdevices_count
;
2769 devices
= g_in_mmdevices
;
2773 return MMSYSERR_INVALHANDLE
;
2775 id_len
= (lstrlenW(devices
[device
].dev_id
) + 1) * sizeof(WCHAR
);
2777 return MMSYSERR_ERROR
;
2779 memcpy(str
, devices
[device
].dev_id
, id_len
);
2781 return MMSYSERR_NOERROR
;
2784 /**************************************************************************
2785 * waveOutMessage [WINMM.@]
2787 UINT WINAPI
waveOutMessage(HWAVEOUT hWaveOut
, UINT uMessage
,
2788 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
2790 TRACE("(%p, %u, %lx, %lx)\n", hWaveOut
, uMessage
, dwParam1
, dwParam2
);
2793 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
2794 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveOut
),
2795 (DWORD_PTR
*)dwParam1
, TRUE
);
2796 case DRV_QUERYFUNCTIONINSTANCEID
:
2797 return WINMM_QueryInstanceID(HandleToULong(hWaveOut
), (WCHAR
*)dwParam1
, dwParam2
, TRUE
);
2798 case DRV_QUERYMAPPABLE
:
2799 return MMSYSERR_NOERROR
;
2800 case DRVM_MAPPER_PREFERRED_GET
:
2802 if(g_outmmdevices_count
> 0)
2803 /* Device 0 is always the default device */
2804 *(DWORD
*)dwParam1
= 0;
2806 *(DWORD
*)dwParam1
= -1;
2811 *(DWORD
*)dwParam2
= 0;
2813 return MMSYSERR_NOERROR
;
2816 TRACE("Message not supported: %u\n", uMessage
);
2818 return MMSYSERR_NOTSUPPORTED
;
2821 /**************************************************************************
2822 * waveInGetNumDevs [WINMM.@]
2824 UINT WINAPI
waveInGetNumDevs(void)
2826 HRESULT hr
= WINMM_InitMMDevices();
2830 TRACE("count: %u\n", g_inmmdevices_count
);
2832 return g_inmmdevices_count
;
2835 /**************************************************************************
2836 * waveInGetDevCapsW [WINMM.@]
2838 UINT WINAPI
waveInGetDevCapsW(UINT_PTR uDeviceID
, LPWAVEINCAPSW lpCaps
, UINT uSize
)
2840 WAVEINCAPSW mapper_caps
, *caps
;
2843 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2845 hr
= WINMM_InitMMDevices();
2847 return MMSYSERR_ERROR
;
2850 return MMSYSERR_INVALPARAM
;
2852 if(WINMM_IsMapper(uDeviceID
)){
2853 /* FIXME: Should be localized */
2854 static const WCHAR mapper_pnameW
[] = {'W','i','n','e',' ','S','o','u',
2855 'n','d',' ','M','a','p','p','e','r',0};
2857 mapper_caps
.wMid
= 0xFF;
2858 mapper_caps
.wPid
= 0xFF;
2859 mapper_caps
.vDriverVersion
= 0x00010001;
2860 mapper_caps
.dwFormats
= 0xFFFFFFFF;
2861 mapper_caps
.wReserved1
= 0;
2862 mapper_caps
.wChannels
= 2;
2863 lstrcpyW(mapper_caps
.szPname
, mapper_pnameW
);
2865 caps
= &mapper_caps
;
2867 if(uDeviceID
>= g_inmmdevices_count
)
2868 return MMSYSERR_BADDEVICEID
;
2870 caps
= &g_in_mmdevices
[uDeviceID
].in_caps
;
2873 memcpy(lpCaps
, caps
, min(uSize
, sizeof(*lpCaps
)));
2875 return MMSYSERR_NOERROR
;
2878 /**************************************************************************
2879 * waveInGetDevCapsA [WINMM.@]
2881 UINT WINAPI
waveInGetDevCapsA(UINT_PTR uDeviceID
, LPWAVEINCAPSA lpCaps
, UINT uSize
)
2886 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
2889 return MMSYSERR_INVALPARAM
;
2891 ret
= waveInGetDevCapsW(uDeviceID
, &wicW
, sizeof(wicW
));
2893 if (ret
== MMSYSERR_NOERROR
) {
2895 wicA
.wMid
= wicW
.wMid
;
2896 wicA
.wPid
= wicW
.wPid
;
2897 wicA
.vDriverVersion
= wicW
.vDriverVersion
;
2898 WideCharToMultiByte( CP_ACP
, 0, wicW
.szPname
, -1, wicA
.szPname
,
2899 sizeof(wicA
.szPname
), NULL
, NULL
);
2900 wicA
.dwFormats
= wicW
.dwFormats
;
2901 wicA
.wChannels
= wicW
.wChannels
;
2902 memcpy(lpCaps
, &wicA
, min(uSize
, sizeof(wicA
)));
2907 /**************************************************************************
2908 * waveInOpen [WINMM.@]
2910 MMRESULT WINAPI
waveInOpen(HWAVEIN
* lphWaveIn
, UINT uDeviceID
,
2911 LPCWAVEFORMATEX lpFormat
, DWORD_PTR dwCallback
,
2912 DWORD_PTR dwInstance
, DWORD dwFlags
)
2915 WINMM_OpenInfo info
;
2916 WINMM_CBInfo cb_info
;
2918 TRACE("(%p, %x, %p, %lx, %lx, %08x)\n", lphWaveIn
, uDeviceID
, lpFormat
,
2919 dwCallback
, dwInstance
, dwFlags
);
2921 if(!WINMM_StartDevicesThread())
2922 return MMSYSERR_ERROR
;
2924 if(!lphWaveIn
&& !(dwFlags
& WAVE_FORMAT_QUERY
))
2925 return MMSYSERR_INVALPARAM
;
2927 res
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
2928 if(res
!= MMSYSERR_NOERROR
)
2931 info
.format
= (WAVEFORMATEX
*)lpFormat
;
2932 info
.callback
= dwCallback
;
2933 info
.cb_user
= dwInstance
;
2934 info
.req_device
= uDeviceID
;
2935 info
.flags
= dwFlags
;
2937 res
= SendMessageW(g_devices_hwnd
, WIDM_OPEN
, (DWORD_PTR
)&info
, 0);
2938 if(res
!= MMSYSERR_NOERROR
)
2942 *lphWaveIn
= (HWAVEIN
)info
.handle
;
2944 cb_info
.flags
= HIWORD(dwFlags
& CALLBACK_TYPEMASK
);
2945 cb_info
.callback
= dwCallback
;
2946 cb_info
.user
= dwInstance
;
2947 cb_info
.hwave
= info
.handle
;
2949 WINMM_NotifyClient(&cb_info
, WIM_OPEN
, 0, 0);
2954 /**************************************************************************
2955 * waveInClose [WINMM.@]
2957 UINT WINAPI
waveInClose(HWAVEIN hWaveIn
)
2959 WINMM_Device
*device
;
2960 WINMM_CBInfo cb_info
;
2963 TRACE("(%p)\n", hWaveIn
);
2965 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
2967 if(!WINMM_ValidateAndLock(device
))
2968 return MMSYSERR_INVALHANDLE
;
2970 cb_info
= device
->cb_info
;
2972 LeaveCriticalSection(&device
->lock
);
2974 res
= SendMessageW(g_devices_hwnd
, WIDM_CLOSE
, (WPARAM
)hWaveIn
, 0);
2976 if(res
== MMSYSERR_NOERROR
)
2977 WINMM_NotifyClient(&cb_info
, WIM_CLOSE
, 0, 0);
2982 /**************************************************************************
2983 * waveInPrepareHeader [WINMM.@]
2985 UINT WINAPI
waveInPrepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
2988 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
2990 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
2991 return MMSYSERR_INVALPARAM
;
2993 if(lpWaveInHdr
->dwFlags
& WHDR_INQUEUE
)
2994 return WAVERR_STILLPLAYING
;
2996 return WINMM_PrepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
2999 /**************************************************************************
3000 * waveInUnprepareHeader [WINMM.@]
3002 UINT WINAPI
waveInUnprepareHeader(HWAVEIN hWaveIn
, WAVEHDR
* lpWaveInHdr
,
3005 TRACE("(%p, %p, %u)\n", hWaveIn
, lpWaveInHdr
, uSize
);
3007 if(!lpWaveInHdr
|| uSize
< sizeof(WAVEHDR
))
3008 return MMSYSERR_INVALPARAM
;
3010 if(!(lpWaveInHdr
->dwFlags
& WHDR_PREPARED
))
3011 return MMSYSERR_NOERROR
;
3013 if(lpWaveInHdr
->dwFlags
& WHDR_INQUEUE
)
3014 return WAVERR_STILLPLAYING
;
3016 return WINMM_UnprepareHeader((HWAVE
)hWaveIn
, lpWaveInHdr
);
3019 /**************************************************************************
3020 * waveInAddBuffer [WINMM.@]
3022 UINT WINAPI
waveInAddBuffer(HWAVEIN hWaveIn
, WAVEHDR
*header
, UINT uSize
)
3024 WINMM_Device
*device
;
3026 TRACE("(%p, %p, %u)\n", hWaveIn
, header
, uSize
);
3028 if(!header
|| uSize
< sizeof(WAVEHDR
))
3029 return MMSYSERR_INVALPARAM
;
3031 if(!(header
->dwFlags
& WHDR_PREPARED
))
3032 return WAVERR_UNPREPARED
;
3034 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3036 if(!WINMM_ValidateAndLock(device
))
3037 return MMSYSERR_INVALHANDLE
;
3040 device
->first
= device
->last
= header
;
3042 device
->last
->lpNext
= header
;
3043 device
->last
= header
;
3046 header
->dwBytesRecorded
= 0;
3047 header
->lpNext
= NULL
;
3048 header
->dwFlags
&= ~WHDR_DONE
;
3049 header
->dwFlags
|= WHDR_INQUEUE
;
3051 LeaveCriticalSection(&device
->lock
);
3053 return MMSYSERR_NOERROR
;
3056 /**************************************************************************
3057 * waveInReset [WINMM.@]
3059 UINT WINAPI
waveInReset(HWAVEIN hWaveIn
)
3061 TRACE("(%p)\n", hWaveIn
);
3063 return WINMM_Reset((HWAVE
)hWaveIn
);
3066 /**************************************************************************
3067 * waveInStart [WINMM.@]
3069 UINT WINAPI
waveInStart(HWAVEIN hWaveIn
)
3071 WINMM_Device
*device
;
3074 TRACE("(%p)\n", hWaveIn
);
3076 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3078 if(!WINMM_ValidateAndLock(device
))
3079 return MMSYSERR_INVALHANDLE
;
3081 hr
= WINMM_BeginPlaying(device
);
3083 LeaveCriticalSection(&device
->lock
);
3084 return MMSYSERR_ERROR
;
3087 LeaveCriticalSection(&device
->lock
);
3089 return MMSYSERR_NOERROR
;
3092 /**************************************************************************
3093 * waveInStop [WINMM.@]
3095 UINT WINAPI
waveInStop(HWAVEIN hWaveIn
)
3097 WINMM_CBInfo cb_info
;
3098 WINMM_Device
*device
;
3102 TRACE("(%p)\n", hWaveIn
);
3104 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3106 if(!WINMM_ValidateAndLock(device
))
3107 return MMSYSERR_INVALHANDLE
;
3109 hr
= WINMM_Pause((HWAVE
)hWaveIn
);
3111 LeaveCriticalSection(&device
->lock
);
3112 return MMSYSERR_ERROR
;
3114 device
->stopped
= TRUE
;
3116 buf
= device
->first
;
3117 if(buf
&& buf
->dwBytesRecorded
> 0){
3118 device
->first
= buf
->lpNext
;
3122 cb_info
= device
->cb_info
;
3124 LeaveCriticalSection(&device
->lock
);
3127 buf
->dwFlags
&= ~WHDR_INQUEUE
;
3128 buf
->dwFlags
|= WHDR_DONE
;
3129 WINMM_NotifyClient(&cb_info
, WIM_DATA
, (DWORD_PTR
)buf
, 0);
3132 return MMSYSERR_NOERROR
;
3135 /**************************************************************************
3136 * waveInGetPosition [WINMM.@]
3138 UINT WINAPI
waveInGetPosition(HWAVEIN hWaveIn
, LPMMTIME lpTime
,
3141 TRACE("(%p, %p, %u)\n", hWaveIn
, lpTime
, uSize
);
3143 if(!uSize
|| !lpTime
|| uSize
!= sizeof(MMTIME
))
3144 return MMSYSERR_INVALPARAM
;
3146 return WINMM_GetPosition((HWAVE
)hWaveIn
, lpTime
);
3149 /**************************************************************************
3150 * waveInGetID [WINMM.@]
3152 UINT WINAPI
waveInGetID(HWAVEIN hWaveIn
, UINT
* lpuDeviceID
)
3156 WINMM_Device
*device
;
3158 TRACE("(%p, %p)\n", hWaveIn
, lpuDeviceID
);
3161 return MMSYSERR_INVALPARAM
;
3163 device
= WINMM_GetDeviceFromHWAVE((HWAVE
)hWaveIn
);
3164 if(!WINMM_ValidateAndLock(device
))
3165 return MMSYSERR_INVALHANDLE
;
3167 LeaveCriticalSection(&device
->lock
);
3169 WINMM_DecomposeHWAVE((HWAVE
)hWaveIn
, lpuDeviceID
, &is_out
, &dev
, &junk
);
3171 return MMSYSERR_NOERROR
;
3174 /**************************************************************************
3175 * waveInMessage [WINMM.@]
3177 UINT WINAPI
waveInMessage(HWAVEIN hWaveIn
, UINT uMessage
,
3178 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3180 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn
, uMessage
, dwParam1
, dwParam2
);
3183 case DRV_QUERYFUNCTIONINSTANCEIDSIZE
:
3184 return WINMM_QueryInstanceIDSize(HandleToULong(hWaveIn
),
3185 (DWORD_PTR
*)dwParam1
, FALSE
);
3186 case DRV_QUERYFUNCTIONINSTANCEID
:
3187 return WINMM_QueryInstanceID(HandleToULong(hWaveIn
), (WCHAR
*)dwParam1
, dwParam2
, FALSE
);
3188 case DRV_QUERYMAPPABLE
:
3189 return MMSYSERR_NOERROR
;
3190 case DRVM_MAPPER_PREFERRED_GET
:
3192 if(g_inmmdevices_count
> 0)
3193 /* Device 0 is always the default device */
3194 *(DWORD
*)dwParam1
= 0;
3196 *(DWORD
*)dwParam1
= -1;
3201 *(DWORD
*)dwParam2
= 0;
3203 return MMSYSERR_NOERROR
;
3206 TRACE("Message not supported: %u\n", uMessage
);
3208 return MMSYSERR_NOTSUPPORTED
;
3211 UINT WINAPI
mixerGetNumDevs(void)
3217 hr
= WINMM_InitMMDevices();
3221 return g_outmmdevices_count
+ g_inmmdevices_count
;
3224 /**************************************************************************
3225 * mixerGetDevCapsA [WINMM.@]
3227 UINT WINAPI
mixerGetDevCapsA(UINT_PTR uDeviceID
, LPMIXERCAPSA lpCaps
, UINT uSize
)
3232 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3235 return MMSYSERR_INVALPARAM
;
3237 ret
= mixerGetDevCapsW(uDeviceID
, &micW
, sizeof(micW
));
3239 if (ret
== MMSYSERR_NOERROR
) {
3241 micA
.wMid
= micW
.wMid
;
3242 micA
.wPid
= micW
.wPid
;
3243 micA
.vDriverVersion
= micW
.vDriverVersion
;
3244 WideCharToMultiByte( CP_ACP
, 0, micW
.szPname
, -1, micA
.szPname
,
3245 sizeof(micA
.szPname
), NULL
, NULL
);
3246 micA
.fdwSupport
= micW
.fdwSupport
;
3247 micA
.cDestinations
= micW
.cDestinations
;
3248 memcpy(lpCaps
, &micA
, min(uSize
, sizeof(micA
)));
3253 /**************************************************************************
3254 * mixerGetDevCapsW [WINMM.@]
3256 UINT WINAPI
mixerGetDevCapsW(UINT_PTR uDeviceID
, LPMIXERCAPSW lpCaps
, UINT uSize
)
3258 WINMM_MMDevice
*mmdevice
;
3262 TRACE("(%lu, %p, %u)\n", uDeviceID
, lpCaps
, uSize
);
3264 hr
= WINMM_InitMMDevices();
3266 return MMSYSERR_ERROR
;
3269 return MMSYSERR_INVALPARAM
;
3272 return MMSYSERR_NOERROR
;
3274 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3275 return MMSYSERR_BADDEVICEID
;
3277 if(uDeviceID
< g_outmmdevices_count
){
3278 mmdevice
= &g_out_mmdevices
[uDeviceID
];
3279 memcpy(caps
.szPname
, mmdevice
->out_caps
.szPname
, sizeof(caps
.szPname
));
3281 mmdevice
= &g_in_mmdevices
[uDeviceID
- g_outmmdevices_count
];
3282 memcpy(caps
.szPname
, mmdevice
->in_caps
.szPname
, sizeof(caps
.szPname
));
3287 caps
.vDriverVersion
= 0x00010001;
3288 caps
.fdwSupport
= 0;
3289 caps
.cDestinations
= 1;
3291 memcpy(lpCaps
, &caps
, uSize
);
3293 return MMSYSERR_NOERROR
;
3296 /**************************************************************************
3297 * mixerOpen [WINMM.@]
3299 UINT WINAPI
mixerOpen(LPHMIXER lphMix
, UINT uDeviceID
, DWORD_PTR dwCallback
,
3300 DWORD_PTR dwInstance
, DWORD fdwOpen
)
3302 WINMM_MMDevice
*mmdevice
;
3306 TRACE("(%p, %d, %lx, %lx, %x)\n", lphMix
, uDeviceID
, dwCallback
,
3307 dwInstance
, fdwOpen
);
3309 hr
= WINMM_InitMMDevices();
3311 return MMSYSERR_ERROR
;
3314 return MMSYSERR_INVALPARAM
;
3316 mr
= WINMM_CheckCallback(dwCallback
, fdwOpen
, TRUE
);
3317 if(mr
!= MMSYSERR_NOERROR
)
3320 if(uDeviceID
>= g_outmmdevices_count
+ g_inmmdevices_count
)
3321 return MMSYSERR_BADDEVICEID
;
3323 if(uDeviceID
< g_outmmdevices_count
){
3324 mmdevice
= &g_out_mmdevices
[uDeviceID
];
3325 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
, TRUE
,
3326 mmdevice
->mixer_count
);
3328 mmdevice
= &g_in_mmdevices
[uDeviceID
- g_outmmdevices_count
];
3329 *lphMix
= (HMIXER
)WINMM_MakeHWAVE(uDeviceID
- g_outmmdevices_count
,
3330 FALSE
, mmdevice
->mixer_count
);
3333 ++mmdevice
->mixer_count
;
3335 return MMSYSERR_NOERROR
;
3338 /**************************************************************************
3339 * mixerClose [WINMM.@]
3341 UINT WINAPI
mixerClose(HMIXER hMix
)
3343 TRACE("(%p)\n", hMix
);
3345 return MMSYSERR_NOERROR
;
3348 /**************************************************************************
3349 * mixerGetID [WINMM.@]
3351 UINT WINAPI
mixerGetID(HMIXEROBJ hmix
, LPUINT lpid
, DWORD fdwID
)
3353 WINMM_MMDevice
*mmdevice
;
3356 TRACE("(%p, %p, %x)\n", hmix
, lpid
, fdwID
);
3358 hr
= WINMM_InitMMDevices();
3360 return MMSYSERR_ERROR
;
3363 return MMSYSERR_INVALPARAM
;
3365 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwID
, lpid
);
3367 return MMSYSERR_INVALHANDLE
;
3369 if(mmdevice
->in_caps
.szPname
[0] != '\0')
3370 *lpid
+= g_outmmdevices_count
;
3372 return MMSYSERR_NOERROR
;
3375 /**************************************************************************
3376 * mixerGetControlDetailsW [WINMM.@]
3378 UINT WINAPI
mixerGetControlDetailsW(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdW
,
3381 WINMM_ControlDetails details
;
3384 TRACE("(%p, %p, %x)\n", hmix
, lpmcdW
, fdwDetails
);
3386 hr
= WINMM_InitMMDevices();
3388 return MMSYSERR_ERROR
;
3391 return MMSYSERR_INVALPARAM
;
3393 TRACE("dwControlID: %u\n", lpmcdW
->dwControlID
);
3395 details
.hmix
= hmix
;
3396 details
.details
= lpmcdW
;
3397 details
.flags
= fdwDetails
;
3399 return SendMessageW(g_devices_hwnd
, MXDM_GETCONTROLDETAILS
,
3400 (DWORD_PTR
)&details
, 0);
3403 /**************************************************************************
3404 * mixerGetControlDetailsA [WINMM.@]
3406 UINT WINAPI
mixerGetControlDetailsA(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcdA
,
3409 UINT ret
= MMSYSERR_NOTSUPPORTED
;
3411 TRACE("(%p, %p, %08x)\n", hmix
, lpmcdA
, fdwDetails
);
3413 if (lpmcdA
== NULL
|| lpmcdA
->cbStruct
!= sizeof(*lpmcdA
))
3414 return MMSYSERR_INVALPARAM
;
3416 switch (fdwDetails
& MIXER_GETCONTROLDETAILSF_QUERYMASK
) {
3417 case MIXER_GETCONTROLDETAILSF_VALUE
:
3418 /* can safely use A structure as it is, no string inside */
3419 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3421 case MIXER_GETCONTROLDETAILSF_LISTTEXT
:
3423 MIXERCONTROLDETAILS_LISTTEXTA
*pDetailsA
= lpmcdA
->paDetails
;
3424 MIXERCONTROLDETAILS_LISTTEXTW
*pDetailsW
;
3425 int size
= max(1, lpmcdA
->cChannels
) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3428 if (lpmcdA
->u
.cMultipleItems
!= 0) {
3429 size
*= lpmcdA
->u
.cMultipleItems
;
3431 pDetailsW
= HeapAlloc(GetProcessHeap(), 0, size
);
3432 lpmcdA
->paDetails
= pDetailsW
;
3433 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTW
);
3434 /* set up lpmcd->paDetails */
3435 ret
= mixerGetControlDetailsW(hmix
, lpmcdA
, fdwDetails
);
3436 /* copy from lpmcd->paDetails back to paDetailsW; */
3437 if (ret
== MMSYSERR_NOERROR
) {
3438 for (i
= 0; i
< lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
; i
++) {
3439 pDetailsA
->dwParam1
= pDetailsW
->dwParam1
;
3440 pDetailsA
->dwParam2
= pDetailsW
->dwParam2
;
3441 WideCharToMultiByte( CP_ACP
, 0, pDetailsW
->szName
, -1,
3443 sizeof(pDetailsA
->szName
), NULL
, NULL
);
3447 pDetailsA
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3448 pDetailsW
-= lpmcdA
->u
.cMultipleItems
* lpmcdA
->cChannels
;
3450 HeapFree(GetProcessHeap(), 0, pDetailsW
);
3451 lpmcdA
->paDetails
= pDetailsA
;
3452 lpmcdA
->cbDetails
= sizeof(MIXERCONTROLDETAILS_LISTTEXTA
);
3456 WARN("Unsupported fdwDetails=0x%08x\n", fdwDetails
);
3462 /**************************************************************************
3463 * mixerGetLineControlsA [WINMM.@]
3465 UINT WINAPI
mixerGetLineControlsA(HMIXEROBJ hmix
, LPMIXERLINECONTROLSA lpmlcA
,
3468 MIXERLINECONTROLSW mlcW
;
3472 TRACE("(%p, %p, %x)\n", hmix
, lpmlcA
, fdwControls
);
3474 if (lpmlcA
== NULL
|| lpmlcA
->cbStruct
!= sizeof(*lpmlcA
) ||
3475 lpmlcA
->cbmxctrl
!= sizeof(MIXERCONTROLA
))
3476 return MMSYSERR_INVALPARAM
;
3478 mlcW
.cbStruct
= sizeof(mlcW
);
3479 mlcW
.dwLineID
= lpmlcA
->dwLineID
;
3480 mlcW
.u
.dwControlID
= lpmlcA
->u
.dwControlID
;
3481 mlcW
.u
.dwControlType
= lpmlcA
->u
.dwControlType
;
3483 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
3484 the control count is assumed to be 1 - This is relied upon by a game,
3485 "Dynomite Deluze" */
3486 if (MIXER_GETLINECONTROLSF_ONEBYTYPE
== (fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
)) {
3489 mlcW
.cControls
= lpmlcA
->cControls
;
3491 mlcW
.cbmxctrl
= sizeof(MIXERCONTROLW
);
3492 mlcW
.pamxctrl
= HeapAlloc(GetProcessHeap(), 0,
3493 mlcW
.cControls
* mlcW
.cbmxctrl
);
3495 ret
= mixerGetLineControlsW(hmix
, &mlcW
, fdwControls
);
3497 if (ret
== MMSYSERR_NOERROR
) {
3498 lpmlcA
->dwLineID
= mlcW
.dwLineID
;
3499 lpmlcA
->u
.dwControlID
= mlcW
.u
.dwControlID
;
3500 lpmlcA
->u
.dwControlType
= mlcW
.u
.dwControlType
;
3502 for (i
= 0; i
< mlcW
.cControls
; i
++) {
3503 lpmlcA
->pamxctrl
[i
].cbStruct
= sizeof(MIXERCONTROLA
);
3504 lpmlcA
->pamxctrl
[i
].dwControlID
= mlcW
.pamxctrl
[i
].dwControlID
;
3505 lpmlcA
->pamxctrl
[i
].dwControlType
= mlcW
.pamxctrl
[i
].dwControlType
;
3506 lpmlcA
->pamxctrl
[i
].fdwControl
= mlcW
.pamxctrl
[i
].fdwControl
;
3507 lpmlcA
->pamxctrl
[i
].cMultipleItems
= mlcW
.pamxctrl
[i
].cMultipleItems
;
3508 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szShortName
, -1,
3509 lpmlcA
->pamxctrl
[i
].szShortName
,
3510 sizeof(lpmlcA
->pamxctrl
[i
].szShortName
), NULL
, NULL
);
3511 WideCharToMultiByte( CP_ACP
, 0, mlcW
.pamxctrl
[i
].szName
, -1,
3512 lpmlcA
->pamxctrl
[i
].szName
,
3513 sizeof(lpmlcA
->pamxctrl
[i
].szName
), NULL
, NULL
);
3514 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
3515 * sizeof(mlcW.pamxctrl[i].Bounds) */
3516 memcpy(&lpmlcA
->pamxctrl
[i
].Bounds
, &mlcW
.pamxctrl
[i
].Bounds
,
3517 sizeof(mlcW
.pamxctrl
[i
].Bounds
));
3518 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
3519 * sizeof(mlcW.pamxctrl[i].Metrics) */
3520 memcpy(&lpmlcA
->pamxctrl
[i
].Metrics
, &mlcW
.pamxctrl
[i
].Metrics
,
3521 sizeof(mlcW
.pamxctrl
[i
].Metrics
));
3525 HeapFree(GetProcessHeap(), 0, mlcW
.pamxctrl
);
3530 static UINT
WINMM_GetVolumeLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
3531 MIXERCONTROLW
*ctl
, DWORD flags
)
3533 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 0 : 2;
3534 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_VOLUME
;
3535 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
3536 ctl
->cMultipleItems
= 0;
3537 lstrcpyW(ctl
->szShortName
, volumeW
);
3538 lstrcpyW(ctl
->szName
, volumeW
);
3539 ctl
->Bounds
.s1
.dwMinimum
= 0;
3540 ctl
->Bounds
.s1
.dwMaximum
= 0xFFFF;
3541 ctl
->Metrics
.cSteps
= 192;
3543 return MMSYSERR_NOERROR
;
3546 static UINT
WINMM_GetMuteLineControl(WINMM_MMDevice
*mmdevice
, DWORD line
,
3547 MIXERCONTROLW
*ctl
, DWORD flags
)
3549 ctl
->dwControlID
= (line
== 0xFFFF0000) ? 1 : 3;
3550 ctl
->dwControlType
= MIXERCONTROL_CONTROLTYPE_MUTE
;
3551 ctl
->fdwControl
= MIXERCONTROL_CONTROLF_UNIFORM
;
3552 ctl
->cMultipleItems
= 0;
3553 lstrcpyW(ctl
->szShortName
, muteW
);
3554 lstrcpyW(ctl
->szName
, muteW
);
3555 ctl
->Bounds
.s1
.dwMinimum
= 0;
3556 ctl
->Bounds
.s1
.dwMaximum
= 1;
3557 ctl
->Metrics
.cSteps
= 0;
3559 return MMSYSERR_NOERROR
;
3562 /**************************************************************************
3563 * mixerGetLineControlsW [WINMM.@]
3565 UINT WINAPI
mixerGetLineControlsW(HMIXEROBJ hmix
, LPMIXERLINECONTROLSW lpmlcW
,
3568 WINMM_MMDevice
*mmdevice
;
3571 TRACE("(%p, %p, %08x)\n", hmix
, lpmlcW
, fdwControls
);
3573 hr
= WINMM_InitMMDevices();
3575 return MMSYSERR_ERROR
;
3577 if(fdwControls
& ~(MIXER_GETLINECONTROLSF_ALL
|
3578 MIXER_GETLINECONTROLSF_ONEBYID
|
3579 MIXER_GETLINECONTROLSF_ONEBYTYPE
|
3580 MIXER_OBJECTF_HMIXER
|
3581 MIXER_OBJECTF_MIXER
)){
3582 WARN("Unknown GetLineControls flag: %x\n", fdwControls
);
3583 return MMSYSERR_INVALFLAG
;
3586 if(!lpmlcW
|| lpmlcW
->cbStruct
< sizeof(*lpmlcW
) || !lpmlcW
->pamxctrl
)
3587 return MMSYSERR_INVALPARAM
;
3589 TRACE("dwLineID: %u\n", lpmlcW
->dwLineID
);
3590 TRACE("dwControl: %x\n", lpmlcW
->u
.dwControlID
);
3591 TRACE("cControls: %u\n", lpmlcW
->cControls
);
3593 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwControls
, NULL
);
3595 return MMSYSERR_INVALHANDLE
;
3597 switch(fdwControls
& MIXER_GETLINECONTROLSF_QUERYMASK
){
3598 case MIXER_GETLINECONTROLSF_ALL
:
3599 if(lpmlcW
->cControls
!= 2)
3600 return MMSYSERR_INVALPARAM
;
3601 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
3602 return MMSYSERR_INVALPARAM
;
3603 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
3604 return MIXERR_INVALLINE
;
3605 WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
3606 &lpmlcW
->pamxctrl
[0], fdwControls
);
3607 WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
3608 &lpmlcW
->pamxctrl
[1], fdwControls
);
3609 return MMSYSERR_NOERROR
;
3610 case MIXER_GETLINECONTROLSF_ONEBYID
:
3611 if(lpmlcW
->cControls
!= 1)
3612 return MMSYSERR_INVALPARAM
;
3613 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
3614 return MMSYSERR_INVALPARAM
;
3615 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
3616 return MIXERR_INVALLINE
;
3617 if(lpmlcW
->u
.dwControlID
== 0)
3618 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
3619 lpmlcW
->pamxctrl
, fdwControls
);
3620 if(lpmlcW
->u
.dwControlID
== 1)
3621 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
3622 lpmlcW
->pamxctrl
, fdwControls
);
3623 return MMSYSERR_NOTSUPPORTED
;
3624 case MIXER_GETLINECONTROLSF_ONEBYTYPE
:
3625 if(lpmlcW
->cControls
!= 1)
3626 return MMSYSERR_INVALPARAM
;
3627 if(lpmlcW
->cbmxctrl
< sizeof(MIXERCONTROLW
))
3628 return MMSYSERR_INVALPARAM
;
3629 if(lpmlcW
->dwLineID
!= 0 && lpmlcW
->dwLineID
!= 0xFFFF0000)
3630 return MIXERR_INVALLINE
;
3631 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_VOLUME
)
3632 return WINMM_GetVolumeLineControl(mmdevice
, lpmlcW
->dwLineID
,
3633 lpmlcW
->pamxctrl
, fdwControls
);
3634 if(lpmlcW
->u
.dwControlType
== MIXERCONTROL_CONTROLTYPE_MUTE
)
3635 return WINMM_GetMuteLineControl(mmdevice
, lpmlcW
->dwLineID
,
3636 lpmlcW
->pamxctrl
, fdwControls
);
3637 return MMSYSERR_NOTSUPPORTED
;
3640 return MMSYSERR_NOTSUPPORTED
;
3643 static UINT
WINMM_GetSourceLineInfo(WINMM_MMDevice
*mmdevice
, UINT mmdev_index
,
3644 MIXERLINEW
*info
, DWORD flags
)
3647 if(mmdevice
->in_caps
.szPname
[0] != '\0')
3650 if(info
->dwSource
!= 0)
3651 return MIXERR_INVALLINE
;
3653 info
->dwDestination
= 0;
3655 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
| MIXERLINE_LINEF_SOURCE
;
3656 info
->cConnections
= 0;
3657 info
->cControls
= 2;
3658 /* volume & mute always affect all channels, so claim 1 channel */
3659 info
->cChannels
= 1;
3660 info
->Target
.dwDeviceID
= mmdev_index
;
3661 info
->Target
.wMid
= ~0;
3662 info
->Target
.wPid
= ~0;
3663 info
->Target
.vDriverVersion
= 0;
3665 lstrcpyW(info
->szShortName
, volumeW
);
3666 lstrcpyW(info
->szName
, mastervolumeW
);
3669 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
;
3670 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEOUT
;
3671 memcpy(info
->Target
.szPname
, mmdevice
->out_caps
.szPname
,
3672 sizeof(info
->Target
.szPname
));
3674 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
;
3675 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
3676 info
->Target
.szPname
[0] = '\0';
3679 return MMSYSERR_NOERROR
;
3682 static UINT
WINMM_GetDestinationLineInfo(WINMM_MMDevice
*mmdevice
,
3683 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
3686 if(mmdevice
->in_caps
.szPname
[0] != '\0')
3689 if(info
->dwDestination
!= 0)
3690 return MIXERR_INVALLINE
;
3692 info
->dwSource
= 0xFFFFFFFF;
3693 info
->dwLineID
= 0xFFFF0000;
3694 info
->fdwLine
= MIXERLINE_LINEF_ACTIVE
;
3695 info
->cConnections
= 1;
3696 info
->cControls
= 2;
3698 lstrcpyW(info
->szShortName
, volumeW
);
3699 lstrcpyW(info
->szName
, mastervolumeW
);
3701 info
->Target
.dwDeviceID
= mmdev_index
;
3702 info
->Target
.wMid
= ~0;
3703 info
->Target
.wPid
= ~0;
3704 info
->Target
.vDriverVersion
= 0;
3707 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
;
3708 info
->cChannels
= mmdevice
->out_caps
.wChannels
;
3709 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_UNDEFINED
;
3710 info
->Target
.szPname
[0] = '\0';
3712 info
->dwComponentType
= MIXERLINE_COMPONENTTYPE_DST_WAVEIN
;
3713 info
->cChannels
= mmdevice
->in_caps
.wChannels
;
3714 info
->Target
.dwType
= MIXERLINE_TARGETTYPE_WAVEIN
;
3715 memcpy(info
->Target
.szPname
, mmdevice
->in_caps
.szPname
,
3716 sizeof(info
->Target
.szPname
));
3719 return MMSYSERR_NOERROR
;
3722 static UINT
WINMM_GetComponentTypeLineInfo(WINMM_MMDevice
*mmdevice
,
3723 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
3726 if(mmdevice
->in_caps
.szPname
[0] != '\0')
3729 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_WAVEIN
){
3731 return MIXERR_INVALLINE
;
3732 info
->dwDestination
= 0;
3733 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
3736 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
){
3738 return MIXERR_INVALLINE
;
3739 info
->dwDestination
= 0;
3740 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
3743 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE
){
3745 return MIXERR_INVALLINE
;
3747 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
3750 if(info
->dwComponentType
== MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT
){
3752 return MIXERR_INVALLINE
;
3754 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
3757 TRACE("Returning INVALLINE on this component type: %u\n",
3758 info
->dwComponentType
);
3760 return MIXERR_INVALLINE
;
3763 static UINT
WINMM_GetLineIDLineInfo(WINMM_MMDevice
*mmdevice
,
3764 UINT mmdev_index
, MIXERLINEW
*info
, DWORD flags
)
3766 if(info
->dwLineID
== 0xFFFF0000){
3767 info
->dwDestination
= 0;
3768 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, info
, flags
);
3771 if(info
->dwLineID
== 0){
3773 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, info
, flags
);
3776 TRACE("Returning INVALLINE on this dwLineID: %u\n", info
->dwLineID
);
3777 return MIXERR_INVALLINE
;
3780 /**************************************************************************
3781 * mixerGetLineInfoW [WINMM.@]
3783 UINT WINAPI
mixerGetLineInfoW(HMIXEROBJ hmix
, LPMIXERLINEW lpmliW
, DWORD fdwInfo
)
3786 WINMM_MMDevice
*mmdevice
;
3789 TRACE("(%p, %p, %x)\n", hmix
, lpmliW
, fdwInfo
);
3791 hr
= WINMM_InitMMDevices();
3793 return MMSYSERR_ERROR
;
3795 if(!lpmliW
|| lpmliW
->cbStruct
< sizeof(MIXERLINEW
))
3796 return MMSYSERR_INVALPARAM
;
3798 TRACE("dwDestination: %u\n", lpmliW
->dwDestination
);
3799 TRACE("dwSource: %u\n", lpmliW
->dwSource
);
3800 TRACE("dwLineID: %u\n", lpmliW
->dwLineID
);
3801 TRACE("fdwLine: 0x%x\n", lpmliW
->fdwLine
);
3802 TRACE("dwComponentType: 0x%x\n", lpmliW
->dwComponentType
);
3804 if(fdwInfo
& ~(MIXER_GETLINEINFOF_COMPONENTTYPE
|
3805 MIXER_GETLINEINFOF_DESTINATION
|
3806 MIXER_GETLINEINFOF_LINEID
|
3807 MIXER_GETLINEINFOF_SOURCE
|
3808 MIXER_GETLINEINFOF_TARGETTYPE
|
3809 MIXER_OBJECTF_HMIXER
|
3810 MIXER_OBJECTF_MIXER
)){
3811 WARN("Unknown GetLineInfo flag: %x\n", fdwInfo
);
3812 return MMSYSERR_INVALFLAG
;
3815 mmdevice
= WINMM_GetMixerMMDevice(hmix
, fdwInfo
, &mmdev_index
);
3817 return MMSYSERR_INVALHANDLE
;
3819 switch(fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
){
3820 case MIXER_GETLINEINFOF_DESTINATION
:
3821 return WINMM_GetDestinationLineInfo(mmdevice
, mmdev_index
, lpmliW
,
3823 case MIXER_GETLINEINFOF_SOURCE
:
3824 return WINMM_GetSourceLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
3825 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
3826 return WINMM_GetComponentTypeLineInfo(mmdevice
, mmdev_index
, lpmliW
,
3828 case MIXER_GETLINEINFOF_LINEID
:
3829 return WINMM_GetLineIDLineInfo(mmdevice
, mmdev_index
, lpmliW
, fdwInfo
);
3830 case MIXER_GETLINEINFOF_TARGETTYPE
:
3831 FIXME("TARGETTYPE flag not implemented!\n");
3832 return MIXERR_INVALLINE
;
3835 TRACE("Returning INVALFLAG on these flags: %x\n", fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
);
3836 return MMSYSERR_INVALFLAG
;
3839 /**************************************************************************
3840 * mixerGetLineInfoA [WINMM.@]
3842 UINT WINAPI
mixerGetLineInfoA(HMIXEROBJ hmix
, LPMIXERLINEA lpmliA
,
3848 TRACE("(%p, %p, %x)\n", hmix
, lpmliA
, fdwInfo
);
3850 if (lpmliA
== NULL
|| lpmliA
->cbStruct
!= sizeof(*lpmliA
))
3851 return MMSYSERR_INVALPARAM
;
3853 mliW
.cbStruct
= sizeof(mliW
);
3854 switch (fdwInfo
& MIXER_GETLINEINFOF_QUERYMASK
) {
3855 case MIXER_GETLINEINFOF_COMPONENTTYPE
:
3856 mliW
.dwComponentType
= lpmliA
->dwComponentType
;
3858 case MIXER_GETLINEINFOF_DESTINATION
:
3859 mliW
.dwDestination
= lpmliA
->dwDestination
;
3861 case MIXER_GETLINEINFOF_LINEID
:
3862 mliW
.dwLineID
= lpmliA
->dwLineID
;
3864 case MIXER_GETLINEINFOF_SOURCE
:
3865 mliW
.dwDestination
= lpmliA
->dwDestination
;
3866 mliW
.dwSource
= lpmliA
->dwSource
;
3868 case MIXER_GETLINEINFOF_TARGETTYPE
:
3869 mliW
.Target
.dwType
= lpmliA
->Target
.dwType
;
3870 mliW
.Target
.wMid
= lpmliA
->Target
.wMid
;
3871 mliW
.Target
.wPid
= lpmliA
->Target
.wPid
;
3872 mliW
.Target
.vDriverVersion
= lpmliA
->Target
.vDriverVersion
;
3873 MultiByteToWideChar( CP_ACP
, 0, lpmliA
->Target
.szPname
, -1, mliW
.Target
.szPname
, sizeof(mliW
.Target
.szPname
)/sizeof(WCHAR
));
3876 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo
);
3877 return MMSYSERR_INVALFLAG
;
3880 ret
= mixerGetLineInfoW(hmix
, &mliW
, fdwInfo
);
3882 if(ret
== MMSYSERR_NOERROR
)
3884 lpmliA
->dwDestination
= mliW
.dwDestination
;
3885 lpmliA
->dwSource
= mliW
.dwSource
;
3886 lpmliA
->dwLineID
= mliW
.dwLineID
;
3887 lpmliA
->fdwLine
= mliW
.fdwLine
;
3888 lpmliA
->dwUser
= mliW
.dwUser
;
3889 lpmliA
->dwComponentType
= mliW
.dwComponentType
;
3890 lpmliA
->cChannels
= mliW
.cChannels
;
3891 lpmliA
->cConnections
= mliW
.cConnections
;
3892 lpmliA
->cControls
= mliW
.cControls
;
3893 WideCharToMultiByte( CP_ACP
, 0, mliW
.szShortName
, -1, lpmliA
->szShortName
,
3894 sizeof(lpmliA
->szShortName
), NULL
, NULL
);
3895 WideCharToMultiByte( CP_ACP
, 0, mliW
.szName
, -1, lpmliA
->szName
,
3896 sizeof(lpmliA
->szName
), NULL
, NULL
);
3897 lpmliA
->Target
.dwType
= mliW
.Target
.dwType
;
3898 lpmliA
->Target
.dwDeviceID
= mliW
.Target
.dwDeviceID
;
3899 lpmliA
->Target
.wMid
= mliW
.Target
.wMid
;
3900 lpmliA
->Target
.wPid
= mliW
.Target
.wPid
;
3901 lpmliA
->Target
.vDriverVersion
= mliW
.Target
.vDriverVersion
;
3902 WideCharToMultiByte( CP_ACP
, 0, mliW
.Target
.szPname
, -1, lpmliA
->Target
.szPname
,
3903 sizeof(lpmliA
->Target
.szPname
), NULL
, NULL
);
3908 /**************************************************************************
3909 * mixerSetControlDetails [WINMM.@]
3911 UINT WINAPI
mixerSetControlDetails(HMIXEROBJ hmix
, LPMIXERCONTROLDETAILS lpmcd
,
3914 WINMM_ControlDetails details
;
3916 TRACE("(%p, %p, %x)\n", hmix
, lpmcd
, fdwDetails
);
3918 if(!WINMM_StartDevicesThread())
3919 return MMSYSERR_ERROR
;
3921 if((fdwDetails
& MIXER_SETCONTROLDETAILSF_QUERYMASK
) ==
3922 MIXER_SETCONTROLDETAILSF_CUSTOM
)
3923 return MMSYSERR_NOTSUPPORTED
;
3926 return MMSYSERR_INVALPARAM
;
3928 TRACE("dwControlID: %u\n", lpmcd
->dwControlID
);
3930 details
.hmix
= hmix
;
3931 details
.details
= lpmcd
;
3932 details
.flags
= fdwDetails
;
3934 return SendMessageW(g_devices_hwnd
, MXDM_SETCONTROLDETAILS
,
3935 (DWORD_PTR
)&details
, 0);
3938 /**************************************************************************
3939 * mixerMessage [WINMM.@]
3941 DWORD WINAPI
mixerMessage(HMIXER hmix
, UINT uMsg
, DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
3943 TRACE("(%p, %d, %lx, %lx)\n", hmix
, uMsg
, dwParam1
, dwParam2
);
3945 return MMSYSERR_NOTSUPPORTED
;