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
24 #include "wine/test.h"
26 extern const char* mmsys_error(MMRESULT error); /* from wave.c */
28 /* Test in order of increasing probability to hang.
29 * On many UNIX systems, the Timidity softsynth provides
30 * MIDI sequencer services and it is not particularly robust.
33 #define MYCBINST 0x4CAFE5A8 /* not used with window or thread callbacks */
34 #define WHATEVER 0xFEEDF00D
36 static BOOL spurious_message(LPMSG msg)
38 /* WM_DEVICECHANGE 0x0219 appears randomly */
39 if(msg->message == WM_DEVICECHANGE) {
40 trace("skipping spurious message %04x\n", msg->message);
46 static UINT cbmsg = 0;
47 static DWORD_PTR cbval1 = WHATEVER;
48 static DWORD_PTR cbval2 = 0;
49 static DWORD_PTR cbinst = MYCBINST;
51 static void CALLBACK callback_func(HWAVEOUT hwo, UINT uMsg,
53 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
56 trace("Callback! msg=%x %lx %lx\n", uMsg, dwParam1, dwParam2);
58 cbval1 = dwParam1; /* mhdr or 0 */
59 cbval2 = dwParam2; /* always 0 */
60 cbinst = dwInstance; /* MYCBINST, see midiOut/StreamOpen */
63 static void test_notification(HWND hwnd, const char* command, UINT m1, DWORD_PTR p2)
64 { /* Use message type 0 as meaning no message */
67 /* msg.wParam is hmidiout, msg.lParam is the mhdr (or 0) */
69 do { seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE); }
70 while(seen && spurious_message(&msg));
72 trace("Message %x, wParam=%lx, lParam=%lx from %s\n",
73 msg.message, msg.wParam, msg.lParam, command);
74 ok(msg.hwnd==hwnd, "Didn't get the handle to our test window\n");
75 ok(msg.message==m1 && msg.lParam==p2, "bad message %x/%lx from %s, expect %x/%lx\n", msg.message, msg.lParam, command, m1, p2);
77 else ok(m1==0, "Expect message %x from %s\n", m1, command);
80 /* FIXME: MOM_POSITIONCB and MOM_DONE are so close that a queue is needed. */
82 ok(cbmsg==m1 && cbval1==p2 && cbval2==0, "bad callback %x/%lx/%lx from %s, expect %x/%lx\n", cbmsg, cbval1, cbval2, command, m1, p2);
83 cbmsg = 0; /* Mark as read */
84 cbval1 = cbval2 = WHATEVER;
85 ok(cbinst==MYCBINST, "callback dwInstance changed to %lx\n", cbinst);
87 else ok(m1==0, "Expect callback %x from %s\n", m1, command);
92 static void test_midiIn_device(UINT udev, HWND hwnd)
99 rc = midiInGetDevCapsA(udev, &capsA, sizeof(capsA));
100 ok((MIDIMAPPER==udev) ? (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)) : rc==0,
101 "midiInGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
103 /* MIDI IN capsA.dwSupport may contain garbage, absent in old MS-Windows */
104 trace("* %s: manufacturer=%d, product=%d, support=%X\n", capsA.szPname, capsA.wMid, capsA.wPid, capsA.dwSupport);
108 rc = midiInOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
110 rc = midiInOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
111 ok((MIDIMAPPER!=udev) ? rc==0 : (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)),
112 "midiInOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
115 test_notification(hwnd, "midiInOpen", MIM_OPEN, 0);
118 mhdr.dwUser = 0x56FA552C;
119 mhdr.dwBufferLength = 70000; /* > 64KB! */
120 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
121 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
123 rc = midiInPrepareHeader(hm, &mhdr, sizeof(mhdr));
124 ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
125 rc = midiInUnprepareHeader(hm, &mhdr, sizeof(mhdr));
126 ok(!rc, "midiInUnprepare rc=%s\n", mmsys_error(rc));
127 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
129 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
131 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
133 rc = midiInReset(hm); /* Return any pending buffer */
134 ok(!rc, "midiInReset rc=%s\n", mmsys_error(rc));
136 rc = midiInClose(hm);
137 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
138 test_notification(hwnd, "midiInClose", MIM_CLOSE, 0);
139 test_notification(hwnd, "midiIn over", 0, WHATEVER);
142 static void test_midi_infns(HWND hwnd)
146 UINT udev, ndevs = midiInGetNumDevs();
148 rc = midiInOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
149 ok(rc==MMSYSERR_BADDEVICEID, "midiInOpen udev>max rc=%s\n", mmsys_error(rc));
151 rc = midiInClose(hm);
152 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
155 trace("Found no MIDI IN device\n"); /* no skip for this common situation */
156 rc = midiInOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
157 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiInOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
159 rc = midiInClose(hm);
160 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
164 trace("Found %d MIDI IN devices\n", ndevs);
165 for (udev=0; udev < ndevs; udev++) {
166 trace("** Testing device %d\n", udev);
167 test_midiIn_device(udev, hwnd);
170 trace("** Testing MIDI mapper\n");
171 test_midiIn_device(MIDIMAPPER, hwnd);
175 static void test_midi_mci(HWND hwnd)
179 memset(buf, 0, sizeof(buf));
181 err = mciSendString("sysinfo sequencer quantity", buf, sizeof(buf), hwnd);
182 ok(!err, "mci sysinfo sequencer quantity returned %d\n", err);
183 if (!err) trace("Found %s MCI sequencer devices\n", buf);
187 static void test_midiOut_device(UINT udev, HWND hwnd)
195 rc = midiOutGetDevCapsA(udev, &capsA, sizeof(capsA));
196 ok(!rc, "midiOutGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
198 trace("* %s: manufacturer=%d, product=%d, tech=%d, support=%X: %d voices, %d notes\n",
199 capsA.szPname, capsA.wMid, capsA.wPid, capsA.wTechnology, capsA.dwSupport, capsA.wVoices, capsA.wNotes);
203 rc = midiOutOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
205 rc = midiOutOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
206 ok(!rc, "midiOutOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
209 test_notification(hwnd, "midiOutOpen", MOM_OPEN, 0);
211 rc = midiOutGetVolume(hm, &ovolume);
212 ok(rc==((capsA.dwSupport & MIDICAPS_VOLUME) ? MMSYSERR_NOERROR : MMSYSERR_NOTSUPPORTED), "midiOutGetVolume rc=%s\n", mmsys_error(rc));
213 /* The native mapper responds with FFFFFFFF initially,
214 * real devices with the volume GUI SW-synth settings. */
215 if (!rc) trace("Current volume %x on device %d\n", ovolume, udev);
217 /* Tests with midiOutSetvolume show that the midi mapper forwards
218 * the value to the real device, but Get initially always reports
219 * FFFFFFFF. Therefore, a Get+SetVolume pair with the mapper is
220 * not adequate to restore the value prior to tests.
222 if (winetest_interactive && (capsA.dwSupport & MIDICAPS_VOLUME)) {
223 DWORD volume2 = (ovolume < 0x80000000) ? 0xC000C000 : 0x40004000;
224 rc = midiOutSetVolume(hm, volume2);
225 ok(!rc, "midiOutSetVolume rc=%s\n", mmsys_error(rc));
228 rc = midiOutGetVolume(hm, &volume3);
229 ok(!rc, "midiOutGetVolume new rc=%s\n", mmsys_error(rc));
230 if (!rc) trace("New volume %x on device %d\n", volume3, udev);
231 todo_wine ok(volume2==volume3, "volume Set %x = Get %x\n", volume2, volume3);
233 rc = midiOutSetVolume(hm, ovolume);
234 ok(!rc, "midiOutSetVolume restore rc=%s\n", mmsys_error(rc));
237 rc = midiOutGetDevCapsA((UINT)hm, &capsA, sizeof(capsA));
238 ok(!rc, "midiOutGetDevCaps(dev=%d) by handle rc=%s\n", udev, mmsys_error(rc));
239 rc = midiInGetDevCapsA((UINT)hm, (LPMIDIINCAPSA)&capsA, sizeof(DWORD));
240 ok(rc==MMSYSERR_BADDEVICEID, "midiInGetDevCaps(dev=%d) by out handle rc=%s\n", udev, mmsys_error(rc));
242 { DWORD e = 0x006F4893; /* velocity, note (#69 would be 440Hz) channel */
243 trace("ShortMsg type %x\n", LOBYTE(LOWORD(e)));
244 rc = midiOutShortMsg(hm, e);
245 ok(!rc, "midiOutShortMsg rc=%s\n", mmsys_error(rc));
246 if (!rc) Sleep(400); /* Hear note */
250 mhdr.dwUser = 0x56FA552C;
251 mhdr.dwBufferLength = 70000; /* > 64KB! */
252 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
253 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
255 rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
256 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
257 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
259 rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
260 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
261 rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
262 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
263 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
265 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
267 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
269 rc = midiOutReset(hm); /* Quiet everything */
270 ok(!rc, "midiOutReset rc=%s\n", mmsys_error(rc));
272 rc = midiOutClose(hm);
273 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
274 test_notification(hwnd, "midiOutClose", MOM_CLOSE, 0);
275 test_notification(hwnd, "midiOut over", 0, WHATEVER);
278 static void test_position(HMIDISTRM hm, UINT typein, UINT typeout)
282 mmtime.wType = typein;
283 rc = midiStreamPosition(hm, &mmtime, sizeof(MMTIME));
284 /* Ugly, but a single ok() herein enables using the todo_wine prefix */
285 ok(!rc && (mmtime.wType == typeout), "midiStreamPosition type %x converted to %x rc=%s\n", typein, mmtime.wType, mmsys_error(rc));
286 if (!rc) switch(mmtime.wType) {
288 trace("Stream position %ums\n", mmtime.u.ms);
291 trace("Stream position %u ticks\n", mmtime.u.ticks);
294 trace("Stream position song pointer %u\n", mmtime.u.midi.songptrpos);
299 static BYTE strmEvents[] = { /* A set of variable-sized MIDIEVENT structs */
300 0, 0, 0, 0, 0, 0, 0, 0, /* dwDeltaTime and dwStreamID */
301 0, 0, 0, MEVT_NOP | 0x40, /* with MEVT_F_CALLBACK */
302 0, 0, 0, 0, 0, 0, 0, 0, /* dwDeltaTime and dwStreamID */
303 0x93, 0x48, 0x6F, MEVT_SHORTMSG,
306 static void test_midiStream(UINT udev, HWND hwnd)
313 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
315 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
316 ok(!rc, "midiStreamOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
319 test_notification(hwnd, "midiStreamOpen", MOM_OPEN, 0);
322 mhdr.dwUser = 0x56FA552C;
323 mhdr.dwBufferLength = sizeof(strmEvents) * sizeof(strmEvents[0]);
324 mhdr.dwBytesRecorded = mhdr.dwBufferLength; /* unused? */
325 mhdr.lpData = (LPSTR)&strmEvents[0];
327 rc = midiOutLongMsg((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
328 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
329 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
331 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
332 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
333 ok(mhdr.dwFlags & MHDR_PREPARED, "MHDR.dwFlags when prepared %x\n", mhdr.dwFlags);
335 /* The device is still in paused mode and should queue the message. */
336 rc = midiStreamOut(hm, &mhdr, sizeof(mhdr));
337 ok(!rc, "midiStreamOut rc=%s\n", mmsys_error(rc));
339 trace("MIDIHDR flags=%x when submitted\n", mhdr.dwFlags);
340 /* w9X/me does not set MHDR_ISSTRM when StreamOut exits,
341 * but it will be set on all systems after the job is finished. */
344 /* Wine <1.1.39 started playing immediately */
345 test_notification(hwnd, "midiStream still paused", 0, WHATEVER);
347 /* MSDN asks to use midiStreamRestart prior to midiStreamOut()
348 * because the starting state is 'pause', but some apps seem to
349 * work with the inverse order.
352 rc = midiStreamRestart(hm);
353 ok(!rc, "midiStreamRestart rc=%s\n", mmsys_error(rc));
355 if (!rc2) while(mhdr.dwFlags & MHDR_INQUEUE) {
356 trace("async MIDI still queued\n");
358 } /* Checking INQUEUE is not the recommended way to wait for the end of a job, but we're testing. */
359 /* MHDR_ISSTRM is not necessarily set when midiStreamOut returns
360 * rather than when the queue is eventually processed. */
361 ok(mhdr.dwFlags & MHDR_ISSTRM, "MHDR.dwFlags %x no ISSTRM when out of queue\n", mhdr.dwFlags);
362 if (!rc2) while(!(mhdr.dwFlags & MHDR_DONE)) {
363 /* Never to be seen except perhaps on multicore */
364 trace("async MIDI still not done\n");
367 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags %x not DONE when out of queue\n", mhdr.dwFlags);
368 test_notification(hwnd, "midiStream callback", MOM_POSITIONCB, (DWORD)&mhdr);
369 test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD)&mhdr);
371 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
372 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
373 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
374 ok(!rc, "midiOutUnprepare #2 rc=%s\n", mmsys_error(rc));
376 trace("MIDIHDR stream flags=%x when finished\n", mhdr.dwFlags);
377 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags when done %x\n", mhdr.dwFlags);
379 test_position(hm, TIME_MS, TIME_MS);
380 test_position(hm, TIME_TICKS, TIME_TICKS);
381 todo_wine test_position(hm, TIME_MIDI, TIME_MIDI);
382 test_position(hm, TIME_SMPTE, TIME_MS);
383 test_position(hm, TIME_SAMPLES, TIME_MS);
384 test_position(hm, TIME_BYTES, TIME_MS);
386 Sleep(400); /* Hear note */
388 rc = midiStreamRestart(hm);
389 ok(!rc, "midiStreamRestart #2 rc=%s\n", mmsys_error(rc));
391 mhdr.dwFlags |= MHDR_ISSTRM; /* just in case */
392 /* Preset flags (e.g. MHDR_ISSTRM) do not disturb. */
393 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
394 ok(!rc, "midiOutPrepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
395 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
396 ok(!rc, "midiOutUnprepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
398 rc = midiStreamRestart(hm);
399 ok(!rc, "midiStreamRestart #3 rc=%s\n", mmsys_error(rc));
401 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
403 rc = midiStreamStop(hm);
404 ok(!rc, "midiStreamStop rc=%s\n", mmsys_error(rc));
406 mhdr.dwBufferLength = 70000; /* > 64KB! */
407 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
408 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
411 /* PrepareHeader detects the too large buffer is for a stream. */
412 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
413 todo_wine ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare stream too large rc=%s\n", mmsys_error(rc));
415 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
416 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
418 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
421 rc = midiStreamClose(hm);
422 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
423 test_notification(hwnd, "midiStreamClose", MOM_CLOSE, 0);
424 test_notification(hwnd, "midiStream over", 0, WHATEVER);
427 static void test_midi_outfns(HWND hwnd)
431 UINT udev, ndevs = midiOutGetNumDevs();
433 rc = midiOutOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
434 ok(rc==MMSYSERR_BADDEVICEID, "midiOutOpen udev>max rc=%s\n", mmsys_error(rc));
436 rc = midiOutClose(hm);
437 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
440 skip("Found no MIDI out device\n");
441 rc = midiOutOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
442 if (rc==MIDIERR_INVALIDSETUP) todo_wine /* Wine without snd-seq */
443 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k*/), "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
445 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k sound disabled*/),
446 "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
448 rc = midiOutClose(hm);
449 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
453 trace("Found %d MIDI OUT devices\n", ndevs);
457 for (udev=0; udev < ndevs; udev++) {
458 trace("** Testing device %d\n", udev);
459 test_midiOut_device(udev, hwnd);
460 Sleep(800); /* Let the synth rest */
461 test_midiStream(udev, hwnd);
464 trace("** Testing MIDI mapper\n");
465 test_midiOut_device(MIDIMAPPER, hwnd);
467 test_midiStream(MIDIMAPPER, hwnd);
473 if (1) /* select 1 for CALLBACK_WINDOW or 0 for CALLBACK_FUNCTION */
474 hwnd = CreateWindowExA(0, "static", "winmm midi test", WS_POPUP, 0,0,100,100,
476 test_midi_infns(hwnd);
477 test_midi_outfns(hwnd);
478 if (hwnd) DestroyWindow(hwnd);