quartz: Avoid asserting from used samples on allocator release.
[wine] / dlls / dmusic / collection.c
1 /* IDirectMusicCollection Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (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
13  * GNU Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * 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 "dmusic_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface);
26 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface);
27 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface);
28 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface);
29
30 /*****************************************************************************
31  * IDirectMusicCollectionImpl implementation
32  */
33 /* IDirectMusicCollectionImpl IUnknown part: */
34 static HRESULT WINAPI IDirectMusicCollectionImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
35         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
36         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
37
38         if (IsEqualIID (riid, &IID_IUnknown)) {
39                 *ppobj = (LPVOID)&This->UnknownVtbl;
40                 IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
41                 return S_OK;    
42         } else if (IsEqualIID (riid, &IID_IDirectMusicCollection)) {
43                 *ppobj = (LPVOID)&This->CollectionVtbl;
44                 IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef ((LPDIRECTMUSICCOLLECTION)&This->CollectionVtbl);
45                 return S_OK;
46         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
47                 *ppobj = (LPVOID)&This->ObjectVtbl;
48                 IDirectMusicCollectionImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);          
49                 return S_OK;
50         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
51                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
52                 IDirectMusicCollectionImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);           
53                 return S_OK;
54         }
55         
56         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
57         return E_NOINTERFACE;
58 }
59
60 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface) {
61         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
62         ULONG refCount = InterlockedIncrement(&This->ref);
63
64         TRACE("(%p)->(ref before=%u)\n", This, refCount - 1);
65
66         DMUSIC_LockModule();
67
68         return refCount;
69 }
70
71 static ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_Release (LPUNKNOWN iface) {
72         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, UnknownVtbl, iface);
73         ULONG refCount = InterlockedDecrement(&This->ref);
74
75         TRACE("(%p)->(ref before=%u)\n", This, refCount + 1);
76
77         if (!refCount) {
78                 HeapFree(GetProcessHeap(), 0, This);
79         }
80
81         DMUSIC_UnlockModule();
82
83         return refCount;
84 }
85
86 static const IUnknownVtbl DirectMusicCollection_Unknown_Vtbl = {
87         IDirectMusicCollectionImpl_IUnknown_QueryInterface,
88         IDirectMusicCollectionImpl_IUnknown_AddRef,
89         IDirectMusicCollectionImpl_IUnknown_Release
90 };
91
92 /* IDirectMusicCollectionImpl IDirectMusicCollection part: */
93 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface (LPDIRECTMUSICCOLLECTION iface, REFIID riid, LPVOID *ppobj) {
94         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
95         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
96 }
97
98 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef (LPDIRECTMUSICCOLLECTION iface) {
99         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
100         return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
101 }
102
103 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_Release (LPDIRECTMUSICCOLLECTION iface) {
104         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
105         return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
106 }
107
108 /* IDirectMusicCollection Interface follow: */
109 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwPatch, IDirectMusicInstrument** ppInstrument) {
110         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
111         DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
112         struct list *listEntry;
113         DWORD dwInstPatch;
114
115         TRACE("(%p, %d, %p)\n", This, dwPatch, ppInstrument);
116         
117         LIST_FOR_EACH (listEntry, &This->Instruments) {
118                 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
119                 IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, &dwInstPatch);
120                 if (dwPatch == dwInstPatch) {
121                         *ppInstrument = (LPDIRECTMUSICINSTRUMENT)tmpEntry->pInstrument;
122                         IDirectMusicInstrument_AddRef (tmpEntry->pInstrument);
123                         IDirectMusicInstrumentImpl_Custom_Load (tmpEntry->pInstrument, This->pStm); /* load instrument before returning it */
124                         TRACE(": returning instrument %p\n", *ppInstrument);
125                         return S_OK;
126                 }
127                         
128         }
129         TRACE(": instrument not found\n");
130         
131         return DMUS_E_INVALIDPATCH;
132 }
133
134 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument (LPDIRECTMUSICCOLLECTION iface, DWORD dwIndex, DWORD* pdwPatch, LPWSTR pwszName, DWORD dwNameLen) {
135         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, CollectionVtbl, iface);
136         unsigned int r = 0;
137         DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
138         struct list *listEntry;
139        DWORD dwLen;
140                 
141         TRACE("(%p, %d, %p, %p, %d)\n", This, dwIndex, pdwPatch, pwszName, dwNameLen);
142         LIST_FOR_EACH (listEntry, &This->Instruments) {
143                 tmpEntry = LIST_ENTRY(listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry);
144                 if (r == dwIndex) {
145                         ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, tmpEntry->pInstrument, pInstrument);
146                         IDirectMusicInstrument_GetPatch (tmpEntry->pInstrument, pdwPatch);
147                        if (pwszName) {
148                                dwLen = min(strlenW(pInstrument->wszName),dwNameLen-1);
149                                memcpy (pwszName, pInstrument->wszName, dwLen * sizeof(WCHAR));
150                                pwszName[dwLen] = '\0';
151                        }
152                         return S_OK;
153                 }
154                 r++;            
155         }
156         
157         return S_FALSE;
158 }
159
160 static const IDirectMusicCollectionVtbl DirectMusicCollection_Collection_Vtbl = {
161         IDirectMusicCollectionImpl_IDirectMusicCollection_QueryInterface,
162         IDirectMusicCollectionImpl_IDirectMusicCollection_AddRef,
163         IDirectMusicCollectionImpl_IDirectMusicCollection_Release,
164         IDirectMusicCollectionImpl_IDirectMusicCollection_GetInstrument,
165         IDirectMusicCollectionImpl_IDirectMusicCollection_EnumInstrument
166 };
167
168 /* IDirectMusicCollectionImpl IDirectMusicObject part: */
169 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
170         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
171         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
172 }
173
174 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
175         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
176         return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
177 }
178
179 static ULONG WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
180         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
181         return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
182 }
183
184 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
185         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
186         TRACE("(%p, %p)\n", This, pDesc);
187         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
188         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
189         return S_OK;
190 }
191
192 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
193         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
194         TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
195         
196         /* According to MSDN, we should copy only given values, not whole struct */     
197         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
198                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
199         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
200                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
201         if (pDesc->dwValidData & DMUS_OBJ_NAME)
202                lstrcpynW(This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
203         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
204                lstrcpynW(This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
205         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
206                lstrcpynW(This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
207         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
208                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
209         if (pDesc->dwValidData & DMUS_OBJ_DATE)
210                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
211         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
212                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
213                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
214         }
215         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
216                 /* according to MSDN, we copy the stream */
217                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
218         }
219         
220         /* add new flags */
221         This->pDesc->dwValidData |= pDesc->dwValidData;
222
223         return S_OK;
224 }
225
226 static HRESULT WINAPI IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
227         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, ObjectVtbl, iface);
228         DMUS_PRIVATE_CHUNK Chunk;
229         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
230         LARGE_INTEGER liMove; /* used when skipping chunks */
231
232         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
233         
234         /* FIXME: should this be determined from stream? */
235         pDesc->dwValidData |= DMUS_OBJ_CLASS;
236         memcpy (&pDesc->guidClass, &CLSID_DirectMusicCollection, sizeof(CLSID));
237         
238         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
239         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
240         switch (Chunk.fccID) {  
241                 case FOURCC_RIFF: {
242                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
243                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
244                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
245                         StreamCount = 0;
246                         if (Chunk.fccID == mmioFOURCC('D','L','S',' ')) {
247                                 TRACE_(dmfile)(": collection form\n");
248                                 do {
249                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
250                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
251                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
252                                         switch (Chunk.fccID) {
253                                                 case FOURCC_DLID: {
254                                                         TRACE_(dmfile)(": GUID chunk\n");
255                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
256                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
257                                                         break;
258                                                 }
259                                                 case DMUS_FOURCC_VERSION_CHUNK: {
260                                                         TRACE_(dmfile)(": version chunk\n");
261                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
262                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
263                                                         break;
264                                                 }
265                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
266                                                         TRACE_(dmfile)(": category chunk\n");
267                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
268                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
269                                                         break;
270                                                 }
271                                                 case FOURCC_LIST: {
272                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
273                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
274                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
275                                                         ListCount[0] = 0;
276                                                         switch (Chunk.fccID) {
277                                                                 /* pure INFO list, such can be found in dls collections */
278                                                                 case mmioFOURCC('I','N','F','O'): {
279                                                                         TRACE_(dmfile)(": INFO list\n");
280                                                                         do {
281                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
282                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
283                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
284                                                                                 switch (Chunk.fccID) {
285                                                                                         case mmioFOURCC('I','N','A','M'):{
286                                                                                                 CHAR szName[DMUS_MAX_NAME];                                                                                             
287                                                                                                 TRACE_(dmfile)(": name chunk\n");
288                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
289                                                                                                 IStream_Read (pStream, szName, Chunk.dwSize, NULL);
290                                                                                                 MultiByteToWideChar (CP_ACP, 0, szName, -1, pDesc->wszName, DMUS_MAX_NAME);
291                                                                                                 if (even_or_odd(Chunk.dwSize)) {
292                                                                                                         ListCount[0] ++;
293                                                                                                         liMove.QuadPart = 1;
294                                                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
295                                                                                                 }
296                                                                                                 break;
297                                                                                         }
298                                                                                         case mmioFOURCC('I','A','R','T'): {
299                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
300                                                                                                 if (even_or_odd(Chunk.dwSize)) {
301                                                                                                         ListCount[0] ++;
302                                                                                                         Chunk.dwSize++;
303                                                                                                 }
304                                                                                                 liMove.QuadPart = Chunk.dwSize;
305                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
306                                                                                                 break;
307                                                                                         }
308                                                                                         case mmioFOURCC('I','C','O','P'): {
309                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
310                                                                                                 if (even_or_odd(Chunk.dwSize)) {
311                                                                                                         ListCount[0] ++;
312                                                                                                         Chunk.dwSize++;
313                                                                                                 }
314                                                                                                 liMove.QuadPart = Chunk.dwSize;
315                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
316                                                                                                 break;
317                                                                                         }
318                                                                                         case mmioFOURCC('I','S','B','J'): {
319                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
320                                                                                                 if (even_or_odd(Chunk.dwSize)) {
321                                                                                                         ListCount[0] ++;
322                                                                                                         Chunk.dwSize++;
323                                                                                                 }
324                                                                                                 liMove.QuadPart = Chunk.dwSize;
325                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
326                                                                                                 break;
327                                                                                         }
328                                                                                         case mmioFOURCC('I','C','M','T'): {
329                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
330                                                                                                 if (even_or_odd(Chunk.dwSize)) {
331                                                                                                         ListCount[0] ++;
332                                                                                                         Chunk.dwSize++;
333                                                                                                 }
334                                                                                                 liMove.QuadPart = Chunk.dwSize;
335                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
336                                                                                                 break;
337                                                                                         }
338                                                                                         default: {
339                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
340                                                                                                 if (even_or_odd(Chunk.dwSize)) {
341                                                                                                         ListCount[0] ++;
342                                                                                                         Chunk.dwSize++;
343                                                                                                 }
344                                                                                                 liMove.QuadPart = Chunk.dwSize;
345                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
346                                                                                                 break;                                          
347                                                                                         }
348                                                                                 }
349                                                                                 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
350                                                                         } while (ListCount[0] < ListSize[0]);
351                                                                         break;
352                                                                 }
353                                                                 default: {
354                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
355                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
356                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
357                                                                         break;                                          
358                                                                 }
359                                                         }
360                                                         break;
361                                                 }       
362                                                 default: {
363                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
364                                                         liMove.QuadPart = Chunk.dwSize;
365                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
366                                                         break;                                          
367                                                 }
368                                         }
369                                         TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
370                                 } while (StreamCount < StreamSize);
371                         } else {
372                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
373                                 liMove.QuadPart = StreamSize;
374                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
375                                 return E_FAIL;
376                         }
377                 
378                         TRACE_(dmfile)(": reading finished\n");
379                         break;
380                 }
381                 default: {
382                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
383                         liMove.QuadPart = Chunk.dwSize;
384                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
385                         return DMUS_E_INVALIDFILE;
386                 }
387         }       
388         
389         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
390         
391         return S_OK;
392 }
393
394 static const IDirectMusicObjectVtbl DirectMusicCollection_Object_Vtbl = {
395         IDirectMusicCollectionImpl_IDirectMusicObject_QueryInterface,
396         IDirectMusicCollectionImpl_IDirectMusicObject_AddRef,
397         IDirectMusicCollectionImpl_IDirectMusicObject_Release,
398         IDirectMusicCollectionImpl_IDirectMusicObject_GetDescriptor,
399         IDirectMusicCollectionImpl_IDirectMusicObject_SetDescriptor,
400         IDirectMusicCollectionImpl_IDirectMusicObject_ParseDescriptor
401 };
402
403 /* IDirectMusicCollectionImpl IPersistStream part: */
404 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
405         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
406         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
407 }
408
409 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
410         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
411         return IDirectMusicCollectionImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
412 }
413
414 static ULONG WINAPI IDirectMusicCollectionImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
415         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
416         return IDirectMusicCollectionImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
417 }
418
419 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
420         return E_NOTIMPL;
421 }
422
423 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
424         return E_NOTIMPL;
425 }
426
427 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
428         ICOM_THIS_MULTI(IDirectMusicCollectionImpl, PersistStreamVtbl, iface);
429
430         DMUS_PRIVATE_CHUNK Chunk;
431         DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
432         LARGE_INTEGER liMove; /* used when skipping chunks */
433         ULARGE_INTEGER dlibCollectionPosition, dlibInstrumentPosition, dlibWavePoolPosition;
434         
435         IStream_AddRef (pStm); /* add count for later references */
436         liMove.QuadPart = 0;
437         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibCollectionPosition); /* store offset, in case it'll be needed later */
438         This->liCollectionPosition.QuadPart = dlibCollectionPosition.QuadPart;
439         This->pStm = pStm;
440         
441         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
442         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
443         switch (Chunk.fccID) {  
444                 case FOURCC_RIFF: {
445                         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
446                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
447                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
448                         StreamCount = 0;
449                         switch (Chunk.fccID) {
450                                 case FOURCC_DLS: {
451                                         TRACE_(dmfile)(": collection form\n");
452                                         do {
453                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
454                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
455                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
456                                                 switch (Chunk.fccID) {
457                                                         case FOURCC_COLH: {
458                                                                 TRACE_(dmfile)(": collection header chunk\n");
459                                                                 This->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
460                                                                 IStream_Read (pStm, This->pHeader, Chunk.dwSize, NULL);
461                                                                 break;                                                          
462                                                         }
463                                                         case FOURCC_DLID: {
464                                                                 TRACE_(dmfile)(": DLID (GUID) chunk\n");
465                                                                 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
466                                                                 IStream_Read (pStm, &This->pDesc->guidObject, Chunk.dwSize, NULL);
467                                                                 break;
468                                                         }
469                                                         case FOURCC_VERS: {
470                                                                 TRACE_(dmfile)(": version chunk\n");
471                                                                 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
472                                                                 IStream_Read (pStm, &This->pDesc->vVersion, Chunk.dwSize, NULL);
473                                                                 break;
474                                                         }
475                                                         case FOURCC_PTBL: {
476                                                                 TRACE_(dmfile)(": pool table chunk\n");
477                                                                 This->pPoolTable = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(POOLTABLE));
478                                                                 IStream_Read (pStm, This->pPoolTable, sizeof(POOLTABLE), NULL);
479                                                                 Chunk.dwSize -= sizeof(POOLTABLE);
480                                                                 This->pPoolCues = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, This->pPoolTable->cCues*sizeof(POOLCUE));
481                                                                 IStream_Read (pStm, This->pPoolCues, Chunk.dwSize, NULL);
482                                                                 break;
483                                                         }
484                                                         case FOURCC_LIST: {
485                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
486                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
487                                                                 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
488                                                                 ListCount[0] = 0;
489                                                                 switch (Chunk.fccID) {
490                                                                         case mmioFOURCC('I','N','F','O'): {
491                                                                                 TRACE_(dmfile)(": INFO list\n");
492                                                                                 do {
493                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
494                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
495                                                                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
496                                                                                         switch (Chunk.fccID) {
497                                                                                                 case mmioFOURCC('I','N','A','M'): {
498                                                                                                         CHAR szName[DMUS_MAX_NAME];
499                                                                                                         TRACE_(dmfile)(": name chunk\n");
500                                                                                                         This->pDesc->dwValidData |= DMUS_OBJ_NAME;
501                                                                                                         IStream_Read (pStm, szName, Chunk.dwSize, NULL);
502                                                                                                         MultiByteToWideChar (CP_ACP, 0, szName, -1, This->pDesc->wszName, DMUS_MAX_NAME);
503                                                                                                         if (even_or_odd(Chunk.dwSize)) {
504                                                                                                                 ListCount[0] ++;
505                                                                                                                 liMove.QuadPart = 1;
506                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
507                                                                                                         }
508                                                                                                         break;
509                                                                                                 }
510                                                                                                 case mmioFOURCC('I','A','R','T'): {
511                                                                                                         TRACE_(dmfile)(": artist chunk (ignored)\n");
512                                                                                                         if (even_or_odd(Chunk.dwSize)) {
513                                                                                                                 ListCount[0] ++;
514                                                                                                                 Chunk.dwSize++;
515                                                                                                         }
516                                                                                                         liMove.QuadPart = Chunk.dwSize;
517                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
518                                                                                                         break;
519                                                                                                 }
520                                                                                                 case mmioFOURCC('I','C','O','P'): {
521                                                                                                         TRACE_(dmfile)(": copyright chunk\n");
522                                                                                                         This->szCopyright = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
523                                                                                                         IStream_Read (pStm, This->szCopyright, Chunk.dwSize, NULL);
524                                                                                                         if (even_or_odd(Chunk.dwSize)) {
525                                                                                                                 ListCount[0] ++;
526                                                                                                                 liMove.QuadPart = 1;
527                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
528                                                                                                         }
529                                                                                                         break;
530                                                                                                 }
531                                                                                                 case mmioFOURCC('I','S','B','J'): {
532                                                                                                         TRACE_(dmfile)(": subject chunk (ignored)\n");
533                                                                                                         if (even_or_odd(Chunk.dwSize)) {
534                                                                                                                 ListCount[0] ++;
535                                                                                                                 Chunk.dwSize++;
536                                                                                                         }
537                                                                                                         liMove.QuadPart = Chunk.dwSize;
538                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
539                                                                                                         break;
540                                                                                                 }
541                                                                                                 case mmioFOURCC('I','C','M','T'): {
542                                                                                                         TRACE_(dmfile)(": comment chunk (ignored)\n");
543                                                                                                         if (even_or_odd(Chunk.dwSize)) {
544                                                                                                                 ListCount[0] ++;
545                                                                                                                 Chunk.dwSize++;
546                                                                                                         }
547                                                                                                         liMove.QuadPart = Chunk.dwSize;
548                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
549                                                                                                         break;
550                                                                                                 }
551                                                                                                 default: {
552                                                                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
553                                                                                                         if (even_or_odd(Chunk.dwSize)) {
554                                                                                                                 ListCount[0] ++;
555                                                                                                                 Chunk.dwSize++;
556                                                                                                         }
557                                                                                                         liMove.QuadPart = Chunk.dwSize;
558                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
559                                                                                                         break;                                          
560                                                                                                 }
561                                                                                         }
562                                                                                         TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
563                                                                                 } while (ListCount[0] < ListSize[0]);
564                                                                                 break;
565                                                                         }
566                                                                         case FOURCC_WVPL: {
567                                                                                 TRACE_(dmfile)(": wave pool list (mark & skip)\n");
568                                                                                 liMove.QuadPart = 0;
569                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibWavePoolPosition); /* store position */
570                                                                                 This->liWavePoolTablePosition.QuadPart = dlibWavePoolPosition.QuadPart;
571                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
572                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
573                                                                                 break;  
574                                                                         }
575                                                                         case FOURCC_LINS: {
576                                                                                 TRACE_(dmfile)(": instruments list\n");
577                                                                                 do {
578                                                                                         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
579                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
580                                                                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
581                                                                                         switch (Chunk.fccID) {
582                                                                                                 case FOURCC_LIST: {
583                                                                                                         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
584                                                                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
585                                                                                                         ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
586                                                                                                         ListCount[1] = 0;                                                                                                       
587                                                                                                         switch (Chunk.fccID) {
588                                                                                                                 case FOURCC_INS: {
589                                                                                                                         LPDMUS_PRIVATE_INSTRUMENTENTRY pNewInstrument = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_INSTRUMENTENTRY));
590                                                                                                                         TRACE_(dmfile)(": instrument list\n");
591                                                                                                                         DMUSIC_CreateDirectMusicInstrumentImpl (&IID_IDirectMusicInstrument, (LPVOID*)&pNewInstrument->pInstrument, NULL); /* only way to create this one... even M$ does it discretly */
592                                                                                                                         {
593                                                                                                                             ICOM_NAME_MULTI (IDirectMusicInstrumentImpl, InstrumentVtbl, pNewInstrument->pInstrument, pInstrument);
594                                                                                                                             liMove.QuadPart = 0;
595                                                                                                                             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, &dlibInstrumentPosition);
596                                                                                                                             pInstrument->liInstrumentPosition.QuadPart = dlibInstrumentPosition.QuadPart - (2*sizeof(FOURCC) + sizeof(DWORD)); /* store offset, it'll be needed later */
597
598                                                                                                                             do {
599                                                                                                                                 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
600                                                                                                                                 ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
601                                                                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
602                                                                                                                                 switch (Chunk.fccID) {
603                                                                                                                                         case FOURCC_INSH: {
604                                                                                                                                                 TRACE_(dmfile)(": instrument header chunk\n");
605                                                                                                                                                 pInstrument->pHeader = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
606                                                                                                                                                 IStream_Read (pStm, pInstrument->pHeader, Chunk.dwSize, NULL);
607                                                                                                                                                 break;  
608                                                                                                                                         }
609                                                                                                                                         case FOURCC_DLID: {
610                                                                                                                                                 TRACE_(dmfile)(": DLID (GUID) chunk\n");
611                                                                                                                                                 pInstrument->pInstrumentID = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, Chunk.dwSize);
612                                                                                                                                                 IStream_Read (pStm, pInstrument->pInstrumentID, Chunk.dwSize, NULL);
613                                                                                                                                                 break;
614                                                                                                                                         }
615                                                                                                                                         case FOURCC_LIST: {
616                                                                                                                                                 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                                
617                                                                                                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
618                                                                                                                                                 ListSize[2] = Chunk.dwSize - sizeof(FOURCC);
619                                                                                                                                                 ListCount[2] = 0;
620                                                                                                                                                 switch (Chunk.fccID) {
621                                                                                                                                                         default: {
622                                                                                                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
623                                                                                                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
624                                                                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
625                                                                                                                                                                 break;                                          
626                                                                                                                                                         }
627                                                                                                                                                 }
628                                                                                                                                                 break;
629                                                                                                                                         }                               
630                                                                                                                                         default: {
631                                                                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
632                                                                                                                                                 liMove.QuadPart = Chunk.dwSize;
633                                                                                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
634                                                                                                                                                 break;                                          
635                                                                                                                                         }
636                                                                                                                                 }
637                                                                                                                                 TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
638                                                                                                                             } while (ListCount[1] < ListSize[1]);
639                                                                                                                             /* DEBUG: dumps whole instrument object tree: */
640                                                                                                                             if (TRACE_ON(dmusic)) {
641                                                                                                                                 TRACE("*** IDirectMusicInstrument (%p) ***\n", pInstrument);
642                                                                                                                                 if (pInstrument->pInstrumentID)
643                                                                                                                                         TRACE(" - GUID = %s\n", debugstr_dmguid(pInstrument->pInstrumentID));
644                                                                                                                                 
645                                                                                                                                 TRACE(" - Instrument header:\n");
646                                                                                                                                 TRACE("    - cRegions: %d\n", pInstrument->pHeader->cRegions);
647                                                                                                                                 TRACE("    - Locale:\n");
648                                                                                                                                 TRACE("       - ulBank: %d\n", pInstrument->pHeader->Locale.ulBank);
649                                                                                                                                 TRACE("       - ulInstrument: %d\n", pInstrument->pHeader->Locale.ulInstrument);
650                                                                                                                                 TRACE("       => dwPatch: %d\n", MIDILOCALE2Patch(&pInstrument->pHeader->Locale));
651                                                                                                                             }
652                                                                                                                             list_add_tail (&This->Instruments, &pNewInstrument->entry);
653                                                                                                                         }
654                                                                                                                         break;
655                                                                                                                 }
656                                                                                                         }
657                                                                                                         break;
658                                                                                                 }
659                                                                                                 default: {
660                                                                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
661                                                                                                         liMove.QuadPart = Chunk.dwSize;
662                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
663                                                                                                         break;                                          
664                                                                                                 }
665                                                                                         }
666                                                                                         TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
667                                                                                 } while (ListCount[0] < ListSize[0]);
668                                                                                 break;
669                                                                         }
670                                                                         default: {
671                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
672                                                                                 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
673                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
674                                                                                 break;                                          
675                                                                         }
676                                                                 }
677                                                                 break;
678                                                         }       
679                                                         default: {
680                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
681                                                                 liMove.QuadPart = Chunk.dwSize;
682                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
683                                                                 break;                                          
684                                                         }
685                                                 }
686                                                 TRACE_(dmfile)(": StreamCount = %d < StreamSize = %d\n", StreamCount, StreamSize);
687                                         } while (StreamCount < StreamSize);
688                                         break;
689                                 }
690                                 default: {
691                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
692                                         liMove.QuadPart = StreamSize;
693                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
694                                         return E_FAIL;
695                                 }
696                         }
697                         TRACE_(dmfile)(": reading finished\n");
698                         break;
699                 }
700                 default: {
701                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
702                         liMove.QuadPart = Chunk.dwSize;
703                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
704                         return E_FAIL;
705                 }
706         }
707
708         /* DEBUG: dumps whole collection object tree: */
709         if (TRACE_ON(dmusic)) {
710                 int r = 0;
711                 DMUS_PRIVATE_INSTRUMENTENTRY *tmpEntry;
712                 struct list *listEntry;
713
714                 TRACE("*** IDirectMusicCollection (%p) ***\n", This->CollectionVtbl);
715                 if (This->pDesc->dwValidData & DMUS_OBJ_OBJECT)
716                         TRACE(" - GUID = %s\n", debugstr_dmguid(&This->pDesc->guidObject));
717                 if (This->pDesc->dwValidData & DMUS_OBJ_VERSION)
718                         TRACE(" - Version = %i,%i,%i,%i\n", (This->pDesc->vVersion.dwVersionMS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionMS & 0x0000FFFF,
719                                 (This->pDesc->vVersion.dwVersionLS >> 8) & 0x0000FFFF, This->pDesc->vVersion.dwVersionLS & 0x0000FFFF);
720                 if (This->pDesc->dwValidData & DMUS_OBJ_NAME)
721                         TRACE(" - Name = %s\n", debugstr_w(This->pDesc->wszName));
722                 
723                 TRACE(" - Collection header:\n");
724                 TRACE("    - cInstruments: %d\n", This->pHeader->cInstruments);
725                 TRACE(" - Instruments:\n");
726                 
727                 LIST_FOR_EACH (listEntry, &This->Instruments) {
728                         tmpEntry = LIST_ENTRY( listEntry, DMUS_PRIVATE_INSTRUMENTENTRY, entry );
729                         TRACE("    - Instrument[%i]: %p\n", r, tmpEntry->pInstrument);
730                         r++;
731                 }
732         }
733         
734         return S_OK;
735 }
736
737 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
738         return E_NOTIMPL;
739 }
740
741 static HRESULT WINAPI IDirectMusicCollectionImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
742         return E_NOTIMPL;
743 }
744
745 static const IPersistStreamVtbl DirectMusicCollection_PersistStream_Vtbl = {
746         IDirectMusicCollectionImpl_IPersistStream_QueryInterface,
747         IDirectMusicCollectionImpl_IPersistStream_AddRef,
748         IDirectMusicCollectionImpl_IPersistStream_Release,
749         IDirectMusicCollectionImpl_IPersistStream_GetClassID,
750         IDirectMusicCollectionImpl_IPersistStream_IsDirty,
751         IDirectMusicCollectionImpl_IPersistStream_Load,
752         IDirectMusicCollectionImpl_IPersistStream_Save,
753         IDirectMusicCollectionImpl_IPersistStream_GetSizeMax
754 };
755
756
757 /* for ClassFactory */
758 HRESULT WINAPI DMUSIC_CreateDirectMusicCollectionImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
759         IDirectMusicCollectionImpl* obj;
760         
761         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicCollectionImpl));
762         if (NULL == obj) {
763                 *ppobj = NULL;
764                 return E_OUTOFMEMORY;
765         }
766         obj->UnknownVtbl = &DirectMusicCollection_Unknown_Vtbl;
767         obj->CollectionVtbl = &DirectMusicCollection_Collection_Vtbl;
768         obj->ObjectVtbl = &DirectMusicCollection_Object_Vtbl;
769         obj->PersistStreamVtbl = &DirectMusicCollection_PersistStream_Vtbl;
770         obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
771         DM_STRUCT_INIT(obj->pDesc);
772         obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
773         memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicCollection, sizeof (CLSID));
774         obj->ref = 0; /* will be inited by QueryInterface */
775         list_init (&obj->Instruments);
776         
777         return IDirectMusicCollectionImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
778 }