1 /*
2 * IDirectMusicInstrument Implementation
3 *
4 * Copyright (C) 2003-2004 Rok Mandeljc
5 *
6 * This program 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 program 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 program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "dmusic_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
24
25 static const GUID IID_IDirectMusicInstrumentPRIVATE = { 0xbcb20080, 0xa40c, 0x11d1, { 0x86, 0xbc, 0x00, 0xc0, 0x4f, 0xbf, 0x8f, 0xef } };
26
27 /* IDirectMusicInstrument IUnknown part: */
IDirectMusicInstrumentImpl_QueryInterface(LPDIRECTMUSICINSTRUMENT iface,REFIID riid,LPVOID * ret_iface)28 static HRESULT WINAPI IDirectMusicInstrumentImpl_QueryInterface(LPDIRECTMUSICINSTRUMENT iface, REFIID riid, LPVOID *ret_iface)
29 {
30 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
31
32 if (IsEqualIID(riid, &IID_IUnknown) ||
33 IsEqualIID(riid, &IID_IDirectMusicInstrument))
34 {
35 *ret_iface = iface;
36 IDirectMusicInstrument_AddRef(iface);
37 return S_OK;
38 }
39 else if (IsEqualIID(riid, &IID_IDirectMusicInstrumentPRIVATE))
40 {
41 /* it seems to me that this interface is only basic IUnknown, without any
42 * other inherited functions... *sigh* this is the worst scenario, since it means
43 * that whoever calls it knows the layout of original implementation table and therefore
44 * tries to get data by direct access... expect crashes
45 */
46 FIXME("*sigh*... requested private/unspecified interface\n");
47
48 *ret_iface = iface;
49 IDirectMusicInstrument_AddRef(iface);
50 return S_OK;
51 }
52
53 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
54
55 return E_NOINTERFACE;
56 }
57
IDirectMusicInstrumentImpl_AddRef(LPDIRECTMUSICINSTRUMENT iface)58 static ULONG WINAPI IDirectMusicInstrumentImpl_AddRef(LPDIRECTMUSICINSTRUMENT iface)
59 {
60 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
61 ULONG ref = InterlockedIncrement(&This->ref);
62
63 TRACE("(%p)->(): new ref = %u\n", iface, ref);
64
65 return ref;
66 }
67
IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)68 static ULONG WINAPI IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)
69 {
70 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
71 ULONG ref = InterlockedDecrement(&This->ref);
72
73 TRACE("(%p)->(): new ref = %u\n", iface, ref);
74
75 if (!ref)
76 {
77 ULONG i;
78
79 HeapFree(GetProcessHeap(), 0, This->regions);
80 for (i = 0; i < This->nb_articulations; i++)
81 HeapFree(GetProcessHeap(), 0, This->articulations->connections);
82 HeapFree(GetProcessHeap(), 0, This->articulations);
83 HeapFree(GetProcessHeap(), 0, This);
84 DMUSIC_UnlockModule();
85 }
86
87 return ref;
88 }
89
90 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface,DWORD * pdwPatch)91 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
92 {
93 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
94
95 TRACE("(%p)->(%p)\n", This, pdwPatch);
96
97 *pdwPatch = MIDILOCALE2Patch(&This->header.Locale);
98
99 return S_OK;
100 }
101
IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface,DWORD dwPatch)102 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
103 {
104 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
105
106 TRACE("(%p)->(%d): stub\n", This, dwPatch);
107
108 Patch2MIDILOCALE(dwPatch, &This->header.Locale);
109
110 return S_OK;
111 }
112
113 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
114 {
115 IDirectMusicInstrumentImpl_QueryInterface,
116 IDirectMusicInstrumentImpl_AddRef,
117 IDirectMusicInstrumentImpl_Release,
118 IDirectMusicInstrumentImpl_GetPatch,
119 IDirectMusicInstrumentImpl_SetPatch
120 };
121
122 /* for ClassFactory */
DMUSIC_CreateDirectMusicInstrumentImpl(LPCGUID lpcGUID,LPVOID * ppobj,LPUNKNOWN pUnkOuter)123 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
124 IDirectMusicInstrumentImpl* dminst;
125 HRESULT hr;
126
127 dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
128 if (NULL == dminst) {
129 *ppobj = NULL;
130 return E_OUTOFMEMORY;
131 }
132 dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
133 dminst->ref = 1;
134
135 DMUSIC_LockModule();
136 hr = IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID,
137 ppobj);
138 IDirectMusicInstrument_Release(&dminst->IDirectMusicInstrument_iface);
139
140 return hr;
141 }
142
read_from_stream(IStream * stream,void * data,ULONG size)143 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
144 {
145 ULONG bytes_read;
146 HRESULT hr;
147
148 hr = IStream_Read(stream, data, size, &bytes_read);
149 if(FAILED(hr)){
150 TRACE("IStream_Read failed: %08x\n", hr);
151 return hr;
152 }
153 if (bytes_read < size) {
154 TRACE("Didn't read full chunk: %u < %u\n", bytes_read, size);
155 return E_FAIL;
156 }
157
158 return S_OK;
159 }
160
subtract_bytes(DWORD len,DWORD bytes)161 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
162 {
163 if(bytes > len){
164 TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
165 return 0;
166 }
167 return len - bytes;
168 }
169
advance_stream(IStream * stream,ULONG bytes)170 static inline HRESULT advance_stream(IStream *stream, ULONG bytes)
171 {
172 LARGE_INTEGER move;
173 HRESULT ret;
174
175 move.QuadPart = bytes;
176
177 ret = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
178 if (FAILED(ret))
179 WARN("IStream_Seek failed: %08x\n", ret);
180
181 return ret;
182 }
183
load_region(IDirectMusicInstrumentImpl * This,IStream * stream,instrument_region * region,ULONG length)184 static HRESULT load_region(IDirectMusicInstrumentImpl *This, IStream *stream, instrument_region *region, ULONG length)
185 {
186 HRESULT ret;
187 DMUS_PRIVATE_CHUNK chunk;
188
189 TRACE("(%p, %p, %p, %u)\n", This, stream, region, length);
190
191 while (length)
192 {
193 ret = read_from_stream(stream, &chunk, sizeof(chunk));
194 if (FAILED(ret))
195 return ret;
196
197 length = subtract_bytes(length, sizeof(chunk));
198
199 switch (chunk.fccID)
200 {
201 case FOURCC_RGNH:
202 TRACE("RGNH chunk (region header): %u bytes\n", chunk.dwSize);
203
204 ret = read_from_stream(stream, ®ion->header, sizeof(region->header));
205 if (FAILED(ret))
206 return ret;
207
208 length = subtract_bytes(length, sizeof(region->header));
209 break;
210
211 case FOURCC_WSMP:
212 TRACE("WSMP chunk (wave sample): %u bytes\n", chunk.dwSize);
213
214 ret = read_from_stream(stream, ®ion->wave_sample, sizeof(region->wave_sample));
215 if (FAILED(ret))
216 return ret;
217 length = subtract_bytes(length, sizeof(region->wave_sample));
218
219 if (!(region->loop_present = (chunk.dwSize != sizeof(region->wave_sample))))
220 break;
221
222 ret = read_from_stream(stream, ®ion->wave_loop, sizeof(region->wave_loop));
223 if (FAILED(ret))
224 return ret;
225
226 length = subtract_bytes(length, sizeof(region->wave_loop));
227 break;
228
229 case FOURCC_WLNK:
230 TRACE("WLNK chunk (wave link): %u bytes\n", chunk.dwSize);
231
232 ret = read_from_stream(stream, ®ion->wave_link, sizeof(region->wave_link));
233 if (FAILED(ret))
234 return ret;
235
236 length = subtract_bytes(length, sizeof(region->wave_link));
237 break;
238
239 default:
240 TRACE("Unknown chunk %s (skipping): %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
241
242 ret = advance_stream(stream, chunk.dwSize);
243 if (FAILED(ret))
244 return ret;
245
246 length = subtract_bytes(length, chunk.dwSize);
247 break;
248 }
249 }
250
251 return S_OK;
252 }
253
load_articulation(IDirectMusicInstrumentImpl * This,IStream * stream,ULONG length)254 static HRESULT load_articulation(IDirectMusicInstrumentImpl *This, IStream *stream, ULONG length)
255 {
256 HRESULT ret;
257 instrument_articulation *articulation;
258
259 if (!This->articulations)
260 This->articulations = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->articulations));
261 else
262 This->articulations = HeapReAlloc(GetProcessHeap(), 0, This->articulations, sizeof(*This->articulations) * (This->nb_articulations + 1));
263 if (!This->articulations)
264 return E_OUTOFMEMORY;
265
266 articulation = &This->articulations[This->nb_articulations];
267
268 ret = read_from_stream(stream, &articulation->connections_list, sizeof(CONNECTIONLIST));
269 if (FAILED(ret))
270 return ret;
271
272 articulation->connections = HeapAlloc(GetProcessHeap(), 0, sizeof(CONNECTION) * articulation->connections_list.cConnections);
273 if (!articulation->connections)
274 return E_OUTOFMEMORY;
275
276 ret = read_from_stream(stream, articulation->connections, sizeof(CONNECTION) * articulation->connections_list.cConnections);
277 if (FAILED(ret))
278 {
279 HeapFree(GetProcessHeap(), 0, articulation->connections);
280 return ret;
281 }
282
283 subtract_bytes(length, sizeof(CONNECTIONLIST) + sizeof(CONNECTION) * articulation->connections_list.cConnections);
284
285 This->nb_articulations++;
286
287 return S_OK;
288 }
289
290 /* Function that loads all instrument data and which is called from IDirectMusicCollection_GetInstrument as in native */
IDirectMusicInstrumentImpl_CustomLoad(IDirectMusicInstrument * iface,IStream * stream)291 HRESULT IDirectMusicInstrumentImpl_CustomLoad(IDirectMusicInstrument *iface, IStream *stream)
292 {
293 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
294 HRESULT hr;
295 DMUS_PRIVATE_CHUNK chunk;
296 ULONG i = 0;
297 ULONG length = This->length;
298
299 TRACE("(%p, %p): offset = 0x%s, length = %u)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart), This->length);
300
301 if (This->loaded)
302 return S_OK;
303
304 hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
305 if (FAILED(hr))
306 {
307 WARN("IStream_Seek failed: %08x\n", hr);
308 return DMUS_E_UNSUPPORTED_STREAM;
309 }
310
311 This->regions = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->regions) * This->header.cRegions);
312 if (!This->regions)
313 return E_OUTOFMEMORY;
314
315 while (length)
316 {
317 hr = read_from_stream(stream, &chunk, sizeof(chunk));
318 if (FAILED(hr))
319 goto error;
320
321 length = subtract_bytes(length, sizeof(chunk) + chunk.dwSize);
322
323 switch (chunk.fccID)
324 {
325 case FOURCC_INSH:
326 case FOURCC_DLID:
327 TRACE("Chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
328
329 /* Instrument header and id are already set so just skip */
330 hr = advance_stream(stream, chunk.dwSize);
331 if (FAILED(hr))
332 goto error;
333
334 break;
335
336 case FOURCC_LIST: {
337 DWORD size = chunk.dwSize;
338
339 TRACE("LIST chunk: %u bytes\n", chunk.dwSize);
340
341 hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
342 if (FAILED(hr))
343 goto error;
344
345 size = subtract_bytes(size, sizeof(chunk.fccID));
346
347 switch (chunk.fccID)
348 {
349 case FOURCC_LRGN:
350 TRACE("LRGN chunk (regions list): %u bytes\n", size);
351
352 while (size)
353 {
354 hr = read_from_stream(stream, &chunk, sizeof(chunk));
355 if (FAILED(hr))
356 goto error;
357
358 if (chunk.fccID != FOURCC_LIST)
359 {
360 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
361 goto error;
362 }
363
364 hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
365 if (FAILED(hr))
366 goto error;
367
368 if (chunk.fccID == FOURCC_RGN)
369 {
370 TRACE("RGN chunk (region): %u bytes\n", chunk.dwSize);
371 hr = load_region(This, stream, &This->regions[i++], chunk.dwSize - sizeof(chunk.fccID));
372 }
373 else
374 {
375 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
376 hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
377 }
378 if (FAILED(hr))
379 goto error;
380
381 size = subtract_bytes(size, chunk.dwSize + sizeof(chunk));
382 }
383 break;
384
385 case FOURCC_LART:
386 TRACE("LART chunk (articulations list): %u bytes\n", size);
387
388 while (size)
389 {
390 hr = read_from_stream(stream, &chunk, sizeof(chunk));
391 if (FAILED(hr))
392 goto error;
393
394 if (chunk.fccID == FOURCC_ART1)
395 {
396 TRACE("ART1 chunk (level 1 articulation): %u bytes\n", chunk.dwSize);
397 hr = load_articulation(This, stream, chunk.dwSize);
398 }
399 else
400 {
401 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
402 hr = advance_stream(stream, chunk.dwSize);
403 }
404 if (FAILED(hr))
405 goto error;
406
407 size = subtract_bytes(size, chunk.dwSize + sizeof(chunk));
408 }
409 break;
410
411 default:
412 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
413
414 hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
415 if (FAILED(hr))
416 goto error;
417
418 size = subtract_bytes(size, chunk.dwSize - sizeof(chunk.fccID));
419 break;
420 }
421 break;
422 }
423
424 default:
425 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
426
427 hr = advance_stream(stream, chunk.dwSize);
428 if (FAILED(hr))
429 goto error;
430
431 break;
432 }
433 }
434
435 This->loaded = TRUE;
436
437 return S_OK;
438
439 error:
440 HeapFree(GetProcessHeap(), 0, This->regions);
441 This->regions = NULL;
442
443 return DMUS_E_UNSUPPORTED_STREAM;
444 }
445