1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 * Wine Wave mapper driver
5 * Copyright 1999,2001 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * + better protection against evilish dwUser parameters
24 * + use asynchronous ACM conversion
25 * + don't use callback functions when none is required in open
26 * + the buffer sizes may not be accurate, so there may be some
27 * remaining bytes in src and dst buffers after ACM conversions...
28 * those should be taken care of...
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
42 typedef struct tagWAVEMAPDATA {
43 struct tagWAVEMAPDATA* self;
46 HACMSTREAM hAcmStream;
47 /* needed data to filter callbacks. Only needed when hAcmStream is not 0 */
49 DWORD dwClientInstance;
53 static BOOL WAVEMAP_IsData(WAVEMAPDATA* wm)
55 return (!IsBadReadPtr(wm, sizeof(WAVEMAPDATA)) && wm->self == wm);
58 /*======================================================================*
60 *======================================================================*/
62 static void CALLBACK wodCallback(HWAVE hWave, UINT uMsg, DWORD dwInstance,
63 DWORD dwParam1, DWORD dwParam2)
65 WAVEMAPDATA* wom = (WAVEMAPDATA*)dwInstance;
67 TRACE("(0x%x %u %ld %lx %lx);\n", hWave, uMsg, dwInstance, dwParam1, dwParam2);
69 if (!WAVEMAP_IsData(wom)) {
74 if (hWave != wom->hInnerWave && uMsg != WOM_OPEN)
75 ERR("Shouldn't happen (%08x %08x)\n", hWave, wom->hInnerWave);
80 /* dwParam1 & dwParam2 are supposed to be 0, nothing to do */
83 if (wom->hAcmStream) {
84 LPWAVEHDR lpWaveHdrDst = (LPWAVEHDR)dwParam1;
85 PACMSTREAMHEADER ash = (PACMSTREAMHEADER)((LPSTR)lpWaveHdrDst - sizeof(ACMSTREAMHEADER));
86 LPWAVEHDR lpWaveHdrSrc = (LPWAVEHDR)ash->dwUser;
88 lpWaveHdrSrc->dwFlags &= ~WHDR_INQUEUE;
89 lpWaveHdrSrc->dwFlags |= WHDR_DONE;
90 dwParam1 = (DWORD)lpWaveHdrSrc;
94 ERR("Unknown msg %u\n", uMsg);
97 DriverCallback(wom->dwCallback, HIWORD(wom->dwFlags), (HDRVR)wom->hOuterWave,
98 uMsg, wom->dwClientInstance, dwParam1, dwParam2);
101 static DWORD wodOpenHelper(WAVEMAPDATA* wom, UINT idx,
102 LPWAVEOPENDESC lpDesc, LPWAVEFORMATEX lpwfx,
107 /* destination is always PCM, so the formulas below apply */
108 lpwfx->nBlockAlign = (lpwfx->nChannels * lpwfx->wBitsPerSample) / 8;
109 lpwfx->nAvgBytesPerSec = lpwfx->nSamplesPerSec * lpwfx->nBlockAlign;
110 if (dwFlags & WAVE_FORMAT_QUERY) {
111 ret = acmStreamOpen(NULL, 0, lpDesc->lpFormat, lpwfx, NULL, 0L, 0L, ACM_STREAMOPENF_QUERY);
113 ret = acmStreamOpen(&wom->hAcmStream, 0, lpDesc->lpFormat, lpwfx, NULL, 0L, 0L, 0L);
115 if (ret == MMSYSERR_NOERROR) {
116 ret = waveOutOpen(&wom->hInnerWave, idx, lpwfx, (DWORD)wodCallback,
117 (DWORD)wom, (dwFlags & ~CALLBACK_TYPEMASK) | CALLBACK_FUNCTION);
118 if (ret != MMSYSERR_NOERROR && !(dwFlags & WAVE_FORMAT_QUERY)) {
119 acmStreamClose(wom->hAcmStream, 0);
126 static DWORD wodOpen(LPDWORD lpdwUser, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
130 WAVEMAPDATA* wom = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEMAPDATA));
132 TRACE("(%p %p %08lx\n", lpdwUser, lpDesc, dwFlags);
135 return MMSYSERR_NOMEM;
137 ndhi = waveOutGetNumDevs();
138 if (dwFlags & WAVE_MAPPED) {
139 if (lpDesc->uMappedDeviceID >= ndhi) return MMSYSERR_INVALPARAM;
140 ndlo = lpDesc->uMappedDeviceID;
142 dwFlags &= ~WAVE_MAPPED;
147 wom->dwCallback = lpDesc->dwCallback;
148 wom->dwFlags = dwFlags;
149 wom->dwClientInstance = lpDesc->dwInstance;
150 wom->hOuterWave = lpDesc->hWave;
152 for (i = ndlo; i < ndhi; i++) {
153 /* if no ACM stuff is involved, no need to handle callbacks at this
154 * level, this will be done transparently
156 if (waveOutOpen(&wom->hInnerWave, i, lpDesc->lpFormat, (DWORD)wodCallback,
157 (DWORD)wom, (dwFlags & ~CALLBACK_TYPEMASK) | CALLBACK_FUNCTION | WAVE_FORMAT_DIRECT) == MMSYSERR_NOERROR) {
163 if ((dwFlags & WAVE_FORMAT_DIRECT) == 0) {
166 wfx.wFormatTag = WAVE_FORMAT_PCM;
167 wfx.cbSize = 0; /* normally, this field is not used for PCM format, just in case */
168 /* try some ACM stuff */
170 #define TRY(sps,bps) wfx.nSamplesPerSec = (sps); wfx.wBitsPerSample = (bps); \
171 if (wodOpenHelper(wom, i, lpDesc, &wfx, dwFlags | WAVE_FORMAT_DIRECT) == MMSYSERR_NOERROR) goto found;
173 for (i = ndlo; i < ndhi; i++) {
174 /* first try with same stereo/mono option as source */
175 wfx.nChannels = lpDesc->lpFormat->nChannels;
180 /* 2^3 => 1, 1^3 => 2, so if stereo, try mono (and the other way around) */
186 /* first try with same stereo/mono option as source */
187 wfx.nChannels = lpDesc->lpFormat->nChannels;
192 /* 2^3 => 1, 1^3 => 2, so if stereo, try mono (and the other way around) */
201 HeapFree(GetProcessHeap(), 0, wom);
202 return MMSYSERR_ALLOCATED;
204 if (dwFlags & WAVE_FORMAT_QUERY) {
206 HeapFree(GetProcessHeap(), 0, wom);
208 *lpdwUser = (DWORD)wom;
210 return MMSYSERR_NOERROR;
213 static DWORD wodClose(WAVEMAPDATA* wom)
215 DWORD ret = waveOutClose(wom->hInnerWave);
217 if (ret == MMSYSERR_NOERROR) {
218 if (wom->hAcmStream) {
219 ret = acmStreamClose(wom->hAcmStream, 0);
221 if (ret == MMSYSERR_NOERROR) {
222 HeapFree(GetProcessHeap(), 0, wom);
228 static DWORD wodWrite(WAVEMAPDATA* wom, LPWAVEHDR lpWaveHdrSrc, DWORD dwParam2)
230 PACMSTREAMHEADER ash;
231 LPWAVEHDR lpWaveHdrDst;
233 if (!wom->hAcmStream) {
234 return waveOutWrite(wom->hInnerWave, lpWaveHdrSrc, dwParam2);
237 lpWaveHdrSrc->dwFlags |= WHDR_INQUEUE;
238 ash = (PACMSTREAMHEADER)lpWaveHdrSrc->reserved;
239 /* acmStreamConvert will actually check that the new size is less than initial size */
240 ash->cbSrcLength = lpWaveHdrSrc->dwBufferLength;
241 if (acmStreamConvert(wom->hAcmStream, ash, 0L) != MMSYSERR_NOERROR)
242 return MMSYSERR_ERROR;
244 lpWaveHdrDst = (LPWAVEHDR)((LPSTR)ash + sizeof(ACMSTREAMHEADER));
245 if (ash->cbSrcLength > ash->cbSrcLengthUsed)
246 FIXME("Not all src buffer has been written, expect bogus sound\n");
247 else if (ash->cbSrcLength < ash->cbSrcLengthUsed)
248 ERR("CoDec has read more data than it is allowed to\n");
250 if (ash->cbDstLengthUsed == 0)
252 /* something went wrong in decoding */
253 FIXME("Got 0 length\n");
254 return MMSYSERR_ERROR;
256 lpWaveHdrDst->dwBufferLength = ash->cbDstLengthUsed;
257 return waveOutWrite(wom->hInnerWave, lpWaveHdrDst, sizeof(*lpWaveHdrDst));
260 static DWORD wodPrepare(WAVEMAPDATA* wom, LPWAVEHDR lpWaveHdrSrc, DWORD dwParam2)
262 PACMSTREAMHEADER ash;
265 LPWAVEHDR lpWaveHdrDst;
267 if (!wom->hAcmStream) {
268 return waveOutPrepareHeader(wom->hInnerWave, lpWaveHdrSrc, dwParam2);
270 if (acmStreamSize(wom->hAcmStream, lpWaveHdrSrc->dwBufferLength, &size, ACM_STREAMSIZEF_SOURCE) != MMSYSERR_NOERROR)
271 return MMSYSERR_ERROR;
273 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + sizeof(WAVEHDR) + size);
275 return MMSYSERR_NOMEM;
277 ash->cbStruct = sizeof(*ash);
279 ash->dwUser = (DWORD)lpWaveHdrSrc;
280 ash->pbSrc = lpWaveHdrSrc->lpData;
281 ash->cbSrcLength = lpWaveHdrSrc->dwBufferLength;
282 /* ash->cbSrcLengthUsed */
283 ash->dwSrcUser = lpWaveHdrSrc->dwUser; /* FIXME ? */
284 ash->pbDst = (LPSTR)ash + sizeof(ACMSTREAMHEADER) + sizeof(WAVEHDR);
285 ash->cbDstLength = size;
286 /* ash->cbDstLengthUsed */
287 ash->dwDstUser = 0; /* FIXME ? */
288 dwRet = acmStreamPrepareHeader(wom->hAcmStream, ash, 0L);
289 if (dwRet != MMSYSERR_NOERROR)
292 lpWaveHdrDst = (LPWAVEHDR)((LPSTR)ash + sizeof(ACMSTREAMHEADER));
293 lpWaveHdrDst->lpData = ash->pbDst;
294 lpWaveHdrDst->dwBufferLength = size; /* conversion is not done yet */
295 lpWaveHdrDst->dwFlags = 0;
296 lpWaveHdrDst->dwLoops = 0;
297 dwRet = waveOutPrepareHeader(wom->hInnerWave, lpWaveHdrDst, sizeof(*lpWaveHdrDst));
298 if (dwRet != MMSYSERR_NOERROR)
301 lpWaveHdrSrc->reserved = (DWORD)ash;
302 lpWaveHdrSrc->dwFlags = WHDR_PREPARED;
304 return MMSYSERR_NOERROR;
306 TRACE("=> (%ld)\n", dwRet);
307 HeapFree(GetProcessHeap(), 0, ash);
311 static DWORD wodUnprepare(WAVEMAPDATA* wom, LPWAVEHDR lpWaveHdrSrc, DWORD dwParam2)
313 PACMSTREAMHEADER ash;
314 LPWAVEHDR lpWaveHdrDst;
315 DWORD dwRet1, dwRet2;
317 if (!wom->hAcmStream) {
318 return waveOutUnprepareHeader(wom->hInnerWave, lpWaveHdrSrc, dwParam2);
320 ash = (PACMSTREAMHEADER)lpWaveHdrSrc->reserved;
321 dwRet1 = acmStreamUnprepareHeader(wom->hAcmStream, ash, 0L);
323 lpWaveHdrDst = (LPWAVEHDR)((LPSTR)ash + sizeof(ACMSTREAMHEADER));
324 dwRet2 = waveOutUnprepareHeader(wom->hInnerWave, lpWaveHdrDst, sizeof(*lpWaveHdrDst));
326 HeapFree(GetProcessHeap(), 0, ash);
328 lpWaveHdrSrc->dwFlags &= ~WHDR_PREPARED;
329 return (dwRet1 == MMSYSERR_NOERROR) ? dwRet2 : dwRet1;
332 static DWORD wodGetPosition(WAVEMAPDATA* wom, LPMMTIME lpTime, DWORD dwParam2)
335 FIXME("No position conversion done for PCM => non-PCM, returning PCM position\n");
336 return waveOutGetPosition(wom->hInnerWave, lpTime, dwParam2);
339 static DWORD wodGetDevCaps(UINT wDevID, WAVEMAPDATA* wom, LPWAVEOUTCAPSA lpWaveCaps, DWORD dwParam2)
341 /* if opened low driver, forward message */
342 if (WAVEMAP_IsData(wom))
343 return waveOutGetDevCapsA(wom->hInnerWave, lpWaveCaps, dwParam2);
344 /* otherwise, return caps of mapper itself */
345 if (wDevID == (UINT)-1 || wDevID == (UINT16)-1) {
346 lpWaveCaps->wMid = 0x00FF;
347 lpWaveCaps->wPid = 0x0001;
348 lpWaveCaps->vDriverVersion = 0x0100;
349 strcpy(lpWaveCaps->szPname, "Wine wave out mapper");
350 lpWaveCaps->dwFormats =
351 WAVE_FORMAT_4M08 | WAVE_FORMAT_4S08 | WAVE_FORMAT_4M16 | WAVE_FORMAT_4S16 |
352 WAVE_FORMAT_2M08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_2M16 | WAVE_FORMAT_2S16 |
353 WAVE_FORMAT_1M08 | WAVE_FORMAT_1S08 | WAVE_FORMAT_1M16 | WAVE_FORMAT_1S16;
354 lpWaveCaps->wChannels = 2;
355 lpWaveCaps->dwSupport = WAVECAPS_VOLUME | WAVECAPS_LRVOLUME;
357 return MMSYSERR_NOERROR;
359 ERR("This shouldn't happen\n");
360 return MMSYSERR_ERROR;
363 static DWORD wodGetVolume(UINT wDevID, WAVEMAPDATA* wom, LPDWORD lpVol)
365 if (WAVEMAP_IsData(wom))
366 return waveOutGetVolume(wom->hInnerWave, lpVol);
367 return MMSYSERR_NOERROR;
370 static DWORD wodSetVolume(UINT wDevID, WAVEMAPDATA* wom, DWORD vol)
372 if (WAVEMAP_IsData(wom))
373 return waveOutSetVolume(wom->hInnerWave, vol);
374 return MMSYSERR_NOERROR;
377 static DWORD wodPause(WAVEMAPDATA* wom)
379 return waveOutPause(wom->hInnerWave);
382 static DWORD wodRestart(WAVEMAPDATA* wom)
384 return waveOutRestart(wom->hInnerWave);
387 static DWORD wodReset(WAVEMAPDATA* wom)
389 return waveOutReset(wom->hInnerWave);
392 static DWORD wodBreakLoop(WAVEMAPDATA* wom)
394 return waveOutBreakLoop(wom->hInnerWave);
397 static DWORD wodMapperStatus(WAVEMAPDATA* wom, DWORD flags, LPVOID ptr)
400 DWORD ret = MMSYSERR_NOTSUPPORTED;
403 case WAVEOUT_MAPPER_STATUS_DEVICE:
404 ret = waveOutGetID(wom->hInnerWave, &id);
407 case WAVEOUT_MAPPER_STATUS_MAPPED:
408 FIXME("Unsupported flag=%ld\n", flags);
409 *(LPDWORD)ptr = 0; /* FIXME ?? */
411 case WAVEOUT_MAPPER_STATUS_FORMAT:
412 FIXME("Unsupported flag=%ld\n", flags);
413 /* ptr points to a WAVEFORMATEX struct - before or after streaming ? */
417 FIXME("Unsupported flag=%ld\n", flags);
424 /**************************************************************************
425 * wodMessage (MSACM.@)
427 DWORD WINAPI WAVEMAP_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
428 DWORD dwParam1, DWORD dwParam2)
430 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
431 wDevID, wMsg, dwUser, dwParam1, dwParam2);
438 /* FIXME: Pretend this is supported */
440 case WODM_OPEN: return wodOpen ((LPDWORD)dwUser, (LPWAVEOPENDESC)dwParam1,dwParam2);
441 case WODM_CLOSE: return wodClose ((WAVEMAPDATA*)dwUser);
442 case WODM_WRITE: return wodWrite ((WAVEMAPDATA*)dwUser, (LPWAVEHDR)dwParam1, dwParam2);
443 case WODM_PAUSE: return wodPause ((WAVEMAPDATA*)dwUser);
444 case WODM_GETPOS: return wodGetPosition ((WAVEMAPDATA*)dwUser, (LPMMTIME)dwParam1, dwParam2);
445 case WODM_BREAKLOOP: return wodBreakLoop ((WAVEMAPDATA*)dwUser);
446 case WODM_PREPARE: return wodPrepare ((WAVEMAPDATA*)dwUser, (LPWAVEHDR)dwParam1, dwParam2);
447 case WODM_UNPREPARE: return wodUnprepare ((WAVEMAPDATA*)dwUser, (LPWAVEHDR)dwParam1, dwParam2);
448 case WODM_GETDEVCAPS: return wodGetDevCaps (wDevID, (WAVEMAPDATA*)dwUser, (LPWAVEOUTCAPSA)dwParam1,dwParam2);
449 case WODM_GETNUMDEVS: return 1;
450 case WODM_GETPITCH: return MMSYSERR_NOTSUPPORTED;
451 case WODM_SETPITCH: return MMSYSERR_NOTSUPPORTED;
452 case WODM_GETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
453 case WODM_SETPLAYBACKRATE: return MMSYSERR_NOTSUPPORTED;
454 case WODM_GETVOLUME: return wodGetVolume (wDevID, (WAVEMAPDATA*)dwUser, (LPDWORD)dwParam1);
455 case WODM_SETVOLUME: return wodSetVolume (wDevID, (WAVEMAPDATA*)dwUser, dwParam1);
456 case WODM_RESTART: return wodRestart ((WAVEMAPDATA*)dwUser);
457 case WODM_RESET: return wodReset ((WAVEMAPDATA*)dwUser);
458 case WODM_MAPPER_STATUS: return wodMapperStatus ((WAVEMAPDATA*)dwUser, dwParam1, (LPVOID)dwParam2);
460 FIXME("unknown message %d!\n", wMsg);
462 return MMSYSERR_NOTSUPPORTED;
465 /*======================================================================*
467 *======================================================================*/
469 static void CALLBACK widCallback(HWAVE hWave, UINT uMsg, DWORD dwInstance,
470 DWORD dwParam1, DWORD dwParam2)
472 WAVEMAPDATA* wim = (WAVEMAPDATA*)dwInstance;
474 TRACE("(0x%x %u %ld %lx %lx);\n", hWave, uMsg, dwInstance, dwParam1, dwParam2);
476 if (!WAVEMAP_IsData(wim)) {
481 if (hWave != wim->hInnerWave && uMsg != WIM_OPEN)
482 ERR("Shouldn't happen (%08x %08x)\n", hWave, wim->hInnerWave);
487 /* dwParam1 & dwParam2 are supposed to be 0, nothing to do */
490 if (wim->hAcmStream) {
491 LPWAVEHDR lpWaveHdrSrc = (LPWAVEHDR)dwParam1;
492 PACMSTREAMHEADER ash = (PACMSTREAMHEADER)((LPSTR)lpWaveHdrSrc - sizeof(ACMSTREAMHEADER));
493 LPWAVEHDR lpWaveHdrDst = (LPWAVEHDR)ash->dwUser;
495 /* convert data just gotten from waveIn into requested format */
496 if (acmStreamConvert(wim->hAcmStream, ash, 0L) != MMSYSERR_NOERROR) {
497 ERR("ACM conversion failed\n");
500 TRACE("Converted %ld bytes into %ld\n", ash->cbSrcLengthUsed, ash->cbDstLengthUsed);
502 /* and setup the wavehdr to return accordingly */
503 lpWaveHdrDst->dwFlags &= ~WHDR_INQUEUE;
504 lpWaveHdrDst->dwFlags |= WHDR_DONE;
505 lpWaveHdrDst->dwBytesRecorded = ash->cbDstLengthUsed;
506 dwParam1 = (DWORD)lpWaveHdrDst;
510 ERR("Unknown msg %u\n", uMsg);
513 DriverCallback(wim->dwCallback, HIWORD(wim->dwFlags), (HDRVR)wim->hOuterWave,
514 uMsg, wim->dwClientInstance, dwParam1, dwParam2);
517 static DWORD widOpenHelper(WAVEMAPDATA* wim, UINT idx,
518 LPWAVEOPENDESC lpDesc, LPWAVEFORMATEX lpwfx,
523 /* source is always PCM, so the formulas below apply */
524 lpwfx->nBlockAlign = (lpwfx->nChannels * lpwfx->wBitsPerSample) / 8;
525 lpwfx->nAvgBytesPerSec = lpwfx->nSamplesPerSec * lpwfx->nBlockAlign;
526 if (dwFlags & WAVE_FORMAT_QUERY) {
527 ret = acmStreamOpen(NULL, 0, lpwfx, lpDesc->lpFormat, NULL, 0L, 0L, ACM_STREAMOPENF_QUERY);
529 ret = acmStreamOpen(&wim->hAcmStream, 0, lpwfx, lpDesc->lpFormat, NULL, 0L, 0L, 0L);
531 if (ret == MMSYSERR_NOERROR) {
532 ret = waveInOpen(&wim->hInnerWave, idx, lpwfx, (DWORD)widCallback,
533 (DWORD)wim, (dwFlags & ~CALLBACK_TYPEMASK) | CALLBACK_FUNCTION);
534 if (ret != MMSYSERR_NOERROR && !(dwFlags & WAVE_FORMAT_QUERY)) {
535 acmStreamClose(wim->hAcmStream, 0);
542 static DWORD widOpen(LPDWORD lpdwUser, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
546 WAVEMAPDATA* wim = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEMAPDATA));
548 TRACE("(%p %p %08lx)\n", lpdwUser, lpDesc, dwFlags);
551 return MMSYSERR_NOMEM;
554 wim->dwCallback = lpDesc->dwCallback;
555 wim->dwFlags = dwFlags;
556 wim->dwClientInstance = lpDesc->dwInstance;
557 wim->hOuterWave = lpDesc->hWave;
559 ndhi = waveOutGetNumDevs();
560 if (dwFlags & WAVE_MAPPED) {
561 if (lpDesc->uMappedDeviceID >= ndhi) return MMSYSERR_INVALPARAM;
562 ndlo = lpDesc->uMappedDeviceID;
564 dwFlags &= ~WAVE_MAPPED;
569 for (i = ndlo; i < ndhi; i++) {
570 if (waveInOpen(&wim->hInnerWave, i, lpDesc->lpFormat, (DWORD)widCallback,
571 (DWORD)wim, (dwFlags & ~CALLBACK_TYPEMASK) | CALLBACK_FUNCTION | WAVE_FORMAT_DIRECT) == MMSYSERR_NOERROR) {
577 if ((dwFlags & WAVE_FORMAT_DIRECT) == 0)
581 wfx.wFormatTag = WAVE_FORMAT_PCM;
582 wfx.cbSize = 0; /* normally, this field is not used for PCM format, just in case */
583 /* try some ACM stuff */
585 #define TRY(sps,bps) wfx.nSamplesPerSec = (sps); wfx.wBitsPerSample = (bps); \
586 if (widOpenHelper(wim, i, lpDesc, &wfx, dwFlags | WAVE_FORMAT_DIRECT) == MMSYSERR_NOERROR) goto found;
588 for (i = ndlo; i < ndhi; i++) {
589 /* first try with same stereo/mono option as source */
590 wfx.nChannels = lpDesc->lpFormat->nChannels;
595 /* 2^3 => 1, 1^3 => 2, so if stereo, try mono (and the other way around) */
604 HeapFree(GetProcessHeap(), 0, wim);
605 return MMSYSERR_ALLOCATED;
607 if (dwFlags & WAVE_FORMAT_QUERY) {
609 HeapFree(GetProcessHeap(), 0, wim);
611 *lpdwUser = (DWORD)wim;
613 TRACE("Ok (stream=%08lx)\n", (DWORD)wim->hAcmStream);
614 return MMSYSERR_NOERROR;
617 static DWORD widClose(WAVEMAPDATA* wim)
619 DWORD ret = waveInClose(wim->hInnerWave);
621 if (ret == MMSYSERR_NOERROR) {
622 if (wim->hAcmStream) {
623 ret = acmStreamClose(wim->hAcmStream, 0);
625 if (ret == MMSYSERR_NOERROR) {
626 HeapFree(GetProcessHeap(), 0, wim);
632 static DWORD widAddBuffer(WAVEMAPDATA* wim, LPWAVEHDR lpWaveHdrDst, DWORD dwParam2)
634 PACMSTREAMHEADER ash;
635 LPWAVEHDR lpWaveHdrSrc;
637 if (!wim->hAcmStream) {
638 return waveInAddBuffer(wim->hInnerWave, lpWaveHdrDst, dwParam2);
641 lpWaveHdrDst->dwFlags |= WHDR_INQUEUE;
642 ash = (PACMSTREAMHEADER)lpWaveHdrDst->reserved;
644 lpWaveHdrSrc = (LPWAVEHDR)((LPSTR)ash + sizeof(ACMSTREAMHEADER));
645 return waveInAddBuffer(wim->hInnerWave, lpWaveHdrSrc, sizeof(*lpWaveHdrSrc));
648 static DWORD widPrepare(WAVEMAPDATA* wim, LPWAVEHDR lpWaveHdrDst, DWORD dwParam2)
650 PACMSTREAMHEADER ash;
653 LPWAVEHDR lpWaveHdrSrc;
655 if (!wim->hAcmStream) {
656 return waveInPrepareHeader(wim->hInnerWave, lpWaveHdrDst, dwParam2);
658 if (acmStreamSize(wim->hAcmStream, lpWaveHdrDst->dwBufferLength, &size,
659 ACM_STREAMSIZEF_DESTINATION) != MMSYSERR_NOERROR)
660 return MMSYSERR_ERROR;
662 ash = HeapAlloc(GetProcessHeap(), 0, sizeof(ACMSTREAMHEADER) + sizeof(WAVEHDR) + size);
664 return MMSYSERR_NOMEM;
666 ash->cbStruct = sizeof(*ash);
668 ash->dwUser = (DWORD)lpWaveHdrDst;
669 ash->pbSrc = (LPSTR)ash + sizeof(ACMSTREAMHEADER) + sizeof(WAVEHDR);
670 ash->cbSrcLength = size;
671 /* ash->cbSrcLengthUsed */
672 ash->dwSrcUser = 0L; /* FIXME ? */
673 ash->pbDst = lpWaveHdrDst->lpData;
674 ash->cbDstLength = lpWaveHdrDst->dwBufferLength;
675 /* ash->cbDstLengthUsed */
676 ash->dwDstUser = lpWaveHdrDst->dwUser; /* FIXME ? */
677 dwRet = acmStreamPrepareHeader(wim->hAcmStream, ash, 0L);
678 if (dwRet != MMSYSERR_NOERROR)
681 lpWaveHdrSrc = (LPWAVEHDR)((LPSTR)ash + sizeof(ACMSTREAMHEADER));
682 lpWaveHdrSrc->lpData = ash->pbSrc;
683 lpWaveHdrSrc->dwBufferLength = size; /* conversion is not done yet */
684 lpWaveHdrSrc->dwFlags = 0;
685 lpWaveHdrSrc->dwLoops = 0;
686 dwRet = waveInPrepareHeader(wim->hInnerWave, lpWaveHdrSrc, sizeof(*lpWaveHdrSrc));
687 if (dwRet != MMSYSERR_NOERROR)
690 lpWaveHdrDst->reserved = (DWORD)ash;
691 lpWaveHdrDst->dwFlags = WHDR_PREPARED;
693 return MMSYSERR_NOERROR;
695 TRACE("=> (%ld)\n", dwRet);
696 HeapFree(GetProcessHeap(), 0, ash);
700 static DWORD widUnprepare(WAVEMAPDATA* wim, LPWAVEHDR lpWaveHdrDst, DWORD dwParam2)
702 PACMSTREAMHEADER ash;
703 LPWAVEHDR lpWaveHdrSrc;
704 DWORD dwRet1, dwRet2;
706 if (!wim->hAcmStream) {
707 return waveInUnprepareHeader(wim->hInnerWave, lpWaveHdrDst, dwParam2);
709 ash = (PACMSTREAMHEADER)lpWaveHdrDst->reserved;
710 dwRet1 = acmStreamUnprepareHeader(wim->hAcmStream, ash, 0L);
712 lpWaveHdrSrc = (LPWAVEHDR)((LPSTR)ash + sizeof(ACMSTREAMHEADER));
713 dwRet2 = waveInUnprepareHeader(wim->hInnerWave, lpWaveHdrSrc, sizeof(*lpWaveHdrSrc));
715 HeapFree(GetProcessHeap(), 0, ash);
717 lpWaveHdrDst->dwFlags &= ~WHDR_PREPARED;
718 return (dwRet1 == MMSYSERR_NOERROR) ? dwRet2 : dwRet1;
721 static DWORD widGetPosition(WAVEMAPDATA* wim, LPMMTIME lpTime, DWORD dwParam2)
724 FIXME("No position conversion done for PCM => non-PCM, returning PCM position\n");
725 return waveInGetPosition(wim->hInnerWave, lpTime, dwParam2);
728 static DWORD widGetDevCaps(UINT wDevID, WAVEMAPDATA* wim, LPWAVEINCAPSA lpWaveCaps, DWORD dwParam2)
730 /* if opened low driver, forward message */
731 if (WAVEMAP_IsData(wim))
732 return waveInGetDevCapsA(wim->hInnerWave, lpWaveCaps, dwParam2);
733 /* otherwise, return caps of mapper itself */
734 if (wDevID == (UINT)-1 || wDevID == (UINT16)-1) {
735 lpWaveCaps->wMid = 0x00FF;
736 lpWaveCaps->wPid = 0x0001;
737 lpWaveCaps->vDriverVersion = 0x0001;
738 strcpy(lpWaveCaps->szPname, "Wine wave in mapper");
739 lpWaveCaps->dwFormats =
740 WAVE_FORMAT_4M08 | WAVE_FORMAT_4S08 | WAVE_FORMAT_4M16 | WAVE_FORMAT_4S16 |
741 WAVE_FORMAT_2M08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_2M16 | WAVE_FORMAT_2S16 |
742 WAVE_FORMAT_1M08 | WAVE_FORMAT_1S08 | WAVE_FORMAT_1M16 | WAVE_FORMAT_1S16;
743 lpWaveCaps->wChannels = 2;
744 return MMSYSERR_NOERROR;
746 ERR("This shouldn't happen\n");
747 return MMSYSERR_ERROR;
750 static DWORD widStop(WAVEMAPDATA* wim)
752 return waveInStop(wim->hInnerWave);
755 static DWORD widStart(WAVEMAPDATA* wim)
757 return waveInStart(wim->hInnerWave);
760 static DWORD widReset(WAVEMAPDATA* wim)
762 return waveInReset(wim->hInnerWave);
765 static DWORD widMapperStatus(WAVEMAPDATA* wim, DWORD flags, LPVOID ptr)
768 DWORD ret = MMSYSERR_NOTSUPPORTED;
771 case WAVEIN_MAPPER_STATUS_DEVICE:
772 ret = waveInGetID(wim->hInnerWave, &id);
775 case WAVEIN_MAPPER_STATUS_MAPPED:
776 FIXME("Unsupported yet flag=%ld\n", flags);
777 *(LPDWORD)ptr = 0; /* FIXME ?? */
779 case WAVEIN_MAPPER_STATUS_FORMAT:
780 FIXME("Unsupported flag=%ld\n", flags);
781 /* ptr points to a WAVEFORMATEX struct - before or after streaming ? */
782 *(LPDWORD)ptr = 0; /* FIXME ?? */
785 FIXME("Unsupported flag=%ld\n", flags);
792 /**************************************************************************
793 * widMessage (MSACM.@)
795 DWORD WINAPI WAVEMAP_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
796 DWORD dwParam1, DWORD dwParam2)
798 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
799 wDevID, wMsg, dwUser, dwParam1, dwParam2);
806 /* FIXME: Pretend this is supported */
809 case WIDM_OPEN: return widOpen ((LPDWORD)dwUser, (LPWAVEOPENDESC)dwParam1, dwParam2);
810 case WIDM_CLOSE: return widClose ((WAVEMAPDATA*)dwUser);
812 case WIDM_ADDBUFFER: return widAddBuffer ((WAVEMAPDATA*)dwUser, (LPWAVEHDR)dwParam1, dwParam2);
813 case WIDM_PREPARE: return widPrepare ((WAVEMAPDATA*)dwUser, (LPWAVEHDR)dwParam1, dwParam2);
814 case WIDM_UNPREPARE: return widUnprepare ((WAVEMAPDATA*)dwUser, (LPWAVEHDR)dwParam1, dwParam2);
815 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (WAVEMAPDATA*)dwUser, (LPWAVEINCAPSA)dwParam1, dwParam2);
816 case WIDM_GETNUMDEVS: return 1;
817 case WIDM_GETPOS: return widGetPosition ((WAVEMAPDATA*)dwUser, (LPMMTIME)dwParam1, dwParam2);
818 case WIDM_RESET: return widReset ((WAVEMAPDATA*)dwUser);
819 case WIDM_START: return widStart ((WAVEMAPDATA*)dwUser);
820 case WIDM_STOP: return widStop ((WAVEMAPDATA*)dwUser);
821 case WIDM_MAPPER_STATUS: return widMapperStatus ((WAVEMAPDATA*)dwUser, dwParam1, (LPVOID)dwParam2);
823 FIXME("unknown message %u!\n", wMsg);
825 return MMSYSERR_NOTSUPPORTED;
828 /*======================================================================*
830 *======================================================================*/
832 static struct WINE_WAVEMAP* oss = NULL;
834 /**************************************************************************
835 * WAVEMAP_drvOpen [internal]
837 static DWORD WAVEMAP_drvOpen(LPSTR str)
842 /* I know, this is ugly, but who cares... */
843 oss = (struct WINE_WAVEMAP*)1;
847 /**************************************************************************
848 * WAVEMAP_drvClose [internal]
850 static DWORD WAVEMAP_drvClose(DWORD dwDevID)
859 /**************************************************************************
860 * DriverProc (MSACM.@)
862 LONG CALLBACK WAVEMAP_DriverProc(DWORD dwDevID, HDRVR hDriv, DWORD wMsg,
863 DWORD dwParam1, DWORD dwParam2)
865 TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n",
866 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
869 case DRV_LOAD: return 1;
870 case DRV_FREE: return 1;
871 case DRV_OPEN: return WAVEMAP_drvOpen((LPSTR)dwParam1);
872 case DRV_CLOSE: return WAVEMAP_drvClose(dwDevID);
873 case DRV_ENABLE: return 1;
874 case DRV_DISABLE: return 1;
875 case DRV_QUERYCONFIGURE: return 1;
876 case DRV_CONFIGURE: MessageBoxA(0, "WAVEMAP MultiMedia Driver !", "OSS Driver", MB_OK); return 1;
877 case DRV_INSTALL: return DRVCNF_RESTART;
878 case DRV_REMOVE: return DRVCNF_RESTART;
880 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);