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