dmusic: Midi message takes 4 bytes space but only 3 are relevant.
[wine] / dlls / dmusic / tests / dmusic.c
1 /*
2  * Unit tests for dmusic functions
3  *
4  * Copyright (C) 2012 Christian Costa
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 #define COBJMACROS
22
23 #include <stdio.h>
24
25 #include "wine/test.h"
26 #include "uuids.h"
27 #include "ole2.h"
28 #include "initguid.h"
29 #include "dmusici.h"
30 #include "dmksctrl.h"
31
32 static inline const char* debugstr_guid(CONST GUID *id)
33 {
34     static char string[39];
35     sprintf(string, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
36             id->Data1, id->Data2, id->Data3,
37             id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
38             id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
39     return string;
40 }
41
42 static inline const char* debugstr_longlong(ULONGLONG ll)
43 {
44     static char string[17];
45     if (sizeof(ll) > sizeof(unsigned long) && ll >> 32)
46         sprintf(string, "%lx%08lx", (unsigned long)(ll >> 32), (unsigned long)ll);
47     else
48         sprintf(string, "%lx", (unsigned long)ll);
49     return string;
50 }
51
52 static void test_dmusic(void)
53 {
54     IDirectMusic *dmusic = NULL;
55     HRESULT hr;
56     ULONG index = 0;
57     DMUS_PORTCAPS port_caps;
58     DMUS_PORTPARAMS port_params;
59     IDirectMusicPort *port = NULL;
60     DMUS_CLOCKINFO clock_info;
61
62     hr = CoCreateInstance(&CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusic, (LPVOID*)&dmusic);
63     if (hr != S_OK)
64     {
65         skip("Cannot create DirectMusic object (%x)\n", hr);
66         return;
67     }
68
69     port_params.dwSize = sizeof(port_params);
70     port_params.dwValidParams = DMUS_PORTPARAMS_CHANNELGROUPS | DMUS_PORTPARAMS_AUDIOCHANNELS;
71     port_params.dwChannelGroups = 1;
72     port_params.dwAudioChannels = 2;
73
74     /* No port can be created before SetDirectSound is called */
75     hr = IDirectMusic_CreatePort(dmusic, &GUID_NULL, &port_params, &port, NULL);
76     todo_wine ok(hr == DMUS_E_DSOUND_NOT_SET, "IDirectMusic_CreatePort returned: %x\n", hr);
77
78     hr = IDirectMusic_SetDirectSound(dmusic, NULL, NULL);
79     ok(hr == S_OK, "IDirectMusic_SetDirectSound returned: %x\n", hr);
80
81     /* Check wrong params */
82     hr = IDirectMusic_CreatePort(dmusic, &GUID_NULL, &port_params, &port, (IUnknown*)dmusic);
83     ok(hr == CLASS_E_NOAGGREGATION, "IDirectMusic_CreatePort returned: %x\n", hr);
84     hr = IDirectMusic_CreatePort(dmusic, NULL, &port_params, &port, NULL);
85     ok(hr == E_POINTER, "IDirectMusic_CreatePort returned: %x\n", hr);
86     hr = IDirectMusic_CreatePort(dmusic, &GUID_NULL, NULL, &port, NULL);
87     ok(hr == E_INVALIDARG, "IDirectMusic_CreatePort returned: %x\n", hr);
88     hr = IDirectMusic_CreatePort(dmusic, &GUID_NULL, &port_params, NULL, NULL);
89     ok(hr == E_POINTER, "IDirectMusic_CreatePort returned: %x\n", hr);
90
91     /* Test creation of default port with GUID_NULL */
92     hr = IDirectMusic_CreatePort(dmusic, &GUID_NULL, &port_params, &port, NULL);
93     ok(hr == S_OK, "IDirectMusic_CreatePort returned: %x\n", hr);
94
95     port_caps.dwSize = sizeof(port_caps);
96     while (IDirectMusic_EnumPort(dmusic, index, &port_caps) == S_OK)
97     {
98         ok(port_caps.dwSize == sizeof(port_caps), "DMUS_PORTCAPS dwSize member is wrong (%u)\n", port_caps.dwSize);
99         trace("Port %u:\n", index);
100         trace("  dwFlags            = %x\n", port_caps.dwFlags);
101         trace("  guidPort           = %s\n", debugstr_guid(&port_caps.guidPort));
102         trace("  dwClass            = %u\n", port_caps.dwClass);
103         trace("  dwType             = %u\n", port_caps.dwType);
104         trace("  dwMemorySize       = %u\n", port_caps.dwMemorySize);
105         trace("  dwMaxChannelGroups = %u\n", port_caps.dwMaxChannelGroups);
106         trace("  dwMaxVoices        = %u\n", port_caps.dwMaxVoices);
107         trace("  dwMaxAudioChannels = %u\n", port_caps.dwMaxAudioChannels);
108         trace("  dwEffectFlags      = %x\n", port_caps.dwEffectFlags);
109         trace("  wszDescription     = %s\n", wine_dbgstr_w(port_caps.wszDescription));
110         index++;
111     }
112
113     index = 0;
114     clock_info.dwSize = sizeof(clock_info);
115     while (IDirectMusic_EnumMasterClock(dmusic, index, &clock_info) == S_OK)
116     {
117         ok(clock_info.dwSize == sizeof(clock_info), "DMUS_CLOCKINFO dwSize member is wrong (%u)\n", clock_info.dwSize);
118         trace("Clock %u:\n", index);
119         trace("  ctType         = %u\n", clock_info.ctType);
120         trace("  guidClock      = %s\n", debugstr_guid(&clock_info.guidClock));
121         trace("  wszDescription = %s\n", wine_dbgstr_w(clock_info.wszDescription));
122         index++;
123     }
124
125     if (port)
126         IDirectMusicPort_Release(port);
127     IDirectMusic_Release(dmusic);
128 }
129
130 static void test_dmbuffer(void)
131 {
132     IDirectMusic *dmusic;
133     IDirectMusicBuffer *dmbuffer = NULL;
134     HRESULT hr;
135     DMUS_BUFFERDESC desc;
136     GUID format;
137     DWORD size;
138     DWORD bytes;
139     REFERENCE_TIME time;
140     LPBYTE data;
141
142     hr = CoCreateInstance(&CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusic, (LPVOID*)&dmusic);
143     if (hr != S_OK)
144     {
145         skip("Cannot create DirectMusic object (%x)\n", hr);
146         return;
147     }
148
149     desc.dwSize = sizeof(DMUS_BUFFERDESC);
150     desc.dwFlags = 0;
151     desc.cbBuffer = 1023;
152     memcpy(&desc.guidBufferFormat, &GUID_NULL, sizeof(GUID));
153
154     hr = IDirectMusic_CreateMusicBuffer(dmusic, &desc, &dmbuffer, NULL);
155     ok(hr == S_OK, "IDirectMusic_CreateMusicBuffer return %x\n", hr);
156
157     hr = IDirectMusicBuffer_GetBufferFormat(dmbuffer, &format);
158     ok(hr == S_OK, "IDirectMusicBuffer_GetBufferFormat returned %x\n", hr);
159     ok(IsEqualGUID(&format, &KSDATAFORMAT_SUBTYPE_MIDI), "Wrong format returned %s\n", debugstr_guid(&format));
160     hr = IDirectMusicBuffer_GetMaxBytes(dmbuffer, &size);
161     ok(hr == S_OK, "IDirectMusicBuffer_GetMaxBytes returned %x\n", hr);
162     ok(size == 1024, "Buffer size is %u instead of 1024\n", size);
163
164     hr = IDirectMusicBuffer_GetStartTime(dmbuffer, &time);
165     ok(hr == DMUS_E_BUFFER_EMPTY, "IDirectMusicBuffer_GetStartTime returned %x\n", hr);
166     hr = IDirectMusicBuffer_SetStartTime(dmbuffer, 10);
167     ok(hr == S_OK, "IDirectMusicBuffer_GetStartTime returned %x\n", hr);
168     hr = IDirectMusicBuffer_GetStartTime(dmbuffer, &time);
169     ok(hr == DMUS_E_BUFFER_EMPTY, "IDirectMusicBuffer_GetStartTime returned %x\n", hr);
170
171     hr = IDirectMusicBuffer_PackStructured(dmbuffer, 20, 0, 0);
172     ok(hr == DMUS_E_INVALID_EVENT, "IDirectMusicBuffer_PackStructured returned %x\n", hr);
173     hr = IDirectMusicBuffer_PackStructured(dmbuffer, 20, 0, 0x000090); /* note on : chan 0, note 0 & vel 0 */
174     ok(hr == S_OK, "IDirectMusicBuffer_PackStructured returned %x\n", hr);
175     hr = IDirectMusicBuffer_PackStructured(dmbuffer, 30, 0, 0x000080); /* note off : chan 0, note 0 & vel 0 */
176     ok(hr == S_OK, "IDirectMusicBuffer_PackStructured returned %x\n", hr);
177     hr = IDirectMusicBuffer_GetUsedBytes(dmbuffer, &bytes);
178     ok(hr == S_OK, "IDirectMusicBuffer_GetUsedBytes returned %x\n", hr);
179     ok(bytes == 48, "Buffer size is %u instead of 48\n", bytes);
180
181     hr = IDirectMusicBuffer_GetStartTime(dmbuffer, &time);
182     ok(hr == S_OK, "IDirectMusicBuffer_GetStartTime returned %x\n", hr);
183     ok(time == 20, "Buffer start time is wrong\n");
184     hr = IDirectMusicBuffer_SetStartTime(dmbuffer, 40);
185     ok(hr == S_OK, "IDirectMusicBuffer_GetStartTime returned %x\n", hr);
186     hr = IDirectMusicBuffer_GetStartTime(dmbuffer, &time);
187     ok(hr == S_OK, "IDirectMusicBuffer_GetStartTime returned %x\n", hr);
188     ok(time == 40, "Buffer start time is wrong\n");
189
190     hr = IDirectMusicBuffer_GetRawBufferPtr(dmbuffer, &data);
191     ok(hr == S_OK, "IDirectMusicBuffer_GetRawBufferPtr returned %x\n", hr);
192     if (hr == S_OK)
193     {
194         DMUS_EVENTHEADER* header;
195         DWORD message;
196
197         /* Check message 1 */
198         header = (DMUS_EVENTHEADER*)data;
199         data += sizeof(DMUS_EVENTHEADER);
200         ok(header->cbEvent == 3, "cbEvent is %u instead of 3\n", header->cbEvent);
201         ok(header->dwChannelGroup == 0, "dwChannelGroup is %u instead of 0\n", header->dwChannelGroup);
202         ok(header->rtDelta == 0, "rtDelta is %s instead of 0\n", debugstr_longlong(header->rtDelta));
203         ok(header->dwFlags == DMUS_EVENT_STRUCTURED, "dwFlags is %x instead of %x\n", header->dwFlags, DMUS_EVENT_STRUCTURED);
204         message = *(DWORD*)data & 0xffffff; /* Only 3 bytes are relevant */
205         data += sizeof(DWORD);
206         ok(message == 0x000090, "Message is %0x instead of 0x000090\n", message);
207
208         /* Check message 2 */
209         header = (DMUS_EVENTHEADER*)data;
210         data += sizeof(DMUS_EVENTHEADER);
211         ok(header->cbEvent == 3, "cbEvent is %u instead of 3\n", header->cbEvent);
212         ok(header->dwChannelGroup == 0, "dwChannelGroup is %u instead of 0\n", header->dwChannelGroup);
213         ok(header->rtDelta == 10, "rtDelta is %s instead of 0\n", debugstr_longlong(header->rtDelta));
214         ok(header->dwFlags == DMUS_EVENT_STRUCTURED, "dwFlags is %x instead of %x\n", header->dwFlags, DMUS_EVENT_STRUCTURED);
215         message = *(DWORD*)data & 0xffffff; /* Only 3 bytes are relevant */
216         ok(message == 0x000080, "Message 2 is %0x instead of 0x000080\n", message);
217     }
218
219     if (dmbuffer)
220         IDirectMusicBuffer_Release(dmbuffer);
221     IDirectMusic_Release(dmusic);
222 }
223
224 START_TEST(dmusic)
225 {
226     CoInitializeEx(NULL, COINIT_MULTITHREADED);
227
228     test_dmusic();
229     test_dmbuffer();
230
231     CoUninitialize();
232 }