winmm/tests: Set a struct member to 0 is obsolete after adding a memset.
[wine/multimedia.git] / dlls / winmm / tests / midi.c
blob455bcd02db384cbc68a838ef1a48199f2321558c
1 /*
2 * Test winmm midi
4 * Copyright 2010 Jörg Höhle
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stddef.h>
23 #include "windows.h"
24 #include "mmsystem.h"
25 #include "wine/test.h"
27 extern const char* mmsys_error(MMRESULT error); /* from wave.c */
29 /* Test in order of increasing probability to hang.
30 * On many UNIX systems, the Timidity softsynth provides
31 * MIDI sequencer services and it is not particularly robust.
34 #define MYCBINST 0x4CAFE5A8 /* not used with window or thread callbacks */
35 #define WHATEVER 0xFEEDF00D
37 static BOOL spurious_message(LPMSG msg)
39 /* WM_DEVICECHANGE 0x0219 appears randomly */
40 if(msg->message == WM_DEVICECHANGE) {
41 trace("skipping spurious message %04x\n", msg->message);
42 return TRUE;
44 return FALSE;
47 static UINT cbmsg = 0;
48 static DWORD_PTR cbval1 = WHATEVER;
49 static DWORD_PTR cbval2 = 0;
50 static DWORD_PTR cbinst = MYCBINST;
52 static void CALLBACK callback_func(HWAVEOUT hwo, UINT uMsg,
53 DWORD_PTR dwInstance,
54 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
56 if (winetest_debug>1)
57 trace("Callback! msg=%x %lx %lx\n", uMsg, dwParam1, dwParam2);
58 cbmsg = uMsg;
59 cbval1 = dwParam1; /* mhdr or 0 */
60 cbval2 = dwParam2; /* always 0 */
61 cbinst = dwInstance; /* MYCBINST, see midiOut/StreamOpen */
64 #define test_notification(hwnd, command, m1, p2) test_notification_dbg(hwnd, command, m1, p2, __LINE__)
65 static void test_notification_dbg(HWND hwnd, const char* command, UINT m1, DWORD_PTR p2, int line)
66 { /* Use message type 0 as meaning no message */
67 MSG msg;
68 if (hwnd) {
69 /* msg.wParam is hmidiout, msg.lParam is the mhdr (or 0) */
70 BOOL seen;
71 do { seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE); }
72 while(seen && spurious_message(&msg));
73 if (m1 && !seen) {
74 /* We observe transient delayed notification, mostly on native.
75 * Perhaps the OS preempts the player thread after setting MHDR_DONE
76 * or clearing MHDR_INQUEUE, before calling DriverCallback. */
77 DWORD rc;
78 trace_(__FILE__,line)("Waiting for delayed message %x from %s\n", m1, command);
79 SetLastError(0xDEADBEEF);
80 rc = MsgWaitForMultipleObjects(0, NULL, FALSE, 3000, QS_POSTMESSAGE);
81 ok_(__FILE__,line)(rc==WAIT_OBJECT_0, "Wait failed: %04x %d\n", rc, GetLastError());
82 seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE);
84 if (seen) {
85 trace_(__FILE__,line)("Message %x, wParam=%lx, lParam=%lx from %s\n",
86 msg.message, msg.wParam, msg.lParam, command);
87 ok_(__FILE__,line)(msg.hwnd==hwnd, "Didn't get the handle to our test window\n");
88 ok_(__FILE__,line)(msg.message==m1 && msg.lParam==p2, "bad message %x/%lx from %s, expect %x/%lx\n", msg.message, msg.lParam, command, m1, p2);
90 else ok_(__FILE__,line)(m1==0, "Expect message %x from %s\n", m1, command);
92 else {
93 /* FIXME: MOM_POSITIONCB and MOM_DONE are so close that a queue is needed. */
94 if (cbmsg) {
95 ok_(__FILE__,line)(cbmsg==m1 && cbval1==p2 && cbval2==0, "bad callback %x/%lx/%lx from %s, expect %x/%lx\n", cbmsg, cbval1, cbval2, command, m1, p2);
96 cbmsg = 0; /* Mark as read */
97 cbval1 = cbval2 = WHATEVER;
98 ok_(__FILE__,line)(cbinst==MYCBINST, "callback dwInstance changed to %lx\n", cbinst);
100 else ok_(__FILE__,line)(m1==0, "Expect callback %x from %s\n", m1, command);
105 static void test_midiIn_device(UINT udev, HWND hwnd)
107 HMIDIIN hm;
108 MMRESULT rc;
109 MIDIINCAPSA capsA;
110 MIDIHDR mhdr;
112 rc = midiInGetDevCapsA(udev, &capsA, sizeof(capsA));
113 ok((MIDIMAPPER==udev) ? (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)) : rc==0,
114 "midiInGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
115 if (!rc) {
116 /* MIDI IN capsA.dwSupport may contain garbage, absent in old MS-Windows */
117 trace("* %s: manufacturer=%d, product=%d, support=%X\n", capsA.szPname, capsA.wMid, capsA.wPid, capsA.dwSupport);
120 if (hwnd)
121 rc = midiInOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
122 else
123 rc = midiInOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
124 ok((MIDIMAPPER!=udev) ? rc==0 : (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)),
125 "midiInOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
126 if (rc) return;
128 test_notification(hwnd, "midiInOpen", MIM_OPEN, 0);
130 memset(&mhdr, 0, sizeof(mhdr));
131 mhdr.dwUser = 0x56FA552C;
132 mhdr.dwBufferLength = 70000; /* > 64KB! */
133 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
134 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
135 if (mhdr.lpData) {
136 rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
137 ok(rc==MMSYSERR_INVALPARAM, "midiInPrepare tiny rc=%s\n", mmsys_error(rc));
138 rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
139 ok(!rc, "midiInPrepare old size rc=%s\n", mmsys_error(rc));
140 rc = midiInPrepareHeader(hm, &mhdr, sizeof(mhdr));
141 ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
142 rc = midiInUnprepareHeader(hm, &mhdr, sizeof(mhdr));
143 ok(!rc, "midiInUnprepare rc=%s\n", mmsys_error(rc));
144 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
146 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
148 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
150 rc = midiInReset(hm); /* Return any pending buffer */
151 ok(!rc, "midiInReset rc=%s\n", mmsys_error(rc));
153 rc = midiInClose(hm);
154 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
155 test_notification(hwnd, "midiInClose", MIM_CLOSE, 0);
156 test_notification(hwnd, "midiIn over", 0, WHATEVER);
159 static void test_midi_infns(HWND hwnd)
161 HMIDIIN hm;
162 MMRESULT rc;
163 UINT udev, ndevs = midiInGetNumDevs();
165 rc = midiInOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
166 ok(rc==MMSYSERR_BADDEVICEID, "midiInOpen udev>max rc=%s\n", mmsys_error(rc));
167 if (!rc) {
168 rc = midiInClose(hm);
169 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
171 if (!ndevs) {
172 trace("Found no MIDI IN device\n"); /* no skip for this common situation */
173 rc = midiInOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
174 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiInOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
175 if (!rc) {
176 rc = midiInClose(hm);
177 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
179 return;
181 trace("Found %d MIDI IN devices\n", ndevs);
182 for (udev=0; udev < ndevs; udev++) {
183 trace("** Testing device %d\n", udev);
184 test_midiIn_device(udev, hwnd);
185 Sleep(50);
187 trace("** Testing MIDI mapper\n");
188 test_midiIn_device(MIDIMAPPER, hwnd);
192 static void test_midi_mci(HWND hwnd)
194 MCIERROR err;
195 char buf[1024];
196 memset(buf, 0, sizeof(buf));
198 err = mciSendString("sysinfo sequencer quantity", buf, sizeof(buf), hwnd);
199 ok(!err, "mci sysinfo sequencer quantity returned %d\n", err);
200 if (!err) trace("Found %s MCI sequencer devices\n", buf);
204 static void test_midiOut_device(UINT udev, HWND hwnd)
206 HMIDIOUT hm;
207 MMRESULT rc;
208 MIDIOUTCAPSA capsA;
209 DWORD ovolume;
210 UINT udevid;
211 MIDIHDR mhdr;
213 rc = midiOutGetDevCapsA(udev, &capsA, sizeof(capsA));
214 ok(!rc, "midiOutGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
215 if (!rc) {
216 trace("* %s: manufacturer=%d, product=%d, tech=%d, support=%X: %d voices, %d notes\n",
217 capsA.szPname, capsA.wMid, capsA.wPid, capsA.wTechnology, capsA.dwSupport, capsA.wVoices, capsA.wNotes);
218 ok(!((MIDIMAPPER==udev) ^ (MOD_MAPPER==capsA.wTechnology)), "technology %d on device %d\n", capsA.wTechnology, udev);
219 if (MOD_MIDIPORT == capsA.wTechnology) {
220 ok(capsA.wVoices == 0 && capsA.wNotes == 0, "external device with notes or voices\n");
221 ok(capsA.wChannelMask == 0xFFFF, "external device channel mask %x\n", capsA.wChannelMask);
222 ok(!(capsA.dwSupport & (MIDICAPS_VOLUME|MIDICAPS_LRVOLUME|MIDICAPS_CACHE)), "external device support=%X\n", capsA.dwSupport);
226 if (hwnd)
227 rc = midiOutOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
228 else
229 rc = midiOutOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
230 if (rc == MMSYSERR_NOTSUPPORTED)
232 skip( "MIDI out not supported\n" );
233 return;
235 ok(!rc, "midiOutOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
236 if (rc) return;
238 test_notification(hwnd, "midiOutOpen", MOM_OPEN, 0);
240 rc = midiOutGetVolume(hm, &ovolume);
241 ok((capsA.dwSupport & MIDICAPS_VOLUME) ? rc==MMSYSERR_NOERROR : rc==MMSYSERR_NOTSUPPORTED, "midiOutGetVolume rc=%s\n", mmsys_error(rc));
242 /* The native mapper responds with FFFFFFFF initially,
243 * real devices with the volume GUI SW-synth settings. */
244 if (!rc) trace("Current volume %x on device %d\n", ovolume, udev);
246 /* The W95 ESFM Synthesis device reports NOTENABLED although
247 * GetVolume by handle works and music plays. */
248 rc = midiOutGetVolume(UlongToHandle(udev), &ovolume);
249 ok((capsA.dwSupport & MIDICAPS_VOLUME) ? rc==MMSYSERR_NOERROR || broken(rc==MMSYSERR_NOTENABLED) : rc==MMSYSERR_NOTSUPPORTED, "midiOutGetVolume(dev=%d) rc=%s\n", udev, mmsys_error(rc));
251 rc = midiOutGetVolume(hm, NULL);
252 ok(rc==MMSYSERR_INVALPARAM, "midiOutGetVolume NULL rc=%s\n", mmsys_error(rc));
254 /* Tests with midiOutSetvolume show that the midi mapper forwards
255 * the value to the real device, but Get initially always reports
256 * FFFFFFFF. Therefore, a Get+SetVolume pair with the mapper is
257 * not adequate to restore the value prior to tests.
259 if (winetest_interactive && (capsA.dwSupport & MIDICAPS_VOLUME)) {
260 DWORD volume2 = (ovolume < 0x80000000) ? 0xC000C000 : 0x40004000;
261 rc = midiOutSetVolume(hm, volume2);
262 ok(!rc, "midiOutSetVolume rc=%s\n", mmsys_error(rc));
263 if (!rc) {
264 DWORD volume3;
265 rc = midiOutGetVolume(hm, &volume3);
266 ok(!rc, "midiOutGetVolume new rc=%s\n", mmsys_error(rc));
267 if (!rc) trace("New volume %x on device %d\n", volume3, udev);
268 todo_wine ok(volume2==volume3, "volume Set %x = Get %x\n", volume2, volume3);
270 rc = midiOutSetVolume(hm, ovolume);
271 ok(!rc, "midiOutSetVolume restore rc=%s\n", mmsys_error(rc));
274 rc = midiOutGetDevCapsA((UINT_PTR)hm, &capsA, sizeof(capsA));
275 ok(!rc, "midiOutGetDevCaps(dev=%d) by handle rc=%s\n", udev, mmsys_error(rc));
276 rc = midiInGetDevCapsA((UINT_PTR)hm, (LPMIDIINCAPSA)&capsA, sizeof(DWORD));
277 ok(rc==MMSYSERR_BADDEVICEID, "midiInGetDevCaps(dev=%d) by out handle rc=%s\n", udev, mmsys_error(rc));
279 { DWORD e = 0x006F4893; /* velocity, note (#69 would be 440Hz) channel */
280 trace("ShortMsg type %x\n", LOBYTE(LOWORD(e)));
281 rc = midiOutShortMsg(hm, e);
282 ok(!rc, "midiOutShortMsg rc=%s\n", mmsys_error(rc));
283 if (!rc) Sleep(400); /* Hear note */
286 memset(&mhdr, 0, sizeof(mhdr));
287 mhdr.dwUser = 0x56FA552C;
288 mhdr.dwOffset = 0xDEADBEEF;
289 mhdr.dwBufferLength = 70000; /* > 64KB! */
290 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
291 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
292 if (mhdr.lpData) {
293 rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
294 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
295 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
297 rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
298 ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
299 rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
300 ok(!rc, "midiOutPrepare old size rc=%s\n", mmsys_error(rc));
301 rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
302 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
303 rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
304 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
305 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
307 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
309 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
310 ok(mhdr.dwOffset==0xDEADBEEF, "MIDIHDR.dwOffset changed to %x\n", mhdr.dwOffset);
312 rc = midiOutGetID(hm, &udevid);
313 ok(!rc, "midiOutGetID rc=%s\n", mmsys_error(rc));
314 if(!rc) ok(udevid==udev, "midiOutGetID gives %d, expect %d\n", udevid, udev);
316 rc = midiOutReset(hm); /* Quiet everything */
317 ok(!rc, "midiOutReset rc=%s\n", mmsys_error(rc));
319 rc = midiOutClose(hm);
320 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
321 test_notification(hwnd, "midiOutClose", MOM_CLOSE, 0);
322 test_notification(hwnd, "midiOut over", 0, WHATEVER);
325 static void test_position(HMIDISTRM hm, UINT typein, UINT typeout)
327 MMRESULT rc;
328 MMTIME mmtime;
329 mmtime.wType = typein;
330 rc = midiStreamPosition(hm, &mmtime, sizeof(MMTIME));
331 /* Ugly, but a single ok() herein enables using the todo_wine prefix */
332 ok(!rc && (mmtime.wType == typeout), "midiStreamPosition type %x converted to %x rc=%s\n", typein, mmtime.wType, mmsys_error(rc));
333 if (!rc) switch(mmtime.wType) {
334 case TIME_MS:
335 trace("Stream position %ums\n", mmtime.u.ms);
336 break;
337 case TIME_TICKS:
338 trace("Stream position %u ticks\n", mmtime.u.ticks);
339 break;
340 case TIME_MIDI:
341 trace("Stream position song pointer %u\n", mmtime.u.midi.songptrpos);
342 break;
346 typedef struct midishortevent_tag { /* ideal size for MEVT_F_SHORT event type */
347 DWORD dwDeltaTime;
348 DWORD dwStreamID;
349 DWORD dwEvent;
350 } MIDISHORTEVENT;
352 /* Native crashes on a second run with the const qualifier set on this data! */
353 static BYTE strmEvents[] = { /* A set of variable-sized MIDIEVENT structs */
354 0, 0, 0, 0, 0, 0, 0, 0, /* dwDeltaTime and dwStreamID */
355 0, 0, 0, MEVT_NOP | 0x40, /* with MEVT_F_CALLBACK */
356 0, 0, 0, 0, 0, 0, 0, 0,
357 0xE0, 0x93, 0x04, MEVT_TEMPO, /* 0493E0 == 300000 */
358 0, 0, 0, 0, 0, 0, 0, 0,
359 0x93, 0x48, 0x6F, MEVT_SHORTMSG,
362 static MIDISHORTEVENT strmNops[] = { /* Test callback + dwOffset */
363 { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
364 { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
367 static MMRESULT playStream(HMIDISTRM hm, LPMIDIHDR lpMidiHdr)
369 MMRESULT rc = midiStreamOut(hm, lpMidiHdr, sizeof(MIDIHDR));
370 /* virtual machines may return MIDIERR_STILLPLAYING from the next request
371 * even after MHDR_DONE is set. It's still too early, so add MHDR_INQUEUE. */
372 if (!rc) while (!(lpMidiHdr->dwFlags & MHDR_DONE) || (lpMidiHdr->dwFlags & MHDR_INQUEUE)) { Sleep(100); }
373 return rc;
376 static void test_midiStream(UINT udev, HWND hwnd)
378 HMIDISTRM hm;
379 MMRESULT rc, rc2;
380 MIDIHDR mhdr;
381 union {
382 MIDIPROPTEMPO tempo;
383 MIDIPROPTIMEDIV tdiv;
384 } midiprop;
386 if (hwnd)
387 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
388 else
389 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
390 if (rc == MMSYSERR_NOTSUPPORTED)
392 skip( "MIDI stream not supported\n" );
393 return;
395 ok(!rc, "midiStreamOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
396 if (rc) return;
398 test_notification(hwnd, "midiStreamOpen", MOM_OPEN, 0);
400 midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
401 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
402 ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
403 ok(midiprop.tempo.dwTempo==500000, "default stream tempo %u microsec per quarter note\n", midiprop.tempo.dwTempo);
405 midiprop.tdiv.cbStruct = sizeof(midiprop.tdiv);
406 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TIMEDIV);
407 ok(!rc, "midiStreamProperty TIMEDIV rc=%s\n", mmsys_error(rc));
408 todo_wine ok(24==LOWORD(midiprop.tdiv.dwTimeDiv), "default stream time division %u\n", midiprop.tdiv.dwTimeDiv);
410 memset(&mhdr, 0, sizeof(mhdr));
411 mhdr.dwUser = 0x56FA552C;
412 mhdr.dwOffset = 1234567890;
413 mhdr.dwBufferLength = sizeof(strmEvents);
414 mhdr.dwBytesRecorded = mhdr.dwBufferLength;
415 mhdr.lpData = (LPSTR)&strmEvents[0];
416 if (mhdr.lpData) {
417 rc = midiOutLongMsg((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
418 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
419 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
421 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
422 ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
423 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
424 ok(!rc, "midiOutPrepare old size rc=%s\n", mmsys_error(rc));
425 ok(mhdr.dwFlags & MHDR_PREPARED, "MHDR.dwFlags when prepared %x\n", mhdr.dwFlags);
427 /* The device is still in paused mode and should queue the message. */
428 rc = midiStreamOut(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
429 ok(!rc, "midiStreamOut old size rc=%s\n", mmsys_error(rc));
430 rc2 = rc;
431 trace("MIDIHDR flags=%x when submitted\n", mhdr.dwFlags);
432 /* w9X/me does not set MHDR_ISSTRM when StreamOut exits,
433 * but it will be set on all systems after the job is finished. */
435 Sleep(90);
436 /* Wine <1.1.39 started playing immediately */
437 test_notification(hwnd, "midiStream still paused", 0, WHATEVER);
439 /* MSDN asks to use midiStreamRestart prior to midiStreamOut()
440 * because the starting state is 'pause', but some apps seem to
441 * work with the inverse order: queue everything, then play.
444 rc = midiStreamRestart(hm);
445 ok(!rc, "midiStreamRestart rc=%s\n", mmsys_error(rc));
447 if (!rc2) while(mhdr.dwFlags & MHDR_INQUEUE) {
448 trace("async MIDI still queued\n");
449 Sleep(100);
450 } /* Checking INQUEUE is not the recommended way to wait for the end of a job, but we're testing. */
451 /* MHDR_ISSTRM is not necessarily set when midiStreamOut returns
452 * rather than when the queue is eventually processed. */
453 ok(mhdr.dwFlags & MHDR_ISSTRM, "MHDR.dwFlags %x no ISSTRM when out of queue\n", mhdr.dwFlags);
454 if (!rc2) while(!(mhdr.dwFlags & MHDR_DONE)) {
455 /* Never to be seen except perhaps on multicore */
456 trace("async MIDI still not done\n");
457 Sleep(100);
459 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags %x not DONE when out of queue\n", mhdr.dwFlags);
460 test_notification(hwnd, "midiStream callback", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
461 test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
463 /* Native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
464 ok(1234567890!=mhdr.dwOffset, "play left MIDIHDR.dwOffset at %u\n", mhdr.dwOffset);
466 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
467 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
468 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
469 ok(!rc, "midiOutUnprepare #2 rc=%s\n", mmsys_error(rc));
471 trace("MIDIHDR stream flags=%x when finished\n", mhdr.dwFlags);
472 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags when done %x\n", mhdr.dwFlags);
474 test_position(hm, TIME_MS, TIME_MS);
475 test_position(hm, TIME_TICKS, TIME_TICKS);
476 todo_wine test_position(hm, TIME_MIDI, TIME_MIDI);
477 test_position(hm, TIME_SMPTE, TIME_MS);
478 test_position(hm, TIME_SAMPLES, TIME_MS);
479 test_position(hm, TIME_BYTES, TIME_MS);
481 Sleep(400); /* Hear note */
483 midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
484 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
485 ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
486 ok(0x0493E0==midiprop.tempo.dwTempo, "stream set tempo %u\n", midiprop.tdiv.dwTimeDiv);
488 rc = midiStreamRestart(hm);
489 ok(!rc, "midiStreamRestart #2 rc=%s\n", mmsys_error(rc));
491 mhdr.dwFlags |= MHDR_ISSTRM;
492 /* Preset flags (e.g. MHDR_ISSTRM) do not disturb. */
493 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
494 ok(!rc, "midiOutPrepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
495 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
496 ok(!rc, "midiOutUnprepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
498 rc = midiStreamRestart(hm);
499 ok(!rc, "midiStreamRestart #3 rc=%s\n", mmsys_error(rc));
501 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
502 ok(0==((MIDISHORTEVENT*)&strmEvents)[0].dwStreamID, "dwStreamID set to %x\n", ((LPMIDIEVENT)&strmEvents[0])->dwStreamID);
504 /* dwBytesRecorded controls how much is played, not dwBufferLength
505 * allowing to immediately forward packets from midiIn to midiOut */
506 mhdr.dwOffset = 1234123123;
507 mhdr.dwBufferLength = sizeof(strmNops);
508 trace("buffer: %u\n", mhdr.dwBufferLength);
509 mhdr.dwBytesRecorded = 0;
510 mhdr.lpData = (LPSTR)&strmNops[0];
511 strmNops[0].dwEvent |= MEVT_F_CALLBACK;
512 strmNops[1].dwEvent |= MEVT_F_CALLBACK;
514 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
515 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
517 rc = playStream(hm, &mhdr);
518 ok(!rc, "midiStreamOut 0 bytes recorded rc=%s\n", mmsys_error(rc));
520 test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
521 test_notification(hwnd, "0 bytes recorded", 0, WHATEVER);
523 /* FIXME: check dwOffset within callback
524 * instead of the unspecified value afterwards */
525 ok(1234123123==mhdr.dwOffset || broken(0==mhdr.dwOffset), "play 0 set MIDIHDR.dwOffset to %u\n", mhdr.dwOffset);
526 /* w2k and later only set dwOffset when processing MEVT_T_CALLBACK,
527 * while w9X/me/nt always sets it. Have Wine behave like w2k because the
528 * dwOffset slot does not exist in the small size MIDIHDR. */
530 mhdr.dwOffset = 1234123123;
531 mhdr.dwBytesRecorded = 1*sizeof(MIDISHORTEVENT);
533 rc = playStream(hm, &mhdr);
534 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
536 test_notification(hwnd, "1 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
537 test_notification(hwnd, "1 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
538 test_notification(hwnd, "1 of 2 events", 0, WHATEVER);
539 ok(0==mhdr.dwOffset, "MIDIHDR.dwOffset 1/2 changed to %u\n", mhdr.dwOffset);
541 mhdr.dwOffset = 1234123123;
542 mhdr.dwBytesRecorded = 2*sizeof(MIDISHORTEVENT);
544 rc = playStream(hm, &mhdr);
545 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
547 test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
548 test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
549 test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
550 test_notification(hwnd, "2 of 2 events", 0, WHATEVER);
551 ok(sizeof(MIDISHORTEVENT)==mhdr.dwOffset, "MIDIHDR.dwOffset 2/2 changed to %u\n", mhdr.dwOffset);
552 ok(mhdr.dwBytesRecorded == 2*sizeof(MIDISHORTEVENT), "dwBytesRecorded changed to %u\n", mhdr.dwBytesRecorded);
554 strmNops[0].dwEvent &= ~MEVT_F_CALLBACK;
555 strmNops[1].dwEvent &= ~MEVT_F_CALLBACK;
556 mhdr.dwOffset = 1234123123;
557 rc = playStream(hm, &mhdr);
558 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
560 test_notification(hwnd, "0 CB in 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
561 test_notification(hwnd, "0 CB in 2 events", 0, WHATEVER);
562 /* w9X/me/nt set dwOffset to the position played last */
563 ok(1234123123==mhdr.dwOffset || broken(sizeof(MIDISHORTEVENT)==mhdr.dwOffset), "MIDIHDR.dwOffset nocb changed to %u\n", mhdr.dwOffset);
565 mhdr.dwBytesRecorded = mhdr.dwBufferLength-1;
566 rc = playStream(hm, &mhdr);
567 ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBytesRecorded modulo MIDIEVENT rc=%s\n", mmsys_error(rc));
568 if (!rc) {
569 test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
572 mhdr.dwBytesRecorded = mhdr.dwBufferLength+1;
573 rc = playStream(hm, &mhdr);
574 ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBufferLength<dwBytesRecorded rc=%s\n", mmsys_error(rc));
575 test_notification(hwnd, "past MIDIHDR tests", 0, WHATEVER);
577 rc = midiStreamStop(hm);
578 ok(!rc, "midiStreamStop rc=%s\n", mmsys_error(rc));
579 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
581 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
582 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
583 ok(0==strmNops[0].dwStreamID, "dwStreamID[0] set to %x\n", strmNops[0].dwStreamID);
584 ok(0==strmNops[1].dwStreamID, "dwStreamID[1] set to %x\n", strmNops[1].dwStreamID);
586 mhdr.dwBufferLength = 70000; /* > 64KB! */
587 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
588 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
589 if (mhdr.lpData) {
590 mhdr.dwFlags = 0;
591 /* PrepareHeader detects the too large buffer is for a stream. */
592 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
593 todo_wine ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare stream too large rc=%s\n", mmsys_error(rc));
595 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
596 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
598 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
601 rc = midiStreamClose(hm);
602 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
603 test_notification(hwnd, "midiStreamClose", MOM_CLOSE, 0);
604 test_notification(hwnd, "midiStream over", 0, WHATEVER);
606 rc = midiStreamOpen(&hm, &udev, 1, 0, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
607 ok(!rc /*w2k*/|| rc==MMSYSERR_INVALPARAM/*w98*/, "midiStreamOpen NULL function rc=%s\n", mmsys_error(rc));
608 if (!rc) {
609 trace("Device %d accepts NULL CALLBACK_FUNCTION\n", udev);
610 rc = midiStreamClose(hm);
611 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
614 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)0xDEADBEEF, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
615 ok(rc==MMSYSERR_INVALPARAM, "midiStreamOpen bad window rc=%s\n", mmsys_error(rc));
616 if (!rc) {
617 rc = midiStreamClose(hm);
618 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
622 static void test_midi_outfns(HWND hwnd)
624 HMIDIOUT hm;
625 MMRESULT rc;
626 UINT udev, ndevs = midiOutGetNumDevs();
628 rc = midiOutOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
629 ok(rc==MMSYSERR_BADDEVICEID, "midiOutOpen udev>max rc=%s\n", mmsys_error(rc));
630 if (!rc) {
631 rc = midiOutClose(hm);
632 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
634 if (!ndevs) {
635 MIDIOUTCAPSA capsA;
636 skip("Found no MIDI out device\n");
638 rc = midiOutGetDevCapsA(MIDIMAPPER, &capsA, sizeof(capsA));
639 /* GetDevCaps and Open must return compatible results */
640 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiOutGetDevCaps MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
642 rc = midiOutOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
643 if (rc==MIDIERR_INVALIDSETUP) todo_wine /* Wine without snd-seq */
644 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k*/), "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
645 else
646 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k sound disabled*/),
647 "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
648 if (!rc) {
649 rc = midiOutClose(hm);
650 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
652 return;
654 trace("Found %d MIDI OUT devices\n", ndevs);
656 test_midi_mci(hwnd);
658 for (udev=0; udev < ndevs; udev++) {
659 trace("** Testing device %d\n", udev);
660 test_midiOut_device(udev, hwnd);
661 Sleep(800); /* Let the synth rest */
662 test_midiStream(udev, hwnd);
663 Sleep(800);
665 trace("** Testing MIDI mapper\n");
666 test_midiOut_device(MIDIMAPPER, hwnd);
667 Sleep(800);
668 test_midiStream(MIDIMAPPER, hwnd);
671 START_TEST(midi)
673 HWND hwnd = 0;
674 if (1) /* select 1 for CALLBACK_WINDOW or 0 for CALLBACK_FUNCTION */
675 hwnd = CreateWindowExA(0, "static", "winmm midi test", WS_POPUP, 0,0,100,100,
676 0, 0, 0, NULL);
677 test_midi_infns(hwnd);
678 test_midi_outfns(hwnd);
679 if (hwnd) DestroyWindow(hwnd);