Release 1.7.13.
[wine/multimedia.git] / dlls / dmband / band.c
blob97e7c300cd2d2a419fc8686150c34d8f830aa268
1 /* IDirectMusicBand Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "dmband_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(dmband);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
26 /*****************************************************************************
27 * IDirectMusicBandImpl implementation
29 static inline IDirectMusicBandImpl *impl_from_IDirectMusicBand(IDirectMusicBand *iface)
31 return CONTAINING_RECORD(iface, IDirectMusicBandImpl, IDirectMusicBand_iface);
34 /* DirectMusicBandImpl IDirectMusicBand part: */
35 static HRESULT WINAPI IDirectMusicBandImpl_QueryInterface(IDirectMusicBand *iface, REFIID riid,
36 void **ret_iface)
38 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
40 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
42 *ret_iface = NULL;
44 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicBand))
45 *ret_iface = iface;
46 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
47 *ret_iface = &This->ObjectVtbl;
48 else if (IsEqualIID(riid, &IID_IPersistStream))
49 *ret_iface = &This->PersistStreamVtbl;
50 else {
51 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
52 return E_NOINTERFACE;
55 IDirectMusicBand_AddRef((IUnknown*)*ret_iface);
56 return S_OK;
59 static ULONG WINAPI IDirectMusicBandImpl_AddRef(IDirectMusicBand *iface)
61 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
62 LONG ref = InterlockedIncrement(&This->ref);
64 TRACE("(%p) ref=%d\n", This, ref);
66 return ref;
69 static ULONG WINAPI IDirectMusicBandImpl_Release(IDirectMusicBand *iface)
71 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
72 LONG ref = InterlockedDecrement(&This->ref);
74 TRACE("(%p) ref=%d\n", This, ref);
76 if (!ref) {
77 HeapFree(GetProcessHeap(), 0, This->pDesc);
78 HeapFree(GetProcessHeap(), 0, This);
79 DMBAND_UnlockModule();
82 return ref;
85 static HRESULT WINAPI IDirectMusicBandImpl_CreateSegment(IDirectMusicBand *iface,
86 IDirectMusicSegment **ppSegment)
88 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
89 FIXME("(%p, %p): stub\n", This, ppSegment);
90 return S_OK;
93 static HRESULT WINAPI IDirectMusicBandImpl_Download(IDirectMusicBand *iface,
94 IDirectMusicPerformance *pPerformance)
96 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
97 FIXME("(%p, %p): stub\n", This, pPerformance);
98 return S_OK;
101 static HRESULT WINAPI IDirectMusicBandImpl_Unload(IDirectMusicBand *iface,
102 IDirectMusicPerformance *pPerformance)
104 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
105 FIXME("(%p, %p): stub\n", This, pPerformance);
106 return S_OK;
109 static const IDirectMusicBandVtbl dmband_vtbl = {
110 IDirectMusicBandImpl_QueryInterface,
111 IDirectMusicBandImpl_AddRef,
112 IDirectMusicBandImpl_Release,
113 IDirectMusicBandImpl_CreateSegment,
114 IDirectMusicBandImpl_Download,
115 IDirectMusicBandImpl_Unload
118 /* IDirectMusicBandImpl IDirectMusicObject part: */
119 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
120 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
121 return IDirectMusicBand_QueryInterface(&This->IDirectMusicBand_iface, riid, ppobj);
124 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
125 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
126 return IDirectMusicBand_AddRef(&This->IDirectMusicBand_iface);
129 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
130 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
131 return IDirectMusicBand_Release(&This->IDirectMusicBand_iface);
134 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
135 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
136 TRACE("(%p, %p)\n", This, pDesc);
137 /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
138 memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
139 return S_OK;
142 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
143 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
144 TRACE("(%p, %p): setting descriptor:\n", This, pDesc); debug_DMUS_OBJECTDESC (pDesc);
146 /* According to MSDN, we should copy only given values, not whole struct */
147 if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
148 This->pDesc->guidObject = pDesc->guidObject;
149 if (pDesc->dwValidData & DMUS_OBJ_CLASS)
150 This->pDesc->guidClass = pDesc->guidClass;
151 if (pDesc->dwValidData & DMUS_OBJ_NAME)
152 lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
153 if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
154 lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
155 if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
156 lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
157 if (pDesc->dwValidData & DMUS_OBJ_VERSION)
158 This->pDesc->vVersion = pDesc->vVersion;
159 if (pDesc->dwValidData & DMUS_OBJ_DATE)
160 This->pDesc->ftDate = pDesc->ftDate;
161 if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
162 This->pDesc->llMemLength = pDesc->llMemLength;
163 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, pDesc->llMemLength);
165 if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
166 /* according to MSDN, we copy the stream */
167 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);
170 /* add new flags */
171 This->pDesc->dwValidData |= pDesc->dwValidData;
173 return S_OK;
176 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
177 DMUS_PRIVATE_CHUNK Chunk;
178 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
179 LARGE_INTEGER liMove; /* used when skipping chunks */
181 TRACE("(%p, %p)\n", pStream, pDesc);
183 /* FIXME: should this be determined from stream? */
184 pDesc->dwValidData |= DMUS_OBJ_CLASS;
185 pDesc->guidClass = CLSID_DirectMusicBand;
187 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
188 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
189 switch (Chunk.fccID) {
190 case FOURCC_RIFF: {
191 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
192 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
193 StreamSize = Chunk.dwSize - sizeof(FOURCC);
194 StreamCount = 0;
195 if (Chunk.fccID == DMUS_FOURCC_BAND_FORM) {
196 TRACE_(dmfile)(": band form\n");
197 do {
198 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
199 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
200 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
201 switch (Chunk.fccID) {
202 case DMUS_FOURCC_GUID_CHUNK: {
203 TRACE_(dmfile)(": GUID chunk\n");
204 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
205 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
206 break;
208 case DMUS_FOURCC_VERSION_CHUNK: {
209 TRACE_(dmfile)(": version chunk\n");
210 pDesc->dwValidData |= DMUS_OBJ_VERSION;
211 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
212 break;
214 case DMUS_FOURCC_CATEGORY_CHUNK: {
215 TRACE_(dmfile)(": category chunk\n");
216 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
217 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
218 break;
220 case FOURCC_LIST: {
221 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
222 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
223 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
224 ListCount[0] = 0;
225 switch (Chunk.fccID) {
226 /* evil M$ UNFO list, which can (!?) contain INFO elements */
227 case DMUS_FOURCC_UNFO_LIST: {
228 TRACE_(dmfile)(": UNFO list\n");
229 do {
230 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
231 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
232 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
233 switch (Chunk.fccID) {
234 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
235 (though strings seem to be valid unicode) */
236 case mmioFOURCC('I','N','A','M'):
237 case DMUS_FOURCC_UNAM_CHUNK: {
238 TRACE_(dmfile)(": name chunk\n");
239 pDesc->dwValidData |= DMUS_OBJ_NAME;
240 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
241 break;
243 case mmioFOURCC('I','A','R','T'):
244 case DMUS_FOURCC_UART_CHUNK: {
245 TRACE_(dmfile)(": artist chunk (ignored)\n");
246 liMove.QuadPart = Chunk.dwSize;
247 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
248 break;
250 case mmioFOURCC('I','C','O','P'):
251 case DMUS_FOURCC_UCOP_CHUNK: {
252 TRACE_(dmfile)(": copyright chunk (ignored)\n");
253 liMove.QuadPart = Chunk.dwSize;
254 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
255 break;
257 case mmioFOURCC('I','S','B','J'):
258 case DMUS_FOURCC_USBJ_CHUNK: {
259 TRACE_(dmfile)(": subject chunk (ignored)\n");
260 liMove.QuadPart = Chunk.dwSize;
261 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
262 break;
264 case mmioFOURCC('I','C','M','T'):
265 case DMUS_FOURCC_UCMT_CHUNK: {
266 TRACE_(dmfile)(": comment chunk (ignored)\n");
267 liMove.QuadPart = Chunk.dwSize;
268 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
269 break;
271 default: {
272 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
273 liMove.QuadPart = Chunk.dwSize;
274 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
275 break;
278 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
279 } while (ListCount[0] < ListSize[0]);
280 break;
282 default: {
283 TRACE_(dmfile)(": unknown (skipping)\n");
284 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
285 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
286 break;
289 break;
291 default: {
292 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
293 liMove.QuadPart = Chunk.dwSize;
294 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
295 break;
298 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
299 } while (StreamCount < StreamSize);
300 } else {
301 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
302 liMove.QuadPart = StreamSize;
303 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
304 return E_FAIL;
307 TRACE_(dmfile)(": reading finished\n");
308 break;
310 default: {
311 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
312 liMove.QuadPart = Chunk.dwSize;
313 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
314 return DMUS_E_INVALIDFILE;
318 TRACE(": returning descriptor:\n"); debug_DMUS_OBJECTDESC (pDesc);
320 return S_OK;
323 static const IDirectMusicObjectVtbl DirectMusicBand_Object_Vtbl = {
324 IDirectMusicBandImpl_IDirectMusicObject_QueryInterface,
325 IDirectMusicBandImpl_IDirectMusicObject_AddRef,
326 IDirectMusicBandImpl_IDirectMusicObject_Release,
327 IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor,
328 IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor,
329 IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor
332 /* IDirectMusicBandImpl IPersistStream part: */
333 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
334 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
335 return IDirectMusicBand_QueryInterface(&This->IDirectMusicBand_iface, riid, ppobj);
338 static ULONG WINAPI IDirectMusicBandImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
339 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
340 return IDirectMusicBand_AddRef(&This->IDirectMusicBand_iface);
343 static ULONG WINAPI IDirectMusicBandImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
344 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
345 return IDirectMusicBand_Release(&This->IDirectMusicBand_iface);
348 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
349 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
350 TRACE("(%p, %p)\n", This, pClassID);
351 *pClassID = CLSID_DirectMusicBand;
352 return S_OK;
355 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
356 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
357 FIXME("(%p): stub, always S_FALSE\n", This);
358 return S_FALSE;
361 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrument (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
362 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
363 DMUS_PRIVATE_CHUNK Chunk;
364 DWORD ListSize[3], ListCount[3];
365 LARGE_INTEGER liMove; /* used when skipping chunks */
366 HRESULT hr;
368 DMUS_IO_INSTRUMENT inst;
369 LPDMUS_PRIVATE_INSTRUMENT pNewInstrument;
370 IDirectMusicObject* pObject = NULL;
372 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENT_LIST) {
373 ERR_(dmfile)(": %s chunk should be an INSTRUMENT list\n", debugstr_fourcc (pChunk->fccID));
374 return E_FAIL;
377 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
378 ListCount[0] = 0;
380 do {
381 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
382 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
383 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
384 switch (Chunk.fccID) {
385 case DMUS_FOURCC_INSTRUMENT_CHUNK: {
386 TRACE_(dmfile)(": Instrument chunk\n");
387 if (Chunk.dwSize != sizeof(DMUS_IO_INSTRUMENT)) return E_FAIL;
388 IStream_Read (pStm, &inst, sizeof(DMUS_IO_INSTRUMENT), NULL);
389 TRACE_(dmfile)(" - dwPatch: %u\n", inst.dwPatch);
390 TRACE_(dmfile)(" - dwAssignPatch: %u\n", inst.dwAssignPatch);
391 TRACE_(dmfile)(" - dwNoteRanges[0]: %u\n", inst.dwNoteRanges[0]);
392 TRACE_(dmfile)(" - dwNoteRanges[1]: %u\n", inst.dwNoteRanges[1]);
393 TRACE_(dmfile)(" - dwNoteRanges[2]: %u\n", inst.dwNoteRanges[2]);
394 TRACE_(dmfile)(" - dwNoteRanges[3]: %u\n", inst.dwNoteRanges[3]);
395 TRACE_(dmfile)(" - dwPChannel: %u\n", inst.dwPChannel);
396 TRACE_(dmfile)(" - dwFlags: %x\n", inst.dwFlags);
397 TRACE_(dmfile)(" - bPan: %u\n", inst.bPan);
398 TRACE_(dmfile)(" - bVolume: %u\n", inst.bVolume);
399 TRACE_(dmfile)(" - nTranspose: %d\n", inst.nTranspose);
400 TRACE_(dmfile)(" - dwChannelPriority: %u\n", inst.dwChannelPriority);
401 TRACE_(dmfile)(" - nPitchBendRange: %d\n", inst.nPitchBendRange);
402 break;
404 case FOURCC_LIST: {
405 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
406 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
407 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
408 ListCount[1] = 0;
409 switch (Chunk.fccID) {
410 case DMUS_FOURCC_REF_LIST: {
411 FIXME_(dmfile)(": DMRF (DM References) list\n");
412 hr = IDirectMusicUtils_IPersistStream_ParseReference (iface, &Chunk, pStm, &pObject);
413 if (FAILED(hr)) {
414 ERR(": could not load Reference\n");
415 return hr;
417 break;
419 default: {
420 TRACE_(dmfile)(": unknown (skipping)\n");
421 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
422 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
423 break;
426 break;
428 default: {
429 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
430 liMove.QuadPart = Chunk.dwSize;
431 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
432 break;
435 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
436 } while (ListCount[0] < ListSize[0]);
439 * @TODO insert pNewInstrument into This
441 pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENT));
442 if (NULL == pNewInstrument) {
443 ERR(": no more memory\n");
444 return E_OUTOFMEMORY;
446 memcpy(&pNewInstrument->pInstrument, &inst, sizeof(DMUS_IO_INSTRUMENT));
447 pNewInstrument->ppReferenceCollection = NULL;
448 if (NULL != pObject) {
449 IDirectMusicCollection* pCol = NULL;
450 hr = IDirectMusicObject_QueryInterface (pObject, &IID_IDirectMusicCollection, (void**) &pCol);
451 if (FAILED(hr)) {
452 ERR(": failed to get IDirectMusicCollection Interface from DMObject\n");
453 HeapFree(GetProcessHeap(), 0, pNewInstrument);
455 return hr;
457 pNewInstrument->ppReferenceCollection = pCol;
458 IDirectMusicObject_Release(pObject);
460 list_add_tail (&This->Instruments, &pNewInstrument->entry);
462 return S_OK;
465 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
466 /*ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);*/
467 HRESULT hr;
468 DMUS_PRIVATE_CHUNK Chunk;
469 DWORD ListSize[3], ListCount[3];
470 LARGE_INTEGER liMove; /* used when skipping chunks */
472 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENTS_LIST) {
473 ERR_(dmfile)(": %s chunk should be an INSTRUMENTS list\n", debugstr_fourcc (pChunk->fccID));
474 return E_FAIL;
477 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
478 ListCount[0] = 0;
480 do {
481 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
482 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
483 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
484 switch (Chunk.fccID) {
485 case FOURCC_LIST: {
486 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
487 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
488 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
489 ListCount[1] = 0;
490 switch (Chunk.fccID) {
491 case DMUS_FOURCC_INSTRUMENT_LIST: {
492 TRACE_(dmfile)(": Instrument list\n");
493 hr = IDirectMusicBandImpl_IPersistStream_ParseInstrument (iface, &Chunk, pStm);
494 if (FAILED(hr)) return hr;
495 break;
497 default: {
498 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
499 liMove.QuadPart = ListSize[1];
500 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
501 break;
504 break;
506 default: {
507 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
508 liMove.QuadPart = Chunk.dwSize;
509 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
510 break;
513 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
514 } while (ListCount[0] < ListSize[0]);
516 return S_OK;
519 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseBandForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
521 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
522 HRESULT hr = E_FAIL;
523 DMUS_PRIVATE_CHUNK Chunk;
524 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
525 LARGE_INTEGER liMove; /* used when skipping chunks */
527 GUID tmp_guid;
529 if (pChunk->fccID != DMUS_FOURCC_BAND_FORM) {
530 ERR_(dmfile)(": %s chunk should be a BAND form\n", debugstr_fourcc (pChunk->fccID));
531 return E_FAIL;
534 StreamSize = pChunk->dwSize - sizeof(FOURCC);
535 StreamCount = 0;
537 do {
538 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
539 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
540 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
542 hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
543 if (FAILED(hr)) return hr;
545 if (hr == S_FALSE) {
546 switch (Chunk.fccID) {
547 case DMUS_FOURCC_GUID_CHUNK: {
548 TRACE_(dmfile)(": GUID\n");
549 IStream_Read (pStm, &tmp_guid, sizeof(GUID), NULL);
550 TRACE_(dmfile)(" - guid: %s\n", debugstr_dmguid(&tmp_guid));
551 break;
553 case FOURCC_LIST: {
554 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
555 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
556 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
557 ListCount[0] = 0;
558 switch (Chunk.fccID) {
559 case DMUS_FOURCC_UNFO_LIST: {
560 TRACE_(dmfile)(": UNFO list\n");
561 do {
562 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
563 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
564 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
566 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
567 if (FAILED(hr)) return hr;
569 if (hr == S_FALSE) {
570 switch (Chunk.fccID) {
571 default: {
572 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
573 liMove.QuadPart = Chunk.dwSize;
574 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
575 break;
579 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
580 } while (ListCount[0] < ListSize[0]);
581 break;
583 case DMUS_FOURCC_INSTRUMENTS_LIST: {
584 TRACE_(dmfile)(": INSTRUMENTS list\n");
585 hr = IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (iface, &Chunk, pStm);
586 if (FAILED(hr)) return hr;
587 break;
589 default: {
590 TRACE_(dmfile)(": unknown (skipping)\n");
591 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
592 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
593 break;
596 break;
598 default: {
599 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
600 liMove.QuadPart = Chunk.dwSize;
601 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
602 break;
606 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
607 } while (StreamCount < StreamSize);
609 return S_OK;
612 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
613 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
615 DMUS_PRIVATE_CHUNK Chunk;
616 LARGE_INTEGER liMove;
617 HRESULT hr;
619 TRACE("(%p,%p): loading\n", This, pStm);
621 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
622 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
623 switch (Chunk.fccID) {
624 case FOURCC_RIFF: {
625 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
626 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
627 switch (Chunk.fccID) {
628 case DMUS_FOURCC_BAND_FORM: {
629 TRACE_(dmfile)(": Band form\n");
630 hr = IDirectMusicBandImpl_IPersistStream_ParseBandForm (iface, &Chunk, pStm);
631 if (FAILED(hr)) return hr;
632 break;
634 default: {
635 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
636 liMove.QuadPart = Chunk.dwSize;
637 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
638 return E_FAIL;
641 TRACE_(dmfile)(": reading finished\n");
642 break;
644 default: {
645 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
646 liMove.QuadPart = Chunk.dwSize;
647 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
648 return E_FAIL;
652 return S_OK;
655 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
656 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
657 FIXME("(%p): Saving not implemented yet\n", This);
658 return E_NOTIMPL;
661 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
662 return E_NOTIMPL;
665 static const IPersistStreamVtbl DirectMusicBand_PersistStream_Vtbl = {
666 IDirectMusicBandImpl_IPersistStream_QueryInterface,
667 IDirectMusicBandImpl_IPersistStream_AddRef,
668 IDirectMusicBandImpl_IPersistStream_Release,
669 IDirectMusicBandImpl_IPersistStream_GetClassID,
670 IDirectMusicBandImpl_IPersistStream_IsDirty,
671 IDirectMusicBandImpl_IPersistStream_Load,
672 IDirectMusicBandImpl_IPersistStream_Save,
673 IDirectMusicBandImpl_IPersistStream_GetSizeMax
676 /* for ClassFactory */
677 HRESULT WINAPI create_dmband(REFIID lpcGUID, void **ppobj)
679 IDirectMusicBandImpl* obj;
680 HRESULT hr;
682 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandImpl));
683 if (NULL == obj) {
684 *ppobj = NULL;
685 return E_OUTOFMEMORY;
687 obj->IDirectMusicBand_iface.lpVtbl = &dmband_vtbl;
688 obj->ObjectVtbl = &DirectMusicBand_Object_Vtbl;
689 obj->PersistStreamVtbl = &DirectMusicBand_PersistStream_Vtbl;
690 obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
691 DM_STRUCT_INIT(obj->pDesc);
692 obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
693 obj->pDesc->guidClass = CLSID_DirectMusicBand;
694 obj->ref = 1;
695 list_init (&obj->Instruments);
697 DMBAND_LockModule();
698 hr = IDirectMusicBand_QueryInterface(&obj->IDirectMusicBand_iface, lpcGUID, ppobj);
699 IDirectMusicBand_Release(&obj->IDirectMusicBand_iface);
701 return hr;