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