Implement A->W call for GetNamedSecurityInfo.
[wine] / dlls / winmm / tests / wave.c
1 /*
2  * Test winmm sound playback in each sound format
3  *
4  * Copyright (c) 2002 Francois Gouget
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
25
26 #include "wine/test.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "mmsystem.h"
31 #include "mmddk.h"
32
33 #include "winmm_test.h"
34
35 /*
36  * Note that in most of this test we may get MMSYSERR_BADDEVICEID errors
37  * at about any time if the user starts another application that uses the
38  * sound device. So we should not report these as test failures.
39  *
40  * This test can play a test tone. But this only makes sense if someone
41  * is going to carefully listen to it, and would only bother everyone else.
42  * So this is only done if the test is being run in interactive mode.
43  */
44
45 #define PI 3.14159265358979323846
46 static char* wave_generate_la(WAVEFORMATEX* wfx, double duration, DWORD* size)
47 {
48     int i;
49     int nb_samples;
50     char* buf;
51     char* b;
52
53     nb_samples=(int)(duration*wfx->nSamplesPerSec);
54     *size=nb_samples*wfx->nBlockAlign;
55     b=buf=malloc(*size);
56     for (i=0;i<nb_samples;i++) {
57         double y=sin(440.0*2*PI*i/wfx->nSamplesPerSec);
58         if (wfx->wBitsPerSample==8) {
59             unsigned char sample=(unsigned char)((double)127.5*(y+1.0));
60             *b++=sample;
61             if (wfx->nChannels==2)
62                *b++=sample;
63         } else {
64             signed short sample=(signed short)((double)32767.5*y-0.5);
65             b[0]=sample & 0xff;
66             b[1]=sample >> 8;
67             b+=2;
68             if (wfx->nChannels==2) {
69                 b[0]=sample & 0xff;
70                 b[1]=sample >> 8;
71                 b+=2;
72             }
73         }
74     }
75     return buf;
76 }
77
78 const char * dev_name(int device)
79 {
80     static char name[16];
81     if (device == WAVE_MAPPER)
82         return "WAVE_MAPPER";
83     sprintf(name, "%d", device);
84     return name;
85 }
86
87 const char* mmsys_error(MMRESULT error)
88 {
89 #define ERR_TO_STR(dev) case dev: return #dev
90     static char unknown[32];
91     switch (error) {
92     ERR_TO_STR(MMSYSERR_NOERROR);
93     ERR_TO_STR(MMSYSERR_ERROR);
94     ERR_TO_STR(MMSYSERR_BADDEVICEID);
95     ERR_TO_STR(MMSYSERR_NOTENABLED);
96     ERR_TO_STR(MMSYSERR_ALLOCATED);
97     ERR_TO_STR(MMSYSERR_INVALHANDLE);
98     ERR_TO_STR(MMSYSERR_NODRIVER);
99     ERR_TO_STR(MMSYSERR_NOMEM);
100     ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
101     ERR_TO_STR(MMSYSERR_BADERRNUM);
102     ERR_TO_STR(MMSYSERR_INVALFLAG);
103     ERR_TO_STR(MMSYSERR_INVALPARAM);
104     ERR_TO_STR(WAVERR_BADFORMAT);
105     ERR_TO_STR(WAVERR_STILLPLAYING);
106     ERR_TO_STR(WAVERR_UNPREPARED);
107     ERR_TO_STR(WAVERR_SYNC);
108     }
109     sprintf(unknown, "Unknown(0x%08x)", error);
110     return unknown;
111 #undef ERR_TO_STR
112 }
113
114 const char* wave_out_error(MMRESULT error)
115 {
116     static char msg[1024];
117     static char long_msg[1100];
118     MMRESULT rc;
119
120     rc = waveOutGetErrorText(error, msg, sizeof(msg));
121     if (rc != MMSYSERR_NOERROR)
122         sprintf(long_msg, "waveOutGetErrorText(%x) failed with error %x", error, rc);
123     else
124         sprintf(long_msg, "%s(%s)", mmsys_error(error), msg);
125     return long_msg;
126 }
127
128 const char * wave_open_flags(DWORD flags)
129 {
130     static char msg[1024];
131     int first = TRUE;
132     msg[0] = 0;
133     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_EVENT) {
134         strcat(msg, "CALLBACK_EVENT");
135         first = FALSE;
136     }
137     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_FUNCTION) {
138         if (!first) strcat(msg, "|");
139         strcat(msg, "CALLBACK_FUNCTION");
140         first = FALSE;
141     }
142     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_NULL) {
143         if (!first) strcat(msg, "|");
144         strcat(msg, "CALLBACK_NULL");
145         first = FALSE;
146     }
147     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_THREAD) {
148         if (!first) strcat(msg, "|");
149         strcat(msg, "CALLBACK_THREAD");
150         first = FALSE;
151     }
152     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_WINDOW) {
153         if (!first) strcat(msg, "|");
154         strcat(msg, "CALLBACK_WINDOW");
155         first = FALSE;
156     }
157     if ((flags & WAVE_ALLOWSYNC) == WAVE_ALLOWSYNC) {
158         if (!first) strcat(msg, "|");
159         strcat(msg, "WAVE_ALLOWSYNC");
160         first = FALSE;
161     }
162     if ((flags & WAVE_FORMAT_DIRECT) == WAVE_FORMAT_DIRECT) {
163         if (!first) strcat(msg, "|");
164         strcat(msg, "WAVE_FORMAT_DIRECT");
165         first = FALSE;
166     }
167     if ((flags & WAVE_FORMAT_QUERY) == WAVE_FORMAT_QUERY) {
168         if (!first) strcat(msg, "|");
169         strcat(msg, "WAVE_FORMAT_QUERY");
170         first = FALSE;
171     }
172     if ((flags & WAVE_MAPPED) == WAVE_MAPPED) {
173         if (!first) strcat(msg, "|");
174         strcat(msg, "WAVE_MAPPED");
175         first = FALSE;
176     }
177     return msg;
178 }
179
180 static const char * wave_out_caps(DWORD dwSupport)
181 {
182 #define ADD_FLAG(f) if (dwSupport & f) strcat(msg, " " #f)
183     static char msg[256];
184     msg[0] = 0;
185
186     ADD_FLAG(WAVECAPS_PITCH);
187     ADD_FLAG(WAVECAPS_PLAYBACKRATE);
188     ADD_FLAG(WAVECAPS_VOLUME);
189     ADD_FLAG(WAVECAPS_LRVOLUME);
190     ADD_FLAG(WAVECAPS_SYNC);
191     ADD_FLAG(WAVECAPS_SAMPLEACCURATE);
192
193     return msg[0] ? msg + 1 : "";
194 #undef ADD_FLAG
195 }
196
197 static const char * wave_time_format(UINT type)
198 {
199     static char msg[32];
200 #define TIME_FORMAT(f) case f: return #f
201     switch (type) {
202     TIME_FORMAT(TIME_MS);
203     TIME_FORMAT(TIME_SAMPLES);
204     TIME_FORMAT(TIME_BYTES);
205     TIME_FORMAT(TIME_SMPTE);
206     TIME_FORMAT(TIME_MIDI);
207     TIME_FORMAT(TIME_TICKS);
208     }
209 #undef TIME_FORMAT
210     sprintf(msg, "Unknown(0x%04x)", type);
211     return msg;
212 }
213
214 static void check_position(int device, HWAVEOUT wout, DWORD bytes, LPWAVEFORMATEX pwfx )
215 {
216     MMTIME mmtime;
217     DWORD samples;
218     double duration;
219     MMRESULT rc;
220
221     samples=bytes/(pwfx->wBitsPerSample/8*pwfx->nChannels);
222     duration=((double)samples)/pwfx->nSamplesPerSec;
223
224     mmtime.wType = TIME_BYTES;
225     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
226     ok(rc==MMSYSERR_NOERROR,
227        "waveOutGetPosition: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
228     if (mmtime.wType == TIME_BYTES)
229         ok(mmtime.u.cb==bytes,
230            "waveOutGetPosition returned %ld bytes, should be %ld\n",
231            mmtime.u.cb, bytes);
232     else
233         trace("TIME_BYTES not supported, returned %s\n",wave_time_format(mmtime.wType));
234
235     mmtime.wType = TIME_SAMPLES;
236     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
237     ok(rc==MMSYSERR_NOERROR,
238        "waveOutGetPosition: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
239     if (mmtime.wType == TIME_SAMPLES)
240         ok(mmtime.u.sample==samples,
241            "waveOutGetPosition returned %ld samples, should be %ld\n",
242            mmtime.u.sample, samples);
243     else
244         trace("TIME_SAMPLES not supported, returned %s\n",wave_time_format(mmtime.wType));
245
246     mmtime.wType = TIME_MS;
247     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
248     ok(rc==MMSYSERR_NOERROR,
249        "waveOutGetPosition: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
250     if (mmtime.wType == TIME_MS)
251         ok(mmtime.u.ms==floor(duration*1000.0),
252            "waveOutGetPosition returned %ld ms, should be %ld\n",
253            mmtime.u.ms, (long)floor(duration*1000.0));
254     else
255         trace("TIME_MS not supported, returned %s\n",wave_time_format(mmtime.wType));
256
257     mmtime.wType = TIME_SMPTE;
258     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
259     ok(rc==MMSYSERR_NOERROR,
260        "waveOutGetPosition: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
261     if (mmtime.wType == TIME_SMPTE)
262     {
263         BYTE frames=ceil(fmod(duration*mmtime.u.smpte.fps, mmtime.u.smpte.fps));
264         ok(mmtime.u.smpte.hour==(BYTE)(floor(duration/(60*60))) &&
265            mmtime.u.smpte.min==(BYTE)(fmod(floor(duration/60), 60)) &&
266            mmtime.u.smpte.sec==(BYTE)(fmod(duration,60)) &&
267            mmtime.u.smpte.frame==frames,
268            "waveOutGetPosition returned %d:%d:%d %d, should be %d:%d:%d %d\n",
269            mmtime.u.smpte.hour, mmtime.u.smpte.min, mmtime.u.smpte.sec, mmtime.u.smpte.frame,
270            (BYTE)(floor(duration/(60*60))),
271            (BYTE)(fmod(floor(duration/60), 60)),
272            (BYTE)(fmod(duration,60)),
273            frames);
274     }
275     else
276         trace("TIME_SMPTE not supported, returned %s\n",wave_time_format(mmtime.wType));
277 }
278
279 static void wave_out_test_deviceOut(int device, double duration, LPWAVEFORMATEX pwfx, DWORD format, DWORD flags, LPWAVEOUTCAPS pcaps)
280 {
281     HWAVEOUT wout;
282     HANDLE hevent;
283     WAVEHDR frag;
284     MMRESULT rc;
285     DWORD volume;
286     WORD nChannels = pwfx->nChannels;
287     WORD wBitsPerSample = pwfx->wBitsPerSample;
288     DWORD nSamplesPerSec = pwfx->nSamplesPerSec;
289
290     hevent=CreateEvent(NULL,FALSE,FALSE,NULL);
291     ok(hevent!=NULL,"CreateEvent: error=%ld\n",GetLastError());
292     if (hevent==NULL)
293         return;
294
295     wout=NULL;
296     rc=waveOutOpen(&wout,device,pwfx,(DWORD)hevent,0,CALLBACK_EVENT|flags);
297     /* Note: Win9x doesn't know WAVE_FORMAT_DIRECT */
298     /* It is acceptable to fail on formats that are not specified to work */
299     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_BADDEVICEID ||
300        rc==MMSYSERR_NOTENABLED || rc==MMSYSERR_NODRIVER || rc==MMSYSERR_ALLOCATED ||
301        ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
302        (flags & WAVE_FORMAT_DIRECT) && !(pcaps->dwFormats & format)) ||
303        ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
304        (!(flags & WAVE_FORMAT_DIRECT) || (flags & WAVE_MAPPED)) && !(pcaps->dwFormats & format)) ||
305        (rc==MMSYSERR_INVALFLAG && (flags & WAVE_FORMAT_DIRECT)),
306        "waveOutOpen: device=%s format=%ldx%2dx%d flags=%lx(%s) rc=%s\n",dev_name(device),
307        pwfx->nSamplesPerSec,pwfx->wBitsPerSample,pwfx->nChannels,CALLBACK_EVENT|flags,
308        wave_open_flags(CALLBACK_EVENT|flags),wave_out_error(rc));
309     if ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
310        (flags & WAVE_FORMAT_DIRECT) && (pcaps->dwFormats & format))
311         trace(" Reason: The device lists this format as supported in it's capabilities but opening it failed.\n");
312     if ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
313        !(pcaps->dwFormats & format))
314         trace("waveOutOpen: device=%s format=%ldx%2dx%d %s rc=%s failed but format not supported so OK.\n",
315               dev_name(device), pwfx->nSamplesPerSec,pwfx->wBitsPerSample,pwfx->nChannels,
316               flags & WAVE_FORMAT_DIRECT ? "flags=WAVE_FORMAT_DIRECT" :
317               flags & WAVE_MAPPED ? "flags=WAVE_MAPPED" : "", mmsys_error(rc));
318     if (rc!=MMSYSERR_NOERROR) {
319         CloseHandle(hevent);
320         return;
321     }
322
323     ok(pwfx->nChannels==nChannels &&
324        pwfx->wBitsPerSample==wBitsPerSample &&
325        pwfx->nSamplesPerSec==nSamplesPerSec,
326        "got the wrong format: %ldx%2dx%d instead of %ldx%2dx%d\n",
327        pwfx->nSamplesPerSec, pwfx->wBitsPerSample,
328        pwfx->nChannels, nSamplesPerSec, wBitsPerSample, nChannels);
329
330     frag.lpData=wave_generate_la(pwfx,duration,&frag.dwBufferLength);
331     frag.dwFlags=0;
332     frag.dwLoops=0;
333
334     rc=waveOutGetVolume(wout,&volume);
335     ok(rc==MMSYSERR_NOERROR,"waveOutGetVolume: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
336
337     rc=waveOutPrepareHeader(wout, &frag, sizeof(frag));
338     ok(rc==MMSYSERR_NOERROR,
339        "waveOutPrepareHeader: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
340
341     if (winetest_interactive && rc==MMSYSERR_NOERROR) {
342         DWORD start,end;
343         trace("Playing %g second 440Hz tone at %5ldx%2dx%d %s\n",duration,
344               pwfx->nSamplesPerSec, pwfx->wBitsPerSample,pwfx->nChannels,
345               flags & WAVE_FORMAT_DIRECT ? "WAVE_FORMAT_DIRECT" :
346               flags & WAVE_MAPPED ? "WAVE_MAPPED" : "");
347
348         /* Check that the position is 0 at start */
349         check_position(device, wout, 0.0, pwfx);
350
351         rc=waveOutSetVolume(wout,0x20002000);
352         ok(rc==MMSYSERR_NOERROR,"waveOutSetVolume: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
353         WaitForSingleObject(hevent,INFINITE);
354
355         start=GetTickCount();
356         rc=waveOutWrite(wout, &frag, sizeof(frag));
357         ok(rc==MMSYSERR_NOERROR,"waveOutWrite: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
358         WaitForSingleObject(hevent,INFINITE);
359
360         /* Check the sound duration was within 10% of the expected value */
361         end=GetTickCount();
362         trace("sound duration=%ld\n",end-start);
363         ok(fabs(1000*duration-end+start)<=100*duration,"The sound played for %ld ms instead of %g ms\n",end-start,1000*duration);
364
365         rc=waveOutSetVolume(wout,volume);
366         ok(rc==MMSYSERR_NOERROR,"waveOutSetVolume: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
367
368         check_position(device, wout, frag.dwBufferLength, pwfx);
369     }
370
371     rc=waveOutUnprepareHeader(wout, &frag, sizeof(frag));
372     ok(rc==MMSYSERR_NOERROR,
373        "waveOutUnprepareHeader: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
374     free(frag.lpData);
375
376     CloseHandle(hevent);
377     rc=waveOutClose(wout);
378     ok(rc==MMSYSERR_NOERROR,"waveOutClose: device=%s rc=%s\n",dev_name(device),wave_out_error(rc));
379 }
380
381 static void wave_out_test_device(int device)
382 {
383     WAVEOUTCAPSA capsA;
384     WAVEOUTCAPSW capsW;
385     WAVEFORMATEX format, oformat;
386     HWAVEOUT wout;
387     MMRESULT rc;
388     UINT f;
389     WCHAR * nameW;
390     CHAR * nameA;
391     DWORD size;
392     DWORD dwPageSize;
393     BYTE * twoPages;
394     SYSTEM_INFO sSysInfo;
395     DWORD flOldProtect;
396     BOOL res;
397
398     GetSystemInfo(&sSysInfo);
399     dwPageSize = sSysInfo.dwPageSize;
400
401     rc=waveOutGetDevCapsA(device,&capsA,sizeof(capsA));
402     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER,
403        "waveOutGetDevCapsA: failed to get capabilities of device %s: rc=%s\n",dev_name(device),wave_out_error(rc));
404     if (rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER)
405         return;
406
407     rc=waveOutGetDevCapsW(device,&capsW,sizeof(capsW));
408     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_NOTSUPPORTED,
409        "waveOutGetDevCapsW: MMSYSERR_NOERROR or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_out_error(rc));
410
411     rc=waveOutGetDevCapsA(device,0,sizeof(capsA));
412     ok(rc==MMSYSERR_INVALPARAM,
413        "waveOutGetDevCapsA: MMSYSERR_INVALPARAM expected, got %s\n",wave_out_error(rc));
414
415     rc=waveOutGetDevCapsW(device,0,sizeof(capsW));
416     ok(rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
417        "waveOutGetDevCapsW: MMSYSERR_INVALPARAM or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_out_error(rc));
418
419 #if 0 /* FIXME: this works on windows but crashes wine */
420     rc=waveOutGetDevCapsA(device,(LPWAVEOUTCAPSA)1,sizeof(capsA));
421     ok(rc==MMSYSERR_INVALPARAM,
422        "waveOutGetDevCapsA: MMSYSERR_INVALPARAM expected, got %s\n",wave_out_error(rc));
423
424     rc=waveOutGetDevCapsW(device,(LPWAVEOUTCAPSW)1,sizeof(capsW));
425     ok(rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
426        "waveOutGetDevCapsW: MMSYSERR_INVALPARAM or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_out_error(rc));
427 #endif
428
429     rc=waveOutGetDevCapsA(device,&capsA,4);
430     ok(rc==MMSYSERR_NOERROR,
431        "waveOutGetDevCapsA: MMSYSERR_NOERROR expected, got %s\n",wave_out_error(rc));
432
433     rc=waveOutGetDevCapsW(device,&capsW,4);
434     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_NOTSUPPORTED,
435        "waveOutGetDevCapsW: MMSYSERR_NOERROR or MMSYSERR_NOTSUPPORTED expected, got %s\n",wave_out_error(rc));
436
437     nameA=NULL;
438     rc=waveOutMessage((HWAVEOUT)device, DRV_QUERYDEVICEINTERFACESIZE, (DWORD_PTR)&size, 0);
439     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
440        "waveOutMessage: failed to get interface size for device: %s rc=%s\n",dev_name(device),wave_out_error(rc));
441     if (rc==MMSYSERR_NOERROR) {
442         nameW = (WCHAR *)malloc(size);
443         rc=waveOutMessage((HWAVEOUT)device, DRV_QUERYDEVICEINTERFACE, (DWORD_PTR)nameW, size);
444         ok(rc==MMSYSERR_NOERROR,"waveOutMessage: failed to get interface name for device: %s rc=%s\n",dev_name(device),wave_out_error(rc));
445         ok(lstrlenW(nameW)+1==size/sizeof(WCHAR),"got an incorrect size: %ld instead of %d\n",size,(lstrlenW(nameW)+1)*sizeof(WCHAR));
446         if (rc==MMSYSERR_NOERROR) {
447             nameA = malloc(size/sizeof(WCHAR));
448             WideCharToMultiByte(CP_ACP, 0, nameW, size/sizeof(WCHAR), nameA, size/sizeof(WCHAR), NULL, NULL);
449         }
450         free(nameW);
451     }
452     else if (rc==MMSYSERR_NOTSUPPORTED) {
453         nameA=strdup("not supported");
454     }
455
456     trace("  %s: \"%s\" (%s) %d.%d (%d:%d): channels=%d formats=%05lx support=%04lx(%s)\n",
457           dev_name(device),capsA.szPname,(nameA?nameA:"failed"),capsA.vDriverVersion >> 8,
458           capsA.vDriverVersion & 0xff,
459           capsA.wMid,capsA.wPid,
460           capsA.wChannels,capsA.dwFormats,capsA.dwSupport,wave_out_caps(capsA.dwSupport));
461     free(nameA);
462
463     if (winetest_interactive && (device != WAVE_MAPPER))
464     {
465         trace("Playing a 5 seconds reference tone.\n");
466         trace("All subsequent tones should be identical to this one.\n");
467         trace("Listen for stutter, changes in pitch, volume, etc.\n");
468         format.wFormatTag=WAVE_FORMAT_PCM;
469         format.nChannels=1;
470         format.wBitsPerSample=8;
471         format.nSamplesPerSec=22050;
472         format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
473         format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
474         format.cbSize=0;
475         wave_out_test_deviceOut(device,5.0,&format,WAVE_FORMAT_2M08,0,&capsA);
476     }
477
478     for (f=0;f<NB_WIN_FORMATS;f++) {
479         format.wFormatTag=WAVE_FORMAT_PCM;
480         format.nChannels=win_formats[f][3];
481         format.wBitsPerSample=win_formats[f][2];
482         format.nSamplesPerSec=win_formats[f][1];
483         format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
484         format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
485         format.cbSize=0;
486         wave_out_test_deviceOut(device,1.0,&format,win_formats[f][0],0,&capsA);
487         wave_out_test_deviceOut(device,1.0,&format,win_formats[f][0],WAVE_FORMAT_DIRECT,&capsA);
488         if (device != WAVE_MAPPER)
489             wave_out_test_deviceOut(device,1.0,&format,win_formats[f][0],WAVE_MAPPED,&capsA);
490     }
491
492     /* Try a PCMWAVEFORMAT aligned next to an unaccessable page for bounds checking */
493     twoPages = VirtualAlloc(NULL, 2 * dwPageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
494     ok(twoPages!=NULL,"Failed to allocate 2 pages of memory\n");
495     if (twoPages) {
496         res = VirtualProtect(twoPages + dwPageSize, dwPageSize, PAGE_NOACCESS, &flOldProtect);
497         ok(res, "Failed to set memory access on second page\n");
498         if (res) {
499             LPWAVEFORMATEX pwfx = (LPWAVEFORMATEX)(twoPages + dwPageSize - sizeof(PCMWAVEFORMAT));
500             pwfx->wFormatTag=WAVE_FORMAT_PCM;
501             pwfx->nChannels=1;
502             pwfx->wBitsPerSample=8;
503             pwfx->nSamplesPerSec=22050;
504             pwfx->nBlockAlign=pwfx->nChannels*pwfx->wBitsPerSample/8;
505             pwfx->nAvgBytesPerSec=pwfx->nSamplesPerSec*pwfx->nBlockAlign;
506             wave_out_test_deviceOut(device,1.0,pwfx,WAVE_FORMAT_2M08,0,&capsA);
507             wave_out_test_deviceOut(device,1.0,pwfx,WAVE_FORMAT_2M08,WAVE_FORMAT_DIRECT,&capsA);
508             if (device != WAVE_MAPPER)
509                 wave_out_test_deviceOut(device,1.0,pwfx,WAVE_FORMAT_2M08,WAVE_MAPPED,&capsA);
510         }
511         VirtualFree(twoPages, 2 * dwPageSize, MEM_RELEASE);
512     }
513
514     /* Try invalid formats to test error handling */
515     trace("Testing invalid format: 11 bits per sample\n");
516     format.wFormatTag=WAVE_FORMAT_PCM;
517     format.nChannels=2;
518     format.wBitsPerSample=11;
519     format.nSamplesPerSec=22050;
520     format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
521     format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
522     format.cbSize=0;
523     oformat=format;
524     rc=waveOutOpen(&wout,device,&format,0,0,CALLBACK_NULL|WAVE_FORMAT_DIRECT);
525     ok(rc==WAVERR_BADFORMAT || rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
526        "waveOutOpen: opening the device in 11 bits mode should fail %s: rc=%s\n",dev_name(device),wave_out_error(rc));
527     if (rc==MMSYSERR_NOERROR) {
528         trace("     got %ldx%2dx%d for %ldx%2dx%d\n",
529               format.nSamplesPerSec, format.wBitsPerSample,
530               format.nChannels,
531               oformat.nSamplesPerSec, oformat.wBitsPerSample,
532               oformat.nChannels);
533         waveOutClose(wout);
534     }
535
536     trace("Testing invalid format: 2 MHz sample rate\n");
537     format.wFormatTag=WAVE_FORMAT_PCM;
538     format.nChannels=2;
539     format.wBitsPerSample=16;
540     format.nSamplesPerSec=2000000;
541     format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
542     format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
543     format.cbSize=0;
544     oformat=format;
545     rc=waveOutOpen(&wout,device,&format,0,0,CALLBACK_NULL|WAVE_FORMAT_DIRECT);
546     ok(rc==WAVERR_BADFORMAT || rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
547        "waveOutOpen: opening the device at 2 MHz sample rate should fail %s: rc=%s\n",dev_name(device),wave_out_error(rc));
548     if (rc==MMSYSERR_NOERROR) {
549         trace("     got %ldx%2dx%d for %ldx%2dx%d\n",
550               format.nSamplesPerSec, format.wBitsPerSample,
551               format.nChannels,
552               oformat.nSamplesPerSec, oformat.wBitsPerSample,
553               oformat.nChannels);
554         waveOutClose(wout);
555     }
556 }
557
558 static void wave_out_tests()
559 {
560     WAVEOUTCAPS caps;
561     WAVEFORMATEX format;
562     HWAVEOUT wout;
563     MMRESULT rc;
564     UINT ndev,d;
565
566     ndev=waveOutGetNumDevs();
567     trace("found %d WaveOut devices\n",ndev);
568
569     rc=waveOutGetDevCapsA(ndev+1,&caps,sizeof(caps));
570     ok(rc==MMSYSERR_BADDEVICEID,
571        "waveOutGetDevCapsA: MMSYSERR_BADDEVICEID expected, got %s\n",mmsys_error(rc));
572
573     rc=waveOutGetDevCapsA(WAVE_MAPPER,&caps,sizeof(caps));
574     if (ndev>0)
575         ok(rc==MMSYSERR_NOERROR,
576            "waveOutGetDevCapsA: MMSYSERR_NOERROR expected, got %s\n",mmsys_error(rc));
577     else
578         ok(rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER,
579            "waveOutGetDevCapsA: MMSYSERR_BADDEVICEID or MMSYSERR_NODRIVER expected, got %s\n",mmsys_error(rc));
580
581     format.wFormatTag=WAVE_FORMAT_PCM;
582     format.nChannels=2;
583     format.wBitsPerSample=16;
584     format.nSamplesPerSec=44100;
585     format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
586     format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
587     format.cbSize=0;
588     rc=waveOutOpen(&wout,ndev+1,&format,0,0,CALLBACK_NULL);
589     ok(rc==MMSYSERR_BADDEVICEID,
590        "waveOutOpen: MMSYSERR_BADDEVICEID expected, got %s\n",mmsys_error(rc));
591
592     for (d=0;d<ndev;d++)
593         wave_out_test_device(d);
594
595     if (ndev>0)
596         wave_out_test_device(WAVE_MAPPER);
597 }
598
599 START_TEST(wave)
600 {
601     wave_out_tests();
602 }