1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1993 Martin Ayotte
7 * 1998-2002 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(winmm
);
40 typedef struct tagWINE_PLAYSOUND
50 static WINE_PLAYSOUND
*PlaySoundCurrent
;
51 static BOOL bPlaySoundStop
;
53 static HMMIO
get_mmioFromFile(LPCWSTR lpszName
)
59 ret
= mmioOpenW((LPWSTR
)lpszName
, NULL
,
60 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
61 if (ret
!= 0) return ret
;
62 if (SearchPathW(NULL
, lpszName
, L
".wav", ARRAY_SIZE(buf
), buf
, &dummy
))
64 return mmioOpenW(buf
, NULL
,
65 MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
70 static HMMIO
get_mmioFromProfile(UINT uFlags
, LPCWSTR lpszName
)
75 HKEY hRegSnd
, hRegApp
, hScheme
, hSnd
;
76 DWORD err
, type
, count
;
78 TRACE("searching in SystemSound list for %s\n", debugstr_w(lpszName
));
79 GetProfileStringW(L
"Sounds", lpszName
, L
"", str
, ARRAY_SIZE(str
));
82 if (uFlags
& SND_NODEFAULT
) goto next
;
83 GetProfileStringW(L
"Sounds", L
"Default", L
"", str
, ARRAY_SIZE(str
));
86 for (ptr
= str
; *ptr
&& *ptr
!= ','; ptr
++);
88 hmmio
= mmioOpenW(str
, NULL
, MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
89 if (hmmio
!= 0) return hmmio
;
91 /* we look up the registry under
92 * HKCU\AppEvents\Schemes\Apps\.Default
93 * HKCU\AppEvents\Schemes\Apps\<AppName>
95 if (RegOpenKeyW(HKEY_CURRENT_USER
, L
"AppEvents\\Schemes\\Apps", &hRegSnd
) != 0) goto none
;
96 if (uFlags
& SND_APPLICATION
)
101 len
= GetModuleFileNameW(0, str
, ARRAY_SIZE(str
));
102 if (len
> 0 && len
< ARRAY_SIZE(str
))
104 for (ptr
= str
+ lstrlenW(str
) - 1; ptr
>= str
; ptr
--)
106 if (*ptr
== '.') *ptr
= 0;
109 err
= RegOpenKeyW(hRegSnd
, ptr
+1, &hRegApp
);
117 err
= RegOpenKeyW(hRegSnd
, L
".Default", &hRegApp
);
119 RegCloseKey(hRegSnd
);
120 if (err
!= 0) goto none
;
121 err
= RegOpenKeyW(hRegApp
, lpszName
, &hScheme
);
122 RegCloseKey(hRegApp
);
123 if (err
!= 0) goto none
;
124 /* what's the difference between .Current and .Default ? */
125 err
= RegOpenKeyW(hScheme
, L
".Default", &hSnd
);
128 err
= RegOpenKeyW(hScheme
, L
".Current", &hSnd
);
129 RegCloseKey(hScheme
);
134 err
= RegQueryValueExW(hSnd
, NULL
, 0, &type
, (LPBYTE
)str
, &count
);
136 if (err
!= 0 || !*str
) goto none
;
137 hmmio
= mmioOpenW(str
, NULL
, MMIO_ALLOCBUF
| MMIO_READ
| MMIO_DENYWRITE
);
138 if (hmmio
) return hmmio
;
140 WARN("can't find SystemSound=%s !\n", debugstr_w(lpszName
));
144 struct playsound_data
150 static void CALLBACK
PlaySound_Callback(HWAVEOUT hwo
, UINT uMsg
,
151 DWORD_PTR dwInstance
,
152 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
154 struct playsound_data
* s
= (struct playsound_data
*)dwInstance
;
161 InterlockedIncrement(&s
->dwEventCount
);
162 TRACE("Returning waveHdr=%lx\n", dwParam1
);
166 ERR("Unknown uMsg=%d\n", uMsg
);
170 static void PlaySound_WaitDone(struct playsound_data
* s
)
173 if (InterlockedDecrement(&s
->dwEventCount
) >= 0) break;
174 InterlockedIncrement(&s
->dwEventCount
);
176 WaitForSingleObject(s
->hEvent
, INFINITE
);
180 static BOOL
PlaySound_IsString(DWORD fdwSound
, const void* psz
)
182 /* SND_RESOURCE is 0x40004 while
183 * SND_MEMORY is 0x00004
185 switch (fdwSound
& (SND_RESOURCE
|SND_ALIAS_ID
|SND_FILENAME
))
187 case SND_RESOURCE
: return HIWORD(psz
) != 0; /* by name or by ID ? */
189 case SND_MEMORY
: return FALSE
;
192 case SND_ALIAS
|SND_FILENAME
:
194 default: FIXME("WTF\n"); return FALSE
;
198 static void PlaySound_Free(WINE_PLAYSOUND
* wps
)
200 EnterCriticalSection(&WINMM_cs
);
201 PlaySoundCurrent
= NULL
;
202 SetEvent(psLastEvent
);
203 LeaveCriticalSection(&WINMM_cs
);
204 if (wps
->bAlloc
) HeapFree(GetProcessHeap(), 0, (void*)wps
->pszSound
);
205 HeapFree(GetProcessHeap(), 0, wps
);
208 static WINE_PLAYSOUND
* PlaySound_Alloc(const void* pszSound
, HMODULE hmod
,
209 DWORD fdwSound
, BOOL bUnicode
)
213 wps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*wps
));
214 if (!wps
) return NULL
;
217 wps
->fdwSound
= fdwSound
;
218 if (PlaySound_IsString(fdwSound
, pszSound
))
222 if (fdwSound
& SND_ASYNC
)
224 LPWSTR sound
= HeapAlloc(GetProcessHeap(), 0,
225 (lstrlenW(pszSound
)+1) * sizeof(WCHAR
));
226 if (!sound
) goto oom_error
;
227 wps
->pszSound
= lstrcpyW(sound
, pszSound
);
231 wps
->pszSound
= pszSound
;
235 UNICODE_STRING usBuffer
;
236 RtlCreateUnicodeStringFromAsciiz(&usBuffer
, pszSound
);
237 wps
->pszSound
= usBuffer
.Buffer
;
238 if (!wps
->pszSound
) goto oom_error
;
243 wps
->pszSound
= pszSound
;
247 if (wps
->bAlloc
) HeapFree(GetProcessHeap(), 0, (void*)wps
->pszSound
);
248 HeapFree(GetProcessHeap(), 0, wps
);
252 static DWORD WINAPI
proc_PlaySound(LPVOID arg
)
254 WINE_PLAYSOUND
* wps
= arg
;
259 LPWAVEFORMATEX lpWaveFormat
= NULL
;
261 LPWAVEHDR waveHdr
= NULL
;
262 INT count
, bufsize
, left
, index
;
263 struct playsound_data s
;
268 TRACE("SoundName=%s !\n", debugstr_w(wps
->pszSound
));
270 /* if resource, grab it */
271 if ((wps
->fdwSound
& SND_RESOURCE
) == SND_RESOURCE
) {
275 if ((hRes
= FindResourceW(wps
->hMod
, wps
->pszSound
, L
"WAVE")) == 0 ||
276 (hGlob
= LoadResource(wps
->hMod
, hRes
)) == 0)
278 if ((data
= LockResource(hGlob
)) == NULL
) {
284 data
= (void*)wps
->pszSound
;
286 /* construct an MMIO stream (either in memory, or from a file */
287 if (wps
->fdwSound
& SND_MEMORY
)
288 { /* NOTE: SND_RESOURCE has the SND_MEMORY bit set */
291 memset(&mminfo
, 0, sizeof(mminfo
));
292 mminfo
.fccIOProc
= FOURCC_MEM
;
293 mminfo
.pchBuffer
= data
;
294 mminfo
.cchBuffer
= -1; /* FIXME: when a resource, could grab real size */
295 TRACE("Memory sound %p\n", data
);
296 hmmio
= mmioOpenW(NULL
, &mminfo
, MMIO_READ
);
298 if (!hmmio
&& wps
->fdwSound
& SND_ALIAS
)
300 if ((wps
->fdwSound
& SND_ALIAS_ID
) == SND_ALIAS_ID
)
302 wps
->fdwSound
&= ~(SND_ALIAS_ID
^ SND_ALIAS
);
303 if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMASTERISK
)
304 wps
->pszSound
= L
"SystemAsterisk";
305 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMDEFAULT
)
306 wps
->pszSound
= L
"SystemDefault";
307 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMEXCLAMATION
)
308 wps
->pszSound
= L
"SystemExclamation";
309 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMEXIT
)
310 wps
->pszSound
= L
"SystemExit";
311 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMHAND
)
312 wps
->pszSound
= L
"SystemHand";
313 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMQUESTION
)
314 wps
->pszSound
= L
"SystemQuestion";
315 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMSTART
)
316 wps
->pszSound
= L
"SystemStart";
317 else if (wps
->pszSound
== (LPCWSTR
)SND_ALIAS_SYSTEMWELCOME
)
318 wps
->pszSound
= L
"SystemWelcome";
319 else goto errCleanUp
;
321 hmmio
= get_mmioFromProfile(wps
->fdwSound
, wps
->pszSound
);
324 wps
->fdwSound
&= ~SND_ALIAS
;
325 wps
->fdwSound
|= SND_FILENAME
;
328 if (!hmmio
&& wps
->fdwSound
& SND_FILENAME
)
330 hmmio
= get_mmioFromFile(wps
->pszSound
);
332 if (!(wps
->fdwSound
& (SND_FILENAME
|SND_ALIAS
|SND_MEMORY
)))
334 if ((hmmio
= get_mmioFromProfile(wps
->fdwSound
| SND_NODEFAULT
, wps
->pszSound
)) == 0)
336 if ((hmmio
= get_mmioFromFile(wps
->pszSound
)) == 0)
338 hmmio
= get_mmioFromProfile(wps
->fdwSound
, wps
->pszSound
);
342 if (hmmio
== 0) goto errCleanUp
;
344 if (mmioDescend(hmmio
, &ckMainRIFF
, NULL
, 0))
347 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
348 (LPSTR
)&ckMainRIFF
.ckid
, (LPSTR
)&ckMainRIFF
.fccType
, ckMainRIFF
.cksize
);
350 if ((ckMainRIFF
.ckid
!= FOURCC_RIFF
) ||
351 (ckMainRIFF
.fccType
!= mmioFOURCC('W', 'A', 'V', 'E')))
354 mmckInfo
.ckid
= mmioFOURCC('f', 'm', 't', ' ');
355 if (mmioDescend(hmmio
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
))
358 TRACE("Chunk Found ckid=%.4s fccType=%08x cksize=%08X\n",
359 (LPSTR
)&mmckInfo
.ckid
, mmckInfo
.fccType
, mmckInfo
.cksize
);
361 lpWaveFormat
= HeapAlloc(GetProcessHeap(), 0, mmckInfo
.cksize
);
364 if (mmioRead(hmmio
, (HPSTR
)lpWaveFormat
, mmckInfo
.cksize
) < sizeof(PCMWAVEFORMAT
))
367 TRACE("wFormatTag=%04X !\n", lpWaveFormat
->wFormatTag
);
368 TRACE("nChannels=%d\n", lpWaveFormat
->nChannels
);
369 TRACE("nSamplesPerSec=%d\n", lpWaveFormat
->nSamplesPerSec
);
370 TRACE("nAvgBytesPerSec=%d\n", lpWaveFormat
->nAvgBytesPerSec
);
371 TRACE("nBlockAlign=%d\n", lpWaveFormat
->nBlockAlign
);
372 TRACE("wBitsPerSample=%u !\n", lpWaveFormat
->wBitsPerSample
);
374 /* move to end of 'fmt ' chunk */
375 mmioAscend(hmmio
, &mmckInfo
, 0);
377 mmckInfo
.ckid
= mmioFOURCC('d', 'a', 't', 'a');
378 if (mmioDescend(hmmio
, &mmckInfo
, &ckMainRIFF
, MMIO_FINDCHUNK
))
381 TRACE("Chunk Found ckid=%.4s fccType=%08x cksize=%08X\n",
382 (LPSTR
)&mmckInfo
.ckid
, mmckInfo
.fccType
, mmckInfo
.cksize
);
384 s
.hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
385 if (!s
.hEvent
|| bPlaySoundStop
)
388 if (waveOutOpen(&hWave
, WAVE_MAPPER
, lpWaveFormat
, (DWORD_PTR
)PlaySound_Callback
,
389 (DWORD_PTR
)&s
, CALLBACK_FUNCTION
) != MMSYSERR_NOERROR
)
392 /* make it so that 3 buffers per second are needed */
393 bufsize
= (((lpWaveFormat
->nAvgBytesPerSec
/ 3) - 1) / lpWaveFormat
->nBlockAlign
+ 1) *
394 lpWaveFormat
->nBlockAlign
;
395 waveHdr
= HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(WAVEHDR
) + 2 * bufsize
);
398 waveHdr
[0].lpData
= (char*)waveHdr
+ 2 * sizeof(WAVEHDR
);
399 waveHdr
[1].lpData
= (char*)waveHdr
+ 2 * sizeof(WAVEHDR
) + bufsize
;
400 waveHdr
[0].dwUser
= waveHdr
[1].dwUser
= 0L;
401 waveHdr
[0].dwLoops
= waveHdr
[1].dwLoops
= 0L;
402 waveHdr
[0].dwFlags
= waveHdr
[1].dwFlags
= 0L;
403 waveHdr
[0].dwBufferLength
= waveHdr
[1].dwBufferLength
= bufsize
;
404 if (waveOutPrepareHeader(hWave
, &waveHdr
[0], sizeof(WAVEHDR
)) ||
405 waveOutPrepareHeader(hWave
, &waveHdr
[1], sizeof(WAVEHDR
))) {
410 s
.dwEventCount
= 1L; /* for first buffer */
414 left
= mmckInfo
.cksize
;
416 mmioSeek(hmmio
, mmckInfo
.dwDataOffset
, SEEK_SET
);
424 count
= mmioRead(hmmio
, waveHdr
[index
].lpData
, min(bufsize
, left
));
425 if (count
< 1) break;
427 waveHdr
[index
].dwBufferLength
= count
;
428 if (waveOutWrite(hWave
, &waveHdr
[index
], sizeof(WAVEHDR
)) == MMSYSERR_NOERROR
) {
430 PlaySound_WaitDone(&s
);
433 ERR("Aborting play loop, waveOutWrite error\n");
439 } while (wps
->bLoop
);
441 PlaySound_WaitDone(&s
); /* to balance first buffer */
444 waveOutUnprepareHeader(hWave
, &waveHdr
[0], sizeof(WAVEHDR
));
445 waveOutUnprepareHeader(hWave
, &waveHdr
[1], sizeof(WAVEHDR
));
448 TRACE("Done playing=%s => %s!\n", debugstr_w(wps
->pszSound
), bRet
? "ok" : "ko");
449 HeapFree(GetProcessHeap(), 0, lpWaveFormat
);
452 EnterCriticalSection(&WINMM_cs
);
453 /* the CS prevents a concurrent waveOutReset */
455 LeaveCriticalSection(&WINMM_cs
);
456 while (waveOutClose(hWave
) == WAVERR_STILLPLAYING
)
459 CloseHandle(s
.hEvent
);
460 HeapFree(GetProcessHeap(), 0, waveHdr
);
461 if (hmmio
) mmioClose(hmmio
, 0);
468 static BOOL
MULTIMEDIA_PlaySound(const void* pszSound
, HMODULE hmod
, DWORD fdwSound
, BOOL bUnicode
)
470 WINE_PLAYSOUND
* wps
= NULL
;
472 TRACE("pszSound='%p' hmod=%p fdwSound=%08X\n",
473 pszSound
, hmod
, fdwSound
);
475 /* SND_NOWAIT is ignored in w95/2k/xp. */
476 if ((fdwSound
& SND_NOSTOP
) && PlaySoundCurrent
!= NULL
)
479 /* alloc internal structure, if we need to play something */
480 if (pszSound
&& !(fdwSound
& SND_PURGE
))
482 if (!(wps
= PlaySound_Alloc(pszSound
, hmod
, fdwSound
, bUnicode
)))
486 EnterCriticalSection(&WINMM_cs
);
487 /* since several threads can enter PlaySound in parallel, we're not
488 * sure, at this point, that another thread didn't start a new playsound
490 while (PlaySoundCurrent
!= NULL
)
492 ResetEvent(psLastEvent
);
493 /* FIXME: doc says we have to stop all instances of pszSound if it's non
494 * NULL... as of today, we stop all playing instances */
495 bPlaySoundStop
= TRUE
;
496 if(PlaySoundCurrent
->hWave
)
497 waveOutReset(PlaySoundCurrent
->hWave
);
499 LeaveCriticalSection(&WINMM_cs
);
500 WaitForSingleObject(psLastEvent
, INFINITE
);
501 EnterCriticalSection(&WINMM_cs
);
503 bPlaySoundStop
= FALSE
;
506 PlaySoundCurrent
= wps
;
507 LeaveCriticalSection(&WINMM_cs
);
509 if (!wps
) return TRUE
;
511 if (fdwSound
& SND_ASYNC
)
514 wps
->bLoop
= (fdwSound
& SND_LOOP
) != 0;
515 if ((handle
= CreateThread(NULL
, 0, proc_PlaySound
, wps
, 0, NULL
)) != 0) {
516 SetThreadPriority(handle
, THREAD_PRIORITY_TIME_CRITICAL
);
521 else return proc_PlaySound(wps
);
528 /**************************************************************************
529 * PlaySoundA [WINMM.@]
531 BOOL WINAPI
PlaySoundA(LPCSTR pszSoundA
, HMODULE hmod
, DWORD fdwSound
)
533 return MULTIMEDIA_PlaySound(pszSoundA
, hmod
, fdwSound
, FALSE
);
536 /**************************************************************************
537 * PlaySoundW [WINMM.@]
539 BOOL WINAPI
PlaySoundW(LPCWSTR pszSoundW
, HMODULE hmod
, DWORD fdwSound
)
541 return MULTIMEDIA_PlaySound(pszSoundW
, hmod
, fdwSound
, TRUE
);
544 /**************************************************************************
545 * sndPlaySoundA [WINMM.@]
547 BOOL WINAPI
sndPlaySoundA(LPCSTR pszSoundA
, UINT uFlags
)
549 uFlags
&= SND_RESOURCE
|SND_ALIAS_ID
|SND_FILENAME
|SND_ASYNC
|SND_LOOP
|SND_MEMORY
|SND_NODEFAULT
|SND_NOSTOP
|SND_SYNC
;
550 return MULTIMEDIA_PlaySound(pszSoundA
, 0, uFlags
, FALSE
);
553 /**************************************************************************
554 * sndPlaySoundW [WINMM.@]
556 BOOL WINAPI
sndPlaySoundW(LPCWSTR pszSound
, UINT uFlags
)
558 uFlags
&= SND_RESOURCE
|SND_ALIAS_ID
|SND_FILENAME
|SND_ASYNC
|SND_LOOP
|SND_MEMORY
|SND_NODEFAULT
|SND_NOSTOP
|SND_SYNC
;
559 return MULTIMEDIA_PlaySound(pszSound
, 0, uFlags
, TRUE
);
562 /**************************************************************************
563 * mmsystemGetVersion [WINMM.@]
565 UINT WINAPI
mmsystemGetVersion(void)
567 TRACE("3.10 (Win95?)\n");