2 * Test winmm sound capture in each sound format
4 * Copyright (c) 2002 Francois Gouget
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/test.h"
33 #include "winmm_test.h"
35 static const char * wave_in_error(MMRESULT error)
37 static char msg[1024];
38 static char long_msg[1100];
41 rc = waveInGetErrorText(error, msg, sizeof(msg));
42 if (rc != MMSYSERR_NOERROR)
43 sprintf(long_msg, "waveInGetErrorText(%x) failed with error %x", error, rc);
45 sprintf(long_msg, "%s(%s)", mmsys_error(error), msg);
49 static void wave_in_test_deviceIn(int device, LPWAVEFORMATEX pwfx, DWORD format, DWORD flags, LPWAVEINCAPS pcaps)
56 WORD nChannels = pwfx->nChannels;
57 WORD wBitsPerSample = pwfx->wBitsPerSample;
58 DWORD nSamplesPerSec = pwfx->nSamplesPerSec;
60 hevent=CreateEvent(NULL,FALSE,FALSE,NULL);
61 ok(hevent!=NULL,"CreateEvent: error=%ld\n",GetLastError());
66 rc=waveInOpen(&win,device,pwfx,(DWORD)hevent,0,CALLBACK_EVENT|flags);
67 /* Note: Win9x doesn't know WAVE_FORMAT_DIRECT */
68 ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_BADDEVICEID ||
69 rc==MMSYSERR_NOTENABLED || rc==MMSYSERR_NODRIVER || rc==MMSYSERR_ALLOCATED ||
70 ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
71 (flags & WAVE_FORMAT_DIRECT) && !(pcaps->dwFormats & format)) ||
72 ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
73 (!(flags & WAVE_FORMAT_DIRECT) || (flags & WAVE_MAPPED)) && !(pcaps->dwFormats & format)) ||
74 (rc==MMSYSERR_INVALFLAG && (flags & WAVE_FORMAT_DIRECT)),
75 "waveInOpen: device=%s format=%ldx%2dx%d flags=%lx(%s) rc=%s\n",dev_name(device),
76 pwfx->nSamplesPerSec,pwfx->wBitsPerSample,pwfx->nChannels,CALLBACK_EVENT|flags,
77 wave_open_flags(CALLBACK_EVENT|flags),wave_in_error(rc));
78 if ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
79 (flags & WAVE_FORMAT_DIRECT) && (pcaps->dwFormats & format))
80 trace(" Reason: The device lists this format as supported in it's capabilities but opening it failed.\n");
81 if ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
82 !(pcaps->dwFormats & format))
83 trace("waveInOpen: device=%s format=%ldx%2dx%d %s rc=%s failed but format not supported so OK.\n",
84 dev_name(device), pwfx->nSamplesPerSec,pwfx->wBitsPerSample,pwfx->nChannels,
85 flags & WAVE_FORMAT_DIRECT ? "flags=WAVE_FORMAT_DIRECT" :
86 flags & WAVE_MAPPED ? "flags=WAVE_MAPPED" : "", mmsys_error(rc));
87 if (rc!=MMSYSERR_NOERROR) {
91 res=WaitForSingleObject(hevent,1000);
92 ok(res==WAIT_OBJECT_0,"WaitForSingleObject failed for open\n");
94 ok(pwfx->nChannels==nChannels &&
95 pwfx->wBitsPerSample==wBitsPerSample &&
96 pwfx->nSamplesPerSec==nSamplesPerSec,
97 "got the wrong format: %ldx%2dx%d instead of %ldx%2dx%d\n",
98 pwfx->nSamplesPerSec, pwfx->wBitsPerSample,
99 pwfx->nChannels, nSamplesPerSec, wBitsPerSample, nChannels);
101 frag.lpData=malloc(pwfx->nAvgBytesPerSec);
102 frag.dwBufferLength=pwfx->nAvgBytesPerSec;
103 frag.dwBytesRecorded=0;
109 rc=waveInPrepareHeader(win, &frag, sizeof(frag));
110 ok(rc==MMSYSERR_NOERROR, "waveInPrepareHeader: device=%s rc=%s\n",dev_name(device),wave_in_error(rc));
111 ok(frag.dwFlags&WHDR_PREPARED,"waveInPrepareHeader: prepared flag not set\n");
113 if (winetest_interactive && rc==MMSYSERR_NOERROR) {
114 trace("Recording for 1 second at %5ldx%2dx%d %s\n",
115 pwfx->nSamplesPerSec, pwfx->wBitsPerSample,pwfx->nChannels,
116 flags & WAVE_FORMAT_DIRECT ? "WAVE_FORMAT_DIRECT" :
117 flags & WAVE_MAPPED ? "WAVE_MAPPED" : "");
118 rc=waveInAddBuffer(win, &frag, sizeof(frag));
119 ok(rc==MMSYSERR_NOERROR,"waveInAddBuffer: device=%s rc=%s\n",dev_name(device),wave_in_error(rc));
122 ok(rc==MMSYSERR_NOERROR,"waveInStart: device=%s rc=%s\n",dev_name(device),wave_in_error(rc));
124 res = WaitForSingleObject(hevent,1200);
125 ok(res==WAIT_OBJECT_0,"WaitForSingleObject failed for header\n");
126 ok(frag.dwFlags&WHDR_DONE,"WHDR_DONE not set in frag.dwFlags\n");
127 ok(frag.dwBytesRecorded==pwfx->nAvgBytesPerSec,"frag.dwBytesRecorded=%ld, should=%ld\n",
128 frag.dwBytesRecorded,pwfx->nAvgBytesPerSec);
129 /* stop playing on error */
130 if (res!=WAIT_OBJECT_0) {
132 ok(rc==MMSYSERR_NOERROR,
133 "waveInStop: device=%s rc=%s\n",dev_name(device),wave_in_error(rc));
137 rc=waveInUnprepareHeader(win, &frag, sizeof(frag));
138 ok(rc==MMSYSERR_NOERROR,
139 "waveInUnprepareHeader: device=%s rc=%s\n",dev_name(device),wave_in_error(rc));
142 ok(rc==MMSYSERR_NOERROR,
143 "waveInClose: device=%s rc=%s\n",dev_name(device),wave_in_error(rc));
144 res=WaitForSingleObject(hevent,1000);
145 ok(res==WAIT_OBJECT_0,"WaitForSingleObject failed for close\n");
147 if (winetest_interactive)
150 * Now play back what we recorded
154 trace("Playing back recorded sound\n");
155 rc=waveOutOpen(&wout,WAVE_MAPPER,pwfx,(DWORD)hevent,0,CALLBACK_EVENT);
156 ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_BADDEVICEID ||
157 rc==MMSYSERR_NOTENABLED || rc==MMSYSERR_NODRIVER || rc==MMSYSERR_ALLOCATED ||
158 ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
159 !(pcaps->dwFormats & format)),
160 "waveOutOpen: device=%s format=%ldx%2dx%d flags=%lx(%s) rc=%s\n",dev_name(device),
161 pwfx->nSamplesPerSec,pwfx->wBitsPerSample,pwfx->nChannels,CALLBACK_EVENT|flags,
162 wave_open_flags(CALLBACK_EVENT),wave_out_error(rc));
163 if (rc==MMSYSERR_NOERROR)
165 rc=waveOutPrepareHeader(wout, &frag, sizeof(frag));
166 ok(rc==MMSYSERR_NOERROR,
167 "waveOutPrepareHeader: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
169 if (rc==MMSYSERR_NOERROR)
171 WaitForSingleObject(hevent,INFINITE);
172 rc=waveOutWrite(wout, &frag, sizeof(frag));
173 ok(rc==MMSYSERR_NOERROR,"waveOutWrite: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
174 WaitForSingleObject(hevent,INFINITE);
176 rc=waveOutUnprepareHeader(wout, &frag, sizeof(frag));
177 ok(rc==MMSYSERR_NOERROR,
178 "waveOutUnprepareHeader: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
180 rc=waveOutClose(wout);
181 ok(rc==MMSYSERR_NOERROR,"waveOutClose: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
184 trace("Unable to play back the recorded sound\n");
191 static void wave_in_test_device(int device)
195 WAVEFORMATEX format,oformat;
204 SYSTEM_INFO sSysInfo;
208 GetSystemInfo(&sSysInfo);
209 dwPageSize = sSysInfo.dwPageSize;
211 rc=waveInGetDevCapsA(device,&capsA,sizeof(capsA));
212 ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER,
213 "waveInGetDevCapsA: failed to get capabilities of device %s: rc=%s\n",dev_name(device),wave_in_error(rc));
214 if (rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER)
217 rc=waveInGetDevCapsW(device,&capsW,sizeof(capsW));
218 ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_NOTSUPPORTED,
219 "waveInGetDevCapsW: MMSYSERR_NOERROR or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_in_error(rc));
221 rc=waveInGetDevCapsA(device,0,sizeof(capsA));
222 ok(rc==MMSYSERR_INVALPARAM,
223 "waveInGetDevCapsA: MMSYSERR_INVALPARAM expected, got %s\n",wave_in_error(rc));
225 rc=waveInGetDevCapsW(device,0,sizeof(capsW));
226 ok(rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
227 "waveInGetDevCapsW: MMSYSERR_INVALPARAM or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_in_error(rc));
229 #if 0 /* FIXME: this works on windows but crashes wine */
230 rc=waveInGetDevCapsA(device,1,sizeof(capsA));
231 ok(rc==MMSYSERR_INVALPARAM,
232 "waveInGetDevCapsA: MMSYSERR_INVALPARAM expected, got %s\n",wave_in_error(rc));
234 rc=waveInGetDevCapsW(device,1,sizeof(capsW));
235 ok(rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
236 "waveInGetDevCapsW: MMSYSERR_INVALPARAM or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_in_error(rc));
239 rc=waveInGetDevCapsA(device,&capsA,4);
240 ok(rc==MMSYSERR_NOERROR,
241 "waveInGetDevCapsA: MMSYSERR_NOERROR expected, got %s\n",wave_in_error(rc));
243 rc=waveInGetDevCapsW(device,&capsW,4);
244 ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_NOTSUPPORTED,
245 "waveInGetDevCapsW: MMSYSERR_NOERROR or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_in_error(rc));
248 rc=waveInMessage((HWAVEIN)device, DRV_QUERYDEVICEINTERFACESIZE, (DWORD_PTR)&size, 0);
249 ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
250 "waveInMessage: failed to get interface size for device: %s rc=%s\n",dev_name(device),wave_in_error(rc));
251 if (rc==MMSYSERR_NOERROR) {
252 nameW = (WCHAR *)malloc(size);
253 rc=waveInMessage((HWAVEIN)device, DRV_QUERYDEVICEINTERFACE, (DWORD_PTR)nameW, size);
254 ok(rc==MMSYSERR_NOERROR,"waveInMessage: failed to get interface name for device: %s rc=%s\n",dev_name(device),wave_in_error(rc));
255 ok(lstrlenW(nameW)+1==size/sizeof(WCHAR),"got an incorrect size: %ld instead of %d\n",size,(lstrlenW(nameW)+1)*sizeof(WCHAR));
256 if (rc==MMSYSERR_NOERROR) {
257 nameA = malloc(size/sizeof(WCHAR));
258 WideCharToMultiByte(CP_ACP, 0, nameW, size/sizeof(WCHAR), nameA, size/sizeof(WCHAR), NULL, NULL);
261 } else if (rc==MMSYSERR_NOTSUPPORTED) {
262 nameA=strdup("not supported");
265 trace(" %s: \"%s\" (%s) %d.%d (%d:%d): channels=%d formats=%05lx\n",
266 dev_name(device),capsA.szPname,(nameA?nameA:"failed"),capsA.vDriverVersion >> 8,
267 capsA.vDriverVersion & 0xff,
268 capsA.wMid,capsA.wPid,
269 capsA.wChannels,capsA.dwFormats);
273 for (f=0;f<NB_WIN_FORMATS;f++) {
274 format.wFormatTag=WAVE_FORMAT_PCM;
275 format.nChannels=win_formats[f][3];
276 format.wBitsPerSample=win_formats[f][2];
277 format.nSamplesPerSec=win_formats[f][1];
278 format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
279 format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
281 wave_in_test_deviceIn(device,&format,win_formats[f][0],0, &capsA);
282 wave_in_test_deviceIn(device,&format,win_formats[f][0],WAVE_FORMAT_DIRECT, &capsA);
283 if (device != WAVE_MAPPER)
284 wave_in_test_deviceIn(device,&format,win_formats[f][0],WAVE_MAPPED, &capsA);
287 /* Try a PCMWAVEFORMAT aligned next to an unaccessable page for bounds checking */
288 twoPages = VirtualAlloc(NULL, 2 * dwPageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
289 ok(twoPages!=NULL,"Failed to allocate 2 pages of memory\n");
291 res = VirtualProtect(twoPages + dwPageSize, dwPageSize, PAGE_NOACCESS, &flOldProtect);
292 ok(res, "Failed to set memory access on second page\n");
294 LPWAVEFORMATEX pwfx = (LPWAVEFORMATEX)(twoPages + dwPageSize - sizeof(PCMWAVEFORMAT));
295 pwfx->wFormatTag=WAVE_FORMAT_PCM;
297 pwfx->wBitsPerSample=8;
298 pwfx->nSamplesPerSec=22050;
299 pwfx->nBlockAlign=pwfx->nChannels*pwfx->wBitsPerSample/8;
300 pwfx->nAvgBytesPerSec=pwfx->nSamplesPerSec*pwfx->nBlockAlign;
301 wave_in_test_deviceIn(device,pwfx,WAVE_FORMAT_2M08,0, &capsA);
302 wave_in_test_deviceIn(device,pwfx,WAVE_FORMAT_2M08,WAVE_FORMAT_DIRECT, &capsA);
303 if (device != WAVE_MAPPER)
304 wave_in_test_deviceIn(device,pwfx,WAVE_FORMAT_2M08,WAVE_MAPPED, &capsA);
306 VirtualFree(twoPages, 2 * dwPageSize, MEM_RELEASE);
309 /* Try invalid formats to test error handling */
310 trace("Testing invalid format: 2 MHz sample rate\n");
311 format.wFormatTag=WAVE_FORMAT_PCM;
313 format.wBitsPerSample=16;
314 format.nSamplesPerSec=2000000;
315 format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
316 format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
319 rc=waveInOpen(&win,device,&format,0,0,CALLBACK_NULL|WAVE_FORMAT_DIRECT);
320 ok(rc==WAVERR_BADFORMAT || rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
321 "waveInOpen: opening the device with 2 MHz sample rate should fail %s: rc=%s\n",dev_name(device),wave_in_error(rc));
322 if (rc==MMSYSERR_NOERROR) {
323 trace(" got %ldx%2dx%d for %ldx%2dx%d\n",
324 format.nSamplesPerSec, format.wBitsPerSample,
326 oformat.nSamplesPerSec, oformat.wBitsPerSample,
332 static void wave_in_tests()
340 ndev=waveInGetNumDevs();
341 trace("found %d WaveIn devices\n",ndev);
343 rc=waveInGetDevCapsA(ndev+1,&caps,sizeof(caps));
344 ok(rc==MMSYSERR_BADDEVICEID,
345 "waveInGetDevCapsA: MMSYSERR_BADDEVICEID expected, got %s\n",wave_in_error(rc));
347 rc=waveInGetDevCapsA(WAVE_MAPPER,&caps,sizeof(caps));
349 ok(rc==MMSYSERR_NOERROR,
350 "waveInGetDevCapsA: MMSYSERR_NOERROR expected, got %s\n",wave_in_error(rc));
352 ok(rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER,
353 "waveInGetDevCapsA: MMSYSERR_BADDEVICEID or MMSYSERR_NODRIVER expected, got %s\n",wave_in_error(rc));
355 format.wFormatTag=WAVE_FORMAT_PCM;
357 format.wBitsPerSample=16;
358 format.nSamplesPerSec=44100;
359 format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
360 format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
362 rc=waveInOpen(&win,ndev+1,&format,0,0,CALLBACK_NULL);
363 ok(rc==MMSYSERR_BADDEVICEID,
364 "waveInOpen: MMSYSERR_BADDEVICEID expected, got %s\n",wave_in_error(rc));
367 wave_in_test_device(d);
370 wave_in_test_device(WAVE_MAPPER);