ntdll/tests: Add KeyNameInformation tests.
[wine.git] / dlls / dmband / band.c
blob0ab7df74028a19c55d74245cd39ce4c903c57b5f
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 typedef struct IDirectMusicBandImpl {
30 IDirectMusicBand IDirectMusicBand_iface;
31 const IDirectMusicObjectVtbl *ObjectVtbl;
32 const IPersistStreamVtbl *PersistStreamVtbl;
33 LONG ref;
34 DMUS_OBJECTDESC *pDesc;
35 struct list Instruments;
36 } IDirectMusicBandImpl;
38 static inline IDirectMusicBandImpl *impl_from_IDirectMusicBand(IDirectMusicBand *iface)
40 return CONTAINING_RECORD(iface, IDirectMusicBandImpl, IDirectMusicBand_iface);
43 /* DirectMusicBandImpl IDirectMusicBand part: */
44 static HRESULT WINAPI IDirectMusicBandImpl_QueryInterface(IDirectMusicBand *iface, REFIID riid,
45 void **ret_iface)
47 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
49 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
51 *ret_iface = NULL;
53 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicBand))
54 *ret_iface = iface;
55 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
56 *ret_iface = &This->ObjectVtbl;
57 else if (IsEqualIID(riid, &IID_IPersistStream))
58 *ret_iface = &This->PersistStreamVtbl;
59 else {
60 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
61 return E_NOINTERFACE;
64 IDirectMusicBand_AddRef((IUnknown*)*ret_iface);
65 return S_OK;
68 static ULONG WINAPI IDirectMusicBandImpl_AddRef(IDirectMusicBand *iface)
70 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
71 LONG ref = InterlockedIncrement(&This->ref);
73 TRACE("(%p) ref=%d\n", This, ref);
75 return ref;
78 static ULONG WINAPI IDirectMusicBandImpl_Release(IDirectMusicBand *iface)
80 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
81 LONG ref = InterlockedDecrement(&This->ref);
83 TRACE("(%p) ref=%d\n", This, ref);
85 if (!ref) {
86 HeapFree(GetProcessHeap(), 0, This->pDesc);
87 HeapFree(GetProcessHeap(), 0, This);
88 DMBAND_UnlockModule();
91 return ref;
94 static HRESULT WINAPI IDirectMusicBandImpl_CreateSegment(IDirectMusicBand *iface,
95 IDirectMusicSegment **ppSegment)
97 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
98 FIXME("(%p, %p): stub\n", This, ppSegment);
99 return S_OK;
102 static HRESULT WINAPI IDirectMusicBandImpl_Download(IDirectMusicBand *iface,
103 IDirectMusicPerformance *pPerformance)
105 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
106 FIXME("(%p, %p): stub\n", This, pPerformance);
107 return S_OK;
110 static HRESULT WINAPI IDirectMusicBandImpl_Unload(IDirectMusicBand *iface,
111 IDirectMusicPerformance *pPerformance)
113 IDirectMusicBandImpl *This = impl_from_IDirectMusicBand(iface);
114 FIXME("(%p, %p): stub\n", This, pPerformance);
115 return S_OK;
118 static const IDirectMusicBandVtbl dmband_vtbl = {
119 IDirectMusicBandImpl_QueryInterface,
120 IDirectMusicBandImpl_AddRef,
121 IDirectMusicBandImpl_Release,
122 IDirectMusicBandImpl_CreateSegment,
123 IDirectMusicBandImpl_Download,
124 IDirectMusicBandImpl_Unload
127 /* IDirectMusicBandImpl IDirectMusicObject part: */
128 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
129 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
130 return IDirectMusicBand_QueryInterface(&This->IDirectMusicBand_iface, riid, ppobj);
133 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
134 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
135 return IDirectMusicBand_AddRef(&This->IDirectMusicBand_iface);
138 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
139 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
140 return IDirectMusicBand_Release(&This->IDirectMusicBand_iface);
143 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
144 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
145 TRACE("(%p, %p)\n", This, pDesc);
146 /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
147 memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
148 return S_OK;
151 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
152 ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
153 TRACE("(%p, %p): setting descriptor:\n", This, pDesc); debug_DMUS_OBJECTDESC (pDesc);
155 /* According to MSDN, we should copy only given values, not whole struct */
156 if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
157 This->pDesc->guidObject = pDesc->guidObject;
158 if (pDesc->dwValidData & DMUS_OBJ_CLASS)
159 This->pDesc->guidClass = pDesc->guidClass;
160 if (pDesc->dwValidData & DMUS_OBJ_NAME)
161 lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
162 if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
163 lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
164 if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
165 lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
166 if (pDesc->dwValidData & DMUS_OBJ_VERSION)
167 This->pDesc->vVersion = pDesc->vVersion;
168 if (pDesc->dwValidData & DMUS_OBJ_DATE)
169 This->pDesc->ftDate = pDesc->ftDate;
170 if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
171 This->pDesc->llMemLength = pDesc->llMemLength;
172 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, pDesc->llMemLength);
174 if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
175 /* according to MSDN, we copy the stream */
176 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);
179 /* add new flags */
180 This->pDesc->dwValidData |= pDesc->dwValidData;
182 return S_OK;
185 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
186 DMUS_PRIVATE_CHUNK Chunk;
187 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
188 LARGE_INTEGER liMove; /* used when skipping chunks */
190 TRACE("(%p, %p)\n", pStream, pDesc);
192 /* FIXME: should this be determined from stream? */
193 pDesc->dwValidData |= DMUS_OBJ_CLASS;
194 pDesc->guidClass = CLSID_DirectMusicBand;
196 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
197 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
198 switch (Chunk.fccID) {
199 case FOURCC_RIFF: {
200 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
201 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
202 StreamSize = Chunk.dwSize - sizeof(FOURCC);
203 StreamCount = 0;
204 if (Chunk.fccID == DMUS_FOURCC_BAND_FORM) {
205 TRACE_(dmfile)(": band form\n");
206 do {
207 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
208 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
209 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
210 switch (Chunk.fccID) {
211 case DMUS_FOURCC_GUID_CHUNK: {
212 TRACE_(dmfile)(": GUID chunk\n");
213 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
214 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
215 break;
217 case DMUS_FOURCC_VERSION_CHUNK: {
218 TRACE_(dmfile)(": version chunk\n");
219 pDesc->dwValidData |= DMUS_OBJ_VERSION;
220 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
221 break;
223 case DMUS_FOURCC_CATEGORY_CHUNK: {
224 TRACE_(dmfile)(": category chunk\n");
225 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
226 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
227 break;
229 case FOURCC_LIST: {
230 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
231 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
232 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
233 ListCount[0] = 0;
234 switch (Chunk.fccID) {
235 /* evil M$ UNFO list, which can (!?) contain INFO elements */
236 case DMUS_FOURCC_UNFO_LIST: {
237 TRACE_(dmfile)(": UNFO list\n");
238 do {
239 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
240 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
241 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
242 switch (Chunk.fccID) {
243 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
244 (though strings seem to be valid unicode) */
245 case mmioFOURCC('I','N','A','M'):
246 case DMUS_FOURCC_UNAM_CHUNK: {
247 TRACE_(dmfile)(": name chunk\n");
248 pDesc->dwValidData |= DMUS_OBJ_NAME;
249 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
250 break;
252 case mmioFOURCC('I','A','R','T'):
253 case DMUS_FOURCC_UART_CHUNK: {
254 TRACE_(dmfile)(": artist chunk (ignored)\n");
255 liMove.QuadPart = Chunk.dwSize;
256 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
257 break;
259 case mmioFOURCC('I','C','O','P'):
260 case DMUS_FOURCC_UCOP_CHUNK: {
261 TRACE_(dmfile)(": copyright chunk (ignored)\n");
262 liMove.QuadPart = Chunk.dwSize;
263 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
264 break;
266 case mmioFOURCC('I','S','B','J'):
267 case DMUS_FOURCC_USBJ_CHUNK: {
268 TRACE_(dmfile)(": subject chunk (ignored)\n");
269 liMove.QuadPart = Chunk.dwSize;
270 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
271 break;
273 case mmioFOURCC('I','C','M','T'):
274 case DMUS_FOURCC_UCMT_CHUNK: {
275 TRACE_(dmfile)(": comment chunk (ignored)\n");
276 liMove.QuadPart = Chunk.dwSize;
277 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
278 break;
280 default: {
281 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
282 liMove.QuadPart = Chunk.dwSize;
283 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
284 break;
287 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
288 } while (ListCount[0] < ListSize[0]);
289 break;
291 default: {
292 TRACE_(dmfile)(": unknown (skipping)\n");
293 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
294 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
295 break;
298 break;
300 default: {
301 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
302 liMove.QuadPart = Chunk.dwSize;
303 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
304 break;
307 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
308 } while (StreamCount < StreamSize);
309 } else {
310 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
311 liMove.QuadPart = StreamSize;
312 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
313 return E_FAIL;
316 TRACE_(dmfile)(": reading finished\n");
317 break;
319 default: {
320 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
321 liMove.QuadPart = Chunk.dwSize;
322 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
323 return DMUS_E_INVALIDFILE;
327 TRACE(": returning descriptor:\n"); debug_DMUS_OBJECTDESC (pDesc);
329 return S_OK;
332 static const IDirectMusicObjectVtbl DirectMusicBand_Object_Vtbl = {
333 IDirectMusicBandImpl_IDirectMusicObject_QueryInterface,
334 IDirectMusicBandImpl_IDirectMusicObject_AddRef,
335 IDirectMusicBandImpl_IDirectMusicObject_Release,
336 IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor,
337 IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor,
338 IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor
341 /* IDirectMusicBandImpl IPersistStream part: */
342 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
343 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
344 return IDirectMusicBand_QueryInterface(&This->IDirectMusicBand_iface, riid, ppobj);
347 static ULONG WINAPI IDirectMusicBandImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
348 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
349 return IDirectMusicBand_AddRef(&This->IDirectMusicBand_iface);
352 static ULONG WINAPI IDirectMusicBandImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
353 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
354 return IDirectMusicBand_Release(&This->IDirectMusicBand_iface);
357 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
358 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
359 TRACE("(%p, %p)\n", This, pClassID);
360 *pClassID = CLSID_DirectMusicBand;
361 return S_OK;
364 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
365 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
366 FIXME("(%p): stub, always S_FALSE\n", This);
367 return S_FALSE;
370 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrument (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
371 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
372 DMUS_PRIVATE_CHUNK Chunk;
373 DWORD ListSize[3], ListCount[3];
374 LARGE_INTEGER liMove; /* used when skipping chunks */
375 HRESULT hr;
377 DMUS_IO_INSTRUMENT inst;
378 LPDMUS_PRIVATE_INSTRUMENT pNewInstrument;
379 IDirectMusicObject* pObject = NULL;
381 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENT_LIST) {
382 ERR_(dmfile)(": %s chunk should be an INSTRUMENT list\n", debugstr_fourcc (pChunk->fccID));
383 return E_FAIL;
386 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
387 ListCount[0] = 0;
389 do {
390 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
391 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
392 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
393 switch (Chunk.fccID) {
394 case DMUS_FOURCC_INSTRUMENT_CHUNK: {
395 TRACE_(dmfile)(": Instrument chunk\n");
396 if (Chunk.dwSize != sizeof(DMUS_IO_INSTRUMENT)) return E_FAIL;
397 IStream_Read (pStm, &inst, sizeof(DMUS_IO_INSTRUMENT), NULL);
398 TRACE_(dmfile)(" - dwPatch: %u\n", inst.dwPatch);
399 TRACE_(dmfile)(" - dwAssignPatch: %u\n", inst.dwAssignPatch);
400 TRACE_(dmfile)(" - dwNoteRanges[0]: %u\n", inst.dwNoteRanges[0]);
401 TRACE_(dmfile)(" - dwNoteRanges[1]: %u\n", inst.dwNoteRanges[1]);
402 TRACE_(dmfile)(" - dwNoteRanges[2]: %u\n", inst.dwNoteRanges[2]);
403 TRACE_(dmfile)(" - dwNoteRanges[3]: %u\n", inst.dwNoteRanges[3]);
404 TRACE_(dmfile)(" - dwPChannel: %u\n", inst.dwPChannel);
405 TRACE_(dmfile)(" - dwFlags: %x\n", inst.dwFlags);
406 TRACE_(dmfile)(" - bPan: %u\n", inst.bPan);
407 TRACE_(dmfile)(" - bVolume: %u\n", inst.bVolume);
408 TRACE_(dmfile)(" - nTranspose: %d\n", inst.nTranspose);
409 TRACE_(dmfile)(" - dwChannelPriority: %u\n", inst.dwChannelPriority);
410 TRACE_(dmfile)(" - nPitchBendRange: %d\n", inst.nPitchBendRange);
411 break;
413 case FOURCC_LIST: {
414 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
415 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
416 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
417 ListCount[1] = 0;
418 switch (Chunk.fccID) {
419 case DMUS_FOURCC_REF_LIST: {
420 FIXME_(dmfile)(": DMRF (DM References) list\n");
421 hr = IDirectMusicUtils_IPersistStream_ParseReference (iface, &Chunk, pStm, &pObject);
422 if (FAILED(hr)) {
423 ERR(": could not load Reference\n");
424 return hr;
426 break;
428 default: {
429 TRACE_(dmfile)(": unknown (skipping)\n");
430 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
431 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
432 break;
435 break;
437 default: {
438 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
439 liMove.QuadPart = Chunk.dwSize;
440 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
441 break;
444 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
445 } while (ListCount[0] < ListSize[0]);
448 * @TODO insert pNewInstrument into This
450 pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENT));
451 if (NULL == pNewInstrument) {
452 ERR(": no more memory\n");
453 return E_OUTOFMEMORY;
455 memcpy(&pNewInstrument->pInstrument, &inst, sizeof(DMUS_IO_INSTRUMENT));
456 pNewInstrument->ppReferenceCollection = NULL;
457 if (NULL != pObject) {
458 IDirectMusicCollection* pCol = NULL;
459 hr = IDirectMusicObject_QueryInterface (pObject, &IID_IDirectMusicCollection, (void**) &pCol);
460 if (FAILED(hr)) {
461 ERR(": failed to get IDirectMusicCollection Interface from DMObject\n");
462 HeapFree(GetProcessHeap(), 0, pNewInstrument);
464 return hr;
466 pNewInstrument->ppReferenceCollection = pCol;
467 IDirectMusicObject_Release(pObject);
469 list_add_tail (&This->Instruments, &pNewInstrument->entry);
471 return S_OK;
474 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
475 /*ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);*/
476 HRESULT hr;
477 DMUS_PRIVATE_CHUNK Chunk;
478 DWORD ListSize[3], ListCount[3];
479 LARGE_INTEGER liMove; /* used when skipping chunks */
481 if (pChunk->fccID != DMUS_FOURCC_INSTRUMENTS_LIST) {
482 ERR_(dmfile)(": %s chunk should be an INSTRUMENTS list\n", debugstr_fourcc (pChunk->fccID));
483 return E_FAIL;
486 ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
487 ListCount[0] = 0;
489 do {
490 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
491 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
492 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
493 switch (Chunk.fccID) {
494 case FOURCC_LIST: {
495 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
496 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
497 ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
498 ListCount[1] = 0;
499 switch (Chunk.fccID) {
500 case DMUS_FOURCC_INSTRUMENT_LIST: {
501 TRACE_(dmfile)(": Instrument list\n");
502 hr = IDirectMusicBandImpl_IPersistStream_ParseInstrument (iface, &Chunk, pStm);
503 if (FAILED(hr)) return hr;
504 break;
506 default: {
507 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
508 liMove.QuadPart = ListSize[1];
509 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
510 break;
513 break;
515 default: {
516 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
517 liMove.QuadPart = Chunk.dwSize;
518 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
519 break;
522 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
523 } while (ListCount[0] < ListSize[0]);
525 return S_OK;
528 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseBandForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
530 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
531 HRESULT hr = E_FAIL;
532 DMUS_PRIVATE_CHUNK Chunk;
533 DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
534 LARGE_INTEGER liMove; /* used when skipping chunks */
536 GUID tmp_guid;
538 if (pChunk->fccID != DMUS_FOURCC_BAND_FORM) {
539 ERR_(dmfile)(": %s chunk should be a BAND form\n", debugstr_fourcc (pChunk->fccID));
540 return E_FAIL;
543 StreamSize = pChunk->dwSize - sizeof(FOURCC);
544 StreamCount = 0;
546 do {
547 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
548 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
549 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
551 hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
552 if (FAILED(hr)) return hr;
554 if (hr == S_FALSE) {
555 switch (Chunk.fccID) {
556 case DMUS_FOURCC_GUID_CHUNK: {
557 TRACE_(dmfile)(": GUID\n");
558 IStream_Read (pStm, &tmp_guid, sizeof(GUID), NULL);
559 TRACE_(dmfile)(" - guid: %s\n", debugstr_dmguid(&tmp_guid));
560 break;
562 case FOURCC_LIST: {
563 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
564 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
565 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
566 ListCount[0] = 0;
567 switch (Chunk.fccID) {
568 case DMUS_FOURCC_UNFO_LIST: {
569 TRACE_(dmfile)(": UNFO list\n");
570 do {
571 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
572 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
573 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
575 hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
576 if (FAILED(hr)) return hr;
578 if (hr == S_FALSE) {
579 switch (Chunk.fccID) {
580 default: {
581 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
582 liMove.QuadPart = Chunk.dwSize;
583 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
584 break;
588 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
589 } while (ListCount[0] < ListSize[0]);
590 break;
592 case DMUS_FOURCC_INSTRUMENTS_LIST: {
593 TRACE_(dmfile)(": INSTRUMENTS list\n");
594 hr = IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (iface, &Chunk, pStm);
595 if (FAILED(hr)) return hr;
596 break;
598 default: {
599 TRACE_(dmfile)(": unknown (skipping)\n");
600 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
601 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
602 break;
605 break;
607 default: {
608 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
609 liMove.QuadPart = Chunk.dwSize;
610 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
611 break;
615 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
616 } while (StreamCount < StreamSize);
618 return S_OK;
621 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
622 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
624 DMUS_PRIVATE_CHUNK Chunk;
625 LARGE_INTEGER liMove;
626 HRESULT hr;
628 TRACE("(%p,%p): loading\n", This, pStm);
630 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
631 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
632 switch (Chunk.fccID) {
633 case FOURCC_RIFF: {
634 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
635 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
636 switch (Chunk.fccID) {
637 case DMUS_FOURCC_BAND_FORM: {
638 TRACE_(dmfile)(": Band form\n");
639 hr = IDirectMusicBandImpl_IPersistStream_ParseBandForm (iface, &Chunk, pStm);
640 if (FAILED(hr)) return hr;
641 break;
643 default: {
644 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
645 liMove.QuadPart = Chunk.dwSize;
646 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
647 return E_FAIL;
650 TRACE_(dmfile)(": reading finished\n");
651 break;
653 default: {
654 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
655 liMove.QuadPart = Chunk.dwSize;
656 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
657 return E_FAIL;
661 return S_OK;
664 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
665 ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
666 FIXME("(%p): Saving not implemented yet\n", This);
667 return E_NOTIMPL;
670 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
671 return E_NOTIMPL;
674 static const IPersistStreamVtbl DirectMusicBand_PersistStream_Vtbl = {
675 IDirectMusicBandImpl_IPersistStream_QueryInterface,
676 IDirectMusicBandImpl_IPersistStream_AddRef,
677 IDirectMusicBandImpl_IPersistStream_Release,
678 IDirectMusicBandImpl_IPersistStream_GetClassID,
679 IDirectMusicBandImpl_IPersistStream_IsDirty,
680 IDirectMusicBandImpl_IPersistStream_Load,
681 IDirectMusicBandImpl_IPersistStream_Save,
682 IDirectMusicBandImpl_IPersistStream_GetSizeMax
685 /* for ClassFactory */
686 HRESULT WINAPI create_dmband(REFIID lpcGUID, void **ppobj)
688 IDirectMusicBandImpl* obj;
689 HRESULT hr;
691 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandImpl));
692 if (NULL == obj) {
693 *ppobj = NULL;
694 return E_OUTOFMEMORY;
696 obj->IDirectMusicBand_iface.lpVtbl = &dmband_vtbl;
697 obj->ObjectVtbl = &DirectMusicBand_Object_Vtbl;
698 obj->PersistStreamVtbl = &DirectMusicBand_PersistStream_Vtbl;
699 obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
700 DM_STRUCT_INIT(obj->pDesc);
701 obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
702 obj->pDesc->guidClass = CLSID_DirectMusicBand;
703 obj->ref = 1;
704 list_init (&obj->Instruments);
706 DMBAND_LockModule();
707 hr = IDirectMusicBand_QueryInterface(&obj->IDirectMusicBand_iface, lpcGUID, ppobj);
708 IDirectMusicBand_Release(&obj->IDirectMusicBand_iface);
710 return hr;