dmusic: Fix license headers that mistakenly use GPL instead of LGPL.
[wine] / dlls / dmband / band.c
1 /* IDirectMusicBand Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "dmband_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmband);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25
26 /*****************************************************************************
27  * IDirectMusicBandImpl implementation
28  */
29 /* IDirectMusicBandImpl IUnknown part: */
30 static HRESULT WINAPI IDirectMusicBandImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
31         ICOM_THIS_MULTI(IDirectMusicBandImpl, UnknownVtbl, iface);
32         
33         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
34         if (IsEqualIID (riid, &IID_IUnknown)) {
35                 *ppobj = (LPVOID)&This->UnknownVtbl;
36                 IUnknown_AddRef (iface);
37                 return S_OK;    
38         } else if (IsEqualIID (riid, &IID_IDirectMusicBand)) {
39                 *ppobj = (LPVOID)&This->BandVtbl;
40                 IUnknown_AddRef (iface);
41                 return S_OK;
42         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
43                 *ppobj = (LPVOID)&This->ObjectVtbl;
44                 IUnknown_AddRef (iface);
45                 return S_OK;
46         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
47                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
48                 IUnknown_AddRef (iface);
49                 return S_OK;
50         }
51         
52         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
53         return E_NOINTERFACE;
54 }
55
56 static ULONG WINAPI IDirectMusicBandImpl_IUnknown_AddRef (LPUNKNOWN iface) {
57         ICOM_THIS_MULTI(IDirectMusicBandImpl, UnknownVtbl, iface);
58         ULONG ref = InterlockedIncrement(&This->ref);
59
60         TRACE("(%p) : AddRef from %d\n", This, ref - 1);
61
62         DMBAND_LockModule();
63
64         return ref;
65 }
66
67 static ULONG WINAPI IDirectMusicBandImpl_IUnknown_Release (LPUNKNOWN iface) {
68         ICOM_THIS_MULTI(IDirectMusicBandImpl, UnknownVtbl, iface);
69         ULONG ref = InterlockedDecrement(&This->ref);
70
71         TRACE("(%p) : ReleaseRef to %d\n", This, ref);
72
73         if (ref == 0) {
74                 HeapFree(GetProcessHeap(), 0, This);
75         }
76
77         DMBAND_UnlockModule();
78         
79         return ref;
80 }
81
82 static const IUnknownVtbl DirectMusicBand_Unknown_Vtbl = {
83         IDirectMusicBandImpl_IUnknown_QueryInterface,
84         IDirectMusicBandImpl_IUnknown_AddRef,
85         IDirectMusicBandImpl_IUnknown_Release
86 };
87
88 /* IDirectMusicBandImpl IDirectMusicBand part: */
89 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_QueryInterface (LPDIRECTMUSICBAND iface, REFIID riid, LPVOID *ppobj) {
90         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
91         return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
92 }
93
94 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicBand_AddRef (LPDIRECTMUSICBAND iface) {
95         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
96         return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
97 }
98
99 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicBand_Release (LPDIRECTMUSICBAND iface) {
100         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
101         return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
102 }
103
104 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_CreateSegment (LPDIRECTMUSICBAND iface, IDirectMusicSegment** ppSegment) {
105         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
106         FIXME("(%p, %p): stub\n", This, ppSegment);
107         return S_OK;
108 }
109
110 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_Download (LPDIRECTMUSICBAND iface, IDirectMusicPerformance* pPerformance) {
111         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
112         FIXME("(%p, %p): stub\n", This, pPerformance);
113         return S_OK;
114 }
115
116 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicBand_Unload (LPDIRECTMUSICBAND iface, IDirectMusicPerformance* pPerformance) {
117         ICOM_THIS_MULTI(IDirectMusicBandImpl, BandVtbl, iface);
118         FIXME("(%p, %p): stub\n", This, pPerformance);
119         return S_OK;
120 }
121
122 static const IDirectMusicBandVtbl DirectMusicBand_Band_Vtbl = {
123         IDirectMusicBandImpl_IDirectMusicBand_QueryInterface,
124         IDirectMusicBandImpl_IDirectMusicBand_AddRef,
125         IDirectMusicBandImpl_IDirectMusicBand_Release,
126         IDirectMusicBandImpl_IDirectMusicBand_CreateSegment,
127         IDirectMusicBandImpl_IDirectMusicBand_Download,
128         IDirectMusicBandImpl_IDirectMusicBand_Unload
129 };
130
131 /* IDirectMusicBandImpl IDirectMusicObject part: */
132 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
133         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
134         return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
135 }
136
137 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
138         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
139         return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
140 }
141
142 static ULONG WINAPI IDirectMusicBandImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
143         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
144         return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
145 }
146
147 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
148         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
149         TRACE("(%p, %p)\n", This, pDesc);
150         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
151         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
152         return S_OK;
153 }
154
155 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
156         ICOM_THIS_MULTI(IDirectMusicBandImpl, ObjectVtbl, iface);
157         TRACE("(%p, %p): setting descriptor:\n", This, pDesc); debug_DMUS_OBJECTDESC (pDesc);
158         
159         /* According to MSDN, we should copy only given values, not whole struct */     
160         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
161                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
162         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
163                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
164         if (pDesc->dwValidData & DMUS_OBJ_NAME)
165                 lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
166         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
167                 lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
168         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
169                 lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
170         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
171                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
172         if (pDesc->dwValidData & DMUS_OBJ_DATE)
173                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
174         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
175                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
176                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
177         }
178         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
179                 /* according to MSDN, we copy the stream */
180                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
181         }
182         
183         /* add new flags */
184         This->pDesc->dwValidData |= pDesc->dwValidData;
185
186         return S_OK;
187 }
188
189 static HRESULT WINAPI IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
190         DMUS_PRIVATE_CHUNK Chunk;
191         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
192         LARGE_INTEGER liMove; /* used when skipping chunks */
193
194         TRACE("(%p, %p)\n", pStream, pDesc);
195         
196         /* FIXME: should this be determined from stream? */
197         pDesc->dwValidData |= DMUS_OBJ_CLASS;
198         memcpy (&pDesc->guidClass, &CLSID_DirectMusicBand, sizeof(CLSID));
199         
200         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
201         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
202         switch (Chunk.fccID) {  
203                 case FOURCC_RIFF: {
204                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
205                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
206                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
207                         StreamCount = 0;
208                         if (Chunk.fccID == DMUS_FOURCC_BAND_FORM) {
209                                 TRACE_(dmfile)(": band form\n");
210                                 do {
211                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
212                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
213                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
214                                         switch (Chunk.fccID) {
215                                                 case DMUS_FOURCC_GUID_CHUNK: {
216                                                         TRACE_(dmfile)(": GUID chunk\n");
217                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
218                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
219                                                         break;
220                                                 }
221                                                 case DMUS_FOURCC_VERSION_CHUNK: {
222                                                         TRACE_(dmfile)(": version chunk\n");
223                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
224                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
225                                                         break;
226                                                 }
227                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
228                                                         TRACE_(dmfile)(": category chunk\n");
229                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
230                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
231                                                         break;
232                                                 }
233                                                 case FOURCC_LIST: {
234                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
235                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
236                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
237                                                         ListCount[0] = 0;
238                                                         switch (Chunk.fccID) {
239                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
240                                                                 case DMUS_FOURCC_UNFO_LIST: {
241                                                                         TRACE_(dmfile)(": UNFO list\n");
242                                                                         do {
243                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
244                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
245                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
246                                                                                 switch (Chunk.fccID) {
247                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
248                                              (though strings seem to be valid unicode) */
249                                                                                         case mmioFOURCC('I','N','A','M'):
250                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
251                                                                                                 TRACE_(dmfile)(": name chunk\n");
252                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
253                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
254                                                                                                 break;
255                                                                                         }
256                                                                                         case mmioFOURCC('I','A','R','T'):
257                                                                                         case DMUS_FOURCC_UART_CHUNK: {
258                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
259                                                                                                 liMove.QuadPart = Chunk.dwSize;
260                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
261                                                                                                 break;
262                                                                                         }
263                                                                                         case mmioFOURCC('I','C','O','P'):
264                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
265                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
266                                                                                                 liMove.QuadPart = Chunk.dwSize;
267                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
268                                                                                                 break;
269                                                                                         }
270                                                                                         case mmioFOURCC('I','S','B','J'):
271                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
272                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
273                                                                                                 liMove.QuadPart = Chunk.dwSize;
274                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
275                                                                                                 break;
276                                                                                         }
277                                                                                         case mmioFOURCC('I','C','M','T'):
278                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
279                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
280                                                                                                 liMove.QuadPart = Chunk.dwSize;
281                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
282                                                                                                 break;
283                                                                                         }
284                                                                                         default: {
285                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
286                                                                                                 liMove.QuadPart = Chunk.dwSize;
287                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
288                                                                                                 break;                                          
289                                                                                         }
290                                                                                 }
291                                                                                 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
292                                                                         } while (ListCount[0] < ListSize[0]);
293                                                                         break;
294                                                                 }
295                                                                 default: {
296                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
297                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
298                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
299                                                                         break;                                          
300                                                                 }
301                                                         }
302                                                         break;
303                                                 }       
304                                                 default: {
305                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
306                                                         liMove.QuadPart = Chunk.dwSize;
307                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
308                                                         break;                                          
309                                                 }
310                                         }
311                                         TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
312                                 } while (StreamCount < StreamSize);
313                         } else {
314                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
315                                 liMove.QuadPart = StreamSize;
316                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
317                                 return E_FAIL;
318                         }
319                 
320                         TRACE_(dmfile)(": reading finished\n");
321                         break;
322                 }
323                 default: {
324                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
325                         liMove.QuadPart = Chunk.dwSize;
326                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
327                         return DMUS_E_INVALIDFILE;
328                 }
329         }       
330         
331         TRACE(": returning descriptor:\n"); debug_DMUS_OBJECTDESC (pDesc);
332         
333         return S_OK;
334 }
335
336 static const IDirectMusicObjectVtbl DirectMusicBand_Object_Vtbl = {
337   IDirectMusicBandImpl_IDirectMusicObject_QueryInterface,
338   IDirectMusicBandImpl_IDirectMusicObject_AddRef,
339   IDirectMusicBandImpl_IDirectMusicObject_Release,
340   IDirectMusicBandImpl_IDirectMusicObject_GetDescriptor,
341   IDirectMusicBandImpl_IDirectMusicObject_SetDescriptor,
342   IDirectMusicBandImpl_IDirectMusicObject_ParseDescriptor
343 };
344
345 /* IDirectMusicBandImpl IPersistStream part: */
346 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
347   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
348   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
349 }
350
351 static ULONG WINAPI IDirectMusicBandImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
352   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
353   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
354 }
355
356 static ULONG WINAPI IDirectMusicBandImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
357   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
358   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);      
359 }
360
361 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
362   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
363   TRACE("(%p, %p)\n", This, pClassID);
364   memcpy(pClassID, &CLSID_DirectMusicBand, sizeof(CLSID));
365   return S_OK;
366 }
367
368 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
369   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
370   FIXME("(%p): stub, always S_FALSE\n", This);
371   return S_FALSE;
372 }
373
374 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrument (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
375   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
376   DMUS_PRIVATE_CHUNK Chunk;
377   DWORD ListSize[3], ListCount[3];
378   LARGE_INTEGER liMove; /* used when skipping chunks */
379   HRESULT hr;
380
381   DMUS_IO_INSTRUMENT inst;
382   LPDMUS_PRIVATE_INSTRUMENT pNewInstrument;
383   IDirectMusicObject* pObject = NULL;
384
385   if (pChunk->fccID != DMUS_FOURCC_INSTRUMENT_LIST) {
386     ERR_(dmfile)(": %s chunk should be an INSTRUMENT list\n", debugstr_fourcc (pChunk->fccID));
387     return E_FAIL;
388   }
389
390   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
391   ListCount[0] = 0;
392
393   do {
394     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
395     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
396     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
397     switch (Chunk.fccID) { 
398     case DMUS_FOURCC_INSTRUMENT_CHUNK: {
399       TRACE_(dmfile)(": Instrument chunk\n");
400       if (Chunk.dwSize != sizeof(DMUS_IO_INSTRUMENT)) return E_FAIL;
401       IStream_Read (pStm, &inst, sizeof(DMUS_IO_INSTRUMENT), NULL);
402       TRACE_(dmfile)(" - dwPatch: %u\n", inst.dwPatch);
403       TRACE_(dmfile)(" - dwAssignPatch: %u\n", inst.dwAssignPatch);
404       TRACE_(dmfile)(" - dwNoteRanges[0]: %u\n", inst.dwNoteRanges[0]);
405       TRACE_(dmfile)(" - dwNoteRanges[1]: %u\n", inst.dwNoteRanges[1]);
406       TRACE_(dmfile)(" - dwNoteRanges[2]: %u\n", inst.dwNoteRanges[2]);
407       TRACE_(dmfile)(" - dwNoteRanges[3]: %u\n", inst.dwNoteRanges[3]);
408       TRACE_(dmfile)(" - dwPChannel: %u\n", inst.dwPChannel);
409       TRACE_(dmfile)(" - dwFlags: %u\n", inst.dwFlags);
410       TRACE_(dmfile)(" - bPan: %u\n", inst.bPan);
411       TRACE_(dmfile)(" - bVolume: %u\n", inst.bVolume);
412       TRACE_(dmfile)(" - nTranspose: %d\n", inst.nTranspose);
413       TRACE_(dmfile)(" - dwChannelPriority: %u\n", inst.dwChannelPriority);
414       TRACE_(dmfile)(" - nPitchBendRange: %d\n", inst.nPitchBendRange);
415       break;
416     } 
417     case FOURCC_LIST: {
418       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
419       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
420       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
421       ListCount[1] = 0;
422       switch (Chunk.fccID) { 
423       case DMUS_FOURCC_REF_LIST: {
424         FIXME_(dmfile)(": DMRF (DM References) list\n");
425         hr = IDirectMusicUtils_IPersistStream_ParseReference (iface,  &Chunk, pStm, &pObject);
426         if (FAILED(hr)) {
427           ERR(": could not load Reference\n");
428           return hr;
429         }
430         break;                                          
431       }
432       default: {
433         TRACE_(dmfile)(": unknown (skipping)\n");
434         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
435         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
436         break;                                          
437       }
438       } 
439       break;
440     }
441     default: {
442       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
443       liMove.QuadPart = Chunk.dwSize;
444       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
445       break;                                            
446     }
447     }
448     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
449   } while (ListCount[0] < ListSize[0]);
450   
451   /*
452    * @TODO insert pNewInstrument into This
453    */
454   pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENT));
455   if (NULL == pNewInstrument) {
456     ERR(": no more memory\n");
457     return  E_OUTOFMEMORY;
458   }
459   memcpy(&pNewInstrument->pInstrument, &inst, sizeof(DMUS_IO_INSTRUMENT));
460   pNewInstrument->ppReferenceCollection = NULL;
461   if (NULL != pObject) {
462     IDirectMusicCollection* pCol = NULL;
463     hr = IDirectMusicObject_QueryInterface (pObject, &IID_IDirectMusicCollection, (void**) &pCol);
464     if (FAILED(hr)) {
465       ERR(": failed to get IDirectMusicCollection Interface from DMObject\n");
466       return hr;
467     }
468     pNewInstrument->ppReferenceCollection = pCol;
469     IDirectMusicObject_Release(pObject);
470   }
471   list_add_tail (&This->Instruments, &pNewInstrument->entry);
472
473   return S_OK;
474 }
475
476 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
477   /*ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);*/
478   HRESULT hr;
479   DMUS_PRIVATE_CHUNK Chunk;
480   DWORD ListSize[3], ListCount[3];
481   LARGE_INTEGER liMove; /* used when skipping chunks */
482
483   if (pChunk->fccID != DMUS_FOURCC_INSTRUMENTS_LIST) {
484     ERR_(dmfile)(": %s chunk should be an INSTRUMENTS list\n", debugstr_fourcc (pChunk->fccID));
485     return E_FAIL;
486   }  
487
488   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
489   ListCount[0] = 0;
490
491   do {
492     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
493     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
494     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
495     switch (Chunk.fccID) {
496     case FOURCC_LIST: {
497       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
498       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
499       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
500       ListCount[1] = 0;
501       switch (Chunk.fccID) { 
502       case DMUS_FOURCC_INSTRUMENT_LIST: {
503         TRACE_(dmfile)(": Instrument list\n");
504         hr = IDirectMusicBandImpl_IPersistStream_ParseInstrument (iface, &Chunk, pStm);
505         if (FAILED(hr)) return hr;
506         break;
507       }
508       default: {
509         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
510         liMove.QuadPart = ListSize[1];
511         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
512         break;                                          
513       }
514       }
515       break;
516     }
517     default: {
518       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
519       liMove.QuadPart = Chunk.dwSize;
520       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
521       break;                                            
522     }
523     }
524     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
525   } while (ListCount[0] < ListSize[0]);
526
527   return S_OK;
528 }
529
530 static HRESULT IDirectMusicBandImpl_IPersistStream_ParseBandForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
531
532   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
533   HRESULT hr = E_FAIL;
534   DMUS_PRIVATE_CHUNK Chunk;
535   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
536   LARGE_INTEGER liMove; /* used when skipping chunks */
537
538   GUID tmp_guid;
539
540   if (pChunk->fccID != DMUS_FOURCC_BAND_FORM) {
541     ERR_(dmfile)(": %s chunk should be a BAND form\n", debugstr_fourcc (pChunk->fccID));
542     return E_FAIL;
543   }  
544
545   StreamSize = pChunk->dwSize - sizeof(FOURCC);
546   StreamCount = 0;
547
548   do {
549     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
550     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
551     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
552
553     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
554     if (FAILED(hr)) return hr;
555     
556     if (hr == S_FALSE) {
557       switch (Chunk.fccID) {
558       case DMUS_FOURCC_GUID_CHUNK: {
559         TRACE_(dmfile)(": GUID\n");
560         IStream_Read (pStm, &tmp_guid, sizeof(GUID), NULL);
561         TRACE_(dmfile)(" - guid: %s\n", debugstr_dmguid(&tmp_guid));
562         break;
563       }
564       case FOURCC_LIST: {
565         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
566         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
567         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
568         ListCount[0] = 0;
569         switch (Chunk.fccID) {
570         case DMUS_FOURCC_UNFO_LIST: { 
571           TRACE_(dmfile)(": UNFO list\n");
572           do {
573             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
574             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
575             TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
576             
577             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
578             if (FAILED(hr)) return hr;
579             
580             if (hr == S_FALSE) {
581               switch (Chunk.fccID) {
582               default: {
583                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
584                 liMove.QuadPart = Chunk.dwSize;
585                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
586                 break;                                          
587               }
588               }
589             }
590             TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
591           } while (ListCount[0] < ListSize[0]);
592           break;
593         }
594         case DMUS_FOURCC_INSTRUMENTS_LIST: {
595           TRACE_(dmfile)(": INSTRUMENTS list\n");
596           hr = IDirectMusicBandImpl_IPersistStream_ParseInstrumentsList (iface, &Chunk, pStm);
597           if (FAILED(hr)) return hr;
598           break;        
599         }
600         default: {
601           TRACE_(dmfile)(": unknown (skipping)\n");
602           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
603           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
604           break;                                                
605         }
606         }
607         break;
608       }
609       default: {
610         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
611         liMove.QuadPart = Chunk.dwSize;
612         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
613         break;                                          
614       }
615       }
616     }
617     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
618   } while (StreamCount < StreamSize);  
619
620   return S_OK;
621 }
622
623 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
624   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
625
626   DMUS_PRIVATE_CHUNK Chunk;
627   LARGE_INTEGER liMove;
628   HRESULT hr;
629   
630   TRACE("(%p,%p): loading\n", This, pStm);
631   
632   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
633   TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
634   switch (Chunk.fccID) {
635   case FOURCC_RIFF: {
636     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
637     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
638     switch (Chunk.fccID) {
639     case DMUS_FOURCC_BAND_FORM: {
640       TRACE_(dmfile)(": Band form\n");
641       hr = IDirectMusicBandImpl_IPersistStream_ParseBandForm (iface, &Chunk, pStm);
642       if (FAILED(hr)) return hr;
643       break;    
644     }
645     default: {
646       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
647       liMove.QuadPart = Chunk.dwSize;
648       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
649       return E_FAIL;
650     }
651     }
652     TRACE_(dmfile)(": reading finished\n");
653     break;
654   }
655   default: {
656     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
657     liMove.QuadPart = Chunk.dwSize;
658     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
659     return E_FAIL;
660   }
661   }
662   
663   return S_OK;
664 }
665
666 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
667   ICOM_THIS_MULTI(IDirectMusicBandImpl, PersistStreamVtbl, iface);
668   FIXME("(%p): Saving not implemented yet\n", This);
669   return E_NOTIMPL;
670 }
671
672 static HRESULT WINAPI IDirectMusicBandImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
673   return E_NOTIMPL;
674 }
675
676 static const IPersistStreamVtbl DirectMusicBand_PersistStream_Vtbl = {
677   IDirectMusicBandImpl_IPersistStream_QueryInterface,
678   IDirectMusicBandImpl_IPersistStream_AddRef,
679   IDirectMusicBandImpl_IPersistStream_Release,
680   IDirectMusicBandImpl_IPersistStream_GetClassID,
681   IDirectMusicBandImpl_IPersistStream_IsDirty,
682   IDirectMusicBandImpl_IPersistStream_Load,
683   IDirectMusicBandImpl_IPersistStream_Save,
684   IDirectMusicBandImpl_IPersistStream_GetSizeMax
685 };
686
687 /* for ClassFactory */
688 HRESULT WINAPI DMUSIC_CreateDirectMusicBandImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
689   IDirectMusicBandImpl* obj;
690         
691   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandImpl));
692   if (NULL == obj) {
693     *ppobj = NULL;
694     return E_OUTOFMEMORY;
695   }
696   obj->UnknownVtbl = &DirectMusicBand_Unknown_Vtbl;
697   obj->BandVtbl = &DirectMusicBand_Band_Vtbl;
698   obj->ObjectVtbl = &DirectMusicBand_Object_Vtbl;
699   obj->PersistStreamVtbl = &DirectMusicBand_PersistStream_Vtbl;
700   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
701   DM_STRUCT_INIT(obj->pDesc);
702   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
703   memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicBand, sizeof (CLSID));
704   obj->ref = 0; /* will be inited by QueryInterface */
705   list_init (&obj->Instruments);
706   
707   return IDirectMusicBandImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
708 }