quartz: More filesource fixes.
[wine] / dlls / dmband / bandtrack.c
1 /* IDirectMusicBandTrack 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  * IDirectMusicBandTrack implementation
27  */
28 /* IDirectMusicBandTrack IUnknown part: */
29 static HRESULT WINAPI IDirectMusicBandTrack_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
30         ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
31         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
32
33         if (IsEqualIID (riid, &IID_IUnknown)) {
34                 *ppobj = (LPUNKNOWN)&This->UnknownVtbl;
35                 IUnknown_AddRef (iface);
36                 return S_OK;
37         } else if (IsEqualIID (riid, &IID_IDirectMusicTrack)
38           || IsEqualIID (riid, &IID_IDirectMusicTrack8)) {
39                 *ppobj = (LPDIRECTMUSICTRACK8)&This->TrackVtbl;
40                 IUnknown_AddRef (iface);
41                 return S_OK;
42         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
43                 *ppobj = (LPPERSISTSTREAM)&This->PersistStreamVtbl;
44                 IUnknown_AddRef (iface);
45                 return S_OK;
46         }
47         
48         WARN("(%p, %s,%p): not found\n", This, debugstr_dmguid(riid), ppobj);
49         return E_NOINTERFACE;
50 }
51
52 static ULONG WINAPI IDirectMusicBandTrack_IUnknown_AddRef (LPUNKNOWN iface) {
53   ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
54   ULONG ref = InterlockedIncrement(&This->ref);
55
56   TRACE("(%p) : AddRef from %d\n", This, ref - 1);
57
58   DMBAND_LockModule();
59
60   return ref;
61 }
62
63 static ULONG WINAPI IDirectMusicBandTrack_IUnknown_Release (LPUNKNOWN iface) {
64   ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
65   ULONG ref = InterlockedDecrement(&This->ref);
66
67   TRACE("(%p) : ReleaseRef to %d\n", This, ref);
68
69   if (ref == 0) {
70     HeapFree(GetProcessHeap(), 0, This);
71   }
72
73   DMBAND_UnlockModule();
74   
75   return ref;
76 }
77
78 static const IUnknownVtbl DirectMusicBandTrack_Unknown_Vtbl = {
79   IDirectMusicBandTrack_IUnknown_QueryInterface,
80   IDirectMusicBandTrack_IUnknown_AddRef,
81   IDirectMusicBandTrack_IUnknown_Release
82 };
83
84 /* IDirectMusicBandTrack IDirectMusicTrack8 part: */
85 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_QueryInterface (LPDIRECTMUSICTRACK8 iface, REFIID riid, LPVOID *ppobj) {
86   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
87   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
88 }
89
90 static ULONG WINAPI IDirectMusicBandTrack_IDirectMusicTrack_AddRef (LPDIRECTMUSICTRACK8 iface) {
91   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
92   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
93 }
94
95 static ULONG WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Release (LPDIRECTMUSICTRACK8 iface) {
96   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
97   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
98 }
99
100 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Init (LPDIRECTMUSICTRACK8 iface, IDirectMusicSegment* pSegment) {
101   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
102   FIXME("(%p, %p): stub\n", This, pSegment);
103   return S_OK;
104 }
105
106 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_InitPlay (LPDIRECTMUSICTRACK8 iface, IDirectMusicSegmentState* pSegmentState, IDirectMusicPerformance* pPerformance, void** ppStateData, DWORD dwVirtualTrack8ID, DWORD dwFlags) {
107   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
108   FIXME("(%p, %p, %p, %p, %d, %d): stub\n", This, pSegmentState, pPerformance, ppStateData, dwVirtualTrack8ID, dwFlags);
109   return S_OK;
110 }
111
112 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_EndPlay (LPDIRECTMUSICTRACK8 iface, void* pStateData) {
113   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
114   FIXME("(%p, %p): stub\n", This, pStateData);
115   return S_OK;
116 }
117
118 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Play (LPDIRECTMUSICTRACK8 iface, void* pStateData, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, MUSIC_TIME mtOffset, DWORD dwFlags, IDirectMusicPerformance* pPerf, IDirectMusicSegmentState* pSegSt, DWORD dwVirtualID) {
119   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
120   
121   FIXME("(%p, %p, %ld, %ld, %ld, %d, %p, %p, %d): semi-stub\n", This, pStateData, mtStart, mtEnd, mtOffset, dwFlags, pPerf, pSegSt, dwVirtualID);
122   /* Sends following pMSG:
123      - DMUS_PATCH_PMSG
124      - DMUS_TRANSPOSE_PMSG
125      - DMUS_CHANNEL_PRIORITY_PMSG
126      - DMUS_MIDI_PMSG
127   */
128
129   return S_OK;
130 }
131
132 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_GetParam (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
133   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
134   FIXME("(%p, %s, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), mtTime, pmtNext, pParam);
135   return S_OK;
136 }
137
138 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_SetParam (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, MUSIC_TIME mtTime, void* pParam) {
139   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
140   FIXME("(%p, %s, %ld, %p): stub\n", This, debugstr_dmguid(rguidType), mtTime, pParam);
141   return S_OK;
142 }
143
144 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_IsParamSupported (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType) {
145   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
146   
147   TRACE("(%p, %s)\n", This, debugstr_dmguid(rguidType));
148   if (IsEqualGUID (rguidType, &GUID_BandParam)
149       || IsEqualGUID (rguidType, &GUID_Clear_All_Bands)
150       || IsEqualGUID (rguidType, &GUID_ConnectToDLSCollection)
151       || IsEqualGUID (rguidType, &GUID_Disable_Auto_Download)
152       || IsEqualGUID (rguidType, &GUID_Download)
153       || IsEqualGUID (rguidType, &GUID_DownloadToAudioPath)
154       || IsEqualGUID (rguidType, &GUID_Enable_Auto_Download)
155       || IsEqualGUID (rguidType, &GUID_IDirectMusicBand)
156       || IsEqualGUID (rguidType, &GUID_StandardMIDIFile)
157       || IsEqualGUID (rguidType, &GUID_Unload)
158       || IsEqualGUID (rguidType, &GUID_UnloadFromAudioPath)) {
159     TRACE("param supported\n");
160     return S_OK;
161   }
162   
163   TRACE("param unsupported\n");
164   return DMUS_E_TYPE_UNSUPPORTED;
165 }
166
167 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_AddNotificationType (LPDIRECTMUSICTRACK8 iface, REFGUID rguidNotificationType) {
168   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
169   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
170   return S_OK;
171 }
172
173 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_RemoveNotificationType (LPDIRECTMUSICTRACK8 iface, REFGUID rguidNotificationType) {
174   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
175   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
176   return S_OK;
177 }
178
179 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Clone (LPDIRECTMUSICTRACK8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, IDirectMusicTrack** ppTrack) {
180   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
181   FIXME("(%p, %ld, %ld, %p): stub\n", This, mtStart, mtEnd, ppTrack);
182   return S_OK;
183 }
184
185 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_PlayEx (LPDIRECTMUSICTRACK8 iface, void* pStateData, REFERENCE_TIME rtStart, REFERENCE_TIME rtEnd, REFERENCE_TIME rtOffset, DWORD dwFlags, IDirectMusicPerformance* pPerf, IDirectMusicSegmentState* pSegSt, DWORD dwVirtualID) {
186   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
187   FIXME("(%p, %p, 0x%s, 0x%s, 0x%s, %d, %p, %p, %d): stub\n", This, pStateData, wine_dbgstr_longlong(rtStart),
188       wine_dbgstr_longlong(rtEnd), wine_dbgstr_longlong(rtOffset), dwFlags, pPerf, pSegSt, dwVirtualID);
189   return S_OK;
190 }
191
192 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_GetParamEx (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, REFERENCE_TIME rtTime, REFERENCE_TIME* prtNext, void* pParam, void* pStateData, DWORD dwFlags) {
193   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
194   FIXME("(%p, %s, 0x%s, %p, %p, %p, %d): stub\n", This, debugstr_dmguid(rguidType),
195       wine_dbgstr_longlong(rtTime), prtNext, pParam, pStateData, dwFlags);
196   return S_OK;
197 }
198
199 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_SetParamEx (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, REFERENCE_TIME rtTime, void* pParam, void* pStateData, DWORD dwFlags) {
200   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
201   FIXME("(%p, %s, 0x%s, %p, %p, %d): stub\n", This, debugstr_dmguid(rguidType),
202       wine_dbgstr_longlong(rtTime), pParam, pStateData, dwFlags);
203   return S_OK;
204 }
205
206 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Compose (LPDIRECTMUSICTRACK8 iface, IUnknown* pContext, DWORD dwTrackGroup, IDirectMusicTrack** ppResultTrack) {
207   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
208   FIXME("(%p, %p, %d, %p): stub\n", This, pContext, dwTrackGroup, ppResultTrack);
209   return S_OK;
210 }
211
212 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Join (LPDIRECTMUSICTRACK8 iface, IDirectMusicTrack* pNewTrack, MUSIC_TIME mtJoin, IUnknown* pContext, DWORD dwTrackGroup, IDirectMusicTrack** ppResultTrack) {
213   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
214   FIXME("(%p, %p, %ld, %p, %d, %p): stub\n", This, pNewTrack, mtJoin, pContext, dwTrackGroup, ppResultTrack);
215   return S_OK;
216 }
217
218 static const IDirectMusicTrack8Vtbl DirectMusicBandTrack_DirectMusicTrack_Vtbl = {
219   IDirectMusicBandTrack_IDirectMusicTrack_QueryInterface,
220   IDirectMusicBandTrack_IDirectMusicTrack_AddRef,
221   IDirectMusicBandTrack_IDirectMusicTrack_Release,
222   IDirectMusicBandTrack_IDirectMusicTrack_Init,
223   IDirectMusicBandTrack_IDirectMusicTrack_InitPlay,
224   IDirectMusicBandTrack_IDirectMusicTrack_EndPlay,
225   IDirectMusicBandTrack_IDirectMusicTrack_Play,
226   IDirectMusicBandTrack_IDirectMusicTrack_GetParam,
227   IDirectMusicBandTrack_IDirectMusicTrack_SetParam,
228   IDirectMusicBandTrack_IDirectMusicTrack_IsParamSupported,
229   IDirectMusicBandTrack_IDirectMusicTrack_AddNotificationType,
230   IDirectMusicBandTrack_IDirectMusicTrack_RemoveNotificationType,
231   IDirectMusicBandTrack_IDirectMusicTrack_Clone,
232   IDirectMusicBandTrack_IDirectMusicTrack_PlayEx,
233   IDirectMusicBandTrack_IDirectMusicTrack_GetParamEx,
234   IDirectMusicBandTrack_IDirectMusicTrack_SetParamEx,
235   IDirectMusicBandTrack_IDirectMusicTrack_Compose,
236   IDirectMusicBandTrack_IDirectMusicTrack_Join
237 };
238
239 /* IDirectMusicBandTrack IPersistStream part: */
240 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
241   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
242   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
243 }
244
245 static ULONG WINAPI IDirectMusicBandTrack_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
246   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
247   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
248 }
249
250 static ULONG WINAPI IDirectMusicBandTrack_IPersistStream_Release (LPPERSISTSTREAM iface) {
251   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
252   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
253 }
254
255 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
256   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
257   TRACE("(%p, %p)\n", This, pClassID);
258   *pClassID = CLSID_DirectMusicBandTrack;
259   return S_OK;
260 }
261
262 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
263   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
264   FIXME("(%p): stub, always S_FALSE\n", This);
265   return S_FALSE;
266 }
267
268 static HRESULT IDirectMusicBandTrack_IPersistStream_LoadBand (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicBand** ppBand,
269                                                               DMUS_PRIVATE_BAND_ITEM_HEADER* pHeader) {
270
271   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
272   HRESULT hr = E_FAIL;
273   IPersistStream* pPersistStream = NULL;
274   
275   hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
276   if (FAILED(hr)) {
277     ERR(": could not create object\n");
278     return hr;
279   }
280   /* acquire PersistStream interface */
281   hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
282   if (FAILED(hr)) {
283     ERR(": could not acquire IPersistStream\n");
284     return hr;
285   }
286   /* load */
287   hr = IPersistStream_Load (pPersistStream, pClonedStream);
288   if (FAILED(hr)) {
289     ERR(": failed to load object\n");
290     return hr;
291   }
292   
293   /* release all loading-related stuff */
294   IPersistStream_Release (pPersistStream);
295
296   /*
297    * @TODO insert pBand into This
298    */
299   if (SUCCEEDED(hr)) {
300     LPDMUS_PRIVATE_BAND pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_BAND));
301     if (NULL == pNewBand) {
302       ERR(": no more memory\n");
303       return  E_OUTOFMEMORY;
304     }
305     pNewBand->BandHeader = *pHeader;
306     pNewBand->pBand = (IDirectMusicBandImpl*)((char*)(*ppBand) - offsetof(IDirectMusicBandImpl,BandVtbl));
307     IDirectMusicBand_AddRef(*ppBand);
308     list_add_tail (&This->Bands, &pNewBand->entry);
309   }
310
311   return S_OK;
312 }
313
314 static HRESULT IDirectMusicBandTrack_IPersistStream_ParseBandsList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
315
316   /*ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);*/
317   HRESULT hr = E_FAIL;
318   DMUS_PRIVATE_CHUNK Chunk;
319   DWORD StreamSize, ListSize[3], ListCount[3];
320   LARGE_INTEGER liMove; /* used when skipping chunks */
321
322   IDirectMusicBand* pBand = NULL;
323   DMUS_PRIVATE_BAND_ITEM_HEADER header;
324
325   memset(&header, 0, sizeof header);
326
327   if (pChunk->fccID != DMUS_FOURCC_BANDS_LIST) {
328     ERR_(dmfile)(": %s chunk should be a BANDS list\n", debugstr_fourcc (pChunk->fccID));
329     return E_FAIL;
330   }  
331
332   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
333   ListCount[0] = 0;
334
335   do {
336     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
337     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
338     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
339     switch (Chunk.fccID) {
340     case FOURCC_LIST: {
341       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
342       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
343       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
344       ListCount[1] = 0;
345       do {
346         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
347         ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
348         TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
349         switch (Chunk.fccID) { 
350         case DMUS_FOURCC_BANDITEM_CHUNK: {
351           DMUS_IO_BAND_ITEM_HEADER tmp_header;
352           TRACE_(dmfile)(": Band Item chunk v1\n");
353           
354           IStream_Read (pStm, &tmp_header, sizeof(DMUS_IO_BAND_ITEM_HEADER), NULL);
355           TRACE_(dmfile)(" - lBandTime: %lu\n", tmp_header.lBandTime);
356           
357           header.dwVersion = 1;
358           header.lBandTime = tmp_header.lBandTime;
359           break;
360         }
361         case DMUS_FOURCC_BANDITEM_CHUNK2: { 
362           DMUS_IO_BAND_ITEM_HEADER2 tmp_header2;
363           TRACE_(dmfile)(": Band Item chunk v2\n");
364           
365           IStream_Read (pStm, &tmp_header2, sizeof(DMUS_IO_BAND_ITEM_HEADER2), NULL);
366           TRACE_(dmfile)(" - lBandTimeLogical: %lu\n", tmp_header2.lBandTimeLogical);
367           TRACE_(dmfile)(" - lBandTimePhysical: %lu\n", tmp_header2.lBandTimePhysical);
368           
369           header.dwVersion = 2;
370           header.lBandTimeLogical = tmp_header2.lBandTimeLogical;
371           header.lBandTimePhysical = tmp_header2.lBandTimePhysical;
372           break;
373         }
374         case FOURCC_RIFF: { 
375           IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
376           TRACE_(dmfile)(": RIFF chunk of type %s\n", debugstr_fourcc(Chunk.fccID));
377           StreamSize = Chunk.dwSize - sizeof(FOURCC);
378           switch (Chunk.fccID) {
379           case DMUS_FOURCC_BAND_FORM: {
380             LPSTREAM pClonedStream = NULL;
381             TRACE_(dmfile)(": BAND RIFF\n");
382             
383             IStream_Clone (pStm, &pClonedStream);
384             
385             liMove.QuadPart = 0;
386             liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
387             IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
388             
389             hr = IDirectMusicBandTrack_IPersistStream_LoadBand (iface, pClonedStream, &pBand, &header);
390             if (FAILED(hr)) {
391               ERR(": could not load track\n");
392               return hr;
393             }
394             IStream_Release (pClonedStream);
395             
396             IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release at as it inserted */
397             
398             /** now safe move the cursor */
399             liMove.QuadPart = StreamSize;
400             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
401             break;
402           }
403           default: {
404             TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
405             liMove.QuadPart = StreamSize;
406             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
407             break;                                              
408           }
409           }
410           break;
411         }
412         default: {
413           TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
414           liMove.QuadPart = Chunk.dwSize;
415           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
416           break;                                                
417          }
418         }
419         TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
420       } while (ListCount[1] < ListSize[1]);
421       break;
422     }
423     default: {
424       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
425       liMove.QuadPart = Chunk.dwSize;
426       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
427       break;                                            
428     }
429     }
430     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
431   } while (ListCount[0] < ListSize[0]);
432
433   return S_OK;
434 }
435
436 static HRESULT IDirectMusicBandTrack_IPersistStream_ParseBandTrackForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
437
438   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
439   HRESULT hr = E_FAIL;
440   DMUS_PRIVATE_CHUNK Chunk;
441   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
442   LARGE_INTEGER liMove; /* used when skipping chunks */
443
444   if (pChunk->fccID != DMUS_FOURCC_BANDTRACK_FORM) {
445     ERR_(dmfile)(": %s chunk should be a BANDTRACK form\n", debugstr_fourcc (pChunk->fccID));
446     return E_FAIL;
447   }  
448
449   StreamSize = pChunk->dwSize - sizeof(FOURCC);
450   StreamCount = 0;
451
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 = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
456     
457     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
458     if (FAILED(hr)) return hr;
459
460     if (hr == S_FALSE) {
461       switch (Chunk.fccID) {
462       case DMUS_FOURCC_BANDTRACK_CHUNK: {
463         TRACE_(dmfile)(": BandTrack chunk\n");
464         IStream_Read (pStm, &This->header, sizeof(DMUS_IO_BAND_TRACK_HEADER), NULL);
465         TRACE_(dmfile)(" - bAutoDownload: %u\n", This->header.bAutoDownload);
466         break;
467       }   
468       case FOURCC_LIST: {
469         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
470         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
471         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
472         ListCount[0] = 0;
473         switch (Chunk.fccID) {
474         case DMUS_FOURCC_UNFO_LIST: { 
475           TRACE_(dmfile)(": UNFO list\n");
476           do {
477             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
478             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
479             TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
480             
481             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
482             if (FAILED(hr)) return hr;
483             
484             if (hr == S_FALSE) {
485               switch (Chunk.fccID) {
486               default: {
487                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
488                 liMove.QuadPart = Chunk.dwSize;
489                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
490                 break;                          
491               }
492               }
493             }  
494             TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
495           } while (ListCount[0] < ListSize[0]);
496           break;
497         }
498         case DMUS_FOURCC_BANDS_LIST: {
499           TRACE_(dmfile)(": TRACK list\n");
500           hr = IDirectMusicBandTrack_IPersistStream_ParseBandsList (iface, &Chunk, pStm);
501           if (FAILED(hr)) return hr;
502           break;
503         }
504         default: {
505           TRACE_(dmfile)(": unknown (skipping)\n");
506           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
507           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
508           break;                                                
509         }
510         }
511         break;
512       }
513       default: {
514         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
515         liMove.QuadPart = Chunk.dwSize;
516         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
517         break;                                          
518       }
519       }
520     }
521     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
522   } while (StreamCount < StreamSize);  
523
524   return S_OK;
525 }
526
527
528 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
529   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
530
531   DMUS_PRIVATE_CHUNK Chunk;
532   LARGE_INTEGER liMove;
533   HRESULT hr;
534
535   TRACE("(%p, %p): Loading\n", This, pStm);
536
537   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
538   TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
539   switch (Chunk.fccID) {
540   case FOURCC_RIFF: {
541     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
542     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
543     switch (Chunk.fccID) {
544     case DMUS_FOURCC_BANDTRACK_FORM: {
545       TRACE_(dmfile)(": Band track form\n");
546       hr = IDirectMusicBandTrack_IPersistStream_ParseBandTrackForm (iface, &Chunk, pStm);
547       if (FAILED(hr)) return hr;
548       break;    
549     }
550     default: {
551       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
552       liMove.QuadPart = Chunk.dwSize;
553       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
554       return E_FAIL;
555     }
556     }
557     TRACE_(dmfile)(": reading finished\n");
558     break;
559   }
560   default: {
561     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
562     liMove.QuadPart = Chunk.dwSize;
563     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
564     return E_FAIL;
565   }
566   }
567
568   return S_OK;
569 }
570
571 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
572   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
573   FIXME("(%p): Saving not implemented yet\n", This);
574   return E_NOTIMPL;
575 }
576
577 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
578   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
579   FIXME("(%p, %p): stub\n", This, pcbSize);
580   return E_NOTIMPL;
581 }
582
583 static const IPersistStreamVtbl DirectMusicBandTrack_PerststStream_Vtbl = {
584   IDirectMusicBandTrack_IPersistStream_QueryInterface,
585   IDirectMusicBandTrack_IPersistStream_AddRef,
586   IDirectMusicBandTrack_IPersistStream_Release,
587   IDirectMusicBandTrack_IPersistStream_GetClassID,
588   IDirectMusicBandTrack_IPersistStream_IsDirty,
589   IDirectMusicBandTrack_IPersistStream_Load,
590   IDirectMusicBandTrack_IPersistStream_Save,
591   IDirectMusicBandTrack_IPersistStream_GetSizeMax
592 };
593
594 /* for ClassFactory */
595 HRESULT WINAPI DMUSIC_CreateDirectMusicBandTrack (LPCGUID lpcGUID, LPVOID *ppobj, LPUNKNOWN pUnkOuter) {
596   IDirectMusicBandTrack* track;
597         
598   track = HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandTrack));
599   if (NULL == track) {
600     *ppobj = NULL;
601     return E_OUTOFMEMORY;
602   }
603   track->UnknownVtbl = &DirectMusicBandTrack_Unknown_Vtbl;
604   track->TrackVtbl = &DirectMusicBandTrack_DirectMusicTrack_Vtbl;
605   track->PersistStreamVtbl = &DirectMusicBandTrack_PerststStream_Vtbl;
606   track->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
607   DM_STRUCT_INIT(track->pDesc);
608   track->pDesc->dwValidData |= DMUS_OBJ_CLASS;
609   track->pDesc->guidClass = CLSID_DirectMusicBandTrack;
610   track->ref = 0; /* will be inited by QueryInterface */
611   list_init (&track->Bands);
612
613   return IDirectMusicBandTrack_IUnknown_QueryInterface ((LPUNKNOWN)&track->UnknownVtbl, lpcGUID, ppobj);
614 }