winmm/tests: Don't use same buffer for both input and output.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winuser.h"
30 #include "winnls.h"
31 #include "mmsystem.h"
32 #define NOBITMAP
33 #include "mmreg.h"
34 #include "ks.h"
35 #include "ksguid.h"
36 #include "ksmedia.h"
37
38 #include "winmm_test.h"
39
40 static void test_multiple_waveopens(void)
41 {
42     HWAVEOUT handle1, handle2;
43     MMRESULT ret;
44     WAVEFORMATEX wfx;
45
46     wfx.wFormatTag = WAVE_FORMAT_PCM;
47     wfx.nChannels = 1;
48     wfx.nSamplesPerSec = 11025;
49     wfx.nBlockAlign = 1;
50     wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
51     wfx.wBitsPerSample = 8;
52     wfx.cbSize = 0;
53
54     ret = waveOutOpen(&handle1, 0, &wfx, 0, 0, 0);
55     if (ret != MMSYSERR_NOERROR)
56     {
57         skip("Could not do the duplicate waveopen test\n");
58         return;
59     }
60
61     ret = waveOutOpen(&handle2, 0, &wfx, 0, 0, 0);
62     /* Modern Windows allows for wave-out devices to be opened multiple times.
63      * Some Wine audio drivers allow that and some don't.  To avoid false alarms
64      * for those that do, don't "todo_wine ok(...)" on success.
65      */
66     if (ret != MMSYSERR_NOERROR)
67     {
68         todo_wine ok(ret == MMSYSERR_NOERROR || broken(ret == MMSYSERR_ALLOCATED), /* winME */
69                      "second waveOutOpen returns: %x\n", ret);
70     }
71     else
72         waveOutClose(handle2);
73
74     waveOutClose(handle1);
75 }
76
77 /*
78  * Note that in most of this test we may get MMSYSERR_BADDEVICEID errors
79  * at about any time if the user starts another application that uses the
80  * sound device. So we should not report these as test failures.
81  *
82  * This test can play a test tone. But this only makes sense if someone
83  * is going to carefully listen to it, and would only bother everyone else.
84  * So this is only done if the test is being run in interactive mode.
85  */
86
87 #define PI 3.14159265358979323846
88 static char* wave_generate_la(WAVEFORMATEX* wfx, double duration, DWORD* size)
89 {
90     int i,j;
91     int nb_samples;
92     char* buf;
93     char* b;
94     WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE*)wfx;
95
96     nb_samples=(int)(duration*wfx->nSamplesPerSec);
97     *size=nb_samples*wfx->nBlockAlign;
98     b=buf=HeapAlloc(GetProcessHeap(), 0, *size);
99     for (i=0;i<nb_samples;i++) {
100         double y=sin(440.0*2*PI*i/wfx->nSamplesPerSec);
101         if (wfx->wBitsPerSample==8) {
102             unsigned char sample=(unsigned char)((double)127.5*(y+1.0));
103             for (j = 0; j < wfx->nChannels; j++)
104                 *b++=sample;
105         } else if (wfx->wBitsPerSample==16) {
106             signed short sample=(signed short)((double)32767.5*y-0.5);
107             for (j = 0; j < wfx->nChannels; j++) {
108                 b[0]=sample & 0xff;
109                 b[1]=sample >> 8;
110                 b+=2;
111             }
112         } else if (wfx->wBitsPerSample==24) {
113             signed int sample=(signed int)(((double)0x7fffff+0.5)*y-0.5);
114             for (j = 0; j < wfx->nChannels; j++) {
115                 b[0]=sample & 0xff;
116                 b[1]=(sample >> 8) & 0xff;
117                 b[2]=(sample >> 16) & 0xff;
118                 b+=3;
119             }
120         } else if ((wfx->wBitsPerSample==32) && ((wfx->wFormatTag == WAVE_FORMAT_PCM) ||
121             ((wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
122             IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))) {
123             signed int sample=(signed int)(((double)0x7fffffff+0.5)*y-0.5);
124             for (j = 0; j < wfx->nChannels; j++) {
125                 b[0]=sample & 0xff;
126                 b[1]=(sample >> 8) & 0xff;
127                 b[2]=(sample >> 16) & 0xff;
128                 b[3]=(sample >> 24) & 0xff;
129                 b+=4;
130             }
131         } else if ((wfx->wBitsPerSample==32) && (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
132             IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
133             union { float f; char c[4]; } sample;
134             sample.f=(float)y;
135             for (j = 0; j < wfx->nChannels; j++) {
136                 b[0]=sample.c[0];
137                 b[1]=sample.c[1];
138                 b[2]=sample.c[2];
139                 b[3]=sample.c[3];
140                 b+=4;
141             }
142         }
143     }
144     return buf;
145 }
146
147 static char* wave_generate_silence(WAVEFORMATEX* wfx, double duration, DWORD* size)
148 {
149     int i,j;
150     int nb_samples;
151     char* buf;
152     char* b;
153     WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE*)wfx;
154
155     nb_samples=(int)(duration*wfx->nSamplesPerSec);
156     *size=nb_samples*wfx->nBlockAlign;
157     b=buf=HeapAlloc(GetProcessHeap(), 0, *size);
158     for (i=0;i<nb_samples;i++) {
159         if (wfx->wBitsPerSample==8) {
160             for (j = 0; j < wfx->nChannels; j++)
161                 *b++=128;
162         } else if (wfx->wBitsPerSample==16) {
163             for (j = 0; j < wfx->nChannels; j++) {
164                 b[0]=0;
165                 b[1]=0;
166                 b+=2;
167             }
168         } else if (wfx->wBitsPerSample==24) {
169             for (j = 0; j < wfx->nChannels; j++) {
170                 b[0]=0;
171                 b[1]=0;
172                 b[2]=0;
173                 b+=3;
174             }
175         } else if ((wfx->wBitsPerSample==32) && ((wfx->wFormatTag == WAVE_FORMAT_PCM) ||
176             ((wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
177             IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))) {
178             for (j = 0; j < wfx->nChannels; j++) {
179                 b[0]=0;
180                 b[1]=0;
181                 b[2]=0;
182                 b[3]=0;
183                 b+=4;
184             }
185         } else if ((wfx->wBitsPerSample==32) && (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
186             IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
187             union { float f; char c[4]; } sample;
188             sample.f=0;
189             for (j = 0; j < wfx->nChannels; j++) {
190                 b[0]=sample.c[0];
191                 b[1]=sample.c[1];
192                 b[2]=sample.c[2];
193                 b[3]=sample.c[3];
194                 b+=4;
195             }
196         }
197     }
198     return buf;
199 }
200
201 const char * dev_name(int device)
202 {
203     static char name[16];
204     if (device == WAVE_MAPPER)
205         return "WAVE_MAPPER";
206     sprintf(name, "%d", device);
207     return name;
208 }
209
210 const char* mmsys_error(MMRESULT error)
211 {
212 #define ERR_TO_STR(dev) case dev: return #dev
213     static char unknown[32];
214     switch (error) {
215     ERR_TO_STR(MMSYSERR_NOERROR);
216     ERR_TO_STR(MMSYSERR_ERROR);
217     ERR_TO_STR(MMSYSERR_BADDEVICEID);
218     ERR_TO_STR(MMSYSERR_NOTENABLED);
219     ERR_TO_STR(MMSYSERR_ALLOCATED);
220     ERR_TO_STR(MMSYSERR_INVALHANDLE);
221     ERR_TO_STR(MMSYSERR_NODRIVER);
222     ERR_TO_STR(MMSYSERR_NOMEM);
223     ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
224     ERR_TO_STR(MMSYSERR_BADERRNUM);
225     ERR_TO_STR(MMSYSERR_INVALFLAG);
226     ERR_TO_STR(MMSYSERR_INVALPARAM);
227     ERR_TO_STR(WAVERR_BADFORMAT);
228     ERR_TO_STR(WAVERR_STILLPLAYING);
229     ERR_TO_STR(WAVERR_UNPREPARED);
230     ERR_TO_STR(WAVERR_SYNC);
231     ERR_TO_STR(MIDIERR_UNPREPARED);
232     ERR_TO_STR(MIDIERR_STILLPLAYING);
233     ERR_TO_STR(MIDIERR_NOTREADY);
234     ERR_TO_STR(MIDIERR_NODEVICE);
235     ERR_TO_STR(MIDIERR_INVALIDSETUP);
236     ERR_TO_STR(TIMERR_NOCANDO);
237     ERR_TO_STR(TIMERR_STRUCT);
238     ERR_TO_STR(JOYERR_PARMS);
239     ERR_TO_STR(JOYERR_NOCANDO);
240     ERR_TO_STR(JOYERR_UNPLUGGED);
241     ERR_TO_STR(MIXERR_INVALLINE);
242     ERR_TO_STR(MIXERR_INVALCONTROL);
243     ERR_TO_STR(MIXERR_INVALVALUE);
244     ERR_TO_STR(MMIOERR_FILENOTFOUND);
245     ERR_TO_STR(MMIOERR_OUTOFMEMORY);
246     ERR_TO_STR(MMIOERR_CANNOTOPEN);
247     ERR_TO_STR(MMIOERR_CANNOTCLOSE);
248     ERR_TO_STR(MMIOERR_CANNOTREAD);
249     ERR_TO_STR(MMIOERR_CANNOTWRITE);
250     ERR_TO_STR(MMIOERR_CANNOTSEEK);
251     ERR_TO_STR(MMIOERR_CANNOTEXPAND);
252     ERR_TO_STR(MMIOERR_CHUNKNOTFOUND);
253     ERR_TO_STR(MMIOERR_UNBUFFERED);
254     }
255     sprintf(unknown, "Unknown(0x%08x)", error);
256     return unknown;
257 #undef ERR_TO_STR
258 }
259
260 const char* wave_out_error(MMRESULT error)
261 {
262     static char msg[1024];
263     static char long_msg[1100];
264     MMRESULT rc;
265
266     rc = waveOutGetErrorText(error, msg, sizeof(msg));
267     if (rc != MMSYSERR_NOERROR)
268         sprintf(long_msg, "waveOutGetErrorText(%x) failed with error %x",
269                 error, rc);
270     else
271         sprintf(long_msg, "%s(%s)", mmsys_error(error), msg);
272     return long_msg;
273 }
274
275 const char * wave_open_flags(DWORD flags)
276 {
277     static char msg[1024];
278     int first = TRUE;
279     msg[0] = 0;
280     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_EVENT) {
281         strcat(msg, "CALLBACK_EVENT");
282         first = FALSE;
283     }
284     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_FUNCTION) {
285         if (!first) strcat(msg, "|");
286         strcat(msg, "CALLBACK_FUNCTION");
287         first = FALSE;
288     }
289     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_NULL) {
290         if (!first) strcat(msg, "|");
291         strcat(msg, "CALLBACK_NULL");
292         first = FALSE;
293     }
294     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_THREAD) {
295         if (!first) strcat(msg, "|");
296         strcat(msg, "CALLBACK_THREAD");
297         first = FALSE;
298     }
299     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_WINDOW) {
300         if (!first) strcat(msg, "|");
301         strcat(msg, "CALLBACK_WINDOW");
302         first = FALSE;
303     }
304     if ((flags & WAVE_ALLOWSYNC) == WAVE_ALLOWSYNC) {
305         if (!first) strcat(msg, "|");
306         strcat(msg, "WAVE_ALLOWSYNC");
307         first = FALSE;
308     }
309     if ((flags & WAVE_FORMAT_DIRECT) == WAVE_FORMAT_DIRECT) {
310         if (!first) strcat(msg, "|");
311         strcat(msg, "WAVE_FORMAT_DIRECT");
312         first = FALSE;
313     }
314     if ((flags & WAVE_FORMAT_QUERY) == WAVE_FORMAT_QUERY) {
315         if (!first) strcat(msg, "|");
316         strcat(msg, "WAVE_FORMAT_QUERY");
317         first = FALSE;
318     }
319     if ((flags & WAVE_MAPPED) == WAVE_MAPPED) {
320         if (!first) strcat(msg, "|");
321         strcat(msg, "WAVE_MAPPED");
322         first = FALSE;
323     }
324     return msg;
325 }
326
327 static const char * wave_header_flags(DWORD flags)
328 {
329 #define WHDR_MASK (WHDR_BEGINLOOP|WHDR_DONE|WHDR_ENDLOOP|WHDR_INQUEUE|WHDR_PREPARED)
330     static char msg[1024];
331     int first = TRUE;
332     msg[0] = 0;
333     if (flags & WHDR_BEGINLOOP) {
334         strcat(msg, "WHDR_BEGINLOOP");
335         first = FALSE;
336     }
337     if (flags & WHDR_DONE) {
338         if (!first) strcat(msg, " ");
339         strcat(msg, "WHDR_DONE");
340         first = FALSE;
341     }
342     if (flags & WHDR_ENDLOOP) {
343         if (!first) strcat(msg, " ");
344         strcat(msg, "WHDR_ENDLOOP");
345         first = FALSE;
346     }
347     if (flags & WHDR_INQUEUE) {
348         if (!first) strcat(msg, " ");
349         strcat(msg, "WHDR_INQUEUE");
350         first = FALSE;
351     }
352     if (flags & WHDR_PREPARED) {
353         if (!first) strcat(msg, " ");
354         strcat(msg, "WHDR_PREPARED");
355         first = FALSE;
356     }
357     if (flags & ~WHDR_MASK) {
358         char temp[32];
359         sprintf(temp, "UNKNOWN(0x%08x)", flags & ~WHDR_MASK);
360         if (!first) strcat(msg, " ");
361         strcat(msg, temp);
362     }
363     return msg;
364 }
365
366 static const char * wave_out_caps(DWORD dwSupport)
367 {
368 #define ADD_FLAG(f) if (dwSupport & f) strcat(msg, " " #f)
369     static char msg[256];
370     msg[0] = 0;
371
372     ADD_FLAG(WAVECAPS_PITCH);
373     ADD_FLAG(WAVECAPS_PLAYBACKRATE);
374     ADD_FLAG(WAVECAPS_VOLUME);
375     ADD_FLAG(WAVECAPS_LRVOLUME);
376     ADD_FLAG(WAVECAPS_SYNC);
377     ADD_FLAG(WAVECAPS_SAMPLEACCURATE);
378
379     return msg[0] ? msg + 1 : "";
380 #undef ADD_FLAG
381 }
382
383 const char * wave_time_format(UINT type)
384 {
385     static char msg[32];
386 #define TIME_FORMAT(f) case f: return #f
387     switch (type) {
388     TIME_FORMAT(TIME_MS);
389     TIME_FORMAT(TIME_SAMPLES);
390     TIME_FORMAT(TIME_BYTES);
391     TIME_FORMAT(TIME_SMPTE);
392     TIME_FORMAT(TIME_MIDI);
393     TIME_FORMAT(TIME_TICKS);
394     }
395 #undef TIME_FORMAT
396     sprintf(msg, "Unknown(0x%04x)", type);
397     return msg;
398 }
399
400 const char * get_format_str(WORD format)
401 {
402     static char msg[32];
403 #define WAVE_FORMAT(f) case f: return #f
404     switch (format) {
405     WAVE_FORMAT(WAVE_FORMAT_PCM);
406     WAVE_FORMAT(WAVE_FORMAT_ADPCM);
407     WAVE_FORMAT(WAVE_FORMAT_IBM_CVSD);
408     WAVE_FORMAT(WAVE_FORMAT_ALAW);
409     WAVE_FORMAT(WAVE_FORMAT_MULAW);
410     WAVE_FORMAT(WAVE_FORMAT_OKI_ADPCM);
411     WAVE_FORMAT(WAVE_FORMAT_IMA_ADPCM);
412     WAVE_FORMAT(WAVE_FORMAT_MEDIASPACE_ADPCM);
413     WAVE_FORMAT(WAVE_FORMAT_SIERRA_ADPCM);
414     WAVE_FORMAT(WAVE_FORMAT_G723_ADPCM);
415     WAVE_FORMAT(WAVE_FORMAT_DIGISTD);
416     WAVE_FORMAT(WAVE_FORMAT_DIGIFIX);
417     WAVE_FORMAT(WAVE_FORMAT_DIALOGIC_OKI_ADPCM);
418     WAVE_FORMAT(WAVE_FORMAT_YAMAHA_ADPCM);
419     WAVE_FORMAT(WAVE_FORMAT_SONARC);
420     WAVE_FORMAT(WAVE_FORMAT_DSPGROUP_TRUESPEECH);
421     WAVE_FORMAT(WAVE_FORMAT_ECHOSC1);
422     WAVE_FORMAT(WAVE_FORMAT_AUDIOFILE_AF36);
423     WAVE_FORMAT(WAVE_FORMAT_APTX);
424     WAVE_FORMAT(WAVE_FORMAT_AUDIOFILE_AF10);
425     WAVE_FORMAT(WAVE_FORMAT_DOLBY_AC2);
426     WAVE_FORMAT(WAVE_FORMAT_GSM610);
427     WAVE_FORMAT(WAVE_FORMAT_ANTEX_ADPCME);
428     WAVE_FORMAT(WAVE_FORMAT_CONTROL_RES_VQLPC);
429     WAVE_FORMAT(WAVE_FORMAT_DIGIREAL);
430     WAVE_FORMAT(WAVE_FORMAT_DIGIADPCM);
431     WAVE_FORMAT(WAVE_FORMAT_CONTROL_RES_CR10);
432     WAVE_FORMAT(WAVE_FORMAT_NMS_VBXADPCM);
433     WAVE_FORMAT(WAVE_FORMAT_G721_ADPCM);
434     WAVE_FORMAT(WAVE_FORMAT_MPEG);
435     WAVE_FORMAT(WAVE_FORMAT_MPEGLAYER3);
436     WAVE_FORMAT(WAVE_FORMAT_CREATIVE_ADPCM);
437     WAVE_FORMAT(WAVE_FORMAT_CREATIVE_FASTSPEECH8);
438     WAVE_FORMAT(WAVE_FORMAT_CREATIVE_FASTSPEECH10);
439     WAVE_FORMAT(WAVE_FORMAT_FM_TOWNS_SND);
440     WAVE_FORMAT(WAVE_FORMAT_OLIGSM);
441     WAVE_FORMAT(WAVE_FORMAT_OLIADPCM);
442     WAVE_FORMAT(WAVE_FORMAT_OLICELP);
443     WAVE_FORMAT(WAVE_FORMAT_OLISBC);
444     WAVE_FORMAT(WAVE_FORMAT_OLIOPR);
445     WAVE_FORMAT(WAVE_FORMAT_DEVELOPMENT);
446     WAVE_FORMAT(WAVE_FORMAT_EXTENSIBLE);
447     }
448 #undef WAVE_FORMAT
449     sprintf(msg, "Unknown(0x%04x)", format);
450     return msg;
451 }
452
453 DWORD bytes_to_samples(DWORD bytes, LPWAVEFORMATEX pwfx)
454 {
455     return bytes / pwfx->nBlockAlign;
456 }
457
458 DWORD bytes_to_ms(DWORD bytes, LPWAVEFORMATEX pwfx)
459 {
460     return bytes_to_samples(bytes, pwfx) * 1000 / pwfx->nSamplesPerSec;
461 }
462
463 DWORD time_to_bytes(LPMMTIME mmtime, LPWAVEFORMATEX pwfx)
464 {
465     if (mmtime->wType == TIME_BYTES)
466         return mmtime->u.cb;
467     else if (mmtime->wType == TIME_SAMPLES)
468         return mmtime->u.sample * pwfx->nBlockAlign;
469     else if (mmtime->wType == TIME_MS)
470         return mmtime->u.ms * pwfx->nAvgBytesPerSec / 1000;
471     else if (mmtime->wType == TIME_SMPTE)
472         return ((mmtime->u.smpte.hour * 60.0 * 60.0) +
473                 (mmtime->u.smpte.min * 60.0) +
474                 (mmtime->u.smpte.sec) +
475                 (mmtime->u.smpte.frame / 30.0)) * pwfx->nAvgBytesPerSec;
476
477     trace("FIXME: time_to_bytes() type not supported\n");
478     return -1;
479 }
480
481 static void check_position(int device, HWAVEOUT wout, DWORD bytes,
482                            LPWAVEFORMATEX pwfx )
483 {
484     MMTIME mmtime;
485     MMRESULT rc;
486     DWORD returned;
487
488     mmtime.wType = TIME_BYTES;
489     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
490     ok(rc==MMSYSERR_NOERROR,
491        "waveOutGetPosition(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
492     if (mmtime.wType != TIME_BYTES && winetest_debug > 1)
493         trace("waveOutGetPosition(%s): TIME_BYTES not supported, returned %s\n",
494               dev_name(device),wave_time_format(mmtime.wType));
495     returned = time_to_bytes(&mmtime, pwfx);
496     ok(returned == bytes, "waveOutGetPosition(%s): returned %d bytes, "
497        "should be %d\n", dev_name(device), returned, bytes);
498
499     mmtime.wType = TIME_SAMPLES;
500     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
501     ok(rc==MMSYSERR_NOERROR,
502        "waveOutGetPosition(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
503     if (mmtime.wType != TIME_SAMPLES && winetest_debug > 1)
504         trace("waveOutGetPosition(%s): TIME_SAMPLES not supported, "
505               "returned %s\n",dev_name(device),wave_time_format(mmtime.wType));
506     returned = time_to_bytes(&mmtime, pwfx);
507     ok(returned == bytes, "waveOutGetPosition(%s): returned %d samples "
508        "(%d bytes), should be %d (%d bytes)\n", dev_name(device),
509        bytes_to_samples(returned, pwfx), returned,
510        bytes_to_samples(bytes, pwfx), bytes);
511
512     mmtime.wType = TIME_MS;
513     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
514     ok(rc==MMSYSERR_NOERROR,
515        "waveOutGetPosition(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
516     if (mmtime.wType != TIME_MS && winetest_debug > 1)
517         trace("waveOutGetPosition(%s): TIME_MS not supported, returned %s\n",
518               dev_name(device), wave_time_format(mmtime.wType));
519     returned = time_to_bytes(&mmtime, pwfx);
520     ok(returned == bytes, "waveOutGetPosition(%s): returned %d ms, "
521        "(%d bytes), should be %d (%d bytes)\n", dev_name(device),
522        bytes_to_ms(returned, pwfx), returned,
523        bytes_to_ms(bytes, pwfx), bytes);
524
525     mmtime.wType = TIME_SMPTE;
526     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
527     ok(rc==MMSYSERR_NOERROR,
528        "waveOutGetPosition(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
529     if (mmtime.wType != TIME_SMPTE && winetest_debug > 1)
530         trace("waveOutGetPosition(%s): TIME_SMPTE not supported, returned %s\n",
531               dev_name(device),wave_time_format(mmtime.wType));
532     returned = time_to_bytes(&mmtime, pwfx);
533     ok(returned == bytes, "waveOutGetPosition(%s): SMPTE test failed\n",
534        dev_name(device));
535
536     mmtime.wType = TIME_MIDI;
537     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
538     ok(rc==MMSYSERR_NOERROR,
539        "waveOutGetPosition(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
540     if (mmtime.wType != TIME_MIDI && winetest_debug > 1)
541         trace("waveOutGetPosition(%s): TIME_MIDI not supported, returned %s\n",
542               dev_name(device),wave_time_format(mmtime.wType));
543     returned = time_to_bytes(&mmtime, pwfx);
544     ok(returned == bytes, "waveOutGetPosition(%s): MIDI test failed\n",
545        dev_name(device));
546
547     mmtime.wType = TIME_TICKS;
548     rc=waveOutGetPosition(wout, &mmtime, sizeof(mmtime));
549     ok(rc==MMSYSERR_NOERROR,
550        "waveOutGetPosition(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
551     if (mmtime.wType != TIME_TICKS && winetest_debug > 1)
552         trace("waveOutGetPosition(%s): TIME_TICKS not supported, returned %s\n",
553               dev_name(device),wave_time_format(mmtime.wType));
554     returned = time_to_bytes(&mmtime, pwfx);
555     ok(returned == bytes, "waveOutGetPosition(%s): TICKS test failed\n",
556        dev_name(device));
557 }
558
559 static void CALLBACK callback_func(HWAVEOUT hwo, UINT uMsg,
560                                    DWORD_PTR dwInstance,
561                                    DWORD dwParam1, DWORD dwParam2)
562 {
563     SetEvent((HANDLE)dwInstance);
564 }
565
566 static DWORD WINAPI callback_thread(LPVOID lpParameter)
567 {
568     MSG msg;
569
570     PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );  /* make sure the thread has a message queue */
571     SetEvent(lpParameter);
572
573     while (GetMessage(&msg, 0, 0, 0)) {
574         UINT message = msg.message;
575         /* for some reason XP sends a WM_USER message before WOM_OPEN */
576         ok (message == WOM_OPEN || message == WOM_DONE ||
577             message == WOM_CLOSE || message == WM_USER || message == WM_APP,
578             "GetMessage returned unexpected message: %u\n", message);
579         if (message == WOM_OPEN || message == WOM_DONE || message == WOM_CLOSE)
580             SetEvent(lpParameter);
581         else if (message == WM_APP) {
582             SetEvent(lpParameter);
583             return 0;
584         }
585     }
586
587     return 0;
588 }
589
590 static void wave_out_test_deviceOut(int device, double duration,
591                                     int headers, int loops,
592                                     LPWAVEFORMATEX pwfx, DWORD format,
593                                     DWORD flags, LPWAVEOUTCAPS pcaps,
594                                     BOOL interactive, BOOL sine, BOOL pause)
595 {
596     HWAVEOUT wout;
597     HANDLE hevent;
598     WAVEHDR *frags = 0;
599     MMRESULT rc;
600     DWORD volume;
601     WORD nChannels = pwfx->nChannels;
602     WORD wBitsPerSample = pwfx->wBitsPerSample;
603     DWORD nSamplesPerSec = pwfx->nSamplesPerSec;
604     BOOL has_volume = pcaps->dwSupport & WAVECAPS_VOLUME ? TRUE : FALSE;
605     double paused = 0.0;
606     DWORD_PTR callback = 0;
607     DWORD_PTR callback_instance = 0;
608     HANDLE thread = 0;
609     DWORD thread_id;
610     char * buffer;
611     DWORD length;
612     DWORD frag_length;
613     int i, j;
614
615     hevent=CreateEvent(NULL,FALSE,FALSE,NULL);
616     ok(hevent!=NULL,"CreateEvent(): error=%d\n",GetLastError());
617     if (hevent==NULL)
618         return;
619
620     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_EVENT) {
621         callback = (DWORD_PTR)hevent;
622         callback_instance = 0;
623     } else if ((flags & CALLBACK_TYPEMASK) == CALLBACK_FUNCTION) {
624         callback = (DWORD_PTR)callback_func;
625         callback_instance = (DWORD_PTR)hevent;
626     } else if ((flags & CALLBACK_TYPEMASK) == CALLBACK_THREAD) {
627         thread = CreateThread(NULL, 0, callback_thread, hevent, 0, &thread_id);
628         if (thread) {
629             /* make sure thread is running */
630             WaitForSingleObject(hevent,10000);
631             callback = thread_id;
632             callback_instance = 0;
633         } else {
634             trace("CreateThread() failed\n");
635             CloseHandle(hevent);
636             return;
637         }
638     } else if ((flags & CALLBACK_TYPEMASK) == CALLBACK_WINDOW) {
639         trace("CALLBACK_THREAD not implemented\n");
640         CloseHandle(hevent);
641         return;
642     } else if (flags & CALLBACK_TYPEMASK) {
643         trace("Undefined callback type!\n");
644         CloseHandle(hevent);
645         return;
646     } else {
647         trace("CALLBACK_NULL not implemented\n");
648         CloseHandle(hevent);
649         return;
650     }
651     wout=NULL;
652     rc=waveOutOpen(&wout,device,pwfx,callback,callback_instance,flags);
653     /* Note: Win9x doesn't know WAVE_FORMAT_DIRECT */
654     /* It is acceptable to fail on formats that are not specified to work */
655     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_BADDEVICEID ||
656        rc==MMSYSERR_NOTENABLED || rc==MMSYSERR_NODRIVER ||
657        rc==MMSYSERR_ALLOCATED ||
658        ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
659        (flags & WAVE_FORMAT_DIRECT) && !(pcaps->dwFormats & format)) ||
660        ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
661        (!(flags & WAVE_FORMAT_DIRECT) || (flags & WAVE_MAPPED)) &&
662        !(pcaps->dwFormats & format)) ||
663        (rc==MMSYSERR_INVALFLAG && (flags & WAVE_FORMAT_DIRECT)),
664        "waveOutOpen(%s): format=%dx%2dx%d flags=%lx(%s) rc=%s\n",
665        dev_name(device),pwfx->nSamplesPerSec,pwfx->wBitsPerSample,
666        pwfx->nChannels,CALLBACK_EVENT|flags,
667        wave_open_flags(CALLBACK_EVENT|flags),wave_out_error(rc));
668     if ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
669        (flags & WAVE_FORMAT_DIRECT) && (pcaps->dwFormats & format))
670         trace(" Reason: The device lists this format as supported in it's "
671               "capabilities but opening it failed.\n");
672     if ((rc==WAVERR_BADFORMAT || rc==MMSYSERR_NOTSUPPORTED) &&
673        !(pcaps->dwFormats & format))
674         trace("waveOutOpen(%s): format=%dx%2dx%d %s rc=%s failed but format "
675               "not supported so OK.\n", dev_name(device), pwfx->nSamplesPerSec,
676               pwfx->wBitsPerSample,pwfx->nChannels,
677               flags & WAVE_FORMAT_DIRECT ? "flags=WAVE_FORMAT_DIRECT" :
678               flags & WAVE_MAPPED ? "flags=WAVE_MAPPED" : "", mmsys_error(rc));
679     if (rc!=MMSYSERR_NOERROR)
680         goto EXIT;
681
682     WaitForSingleObject(hevent,10000);
683
684     ok(pwfx->nChannels==nChannels &&
685        pwfx->wBitsPerSample==wBitsPerSample &&
686        pwfx->nSamplesPerSec==nSamplesPerSec,
687        "got the wrong format: %dx%2dx%d instead of %dx%2dx%d\n",
688        pwfx->nSamplesPerSec, pwfx->wBitsPerSample,
689        pwfx->nChannels, nSamplesPerSec, wBitsPerSample, nChannels);
690
691     frags = HeapAlloc(GetProcessHeap(), 0, headers * sizeof(WAVEHDR));
692
693     if (sine)
694         buffer=wave_generate_la(pwfx,duration / (loops + 1),&length);
695     else
696         buffer=wave_generate_silence(pwfx,duration / (loops + 1),&length);
697
698     rc=waveOutGetVolume(wout,0);
699     ok(rc==MMSYSERR_INVALPARAM,"waveOutGetVolume(%s,0) expected "
700        "MMSYSERR_INVALPARAM, got %s\n", dev_name(device),wave_out_error(rc));
701     rc=waveOutGetVolume(wout,&volume);
702     if (rc == MMSYSERR_NOTSUPPORTED) has_volume = FALSE;
703     ok(has_volume ? rc==MMSYSERR_NOERROR : rc==MMSYSERR_NOTSUPPORTED,
704        "waveOutGetVolume(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
705
706     /* make sure fragment length is a multiple of block size */
707     frag_length = ((length / headers) / pwfx->nBlockAlign) * pwfx->nBlockAlign;
708
709     for (i = 0; i < headers; i++) {
710         frags[i].lpData=buffer + (i * frag_length);
711         if (i != (headers-1))
712             frags[i].dwBufferLength=frag_length;
713         else {
714             /* use remainder of buffer for last fragment */
715             frags[i].dwBufferLength=length - (i * frag_length);
716         }
717         frags[i].dwFlags=0;
718         frags[i].dwLoops=0;
719         rc=waveOutPrepareHeader(wout, &frags[i], sizeof(frags[0]));
720         ok(rc==MMSYSERR_NOERROR,
721            "waveOutPrepareHeader(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
722     }
723
724     if (interactive && rc==MMSYSERR_NOERROR) {
725         trace("Playing %g second %s at %5dx%2dx%d %2d header%s %d loop%s %d bytes %s %s\n",duration,
726               sine ? "440Hz tone" : "silence",pwfx->nSamplesPerSec,
727               pwfx->wBitsPerSample,pwfx->nChannels, headers, headers > 1 ? "s": " ",
728               loops, loops == 1 ? " " : "s", length * (loops + 1),
729               get_format_str(pwfx->wFormatTag),
730               wave_open_flags(flags));
731         if (sine && has_volume && volume == 0)
732             trace("*** Warning the sound is muted, you will not hear the test\n");
733
734         /* Check that the position is 0 at start */
735         check_position(device, wout, 0, pwfx);
736
737         rc=waveOutSetVolume(wout,0x20002000);
738         ok(has_volume ? rc==MMSYSERR_NOERROR : rc==MMSYSERR_NOTSUPPORTED,
739            "waveOutSetVolume(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
740
741         rc=waveOutSetVolume(wout,volume);
742         ok(has_volume ? rc==MMSYSERR_NOERROR : rc==MMSYSERR_NOTSUPPORTED,
743            "waveOutSetVolume(%s): rc=%s\n",dev_name(device),wave_out_error(rc));
744
745         rc=waveOutWrite(wout, &frags[0], sizeof(frags[0]));
746         ok(rc==MMSYSERR_NOERROR,"waveOutWrite(%s): rc=%s\n",
747            dev_name(device),wave_out_error(rc));
748
749         ok(frags[0].dwFlags==(WHDR_PREPARED|WHDR_INQUEUE),
750            "WHDR_INQUEUE WHDR_PREPARED expected, got= %s\n",
751            wave_header_flags(frags[0].dwFlags));
752
753         rc=waveOutWrite(wout, &frags[0], sizeof(frags[0]));
754         ok(rc==WAVERR_STILLPLAYING,
755            "waveOutWrite(%s): WAVE_STILLPLAYING expected, got %s\n",
756            dev_name(device),wave_out_error(rc));
757
758         ok(frags[0].dwFlags==(WHDR_PREPARED|WHDR_INQUEUE),
759            "WHDR_INQUEUE WHDR_PREPARED expected, got %s\n",
760            wave_header_flags(frags[0].dwFlags));
761
762         if (headers == 1 && loops == 0 && pause) {
763             paused = duration / 2;
764             Sleep(paused * 1000);
765             rc=waveOutPause(wout);
766             ok(rc==MMSYSERR_NOERROR,"waveOutPause(%s): rc=%s\n",
767                dev_name(device),wave_out_error(rc));
768             trace("pausing for %g seconds\n", paused);
769             Sleep(paused * 1000);
770             rc=waveOutRestart(wout);
771             ok(rc==MMSYSERR_NOERROR,"waveOutRestart(%s): rc=%s\n",
772                dev_name(device),wave_out_error(rc));
773         }
774
775         for (j = 0; j <= loops; j++) {
776             for (i = 0; i < headers; i++) {
777                 /* don't do last one */
778                 if (!((j == loops) && (i == (headers - 1)))) {
779                     if (j > 0)
780                         frags[(i+1) % headers].dwFlags = WHDR_PREPARED;
781                     rc=waveOutWrite(wout, &frags[(i+1) % headers], sizeof(frags[0]));
782                     ok(rc==MMSYSERR_NOERROR,"waveOutWrite(%s, header[%d]): rc=%s\n",
783                        dev_name(device),(i+1)%headers,wave_out_error(rc));
784                 }
785                 WaitForSingleObject(hevent,10000);
786             }
787         }
788
789         for (i = 0; i < headers; i++) {
790             ok(frags[i].dwFlags==(WHDR_DONE|WHDR_PREPARED) ||
791                broken((flags & CALLBACK_TYPEMASK)==CALLBACK_EVENT &&
792                        frags[i].dwFlags==(WHDR_DONE|WHDR_PREPARED|0x1000)), /* < NT4 */
793                "(%02d) WHDR_DONE WHDR_PREPARED expected, got %s\n",
794                i, wave_header_flags(frags[i].dwFlags));
795         }
796         check_position(device, wout, length * (loops + 1), pwfx);
797     }
798
799     for (i = 0; i < headers; i++) {
800         rc=waveOutUnprepareHeader(wout, &frags[i], sizeof(frags[0]));
801         ok(rc==MMSYSERR_NOERROR,
802            "waveOutUnprepareHeader(%s): rc=%s\n",dev_name(device),
803            wave_out_error(rc));
804     }
805     HeapFree(GetProcessHeap(), 0, buffer);
806
807     rc=waveOutClose(wout);
808     ok(rc==MMSYSERR_NOERROR,"waveOutClose(%s): rc=%s\n",dev_name(device),
809        wave_out_error(rc));
810     WaitForSingleObject(hevent,10000);
811 EXIT:
812     if ((flags & CALLBACK_TYPEMASK) == CALLBACK_THREAD) {
813         PostThreadMessage(thread_id, WM_APP, 0, 0);
814         WaitForSingleObject(hevent,10000);
815     }
816     CloseHandle(hevent);
817     HeapFree(GetProcessHeap(), 0, frags);
818 }
819
820 static void wave_out_test_device(UINT_PTR device)
821 {
822     WAVEOUTCAPSA capsA;
823     WAVEOUTCAPSW capsW;
824     WAVEFORMATEX format, oformat;
825     WAVEFORMATEXTENSIBLE wfex;
826     IMAADPCMWAVEFORMAT wfa;
827     HWAVEOUT wout;
828     MMRESULT rc;
829     UINT f;
830     WCHAR * nameW;
831     CHAR * nameA;
832     DWORD size;
833     DWORD dwPageSize;
834     BYTE * twoPages;
835     SYSTEM_INFO sSysInfo;
836     DWORD flOldProtect;
837     BOOL res;
838
839     GetSystemInfo(&sSysInfo);
840     dwPageSize = sSysInfo.dwPageSize;
841
842     rc=waveOutGetDevCapsA(device,&capsA,sizeof(capsA));
843     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_BADDEVICEID ||
844        rc==MMSYSERR_NODRIVER,
845        "waveOutGetDevCapsA(%s): failed to get capabilities: rc=%s\n",
846        dev_name(device),wave_out_error(rc));
847     if (rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER)
848         return;
849
850     rc=waveOutGetDevCapsW(device,&capsW,sizeof(capsW));
851     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_NOTSUPPORTED,
852        "waveOutGetDevCapsW(%s): MMSYSERR_NOERROR or MMSYSERR_NOTSUPPORTED "
853        "expected, got %s\n",dev_name(device),wave_out_error(rc));
854
855     rc=waveOutGetDevCapsA(device,0,sizeof(capsA));
856     ok(rc==MMSYSERR_INVALPARAM,
857        "waveOutGetDevCapsA(%s): MMSYSERR_INVALPARAM expected, "
858        "got %s\n",dev_name(device),wave_out_error(rc));
859
860     rc=waveOutGetDevCapsW(device,0,sizeof(capsW));
861     ok(rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
862        "waveOutGetDevCapsW(%s): MMSYSERR_INVALPARAM or MMSYSERR_NOTSUPPORTED "
863        "expected, got %s\n",dev_name(device),wave_out_error(rc));
864
865     if (0)
866     {
867     /* FIXME: this works on windows but crashes wine */
868     rc=waveOutGetDevCapsA(device,(LPWAVEOUTCAPSA)1,sizeof(capsA));
869     ok(rc==MMSYSERR_INVALPARAM,
870        "waveOutGetDevCapsA(%s): MMSYSERR_INVALPARAM expected, got %s\n",
871        dev_name(device),wave_out_error(rc));
872
873     rc=waveOutGetDevCapsW(device,(LPWAVEOUTCAPSW)1,sizeof(capsW));
874     ok(rc==MMSYSERR_INVALPARAM || rc==MMSYSERR_NOTSUPPORTED,
875        "waveOutGetDevCapsW(%s): MMSYSERR_INVALPARAM or MMSYSERR_NOTSUPPORTED "
876        "expected, got %s\n",dev_name(device),wave_out_error(rc));
877     }
878
879     rc=waveOutGetDevCapsA(device,&capsA,4);
880     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_INVALPARAM,
881        "waveOutGetDevCapsA(%s): MMSYSERR_NOERROR or MMSYSERR_INVALPARAM "
882        "expected, got %s\n", dev_name(device),wave_out_error(rc));
883
884     rc=waveOutGetDevCapsW(device,&capsW,4);
885     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_NOTSUPPORTED ||
886        rc==MMSYSERR_INVALPARAM, /* Vista, W2K8 */
887        "waveOutGetDevCapsW(%s): unexpected return value %s\n",
888        dev_name(device),wave_out_error(rc));
889
890     nameA=NULL;
891     rc=waveOutMessage((HWAVEOUT)device, DRV_QUERYDEVICEINTERFACESIZE,
892                       (DWORD_PTR)&size, 0);
893     ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_INVALPARAM ||
894        rc==MMSYSERR_NOTSUPPORTED,
895        "waveOutMessage(%s): failed to get interface size, rc=%s\n",
896        dev_name(device),wave_out_error(rc));
897     if (rc==MMSYSERR_NOERROR) {
898         nameW = HeapAlloc(GetProcessHeap(), 0, size);
899         rc=waveOutMessage((HWAVEOUT)device, DRV_QUERYDEVICEINTERFACE,
900                           (DWORD_PTR)nameW, size);
901         ok(rc==MMSYSERR_NOERROR,"waveOutMessage(%s): failed to get interface "
902            "name, rc=%s\n",dev_name(device),wave_out_error(rc));
903         ok(lstrlenW(nameW)+1==size/sizeof(WCHAR),"got an incorrect size %d\n",size);
904         if (rc==MMSYSERR_NOERROR) {
905             nameA = HeapAlloc(GetProcessHeap(), 0, size/sizeof(WCHAR));
906             WideCharToMultiByte(CP_ACP, 0, nameW, size/sizeof(WCHAR), nameA,
907                                 size/sizeof(WCHAR), NULL, NULL);
908         }
909         HeapFree(GetProcessHeap(), 0, nameW);
910     }
911     else if (rc==MMSYSERR_NOTSUPPORTED) {
912         nameA=HeapAlloc(GetProcessHeap(), 0, sizeof("not supported"));
913         strcpy(nameA, "not supported");
914     }
915
916     rc=waveOutGetDevCapsA(device,&capsA,sizeof(capsA));
917     ok(rc==MMSYSERR_NOERROR,
918        "waveOutGetDevCapsA(%s): MMSYSERR_NOERROR expected, got %s\n",
919        dev_name(device),wave_out_error(rc));
920     if (rc!=MMSYSERR_NOERROR)
921         return;
922
923     trace("  %s: \"%s\" (%s) %d.%d (%d:%d)\n",dev_name(device),capsA.szPname,
924           (nameA?nameA:"failed"),capsA.vDriverVersion >> 8,
925           capsA.vDriverVersion & 0xff, capsA.wMid,capsA.wPid);
926     trace("     channels=%d formats=%05x support=%04x\n",
927           capsA.wChannels,capsA.dwFormats,capsA.dwSupport);
928     trace("     %s\n",wave_out_caps(capsA.dwSupport));
929     HeapFree(GetProcessHeap(), 0, nameA);
930
931     if (winetest_interactive && (device != WAVE_MAPPER))
932     {
933         trace("Playing a 5 seconds reference tone.\n");
934         trace("All subsequent tones should be identical to this one.\n");
935         trace("Listen for stutter, changes in pitch, volume, etc.\n");
936         format.wFormatTag=WAVE_FORMAT_PCM;
937         format.nChannels=1;
938         format.wBitsPerSample=8;
939         format.nSamplesPerSec=22050;
940         format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
941         format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
942         format.cbSize=0;
943
944         wave_out_test_deviceOut(device,5.0,1,0,&format,WAVE_FORMAT_2M08,
945                                 CALLBACK_EVENT,&capsA,TRUE,TRUE,FALSE);
946         wave_out_test_deviceOut(device,5.0,1,0,&format,WAVE_FORMAT_2M08,
947                                 CALLBACK_FUNCTION,&capsA,TRUE,TRUE,FALSE);
948         wave_out_test_deviceOut(device,5.0,1,0,&format,WAVE_FORMAT_2M08,
949                                 CALLBACK_THREAD,&capsA,TRUE,TRUE,FALSE);
950
951         wave_out_test_deviceOut(device,5.0,10,0,&format,WAVE_FORMAT_2M08,
952                                 CALLBACK_EVENT,&capsA,TRUE,TRUE,FALSE);
953         wave_out_test_deviceOut(device,5.0,5,1,&format,WAVE_FORMAT_2M08,
954                                 CALLBACK_EVENT,&capsA,TRUE,TRUE,FALSE);
955     } else {
956         format.wFormatTag=WAVE_FORMAT_PCM;
957         format.nChannels=1;
958         format.wBitsPerSample=8;
959         format.nSamplesPerSec=22050;
960         format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
961         format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
962         format.cbSize=0;
963         wave_out_test_deviceOut(device,1.0,1,0,&format,WAVE_FORMAT_2M08,
964                                 CALLBACK_EVENT,&capsA,TRUE,FALSE,FALSE);
965         wave_out_test_deviceOut(device,1.0,1,0,&format,WAVE_FORMAT_2M08,
966                                 CALLBACK_EVENT,&capsA,TRUE,FALSE,TRUE);
967         wave_out_test_deviceOut(device,1.0,1,0,&format,WAVE_FORMAT_2M08,
968                                 CALLBACK_FUNCTION,&capsA,TRUE,FALSE,FALSE);
969         wave_out_test_deviceOut(device,1.0,1,0,&format,WAVE_FORMAT_2M08,
970                                 CALLBACK_FUNCTION,&capsA,TRUE,FALSE,TRUE);
971         wave_out_test_deviceOut(device,1.0,1,0,&format,WAVE_FORMAT_2M08,
972                                 CALLBACK_THREAD,&capsA,TRUE,FALSE,FALSE);
973         wave_out_test_deviceOut(device,1.0,1,0,&format,WAVE_FORMAT_2M08,
974                                 CALLBACK_THREAD,&capsA,TRUE,FALSE,TRUE);
975
976         wave_out_test_deviceOut(device,1.0,10,0,&format,WAVE_FORMAT_2M08,
977                                 CALLBACK_EVENT,&capsA,TRUE,FALSE,FALSE);
978         wave_out_test_deviceOut(device,1.0,5,1,&format,WAVE_FORMAT_2M08,
979                                 CALLBACK_EVENT,&capsA,TRUE,FALSE,FALSE);
980     }
981
982     for (f=0;f<NB_WIN_FORMATS;f++) {
983         format.wFormatTag=WAVE_FORMAT_PCM;
984         format.nChannels=win_formats[f][3];
985         format.wBitsPerSample=win_formats[f][2];
986         format.nSamplesPerSec=win_formats[f][1];
987         format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
988         format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
989         format.cbSize=0;
990         wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
991                                 CALLBACK_EVENT,&capsA,winetest_interactive,
992                                 TRUE,FALSE);
993         wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
994                                 CALLBACK_FUNCTION,&capsA,winetest_interactive,
995                                 TRUE,FALSE);
996         wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
997                                 CALLBACK_THREAD,&capsA,winetest_interactive,
998                                 TRUE,FALSE);
999
1000         wave_out_test_deviceOut(device,1.0,10,0,&format,win_formats[f][0],
1001                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1002                                 TRUE,FALSE);
1003         wave_out_test_deviceOut(device,1.0,5,1,&format,win_formats[f][0],
1004                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1005                                 TRUE,FALSE);
1006
1007         if (winetest_interactive) {
1008             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1009                                     CALLBACK_EVENT,&capsA,winetest_interactive,
1010                                     TRUE,TRUE);
1011             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1012                                     CALLBACK_FUNCTION,&capsA,winetest_interactive,
1013                                     TRUE,TRUE);
1014             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1015                                     CALLBACK_THREAD,&capsA,winetest_interactive,
1016                                     TRUE,TRUE);
1017
1018             wave_out_test_deviceOut(device,1.0,10,0,&format,win_formats[f][0],
1019                                     CALLBACK_EVENT,&capsA,winetest_interactive,
1020                                     TRUE,TRUE);
1021             wave_out_test_deviceOut(device,1.0,5,1,&format,win_formats[f][0],
1022                                     CALLBACK_EVENT,&capsA,winetest_interactive,
1023                                     TRUE,TRUE);
1024         }
1025         if (device != WAVE_MAPPER)
1026         {
1027             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1028                                     CALLBACK_EVENT|WAVE_FORMAT_DIRECT,&capsA,
1029                                     winetest_interactive,TRUE,FALSE);
1030             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1031                                     CALLBACK_EVENT|WAVE_MAPPED,&capsA,
1032                                     winetest_interactive,TRUE,FALSE);
1033             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1034                                     CALLBACK_FUNCTION|WAVE_FORMAT_DIRECT,&capsA,
1035                                     winetest_interactive,TRUE,FALSE);
1036             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1037                                     CALLBACK_FUNCTION|WAVE_MAPPED,&capsA,
1038                                     winetest_interactive,TRUE,FALSE);
1039             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1040                                     CALLBACK_THREAD|WAVE_FORMAT_DIRECT,&capsA,
1041                                     winetest_interactive,TRUE,FALSE);
1042             wave_out_test_deviceOut(device,1.0,1,0,&format,win_formats[f][0],
1043                                     CALLBACK_THREAD|WAVE_MAPPED,&capsA,
1044                                     winetest_interactive,TRUE,FALSE);
1045
1046             wave_out_test_deviceOut(device,1.0,10,0,&format,win_formats[f][0],
1047                                     CALLBACK_EVENT|WAVE_FORMAT_DIRECT,&capsA,
1048                                     winetest_interactive,TRUE,FALSE);
1049             wave_out_test_deviceOut(device,1.0,5,1,&format,win_formats[f][0],
1050                                     CALLBACK_EVENT|WAVE_FORMAT_DIRECT,&capsA,
1051                                     winetest_interactive,TRUE,FALSE);
1052         }
1053     }
1054
1055     /* Try a PCMWAVEFORMAT aligned next to an unaccessible page for bounds
1056      * checking */
1057     twoPages = VirtualAlloc(NULL, 2 * dwPageSize, MEM_RESERVE | MEM_COMMIT,
1058                             PAGE_READWRITE);
1059     ok(twoPages!=NULL,"Failed to allocate 2 pages of memory\n");
1060     if (twoPages) {
1061         res = VirtualProtect(twoPages + dwPageSize, dwPageSize, PAGE_NOACCESS,
1062                              &flOldProtect);
1063         ok(res, "Failed to set memory access on second page\n");
1064         if (res) {
1065             LPWAVEFORMATEX pwfx = (LPWAVEFORMATEX)(twoPages + dwPageSize -
1066                 sizeof(PCMWAVEFORMAT));
1067             pwfx->wFormatTag=WAVE_FORMAT_PCM;
1068             pwfx->nChannels=1;
1069             pwfx->wBitsPerSample=8;
1070             pwfx->nSamplesPerSec=22050;
1071             pwfx->nBlockAlign=pwfx->nChannels*pwfx->wBitsPerSample/8;
1072             pwfx->nAvgBytesPerSec=pwfx->nSamplesPerSec*pwfx->nBlockAlign;
1073             wave_out_test_deviceOut(device,1.0,1,0,pwfx,WAVE_FORMAT_2M08,
1074                                     CALLBACK_EVENT,&capsA,winetest_interactive,
1075                                     TRUE,FALSE);
1076             wave_out_test_deviceOut(device,1.0,10,0,pwfx,WAVE_FORMAT_2M08,
1077                                     CALLBACK_EVENT,&capsA,winetest_interactive,
1078                                     TRUE,FALSE);
1079             wave_out_test_deviceOut(device,1.0,5,1,pwfx,WAVE_FORMAT_2M08,
1080                                     CALLBACK_EVENT,&capsA,winetest_interactive,
1081                                     TRUE,FALSE);
1082             if (device != WAVE_MAPPER)
1083             {
1084                 wave_out_test_deviceOut(device,1.0,1,0,pwfx,WAVE_FORMAT_2M08,
1085                                         CALLBACK_EVENT|WAVE_FORMAT_DIRECT,
1086                                         &capsA,winetest_interactive,TRUE,FALSE);
1087                 wave_out_test_deviceOut(device,1.0,1,0,pwfx,WAVE_FORMAT_2M08,
1088                                         CALLBACK_EVENT|WAVE_MAPPED,&capsA,
1089                                         winetest_interactive,TRUE,FALSE);
1090                 wave_out_test_deviceOut(device,1.0,10,0,pwfx,WAVE_FORMAT_2M08,
1091                                         CALLBACK_EVENT|WAVE_FORMAT_DIRECT,
1092                                         &capsA,winetest_interactive,TRUE,FALSE);
1093                 wave_out_test_deviceOut(device,1.0,10,0,pwfx,WAVE_FORMAT_2M08,
1094                                         CALLBACK_EVENT|WAVE_MAPPED,&capsA,
1095                                         winetest_interactive,TRUE,FALSE);
1096                 wave_out_test_deviceOut(device,1.0,5,1,pwfx,WAVE_FORMAT_2M08,
1097                                         CALLBACK_EVENT|WAVE_FORMAT_DIRECT,
1098                                         &capsA,winetest_interactive,TRUE,FALSE);
1099                 wave_out_test_deviceOut(device,1.0,5,1,pwfx,WAVE_FORMAT_2M08,
1100                                         CALLBACK_EVENT|WAVE_MAPPED,&capsA,
1101                                         winetest_interactive,TRUE,FALSE);
1102             }
1103         }
1104         VirtualFree(twoPages, 0, MEM_RELEASE);
1105     }
1106
1107     /* Testing invalid format: 11 bits per sample */
1108     format.wFormatTag=WAVE_FORMAT_PCM;
1109     format.nChannels=2;
1110     format.wBitsPerSample=11;
1111     format.nSamplesPerSec=22050;
1112     format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
1113     format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
1114     format.cbSize=0;
1115     oformat=format;
1116     rc=waveOutOpen(&wout,device,&format,0,0,CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1117     ok(rc==WAVERR_BADFORMAT || rc==MMSYSERR_INVALFLAG ||
1118        rc==MMSYSERR_INVALPARAM,
1119        "waveOutOpen(%s): opening the device in 11 bits mode should fail: "
1120        "rc=%s\n",dev_name(device),wave_out_error(rc));
1121     if (rc==MMSYSERR_NOERROR) {
1122         trace("     got %dx%2dx%d for %dx%2dx%d\n",
1123               format.nSamplesPerSec, format.wBitsPerSample,
1124               format.nChannels,
1125               oformat.nSamplesPerSec, oformat.wBitsPerSample,
1126               oformat.nChannels);
1127         waveOutClose(wout);
1128     }
1129
1130     /* Testing invalid format: 2 MHz sample rate */
1131     format.wFormatTag=WAVE_FORMAT_PCM;
1132     format.nChannels=2;
1133     format.wBitsPerSample=16;
1134     format.nSamplesPerSec=2000000;
1135     format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
1136     format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
1137     format.cbSize=0;
1138     oformat=format;
1139     rc=waveOutOpen(&wout,device,&format,0,0,CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1140     ok(rc==WAVERR_BADFORMAT || rc==MMSYSERR_INVALFLAG ||
1141        rc==MMSYSERR_INVALPARAM,
1142        "waveOutOpen(%s): opening the device at 2 MHz sample rate should fail: "
1143        "rc=%s\n",dev_name(device),wave_out_error(rc));
1144     if (rc==MMSYSERR_NOERROR) {
1145         trace("     got %dx%2dx%d for %dx%2dx%d\n",
1146               format.nSamplesPerSec, format.wBitsPerSample,
1147               format.nChannels,
1148               oformat.nSamplesPerSec, oformat.wBitsPerSample,
1149               oformat.nChannels);
1150         waveOutClose(wout);
1151     }
1152
1153     /* try some non PCM formats */
1154     format.wFormatTag=WAVE_FORMAT_MULAW;
1155     format.nChannels=1;
1156     format.wBitsPerSample=8;
1157     format.nSamplesPerSec=8000;
1158     format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
1159     format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
1160     format.cbSize=0;
1161     rc=waveOutOpen(&wout,device,&format,0,0,CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1162     ok(rc==MMSYSERR_NOERROR ||rc==WAVERR_BADFORMAT ||
1163        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1164        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1165     if (rc==MMSYSERR_NOERROR) {
1166         waveOutClose(wout);
1167         wave_out_test_deviceOut(device,1.0,1,0,&format,0,CALLBACK_EVENT,
1168                                 &capsA,winetest_interactive,TRUE,FALSE);
1169         wave_out_test_deviceOut(device,1.0,10,0,&format,0,CALLBACK_EVENT,
1170                                 &capsA,winetest_interactive,TRUE,FALSE);
1171         wave_out_test_deviceOut(device,1.0,5,1,&format,0,CALLBACK_EVENT,
1172                                 &capsA,winetest_interactive,TRUE,FALSE);
1173     } else
1174         trace("waveOutOpen(%s): WAVE_FORMAT_MULAW not supported\n",
1175               dev_name(device));
1176
1177     wfa.wfx.wFormatTag=WAVE_FORMAT_IMA_ADPCM;
1178     wfa.wfx.nChannels=1;
1179     wfa.wfx.nSamplesPerSec=11025;
1180     wfa.wfx.nAvgBytesPerSec=5588;
1181     wfa.wfx.nBlockAlign=256;
1182     wfa.wfx.wBitsPerSample=4; /* see imaadp32.c */
1183     wfa.wfx.cbSize=2;
1184     wfa.wSamplesPerBlock=505;
1185     rc=waveOutOpen(&wout,device,&wfa.wfx,0,0,CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1186     ok(rc==MMSYSERR_NOERROR ||rc==WAVERR_BADFORMAT ||
1187        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1188        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1189     if (rc==MMSYSERR_NOERROR) {
1190         waveOutClose(wout);
1191         /* TODO: teach wave_generate_* ADPCM
1192         wave_out_test_deviceOut(device,1.0,1,0,&wfa.wfx,0,CALLBACK_EVENT,
1193                                 &capsA,winetest_interactive,TRUE,FALSE);
1194         wave_out_test_deviceOut(device,1.0,10,0,&wfa.wfx,0,CALLBACK_EVENT,
1195                                 &capsA,winetest_interactive,TRUE,FALSE);
1196         wave_out_test_deviceOut(device,1.0,5,1,&wfa.wfx,0,CALLBACK_EVENT,
1197                                 &capsA,winetest_interactive,TRUE,FALSE);
1198         */
1199     } else
1200         trace("waveOutOpen(%s): WAVE_FORMAT_IMA_ADPCM not supported\n",
1201               dev_name(device));
1202
1203     /* test if WAVEFORMATEXTENSIBLE supported */
1204     wfex.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;
1205     wfex.Format.nChannels=2;
1206     wfex.Format.wBitsPerSample=16;
1207     wfex.Format.nSamplesPerSec=22050;
1208     wfex.Format.nBlockAlign=wfex.Format.nChannels*wfex.Format.wBitsPerSample/8;
1209     wfex.Format.nAvgBytesPerSec=wfex.Format.nSamplesPerSec*
1210         wfex.Format.nBlockAlign;
1211     wfex.Format.cbSize=22;
1212     wfex.Samples.wValidBitsPerSample=wfex.Format.wBitsPerSample;
1213     wfex.dwChannelMask=SPEAKER_ALL;
1214     wfex.SubFormat=KSDATAFORMAT_SUBTYPE_PCM;
1215     rc=waveOutOpen(&wout,device,&wfex.Format,0,0,
1216                    CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1217     ok(rc==MMSYSERR_NOERROR || rc==WAVERR_BADFORMAT ||
1218        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1219        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1220     if (rc==MMSYSERR_NOERROR) {
1221         waveOutClose(wout);
1222         wave_out_test_deviceOut(device,1.0,1,0,&wfex.Format,WAVE_FORMAT_2M16,
1223                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1224                                 TRUE,FALSE);
1225         wave_out_test_deviceOut(device,1.0,10,0,&wfex.Format,WAVE_FORMAT_2M16,
1226                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1227                                 TRUE,FALSE);
1228         wave_out_test_deviceOut(device,1.0,5,1,&wfex.Format,WAVE_FORMAT_2M16,
1229                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1230                                 TRUE,FALSE);
1231     } else
1232         trace("waveOutOpen(%s): WAVE_FORMAT_EXTENSIBLE not supported\n",
1233               dev_name(device));
1234
1235     /* test if 4 channels supported */
1236     wfex.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;
1237     wfex.Format.nChannels=4;
1238     wfex.Format.wBitsPerSample=16;
1239     wfex.Format.nSamplesPerSec=22050;
1240     wfex.Format.nBlockAlign=wfex.Format.nChannels*wfex.Format.wBitsPerSample/8;
1241     wfex.Format.nAvgBytesPerSec=wfex.Format.nSamplesPerSec*
1242         wfex.Format.nBlockAlign;
1243     wfex.Format.cbSize=22;
1244     wfex.Samples.wValidBitsPerSample=wfex.Format.wBitsPerSample;
1245     wfex.dwChannelMask=SPEAKER_ALL;
1246     wfex.SubFormat=KSDATAFORMAT_SUBTYPE_PCM;
1247     rc=waveOutOpen(&wout,device,&wfex.Format,0,0,
1248                    CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1249     ok(rc==MMSYSERR_NOERROR || rc==WAVERR_BADFORMAT ||
1250        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1251        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1252     if (rc==MMSYSERR_NOERROR) {
1253         waveOutClose(wout);
1254         wave_out_test_deviceOut(device,1.0,1,0,&wfex.Format,0,CALLBACK_EVENT,
1255                                 &capsA,winetest_interactive,TRUE,FALSE);
1256         wave_out_test_deviceOut(device,1.0,10,0,&wfex.Format,0,CALLBACK_EVENT,
1257                                 &capsA,winetest_interactive,TRUE,FALSE);
1258         wave_out_test_deviceOut(device,1.0,5,1,&wfex.Format,0,CALLBACK_EVENT,
1259                                 &capsA,winetest_interactive,TRUE,FALSE);
1260     } else
1261         trace("waveOutOpen(%s): 4 channels not supported\n",
1262               dev_name(device));
1263
1264     /* test if 6 channels supported */
1265     wfex.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;
1266     wfex.Format.nChannels=6;
1267     wfex.Format.wBitsPerSample=16;
1268     wfex.Format.nSamplesPerSec=22050;
1269     wfex.Format.nBlockAlign=wfex.Format.nChannels*wfex.Format.wBitsPerSample/8;
1270     wfex.Format.nAvgBytesPerSec=wfex.Format.nSamplesPerSec*
1271         wfex.Format.nBlockAlign;
1272     wfex.Format.cbSize=22;
1273     wfex.Samples.wValidBitsPerSample=wfex.Format.wBitsPerSample;
1274     wfex.dwChannelMask=SPEAKER_ALL;
1275     wfex.SubFormat=KSDATAFORMAT_SUBTYPE_PCM;
1276     rc=waveOutOpen(&wout,device,&wfex.Format,0,0,
1277                    CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1278     ok(rc==MMSYSERR_NOERROR || rc==WAVERR_BADFORMAT ||
1279        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1280        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1281     if (rc==MMSYSERR_NOERROR) {
1282         waveOutClose(wout);
1283         wave_out_test_deviceOut(device,1.0,1,0,&wfex.Format,WAVE_FORMAT_2M16,
1284                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1285                                 TRUE,FALSE);
1286         wave_out_test_deviceOut(device,1.0,10,0,&wfex.Format,WAVE_FORMAT_2M16,
1287                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1288                                 TRUE,FALSE);
1289         wave_out_test_deviceOut(device,1.0,5,1,&wfex.Format,WAVE_FORMAT_2M16,
1290                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1291                                 TRUE,FALSE);
1292     } else
1293         trace("waveOutOpen(%s): 6 channels not supported\n",
1294               dev_name(device));
1295
1296     if (0)
1297     {
1298     /* FIXME: ALSA doesn't like this format */
1299     /* test if 24 bit samples supported */
1300     wfex.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;
1301     wfex.Format.nChannels=2;
1302     wfex.Format.wBitsPerSample=24;
1303     wfex.Format.nSamplesPerSec=22050;
1304     wfex.Format.nBlockAlign=wfex.Format.nChannels*wfex.Format.wBitsPerSample/8;
1305     wfex.Format.nAvgBytesPerSec=wfex.Format.nSamplesPerSec*
1306         wfex.Format.nBlockAlign;
1307     wfex.Format.cbSize=22;
1308     wfex.Samples.wValidBitsPerSample=wfex.Format.wBitsPerSample;
1309     wfex.dwChannelMask=SPEAKER_ALL;
1310     wfex.SubFormat=KSDATAFORMAT_SUBTYPE_PCM;
1311     rc=waveOutOpen(&wout,device,&wfex.Format,0,0,
1312                    CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1313     ok(rc==MMSYSERR_NOERROR || rc==WAVERR_BADFORMAT ||
1314        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1315        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1316     if (rc==MMSYSERR_NOERROR) {
1317         waveOutClose(wout);
1318         wave_out_test_deviceOut(device,1.0,1,0,&wfex.Format,WAVE_FORMAT_2M16,
1319                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1320                                 TRUE,FALSE);
1321     } else
1322         trace("waveOutOpen(%s): 24 bit samples not supported\n",
1323               dev_name(device));
1324     }
1325
1326     /* test if 32 bit samples supported */
1327     wfex.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;
1328     wfex.Format.nChannels=2;
1329     wfex.Format.wBitsPerSample=32;
1330     wfex.Format.nSamplesPerSec=22050;
1331     wfex.Format.nBlockAlign=wfex.Format.nChannels*wfex.Format.wBitsPerSample/8;
1332     wfex.Format.nAvgBytesPerSec=wfex.Format.nSamplesPerSec*
1333         wfex.Format.nBlockAlign;
1334     wfex.Format.cbSize=22;
1335     wfex.Samples.wValidBitsPerSample=wfex.Format.wBitsPerSample;
1336     wfex.dwChannelMask=SPEAKER_ALL;
1337     wfex.SubFormat=KSDATAFORMAT_SUBTYPE_PCM;
1338     rc=waveOutOpen(&wout,device,&wfex.Format,0,0,
1339                    CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1340     ok(rc==MMSYSERR_NOERROR || rc==WAVERR_BADFORMAT ||
1341        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1342        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1343     if (rc==MMSYSERR_NOERROR) {
1344         waveOutClose(wout);
1345         wave_out_test_deviceOut(device,1.0,1,0,&wfex.Format,WAVE_FORMAT_2M16,
1346                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1347                                 TRUE,FALSE);
1348         wave_out_test_deviceOut(device,1.0,10,0,&wfex.Format,WAVE_FORMAT_2M16,
1349                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1350                                 TRUE,FALSE);
1351         wave_out_test_deviceOut(device,1.0,5,1,&wfex.Format,WAVE_FORMAT_2M16,
1352                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1353                                 TRUE,FALSE);
1354     } else
1355         trace("waveOutOpen(%s): 32 bit samples not supported\n",
1356               dev_name(device));
1357
1358     /* test if 32 bit float samples supported */
1359     wfex.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;
1360     wfex.Format.nChannels=2;
1361     wfex.Format.wBitsPerSample=32;
1362     wfex.Format.nSamplesPerSec=22050;
1363     wfex.Format.nBlockAlign=wfex.Format.nChannels*wfex.Format.wBitsPerSample/8;
1364     wfex.Format.nAvgBytesPerSec=wfex.Format.nSamplesPerSec*
1365         wfex.Format.nBlockAlign;
1366     wfex.Format.cbSize=22;
1367     wfex.Samples.wValidBitsPerSample=wfex.Format.wBitsPerSample;
1368     wfex.dwChannelMask=SPEAKER_ALL;
1369     wfex.SubFormat=KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
1370     rc=waveOutOpen(&wout,device,&wfex.Format,0,0,
1371                    CALLBACK_NULL|WAVE_FORMAT_DIRECT);
1372     ok(rc==MMSYSERR_NOERROR || rc==WAVERR_BADFORMAT ||
1373        rc==MMSYSERR_INVALFLAG || rc==MMSYSERR_INVALPARAM,
1374        "waveOutOpen(%s): returned %s\n",dev_name(device),wave_out_error(rc));
1375     if (rc==MMSYSERR_NOERROR) {
1376         waveOutClose(wout);
1377         wave_out_test_deviceOut(device,1.0,1,0,&wfex.Format,WAVE_FORMAT_2M16,
1378                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1379                                 TRUE,FALSE);
1380         wave_out_test_deviceOut(device,1.0,10,0,&wfex.Format,WAVE_FORMAT_2M16,
1381                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1382                                 TRUE,FALSE);
1383         wave_out_test_deviceOut(device,1.0,5,1,&wfex.Format,WAVE_FORMAT_2M16,
1384                                 CALLBACK_EVENT,&capsA,winetest_interactive,
1385                                 TRUE,FALSE);
1386     } else
1387         trace("waveOutOpen(%s): 32 bit float samples not supported\n",
1388               dev_name(device));
1389 }
1390
1391 static void wave_out_tests(void)
1392 {
1393     WAVEOUTCAPSA capsA;
1394     WAVEOUTCAPSW capsW;
1395     WAVEFORMATEX format;
1396     HWAVEOUT wout;
1397     MMRESULT rc;
1398     UINT ndev,d;
1399
1400     ndev=waveOutGetNumDevs();
1401     trace("found %d WaveOut devices\n",ndev);
1402
1403     rc=waveOutGetDevCapsA(ndev+1,&capsA,sizeof(capsA));
1404     ok(rc==MMSYSERR_BADDEVICEID,
1405        "waveOutGetDevCapsA(%s): MMSYSERR_BADDEVICEID expected, got %s\n",
1406        dev_name(ndev+1),mmsys_error(rc));
1407
1408     rc=waveOutGetDevCapsW(ndev+1,&capsW,sizeof(capsW));
1409     ok(rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NOTSUPPORTED,
1410        "waveOutGetDevCapsW(%s): MMSYSERR_BADDEVICEID or MMSYSERR_NOTSUPPORTED "
1411        "expected, got %s\n",dev_name(ndev+1),mmsys_error(rc));
1412
1413     rc=waveOutGetDevCapsA(WAVE_MAPPER,&capsA,sizeof(capsA));
1414     if (ndev>0)
1415         ok(rc==MMSYSERR_NOERROR,
1416            "waveOutGetDevCapsA(%s): MMSYSERR_NOERROR expected, got %s\n",
1417            dev_name(WAVE_MAPPER),mmsys_error(rc));
1418     else
1419         ok(rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER,
1420            "waveOutGetDevCapsA(%s): MMSYSERR_BADDEVICEID or MMSYSERR_NODRIVER "
1421            "expected, got %s\n",dev_name(WAVE_MAPPER),mmsys_error(rc));
1422
1423     rc=waveOutGetDevCapsW(WAVE_MAPPER,&capsW,sizeof(capsW));
1424     if (ndev>0)
1425         ok(rc==MMSYSERR_NOERROR || rc==MMSYSERR_NOTSUPPORTED,
1426            "waveOutGetDevCapsW(%s): MMSYSERR_NOERROR or MMSYSERR_NOTSUPPORTED "
1427            "expected, got %s\n",dev_name(WAVE_MAPPER),mmsys_error(rc));
1428     else
1429         ok(rc==MMSYSERR_BADDEVICEID || rc==MMSYSERR_NODRIVER ||
1430            rc==MMSYSERR_NOTSUPPORTED,
1431            "waveOutGetDevCapsW(%s): MMSYSERR_BADDEVICEID or MMSYSERR_NODRIVER "
1432            " or MMSYSERR_NOTSUPPORTED expected, got %s\n",
1433            dev_name(WAVE_MAPPER),mmsys_error(rc));
1434
1435     format.wFormatTag=WAVE_FORMAT_PCM;
1436     format.nChannels=2;
1437     format.wBitsPerSample=16;
1438     format.nSamplesPerSec=44100;
1439     format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
1440     format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
1441     format.cbSize=0;
1442     rc=waveOutOpen(&wout,ndev+1,&format,0,0,CALLBACK_NULL);
1443     ok(rc==MMSYSERR_BADDEVICEID,
1444        "waveOutOpen(%s): MMSYSERR_BADDEVICEID expected, got %s\n",
1445        dev_name(ndev+1),mmsys_error(rc));
1446
1447     for (d=0;d<ndev;d++)
1448         wave_out_test_device(d);
1449
1450     if (ndev>0)
1451         wave_out_test_device(WAVE_MAPPER);
1452 }
1453
1454 START_TEST(wave)
1455 {
1456     test_multiple_waveopens();
1457     wave_out_tests();
1458 }