[CABMAN]
[reactos.git] / reactos / dll / directx / dmusic / instrument.c
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: */
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
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 DMUSIC_LockModule();
66
67 return ref;
68 }
69
70 static ULONG WINAPI IDirectMusicInstrumentImpl_Release(LPDIRECTMUSICINSTRUMENT iface)
71 {
72 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
73 ULONG ref = InterlockedDecrement(&This->ref);
74
75 TRACE("(%p)->(): new ref = %u\n", iface, ref);
76
77 if (!ref)
78 {
79 ULONG i;
80
81 HeapFree(GetProcessHeap(), 0, This->regions);
82 for (i = 0; i < This->nb_articulations; i++)
83 HeapFree(GetProcessHeap(), 0, This->articulations->connections);
84 HeapFree(GetProcessHeap(), 0, This->articulations);
85 HeapFree(GetProcessHeap(), 0, This);
86 }
87
88 DMUSIC_UnlockModule();
89
90 return ref;
91 }
92
93 /* IDirectMusicInstrumentImpl IDirectMusicInstrument part: */
94 static HRESULT WINAPI IDirectMusicInstrumentImpl_GetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD* pdwPatch)
95 {
96 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
97
98 TRACE("(%p)->(%p)\n", This, pdwPatch);
99
100 *pdwPatch = MIDILOCALE2Patch(&This->header.Locale);
101
102 return S_OK;
103 }
104
105 static HRESULT WINAPI IDirectMusicInstrumentImpl_SetPatch(LPDIRECTMUSICINSTRUMENT iface, DWORD dwPatch)
106 {
107 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
108
109 TRACE("(%p)->(%d): stub\n", This, dwPatch);
110
111 Patch2MIDILOCALE(dwPatch, &This->header.Locale);
112
113 return S_OK;
114 }
115
116 static const IDirectMusicInstrumentVtbl DirectMusicInstrument_Vtbl =
117 {
118 IDirectMusicInstrumentImpl_QueryInterface,
119 IDirectMusicInstrumentImpl_AddRef,
120 IDirectMusicInstrumentImpl_Release,
121 IDirectMusicInstrumentImpl_GetPatch,
122 IDirectMusicInstrumentImpl_SetPatch
123 };
124
125 /* for ClassFactory */
126 HRESULT DMUSIC_CreateDirectMusicInstrumentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
127 IDirectMusicInstrumentImpl* dminst;
128
129 dminst = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicInstrumentImpl));
130 if (NULL == dminst) {
131 *ppobj = NULL;
132 return E_OUTOFMEMORY;
133 }
134 dminst->IDirectMusicInstrument_iface.lpVtbl = &DirectMusicInstrument_Vtbl;
135 dminst->ref = 0; /* will be inited by QueryInterface */
136
137 return IDirectMusicInstrument_QueryInterface(&dminst->IDirectMusicInstrument_iface, lpcGUID, ppobj);
138 }
139
140 static HRESULT read_from_stream(IStream *stream, void *data, ULONG size)
141 {
142 ULONG bytes_read;
143 HRESULT hr;
144
145 hr = IStream_Read(stream, data, size, &bytes_read);
146 if(FAILED(hr)){
147 TRACE("IStream_Read failed: %08x\n", hr);
148 return hr;
149 }
150 if (bytes_read < size) {
151 TRACE("Didn't read full chunk: %u < %u\n", bytes_read, size);
152 return E_FAIL;
153 }
154
155 return S_OK;
156 }
157
158 static inline DWORD subtract_bytes(DWORD len, DWORD bytes)
159 {
160 if(bytes > len){
161 TRACE("Apparent mismatch in chunk lengths? %u bytes remaining, %u bytes read\n", len, bytes);
162 return 0;
163 }
164 return len - bytes;
165 }
166
167 static inline HRESULT advance_stream(IStream *stream, ULONG bytes)
168 {
169 LARGE_INTEGER move;
170 HRESULT ret;
171
172 move.QuadPart = bytes;
173
174 ret = IStream_Seek(stream, move, STREAM_SEEK_CUR, NULL);
175 if (FAILED(ret))
176 WARN("IStream_Seek failed: %08x\n", ret);
177
178 return ret;
179 }
180
181 static HRESULT load_region(IDirectMusicInstrumentImpl *This, IStream *stream, instrument_region *region, ULONG length)
182 {
183 HRESULT ret;
184 DMUS_PRIVATE_CHUNK chunk;
185
186 TRACE("(%p, %p, %p, %u)\n", This, stream, region, length);
187
188 while (length)
189 {
190 ret = read_from_stream(stream, &chunk, sizeof(chunk));
191 if (FAILED(ret))
192 return ret;
193
194 length = subtract_bytes(length, sizeof(chunk));
195
196 switch (chunk.fccID)
197 {
198 case FOURCC_RGNH:
199 TRACE("RGNH chunk (region header): %u bytes\n", chunk.dwSize);
200
201 ret = read_from_stream(stream, &region->header, sizeof(region->header));
202 if (FAILED(ret))
203 return ret;
204
205 length = subtract_bytes(length, sizeof(region->header));
206 break;
207
208 case FOURCC_WSMP:
209 TRACE("WSMP chunk (wave sample): %u bytes\n", chunk.dwSize);
210
211 ret = read_from_stream(stream, &region->wave_sample, sizeof(region->wave_sample));
212 if (FAILED(ret))
213 return ret;
214 length = subtract_bytes(length, sizeof(region->wave_sample));
215
216 if (!(region->loop_present = (chunk.dwSize != sizeof(region->wave_sample))))
217 break;
218
219 ret = read_from_stream(stream, &region->wave_loop, sizeof(region->wave_loop));
220 if (FAILED(ret))
221 return ret;
222
223 length = subtract_bytes(length, sizeof(region->wave_loop));
224 break;
225
226 case FOURCC_WLNK:
227 TRACE("WLNK chunk (wave link): %u bytes\n", chunk.dwSize);
228
229 ret = read_from_stream(stream, &region->wave_link, sizeof(region->wave_link));
230 if (FAILED(ret))
231 return ret;
232
233 length = subtract_bytes(length, sizeof(region->wave_link));
234 break;
235
236 default:
237 TRACE("Unknown chunk %s (skipping): %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
238
239 ret = advance_stream(stream, chunk.dwSize);
240 if (FAILED(ret))
241 return ret;
242
243 length = subtract_bytes(length, chunk.dwSize);
244 break;
245 }
246 }
247
248 return S_OK;
249 }
250
251 static HRESULT load_articulation(IDirectMusicInstrumentImpl *This, IStream *stream, ULONG length)
252 {
253 HRESULT ret;
254 instrument_articulation *articulation;
255
256 if (!This->articulations)
257 This->articulations = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->articulations));
258 else
259 This->articulations = HeapReAlloc(GetProcessHeap(), 0, This->articulations, sizeof(*This->articulations) * (This->nb_articulations + 1));
260 if (!This->articulations)
261 return E_OUTOFMEMORY;
262
263 articulation = &This->articulations[This->nb_articulations];
264
265 ret = read_from_stream(stream, &articulation->connections_list, sizeof(CONNECTIONLIST));
266 if (FAILED(ret))
267 return ret;
268
269 articulation->connections = HeapAlloc(GetProcessHeap(), 0, sizeof(CONNECTION) * articulation->connections_list.cConnections);
270 if (!articulation->connections)
271 return E_OUTOFMEMORY;
272
273 ret = read_from_stream(stream, articulation->connections, sizeof(CONNECTION) * articulation->connections_list.cConnections);
274 if (FAILED(ret))
275 {
276 HeapFree(GetProcessHeap(), 0, articulation->connections);
277 return ret;
278 }
279
280 subtract_bytes(length, sizeof(CONNECTIONLIST) + sizeof(CONNECTION) * articulation->connections_list.cConnections);
281
282 This->nb_articulations++;
283
284 return S_OK;
285 }
286
287 /* Function that loads all instrument data and which is called from IDirectMusicCollection_GetInstrument as in native */
288 HRESULT IDirectMusicInstrumentImpl_CustomLoad(IDirectMusicInstrument *iface, IStream *stream)
289 {
290 IDirectMusicInstrumentImpl *This = impl_from_IDirectMusicInstrument(iface);
291 HRESULT hr;
292 DMUS_PRIVATE_CHUNK chunk;
293 ULONG i = 0;
294 ULONG length = This->length;
295
296 TRACE("(%p, %p): offset = 0x%s, length = %u)\n", This, stream, wine_dbgstr_longlong(This->liInstrumentPosition.QuadPart), This->length);
297
298 if (This->loaded)
299 return S_OK;
300
301 hr = IStream_Seek(stream, This->liInstrumentPosition, STREAM_SEEK_SET, NULL);
302 if (FAILED(hr))
303 {
304 WARN("IStream_Seek failed: %08x\n", hr);
305 return DMUS_E_UNSUPPORTED_STREAM;
306 }
307
308 This->regions = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->regions) * This->header.cRegions);
309 if (!This->regions)
310 return E_OUTOFMEMORY;
311
312 while (length)
313 {
314 hr = read_from_stream(stream, &chunk, sizeof(chunk));
315 if (FAILED(hr))
316 goto error;
317
318 length = subtract_bytes(length, sizeof(chunk) + chunk.dwSize);
319
320 switch (chunk.fccID)
321 {
322 case FOURCC_INSH:
323 case FOURCC_DLID:
324 TRACE("Chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
325
326 /* Instrument header and id are already set so just skip */
327 hr = advance_stream(stream, chunk.dwSize);
328 if (FAILED(hr))
329 goto error;
330
331 break;
332
333 case FOURCC_LIST: {
334 DWORD size = chunk.dwSize;
335
336 TRACE("LIST chunk: %u bytes\n", chunk.dwSize);
337
338 hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
339 if (FAILED(hr))
340 goto error;
341
342 size = subtract_bytes(size, sizeof(chunk.fccID));
343
344 switch (chunk.fccID)
345 {
346 case FOURCC_LRGN:
347 TRACE("LRGN chunk (regions list): %u bytes\n", size);
348
349 while (size)
350 {
351 hr = read_from_stream(stream, &chunk, sizeof(chunk));
352 if (FAILED(hr))
353 goto error;
354
355 if (chunk.fccID != FOURCC_LIST)
356 {
357 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
358 goto error;
359 }
360
361 hr = read_from_stream(stream, &chunk.fccID, sizeof(chunk.fccID));
362 if (FAILED(hr))
363 goto error;
364
365 if (chunk.fccID == FOURCC_RGN)
366 {
367 TRACE("RGN chunk (region): %u bytes\n", chunk.dwSize);
368 hr = load_region(This, stream, &This->regions[i++], chunk.dwSize - sizeof(chunk.fccID));
369 }
370 else
371 {
372 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
373 hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
374 }
375 if (FAILED(hr))
376 goto error;
377
378 size = subtract_bytes(size, chunk.dwSize + sizeof(chunk));
379 }
380 break;
381
382 case FOURCC_LART:
383 TRACE("LART chunk (articulations list): %u bytes\n", size);
384
385 while (size)
386 {
387 hr = read_from_stream(stream, &chunk, sizeof(chunk));
388 if (FAILED(hr))
389 goto error;
390
391 if (chunk.fccID == FOURCC_ART1)
392 {
393 TRACE("ART1 chunk (level 1 articulation): %u bytes\n", chunk.dwSize);
394 hr = load_articulation(This, stream, chunk.dwSize);
395 }
396 else
397 {
398 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
399 hr = advance_stream(stream, chunk.dwSize);
400 }
401 if (FAILED(hr))
402 goto error;
403
404 size = subtract_bytes(size, chunk.dwSize + sizeof(chunk));
405 }
406 break;
407
408 default:
409 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
410
411 hr = advance_stream(stream, chunk.dwSize - sizeof(chunk.fccID));
412 if (FAILED(hr))
413 goto error;
414
415 size = subtract_bytes(size, chunk.dwSize - sizeof(chunk.fccID));
416 break;
417 }
418 break;
419 }
420
421 default:
422 TRACE("Unknown chunk %s: %u bytes\n", debugstr_fourcc(chunk.fccID), chunk.dwSize);
423
424 hr = advance_stream(stream, chunk.dwSize);
425 if (FAILED(hr))
426 goto error;
427
428 break;
429 }
430 }
431
432 This->loaded = TRUE;
433
434 return S_OK;
435
436 error:
437 HeapFree(GetProcessHeap(), 0, This->regions);
438 This->regions = NULL;
439
440 return DMUS_E_UNSUPPORTED_STREAM;
441 }