Implemented syslink control.
[wine] / dlls / dmime / segment.c
1 /* IDirectMusicSegment8 Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  * Copyright (C) 2003-2004 Raphael Junqueira
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "dmime_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25
26 /*****************************************************************************
27  * IDirectMusicSegmentImpl implementation
28  */
29 /* IDirectMusicSegment IUnknown part: */
30 HRESULT WINAPI IDirectMusicSegment8Impl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
31         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, UnknownVtbl, iface);
32         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
33         
34         if (IsEqualIID (riid, &IID_IUnknown)) {
35                 *ppobj = (LPVOID)&This->UnknownVtbl;
36                 IDirectMusicSegment8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
37                 return S_OK;    
38         } else if (IsEqualIID (riid, &IID_IDirectMusicSegment)
39         || IsEqualIID (riid, &IID_IDirectMusicSegment2)
40         || IsEqualIID (riid, &IID_IDirectMusicSegment8)) {
41                 *ppobj = (LPVOID)&This->SegmentVtbl;
42                 IDirectMusicSegment8Impl_IDirectMusicSegment8_AddRef ((LPDIRECTMUSICSEGMENT8)&This->SegmentVtbl);
43                 return S_OK;
44         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
45                 *ppobj = (LPVOID)&This->ObjectVtbl;
46                 IDirectMusicSegment8Impl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);            
47                 return S_OK;
48         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
49                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
50                 IDirectMusicSegment8Impl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);             
51                 return S_OK;
52         }
53         
54         WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
55         return E_NOINTERFACE;
56 }
57
58 ULONG WINAPI IDirectMusicSegment8Impl_IUnknown_AddRef (LPUNKNOWN iface) {
59         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, UnknownVtbl, iface);
60         TRACE("(%p): AddRef from %ld\n", This, This->ref);
61         return ++(This->ref);
62 }
63
64 ULONG WINAPI IDirectMusicSegment8Impl_IUnknown_Release (LPUNKNOWN iface) {
65         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, UnknownVtbl, iface);
66         ULONG ref = --This->ref;
67         TRACE("(%p): ReleaseRef to %ld\n", This, This->ref);
68         if (ref == 0) {
69                 HeapFree(GetProcessHeap(), 0, This);
70         }
71         return ref;
72 }
73
74 ICOM_VTABLE(IUnknown) DirectMusicSegment8_Unknown_Vtbl = {
75   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
76   IDirectMusicSegment8Impl_IUnknown_QueryInterface,
77   IDirectMusicSegment8Impl_IUnknown_AddRef,
78   IDirectMusicSegment8Impl_IUnknown_Release
79 };
80
81 /* IDirectMusicSegmentImpl IDirectMusicSegment part: */
82 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_QueryInterface (LPDIRECTMUSICSEGMENT8 iface, REFIID riid, LPVOID *ppobj) {
83   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
84   return IDirectMusicSegment8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
85 }
86
87 ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_AddRef (LPDIRECTMUSICSEGMENT8 iface) {
88   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
89   return IDirectMusicSegment8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
90 }
91
92 ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Release (LPDIRECTMUSICSEGMENT8 iface) {
93   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
94   return IDirectMusicSegment8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
95 }
96
97 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLength (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtLength) {
98   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
99   TRACE("(%p, %p)\n", This, pmtLength);
100   if (NULL == pmtLength) {
101     return E_POINTER;
102   }
103   *pmtLength = This->header.mtLength;
104   return S_OK;
105 }
106
107 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLength (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtLength) {
108   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
109   TRACE("(%p, %ld)\n", This, mtLength);
110   This->header.mtLength = mtLength;
111   return S_OK;
112 }
113
114 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetRepeats (LPDIRECTMUSICSEGMENT8 iface, DWORD* pdwRepeats) { 
115   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
116   TRACE("(%p, %p)\n", This, pdwRepeats);
117   if (NULL == pdwRepeats) {
118     return E_POINTER;
119   }
120   *pdwRepeats = This->header.dwRepeats;
121   return S_OK;
122 }
123
124 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetRepeats (LPDIRECTMUSICSEGMENT8 iface, DWORD dwRepeats) {
125   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
126   TRACE("(%p, %ld)\n", This, dwRepeats);        
127   This->header.dwRepeats = dwRepeats;
128   return S_OK;
129 }
130
131 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetDefaultResolution (LPDIRECTMUSICSEGMENT8 iface, DWORD* pdwResolution) {
132   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
133   TRACE("(%p, %p)\n", This, pdwResolution);
134   if (NULL == pdwResolution) {
135     return E_POINTER;
136   }
137   *pdwResolution = This->header.dwResolution;
138   return S_OK;
139 }
140
141 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetDefaultResolution (LPDIRECTMUSICSEGMENT8 iface, DWORD dwResolution) {
142   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
143   TRACE("(%p, %ld)\n", This, dwResolution);
144   This->header.dwResolution = dwResolution;
145   return S_OK;
146 }
147
148 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, IDirectMusicTrack** ppTrack) {
149   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
150   
151   CLSID pIt_clsid;
152   struct list* pEntry = NULL;
153   LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
154   IPersistStream* pCLSIDStream = NULL;
155   HRESULT hr = S_OK;
156
157   TRACE("(%p, %s, %ld, 0x%lx, %p)\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, ppTrack);
158
159   if (NULL == ppTrack) {
160     return E_POINTER;
161   }
162
163   LIST_FOR_EACH (pEntry, &This->Tracks) {
164     pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
165     TRACE(" - %p -> 0x%lx,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
166     if (0xFFFFFFFF != dwGroupBits && 0 == (pIt->dwGroupBits & dwGroupBits)) continue ;
167     if (FALSE == IsEqualGUID(&GUID_NULL, rguidType)) {
168       /**
169        * it rguidType is not null we must check if CLSID are equals
170        * and the unqiue way to get it its using IPersistStream Interface
171        */
172       hr = IDirectMusicTrack_QueryInterface(pIt->pTrack, &IID_IPersistStream, (void**) &pCLSIDStream);
173       if (FAILED(hr)) {
174         ERR("(%p): object %p don't implement IPersistStream Interface. Expect a crash (critical problem)\n", This, pIt->pTrack);
175         continue ;
176       }
177       hr = IPersistStream_GetClassID(pCLSIDStream, &pIt_clsid);
178       IPersistStream_Release(pCLSIDStream); pCLSIDStream = NULL;
179       if (FAILED(hr)) {
180         ERR("(%p): non-implemented GetClassID for object %p\n", This, pIt->pTrack);
181         continue ;
182       }
183       TRACE(" - %p -> %s\n", pIt, debugstr_dmguid(&pIt_clsid));
184       if (FALSE == IsEqualGUID(&pIt_clsid, rguidType)) continue ;
185     }
186     if (0 == dwIndex) {
187       *ppTrack = pIt->pTrack;
188       IDirectMusicTrack_AddRef(*ppTrack);
189       return S_OK;
190     } 
191     --dwIndex;
192   }  
193   return DMUS_E_NOT_FOUND;
194 }
195
196 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrackGroup (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicTrack* pTrack, DWORD* pdwGroupBits) {
197   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
198
199   struct list* pEntry = NULL;
200   LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
201
202   TRACE("(%p, %p, %p)\n", This, pTrack, pdwGroupBits);
203
204   if (NULL == pdwGroupBits) {
205     return E_POINTER;
206   }
207
208   LIST_FOR_EACH (pEntry, &This->Tracks) {
209     pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
210     TRACE(" - %p -> %ld,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
211     if (NULL != pIt && pIt->pTrack == pTrack) {
212       *pdwGroupBits = pIt->dwGroupBits;
213       return S_OK;
214     }
215   }
216
217   return DMUS_E_NOT_FOUND;
218 }
219
220 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicTrack* pTrack, DWORD dwGroupBits) {
221   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
222
223   DWORD i = 0;
224   struct list* pEntry = NULL;
225   LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
226   LPDMUS_PRIVATE_SEGMENT_TRACK pNewSegTrack = NULL;
227
228   TRACE("(%p, %p, %ld)\n", This, pTrack, dwGroupBits);
229
230   LIST_FOR_EACH (pEntry, &This->Tracks) {
231     i++;
232     pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
233     TRACE(" - #%lu: %p -> %ld,%p\n", i, pIt, pIt->dwGroupBits, pIt->pTrack);
234     if (NULL != pIt && pIt->pTrack == pTrack) {
235       ERR("(%p, %p): track is already in list\n", This, pTrack);
236       return E_FAIL;
237     }
238   }
239
240   pNewSegTrack = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_SEGMENT_TRACK));
241   if (NULL == pNewSegTrack) {
242     ERR(": no more memory\n");
243     return  E_OUTOFMEMORY;
244   }
245   pNewSegTrack->dwGroupBits = dwGroupBits;
246   pNewSegTrack->pTrack = pTrack;
247   IDirectMusicTrack_Init(pTrack, iface);
248   IDirectMusicTrack_AddRef(pTrack);
249   list_add_tail (&This->Tracks, &pNewSegTrack->entry);
250
251   return S_OK;
252 }
253
254 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveTrack (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicTrack* pTrack) {
255   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
256
257   struct list* pEntry = NULL;
258   LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
259
260   TRACE("(%p, %p)\n", This, pTrack);
261
262   LIST_FOR_EACH (pEntry, &This->Tracks) {
263     pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
264     if (pIt->pTrack == pTrack) {
265       TRACE("(%p, %p): track in list\n", This, pTrack);
266       
267       list_remove(&pIt->entry);
268       IDirectMusicTrack_Init(pIt->pTrack, NULL);
269       IDirectMusicTrack_Release(pIt->pTrack);
270       HeapFree(GetProcessHeap(), 0, pIt);   
271
272       return S_OK;
273     }
274   }
275   
276   return S_FALSE;
277 }
278
279 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_InitPlay (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicSegmentState** ppSegState, IDirectMusicPerformance* pPerformance, DWORD dwFlags) {
280   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
281   HRESULT hr;
282   FIXME("(%p, %p, %p, %ld): semi-stub\n", This, ppSegState, pPerformance, dwFlags);
283   if (NULL == ppSegState) {
284     return E_POINTER;
285   }
286   hr = DMUSIC_CreateDirectMusicSegmentStateImpl(&IID_IDirectMusicSegmentState, (void**) ppSegState, NULL);
287   if (FAILED(hr)) {
288     return hr;
289   }
290   /* TODO: DMUS_SEGF_FLAGS */
291   return S_OK;
292 }
293
294 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetGraph (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicGraph** ppGraph) {
295   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
296   FIXME("(%p, %p): semi-stub\n", This, ppGraph);
297   if (NULL == ppGraph) {
298     return E_POINTER;
299   }
300   if (NULL == This->pGraph) {
301     return DMUS_E_NOT_FOUND;
302   }
303   /** 
304    * should return This, as seen in msdn 
305    * http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/htm/idirectmusicsegment8getgraph.asp
306    * "...The segment object implements IDirectMusicGraph directly..."
307    */
308   *ppGraph = This->pGraph;
309   IDirectMusicGraph_AddRef(This->pGraph);
310   return S_OK;
311 }
312
313 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetGraph (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicGraph* pGraph) {
314   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
315   FIXME("(%p, %p): to complete\n", This, pGraph);
316   if (NULL != This->pGraph) {
317     IDirectMusicGraph_Release(This->pGraph);
318   }
319   This->pGraph = pGraph;
320   if (NULL != This->pGraph) {
321     IDirectMusicGraph_AddRef(This->pGraph);
322   }
323   return S_OK;
324 }
325
326 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_AddNotificationType (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidNotificationType) {
327   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
328   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
329   return S_OK;
330 }
331
332 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveNotificationType (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidNotificationType) {
333   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
334   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
335   return S_OK;
336 }
337
338 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetParam (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
339   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
340
341   CLSID pIt_clsid;
342   struct list* pEntry = NULL;
343   IDirectMusicTrack* pTrack = NULL;
344   IPersistStream* pCLSIDStream = NULL;
345   LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
346   HRESULT hr = S_OK;
347
348   FIXME("(%p, %s, 0x%lx, %ld, %ld, %p, %p)\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
349   
350   if (DMUS_SEG_ANYTRACK == dwIndex) {
351     
352     LIST_FOR_EACH (pEntry, &This->Tracks) {
353       pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
354
355       TRACE(" - %p -> 0x%lx,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
356
357       if (0xFFFFFFFF != dwGroupBits && 0 == (pIt->dwGroupBits & dwGroupBits)) continue ;
358       hr = IPersistStream_GetClassID(pCLSIDStream, &pIt_clsid);
359       IPersistStream_Release(pCLSIDStream); pCLSIDStream = NULL;
360       if (FAILED(hr)) {
361         ERR("(%p): non-implemented GetClassID for object %p\n", This, pIt->pTrack);
362         continue ;
363       }
364       if (FALSE == IsEqualGUID(&pIt_clsid, rguidType)) continue ;
365       if (FAILED(IDirectMusicTrack_IsParamSupported(pIt->pTrack, rguidType))) continue ;
366       hr = IDirectMusicTrack_GetParam(pIt->pTrack, rguidType, mtTime, pmtNext, pParam);
367       if (SUCCEEDED(hr)) return hr;
368     }
369     ERR("(%p): not found\n", This);
370     return DMUS_E_TRACK_NOT_FOUND;
371   } 
372
373   hr = IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack(iface, &GUID_NULL, dwGroupBits, dwIndex, &pTrack);
374   if (FAILED(hr)) {
375     ERR("(%p): not found\n", This);
376     return DMUS_E_TRACK_NOT_FOUND;
377   }
378
379   hr = IDirectMusicTrack_GetParam(pTrack, rguidType, mtTime, pmtNext, pParam);
380   IDirectMusicTrack_Release(pTrack); pTrack = NULL;
381
382   return hr;
383 }
384
385 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetParam (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void* pParam) {
386   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
387   FIXME("(%p, %s, %ld, %ld, %ld, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
388   return S_OK;
389 }
390
391 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Clone (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, IDirectMusicSegment** ppSegment) {
392   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
393   FIXME("(%p, %ld, %ld, %p): stub\n", This, mtStart, mtEnd, ppSegment);
394   return S_OK;
395 }
396
397 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetStartPoint (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart) {
398   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
399   TRACE("(%p, %ld)\n", This, mtStart);
400   if (mtStart >= This->header.mtLength) {
401     return DMUS_E_OUT_OF_RANGE;
402   }
403   This->header.mtPlayStart = mtStart;
404   return S_OK;
405 }
406
407 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetStartPoint (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtStart) {
408   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
409   TRACE("(%p, %p)\n", This, pmtStart);
410   if (NULL == pmtStart) {
411     return E_POINTER;
412   }
413   *pmtStart = This->header.mtPlayStart;
414   return S_OK;
415 }
416
417 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLoopPoints (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd) {
418   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
419   TRACE("(%p, %ld, %ld)\n", This, mtStart, mtEnd);
420   if (mtStart >= This->header.mtLength || mtEnd > This->header.mtLength || mtStart > mtEnd) {
421     return DMUS_E_OUT_OF_RANGE;
422   }
423   This->header.mtLoopStart = mtStart;
424   This->header.mtLoopEnd   = mtEnd;
425   return S_OK;
426 }
427
428 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLoopPoints (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtStart, MUSIC_TIME* pmtEnd) {
429   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
430   TRACE("(%p, %p, %p)\n", This, pmtStart, pmtEnd);
431   if (NULL == pmtStart || NULL == pmtEnd) {
432     return E_POINTER;
433   }
434   *pmtStart = This->header.mtLoopStart;
435   *pmtEnd   = This->header.mtLoopEnd;
436   return S_OK;
437 }
438
439 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetPChannelsUsed (LPDIRECTMUSICSEGMENT8 iface, DWORD dwNumPChannels, DWORD* paPChannels) {
440         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
441         FIXME("(%p, %ld, %p): stub\n", This, dwNumPChannels, paPChannels);      
442         return S_OK;
443 }
444
445 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetTrackConfig (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidTrackClassID, DWORD dwGroupBits, DWORD dwIndex, DWORD dwFlagsOn, DWORD dwFlagsOff) {
446         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
447         FIXME("(%p, %s, %ld, %ld, %ld, %ld): stub\n", This, debugstr_dmguid(rguidTrackClassID), dwGroupBits, dwIndex, dwFlagsOn, dwFlagsOff);
448         return S_OK;
449 }
450
451 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetAudioPathConfig (LPDIRECTMUSICSEGMENT8 iface, IUnknown** ppAudioPathConfig){
452         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
453         FIXME("(%p, %p): stub\n", This, ppAudioPathConfig);
454         return S_OK;
455 }
456
457 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Compose (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtTime, IDirectMusicSegment* pFromSegment, IDirectMusicSegment* pToSegment, IDirectMusicSegment** ppComposedSegment) {
458         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
459         FIXME("(%p, %ld, %p, %p, %p): stub\n", This, mtTime, pFromSegment, pToSegment, ppComposedSegment);
460         return S_OK;
461 }
462
463 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Download (LPDIRECTMUSICSEGMENT8 iface, IUnknown *pAudioPath) {
464         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
465         FIXME("(%p, %p): stub\n", This, pAudioPath);
466         return S_OK;
467 }
468
469 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Unload (LPDIRECTMUSICSEGMENT8 iface, IUnknown *pAudioPath) {
470         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
471         FIXME("(%p, %p): stub\n", This, pAudioPath);
472         return S_OK;
473 }
474
475 ICOM_VTABLE(IDirectMusicSegment8) DirectMusicSegment8_Segment_Vtbl = {
476     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
477         IDirectMusicSegment8Impl_IDirectMusicSegment8_QueryInterface,
478         IDirectMusicSegment8Impl_IDirectMusicSegment8_AddRef,
479         IDirectMusicSegment8Impl_IDirectMusicSegment8_Release,
480         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLength,
481         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLength,
482         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetRepeats,
483         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetRepeats,
484         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetDefaultResolution,
485         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetDefaultResolution,
486         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack,
487         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrackGroup,
488         IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack,
489         IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveTrack,
490         IDirectMusicSegment8Impl_IDirectMusicSegment8_InitPlay,
491         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetGraph,
492         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetGraph,
493         IDirectMusicSegment8Impl_IDirectMusicSegment8_AddNotificationType,
494         IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveNotificationType,
495         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetParam,
496         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetParam,
497         IDirectMusicSegment8Impl_IDirectMusicSegment8_Clone,
498         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetStartPoint,
499         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetStartPoint,
500         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLoopPoints,
501         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLoopPoints,
502         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetPChannelsUsed,
503         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetTrackConfig,
504         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetAudioPathConfig,
505         IDirectMusicSegment8Impl_IDirectMusicSegment8_Compose,
506         IDirectMusicSegment8Impl_IDirectMusicSegment8_Download,
507         IDirectMusicSegment8Impl_IDirectMusicSegment8_Unload
508 };
509
510 /* IDirectMusicSegment8Impl IDirectMusicObject part: */
511 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
512         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
513         return IDirectMusicSegment8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
514 }
515
516 ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
517         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
518         return IDirectMusicSegment8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
519 }
520
521 ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
522         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
523         return IDirectMusicSegment8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
524 }
525
526 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
527         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
528         TRACE("(%p, %p)\n", This, pDesc);
529         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
530         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
531         return S_OK;
532 }
533
534 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
535         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
536         TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
537         
538         /* According to MSDN, we should copy only given values, not whole struct */     
539         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
540                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
541         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
542                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
543         if (pDesc->dwValidData & DMUS_OBJ_NAME)
544                 strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
545         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
546                 strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);             
547         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
548                 strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);             
549         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
550                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
551         if (pDesc->dwValidData & DMUS_OBJ_DATE)
552                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
553         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
554                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
555                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
556         }
557         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
558                 /* according to MSDN, we copy the stream */
559                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
560         }
561         
562         /* add new flags */
563         This->pDesc->dwValidData |= pDesc->dwValidData;
564
565         return S_OK;
566 }
567
568 HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
569         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
570         DMUS_PRIVATE_CHUNK Chunk;
571         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
572         LARGE_INTEGER liMove; /* used when skipping chunks */
573
574         TRACE("(%p,%p, %p)\n", This, pStream, pDesc);
575         
576         /* FIXME: should this be determined from stream? */
577         pDesc->dwValidData |= DMUS_OBJ_CLASS;
578         memcpy (&pDesc->guidClass, &CLSID_DirectMusicSegment, sizeof(CLSID));
579         
580         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
581         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
582         switch (Chunk.fccID) {  
583                 case FOURCC_RIFF: {
584                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
585                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
586                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
587                         StreamCount = 0;
588                         if (Chunk.fccID == DMUS_FOURCC_SEGMENT_FORM) {
589                                 TRACE_(dmfile)(": segment form\n");
590                                 do {
591                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
592                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
593                                         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
594                                         switch (Chunk.fccID) {
595                                                 case DMUS_FOURCC_GUID_CHUNK: {
596                                                         TRACE_(dmfile)(": GUID chunk\n");
597                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
598                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
599                                                         break;
600                                                 }
601                                                 case DMUS_FOURCC_VERSION_CHUNK: {
602                                                         TRACE_(dmfile)(": version chunk\n");
603                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
604                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
605                                                         break;
606                                                 }
607                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
608                                                         TRACE_(dmfile)(": category chunk\n");
609                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
610                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
611                                                         break;
612                                                 }
613                                                 case FOURCC_LIST: {
614                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
615                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
616                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
617                                                         ListCount[0] = 0;
618                                                         switch (Chunk.fccID) {
619                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
620                                                                 case DMUS_FOURCC_UNFO_LIST: {
621                                                                         TRACE_(dmfile)(": UNFO list\n");
622                                                                         do {
623                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
624                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
625                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
626                                                                                 switch (Chunk.fccID) {
627                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
628                                                                                            (though strings seem to be valid unicode) */
629                                                                                         case mmioFOURCC('I','N','A','M'):
630                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
631                                                                                                 TRACE_(dmfile)(": name chunk\n");
632                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
633                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
634                                                                                                 break;
635                                                                                         }
636                                                                                         case mmioFOURCC('I','A','R','T'):
637                                                                                         case DMUS_FOURCC_UART_CHUNK: {
638                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
639                                                                                                 liMove.QuadPart = Chunk.dwSize;
640                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
641                                                                                                 break;
642                                                                                         }
643                                                                                         case mmioFOURCC('I','C','O','P'):
644                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
645                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
646                                                                                                 liMove.QuadPart = Chunk.dwSize;
647                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
648                                                                                                 break;
649                                                                                         }
650                                                                                         case mmioFOURCC('I','S','B','J'):
651                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
652                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
653                                                                                                 liMove.QuadPart = Chunk.dwSize;
654                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
655                                                                                                 break;
656                                                                                         }
657                                                                                         case mmioFOURCC('I','C','M','T'):
658                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
659                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
660                                                                                                 liMove.QuadPart = Chunk.dwSize;
661                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
662                                                                                                 break;
663                                                                                         }
664                                                                                         default: {
665                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
666                                                                                                 liMove.QuadPart = Chunk.dwSize;
667                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
668                                                                                                 break;                                          
669                                                                                         }
670                                                                                 }
671                                                                                 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
672                                                                         } while (ListCount[0] < ListSize[0]);
673                                                                         break;
674                                                                 }
675                                                                 case DMUS_FOURCC_TRACK_LIST: {
676                                                                   TRACE_(dmfile)(": TRACK list\n");
677                                                                   do {
678                                                                     IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
679                                                                     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
680                                                                     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
681                                                                     switch (Chunk.fccID) {
682                                                                     default: {
683                                                                       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
684                                                                       liMove.QuadPart = Chunk.dwSize;
685                                                                       IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
686                                                                       break;                                            
687                                                                     }
688                                                                     }
689                                                                     TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
690                                                                   } while (ListCount[0] < ListSize[0]);
691                                                                   break;
692                                                                 }
693                                                                 default: {
694                                                                   TRACE_(dmfile)(": unknown (skipping)\n");
695                                                                   liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
696                                                                   IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
697                                                                   break;                                                
698                                                                 }
699                                                         }
700                                                         break;
701                                                 }       
702                                                 default: {
703                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
704                                                         liMove.QuadPart = Chunk.dwSize;
705                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
706                                                         break;                                          
707                                                 }
708                                         }
709                                         TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
710                                 } while (StreamCount < StreamSize);
711                                 break;
712                         } else if (Chunk.fccID == mmioFOURCC('W','A','V','E')) {
713                                 TRACE_(dmfile)(": wave form (loading not yet implemented)\n");
714                                 liMove.QuadPart = StreamSize;
715                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
716                         } else {
717                                 TRACE_(dmfile)(": unexpected chunk (loading failed)\n");
718                                 liMove.QuadPart = StreamSize;
719                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
720                                 return E_FAIL;
721                         }
722                 
723                         TRACE_(dmfile)(": reading finished\n");
724                         break;
725                 }
726                 default: {
727                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
728                         liMove.QuadPart = Chunk.dwSize;
729                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
730                         return DMUS_E_INVALIDFILE;
731                 }
732         }       
733         
734         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
735         
736         return S_OK;
737 }
738
739 ICOM_VTABLE(IDirectMusicObject) DirectMusicSegment8_Object_Vtbl = {
740   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
741   IDirectMusicSegment8Impl_IDirectMusicObject_QueryInterface,
742   IDirectMusicSegment8Impl_IDirectMusicObject_AddRef,
743   IDirectMusicSegment8Impl_IDirectMusicObject_Release,
744   IDirectMusicSegment8Impl_IDirectMusicObject_GetDescriptor,
745   IDirectMusicSegment8Impl_IDirectMusicObject_SetDescriptor,
746   IDirectMusicSegment8Impl_IDirectMusicObject_ParseDescriptor
747 };
748
749 /* IDirectMusicSegment8Impl IPersistStream part: */
750 HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
751   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
752   return IDirectMusicSegment8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
753 }
754
755 ULONG WINAPI IDirectMusicSegment8Impl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
756   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
757   return IDirectMusicSegment8Impl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
758 }
759
760 ULONG WINAPI IDirectMusicSegment8Impl_IPersistStream_Release (LPPERSISTSTREAM iface) {
761   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
762   return IDirectMusicSegment8Impl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
763 }
764
765 HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
766   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
767   TRACE("(%p, %p)\n", This, pClassID);
768   memcpy(pClassID, &CLSID_DirectMusicSegment, sizeof(CLSID));
769   return S_OK;
770 }
771
772 HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
773   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
774   FIXME("(%p): stub, always S_FALSE\n", This);
775   return S_FALSE;
776 }
777
778 static HRESULT IDirectMusicSegment8Impl_IPersistStream_LoadTrack (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicTrack** ppTrack, 
779                                                                   DMUS_IO_TRACK_HEADER* pTrack_hdr) {
780
781   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
782   HRESULT hr = E_FAIL;
783   IPersistStream* pPersistStream = NULL;
784   
785   hr = CoCreateInstance (&pTrack_hdr->guidClassID, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicTrack, (LPVOID*) ppTrack);
786   if (FAILED(hr)) {
787     ERR(": could not create object\n");
788     return hr;
789   }
790   /* acquire PersistStream interface */
791   hr = IDirectMusicTrack_QueryInterface (*ppTrack, &IID_IPersistStream, (LPVOID*) &pPersistStream);
792   if (FAILED(hr)) {
793     ERR(": could not acquire IPersistStream\n");
794     return hr;
795   }
796   /* load */
797   hr = IPersistStream_Load (pPersistStream, pClonedStream);
798   if (FAILED(hr)) {
799     ERR(": failed to load object\n");
800     return hr;
801   }
802   
803   /* release all loading-related stuff */
804   IPersistStream_Release (pPersistStream);
805
806   hr = IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack ((LPDIRECTMUSICSEGMENT8)&This->SegmentVtbl, *ppTrack, pTrack_hdr->dwGroup); /* at dsPosition */
807   if (FAILED(hr)) {
808     ERR(": could not insert track\n");
809     return hr;
810   }
811
812   return S_OK;
813 }
814
815 static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseTrackForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
816
817   /*ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);*/
818   HRESULT hr = E_FAIL;
819   DMUS_PRIVATE_CHUNK Chunk;
820   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
821   LARGE_INTEGER liMove; /* used when skipping chunks */
822   
823   DMUS_IO_TRACK_HEADER        track_hdr;
824   DMUS_IO_TRACK_EXTRAS_HEADER track_xhdr;
825   IDirectMusicTrack*          pTrack = NULL;
826
827   if (pChunk->fccID != DMUS_FOURCC_TRACK_FORM) {
828     ERR_(dmfile)(": %s chunk should be a TRACK form\n", debugstr_fourcc (pChunk->fccID));
829     return E_FAIL;
830   }  
831
832   StreamSize = pChunk->dwSize - sizeof(FOURCC);
833   StreamCount = 0;
834
835   do {
836     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
837     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
838     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
839     
840     switch (Chunk.fccID) {
841     case DMUS_FOURCC_TRACK_CHUNK: {
842       TRACE_(dmfile)(": track chunck\n");
843       IStream_Read (pStm, &track_hdr, sizeof(DMUS_IO_TRACK_HEADER), NULL);
844       TRACE_(dmfile)(" - class: %s\n", debugstr_guid (&track_hdr.guidClassID));
845       TRACE_(dmfile)(" - dwGroup: %ld\n", track_hdr.dwGroup);
846       TRACE_(dmfile)(" - ckid: %s\n", debugstr_fourcc (track_hdr.ckid));
847       TRACE_(dmfile)(" - fccType: %s\n", debugstr_fourcc (track_hdr.fccType));
848       break;
849     }
850     case DMUS_FOURCC_TRACK_EXTRAS_CHUNK: {
851       TRACE_(dmfile)(": track extras chunck\n");
852       IStream_Read (pStm, &track_xhdr, sizeof(DMUS_IO_TRACK_EXTRAS_HEADER), NULL);
853       break;
854     }
855
856     case DMUS_FOURCC_COMMANDTRACK_CHUNK: {
857       TRACE_(dmfile)(": COMMANDTRACK track\n");
858       liMove.QuadPart = Chunk.dwSize;
859       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
860       break;
861     }
862
863     case FOURCC_LIST: {
864       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
865       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
866       ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
867       ListCount[0] = 0;
868       if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {
869         LPSTREAM pClonedStream = NULL;
870
871         TRACE_(dmfile)(": TRACK list\n");
872
873         IStream_Clone (pStm, &pClonedStream);
874
875         liMove.QuadPart = 0;
876         liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
877         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
878         
879         hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
880         if (FAILED(hr)) {
881           ERR(": could not load track\n");
882           return hr;
883         }
884         IStream_Release (pClonedStream);
885         
886         IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
887
888         liMove.QuadPart = ListSize[0];
889         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
890         
891       } else {
892         TRACE_(dmfile)(": unknown (skipping)\n");
893         liMove.QuadPart = Chunk.dwSize;
894         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
895       }
896       break;
897     }
898
899     case FOURCC_RIFF: {
900       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
901       TRACE_(dmfile)(": RIFF chunk of type %s\n", debugstr_fourcc(Chunk.fccID));
902
903       ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
904
905       if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {    
906         LPSTREAM pClonedStream = NULL;
907
908         TRACE_(dmfile)(": TRACK RIFF\n");
909
910         IStream_Clone (pStm, &pClonedStream);
911         
912         liMove.QuadPart = 0;
913         liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
914         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
915         
916         hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
917         if (FAILED(hr)) {
918           ERR(": could not load track\n");
919           return hr;
920         }
921         IStream_Release (pClonedStream);
922         
923         IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
924
925         /** now safe move the cursor */
926         liMove.QuadPart = ListSize[0];
927         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
928          
929       } else {
930         TRACE_(dmfile)(": unknown RIFF fmt (skipping)\n");
931         liMove.QuadPart = ListSize[0];
932         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
933       }
934       break;
935     }
936
937     default: {
938       if (0 == track_hdr.fccType && Chunk.fccID == track_hdr.ckid) {
939         LPSTREAM pClonedStream = NULL;
940
941         TRACE_(dmfile)(": TRACK solo\n");
942
943         IStream_Clone (pStm, &pClonedStream);
944         
945         liMove.QuadPart = 0;
946         liMove.QuadPart -= (sizeof(FOURCC) + sizeof(DWORD));
947         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
948
949         hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
950         if (FAILED(hr)) {
951           ERR(": could not load track\n");
952           return hr;
953         }
954         IStream_Release (pClonedStream);
955         
956         IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
957
958         liMove.QuadPart = Chunk.dwSize;
959         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
960
961         break;
962       }
963
964       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
965       liMove.QuadPart = Chunk.dwSize;
966       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
967       break;                                            
968     }
969     }
970     TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
971   } while (StreamCount < StreamSize);  
972
973   return S_OK;
974 }
975
976 static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseTrackList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, IDirectMusicSegment8Impl* This) {
977
978   HRESULT hr = E_FAIL;
979   DMUS_PRIVATE_CHUNK Chunk;
980   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
981   LARGE_INTEGER liMove; /* used when skipping chunks */
982
983   if (pChunk->fccID != DMUS_FOURCC_TRACK_LIST) {
984     ERR_(dmfile)(": %s chunk should be a TRACK list\n", debugstr_fourcc (pChunk->fccID));
985     return E_FAIL;
986   }  
987
988   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
989   ListCount[0] = 0;
990
991   do {
992     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
993     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
994     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
995     switch (Chunk.fccID) { 
996     case FOURCC_RIFF: {
997       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
998       TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
999       StreamSize = Chunk.dwSize - sizeof(FOURCC);
1000       StreamCount = 0;
1001       switch (Chunk.fccID) {
1002       case  DMUS_FOURCC_TRACK_FORM: {
1003         TRACE_(dmfile)(": TRACK form\n");
1004         hr = IDirectMusicSegment8Impl_IPersistStream_ParseTrackForm (iface, &Chunk, pStm);
1005         if (FAILED(hr)) return hr;      
1006         break;
1007       }
1008       default: {
1009         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
1010         liMove.QuadPart = StreamSize;
1011         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1012         break;
1013       }
1014       }
1015       break;
1016     }
1017     default: {
1018       TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
1019       liMove.QuadPart = Chunk.dwSize;
1020       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1021       break;                                            
1022     }
1023     }
1024     TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
1025   } while (ListCount[0] < ListSize[0]);
1026
1027   return S_OK;
1028 }
1029
1030 static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseSegmentForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, IDirectMusicSegment8Impl* This) {
1031
1032   HRESULT hr = E_FAIL;
1033   DMUS_PRIVATE_CHUNK Chunk;
1034   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
1035   LARGE_INTEGER liMove; /* used when skipping chunks */
1036
1037   if (pChunk->fccID != DMUS_FOURCC_SEGMENT_FORM) {
1038     ERR_(dmfile)(": %s chunk should be a segment form\n", debugstr_fourcc (pChunk->fccID));
1039     return E_FAIL;
1040   }
1041
1042   StreamSize = pChunk->dwSize - sizeof(FOURCC);
1043   StreamCount = 0;
1044
1045   do {
1046     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1047     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1048     TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1049     
1050     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
1051     if (FAILED(hr)) return hr;
1052     
1053     if (hr == S_FALSE) {
1054       switch (Chunk.fccID) {
1055       case DMUS_FOURCC_SEGMENT_CHUNK: {
1056         DWORD checkSz = sizeof(FOURCC);
1057         TRACE_(dmfile)(": segment chunk\n");
1058 #if 1
1059         /** DX 7 */
1060         IStream_Read (pStm, &This->header.dwRepeats,    sizeof(This->header.dwRepeats), NULL);
1061         checkSz += sizeof(This->header.dwRepeats);
1062         IStream_Read (pStm, &This->header.mtLength,     sizeof(This->header.mtLength), NULL);
1063         checkSz += sizeof(This->header.mtLength);
1064         IStream_Read (pStm, &This->header.mtPlayStart,  sizeof(This->header.mtPlayStart), NULL);
1065         checkSz += sizeof(This->header.mtPlayStart);
1066         IStream_Read (pStm, &This->header.mtLoopStart,  sizeof(This->header.mtLoopStart), NULL);
1067         checkSz += sizeof(This->header.mtLoopStart);
1068         IStream_Read (pStm, &This->header.mtLoopEnd,    sizeof(This->header.mtLoopEnd), NULL);
1069         checkSz += sizeof(This->header.mtLoopEnd);
1070         IStream_Read (pStm, &This->header.dwResolution, sizeof(This->header.dwResolution), NULL);
1071         checkSz += sizeof(This->header.dwResolution);
1072         TRACE_(dmfile)("dwRepeats: %lu\n", This->header.dwRepeats);
1073         TRACE_(dmfile)("mtLength: %lu\n",  This->header.mtLength);
1074         TRACE_(dmfile)("mtPlayStart: %lu\n",  This->header.mtPlayStart);
1075         TRACE_(dmfile)("mtLoopStart: %lu\n",  This->header.mtLoopStart);
1076         TRACE_(dmfile)("mtLoopEnd: %lu\n",  This->header.mtLoopEnd);
1077         TRACE_(dmfile)("dwResolution: %lu\n", This->header.dwResolution);
1078         /** DX 8 */
1079         if (Chunk.dwSize > checkSz) {
1080           IStream_Read (pStm, &This->header.rtLength,    sizeof(This->header.rtLength), NULL);
1081           checkSz += sizeof(This->header.rtLength);
1082           IStream_Read (pStm, &This->header.dwFlags,     sizeof(This->header.dwFlags), NULL);
1083           checkSz += sizeof(This->header.dwFlags);
1084         }
1085         /** DX 9 */
1086         if (Chunk.dwSize > checkSz) {
1087           IStream_Read (pStm, &This->header.rtLoopStart,  sizeof(This->header.rtLoopStart), NULL);
1088           checkSz += sizeof(This->header.rtLoopStart);
1089           IStream_Read (pStm, &This->header.rtLoopEnd,    sizeof(This->header.rtLoopEnd), NULL);
1090           checkSz += sizeof(This->header.rtLoopEnd);
1091           IStream_Read (pStm, &This->header.rtPlayStart,  sizeof(This->header.rtPlayStart), NULL);
1092           checkSz += sizeof(This->header.rtPlayStart);
1093         }
1094 #endif
1095         liMove.QuadPart = Chunk.dwSize - checkSz + sizeof(FOURCC);
1096         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1097         break;
1098       }
1099       case FOURCC_LIST: {
1100         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
1101         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
1102         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
1103         ListCount[0] = 0;
1104         switch (Chunk.fccID) {
1105         case DMUS_FOURCC_UNFO_LIST: {
1106           TRACE_(dmfile)(": UNFO list\n");
1107           do {
1108             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1109             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1110             TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1111
1112             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
1113             if (FAILED(hr)) return hr;
1114             
1115             if (hr == S_FALSE) {
1116               switch (Chunk.fccID) {
1117               default: {
1118                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
1119                 liMove.QuadPart = Chunk.dwSize;
1120                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1121                 break;                                          
1122               }
1123               }
1124             }
1125
1126             TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
1127           } while (ListCount[0] < ListSize[0]);
1128           break;
1129         }
1130         case DMUS_FOURCC_TRACK_LIST: {
1131           TRACE_(dmfile)(": TRACK list\n");
1132           hr = IDirectMusicSegment8Impl_IPersistStream_ParseTrackList (iface, &Chunk, pStm, This);
1133           if (FAILED(hr)) return hr;
1134           break;
1135         }
1136         default: {
1137           TRACE_(dmfile)(": unknown (skipping)\n");
1138           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
1139           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1140           break;                                                
1141         }
1142         }
1143         break;
1144       }
1145       default: {
1146         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
1147         liMove.QuadPart = Chunk.dwSize;
1148         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1149         break;                                          
1150       }
1151       }
1152     }
1153     TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
1154   } while (StreamCount < StreamSize);
1155
1156   return S_OK;
1157 }
1158
1159 static HRESULT IDirectMusicSegment8Impl_IPersistStream_LoadWave (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicObject** ppWaveObject) {
1160
1161   HRESULT hr = E_FAIL;
1162   IPersistStream* pPersistStream = NULL;
1163   
1164   hr = CoCreateInstance (&CLSID_DirectSoundWave, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*) ppWaveObject);
1165   if (FAILED(hr)) {
1166     ERR(": could not create object\n");
1167     return hr;
1168   }
1169   /* acquire PersistStream interface */
1170   hr = IDirectMusicObject_QueryInterface (*ppWaveObject, &IID_IPersistStream, (LPVOID*) &pPersistStream);
1171   if (FAILED(hr)) {
1172     ERR(": could not acquire IPersistStream\n");
1173     return hr;
1174   }
1175   /* load */
1176   hr = IPersistStream_Load (pPersistStream, pClonedStream);
1177   if (FAILED(hr)) {
1178     ERR(": failed to load object\n");
1179     return hr;
1180   }
1181   
1182   /* release all loading-related stuff */
1183   IPersistStream_Release (pPersistStream);
1184
1185   return S_OK;
1186 }
1187
1188 HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
1189   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
1190   
1191   HRESULT hr;
1192   DMUS_PRIVATE_CHUNK Chunk;
1193   DWORD StreamSize, StreamCount;
1194   /*DWORD ListSize[3], ListCount[3];*/
1195   LARGE_INTEGER liMove; /* used when skipping chunks */
1196   
1197   
1198   TRACE("(%p, %p): Loading\n", This, pStm);
1199   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1200   TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1201   switch (Chunk.fccID) {        
1202   case FOURCC_RIFF: {
1203     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                            
1204     TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
1205     StreamSize = Chunk.dwSize - sizeof(FOURCC);
1206     StreamCount = 0;
1207     switch (Chunk.fccID) {
1208     case DMUS_FOURCC_SEGMENT_FORM: {
1209       TRACE_(dmfile)(": segment form\n");
1210       hr = IDirectMusicSegment8Impl_IPersistStream_ParseSegmentForm (iface, &Chunk, pStm, This);
1211       if (FAILED(hr)) return hr;
1212       break;
1213     }
1214     case mmioFOURCC('W','A','V','E'): {
1215       LPSTREAM pClonedStream = NULL;    
1216       IDirectMusicObject* pWave = NULL;
1217
1218       FIXME_(dmfile)(": WAVE form (loading to be checked)\n");
1219
1220       IStream_Clone (pStm, &pClonedStream);
1221         
1222       liMove.QuadPart = 0;
1223       liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
1224       IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
1225         
1226       hr = IDirectMusicSegment8Impl_IPersistStream_LoadWave (iface, pClonedStream, &pWave);
1227       if (FAILED(hr)) {
1228         ERR(": could not load track\n");
1229         return hr;
1230       }
1231       IStream_Release (pClonedStream);
1232       
1233       IDirectMusicTrack_Release(pWave); pWave = NULL; /* now we can release at as it inserted */
1234
1235       liMove.QuadPart = StreamSize;
1236       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1237       break;      
1238     }
1239     default: {
1240       TRACE_(dmfile)(": unexpected chunk (loading failed)\n");
1241       liMove.QuadPart = StreamSize;
1242       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1243       return E_FAIL;
1244     }
1245     }
1246     TRACE_(dmfile)(": reading finished\n");
1247     break;
1248   }
1249   default: {
1250     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1251     liMove.QuadPart = Chunk.dwSize;
1252     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1253     return E_FAIL;
1254   }
1255   }
1256   
1257   return S_OK;
1258 }
1259
1260 HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
1261   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
1262   FIXME("(%p): Saving not implemented yet\n", This);
1263   return E_NOTIMPL;
1264 }
1265
1266 HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
1267   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
1268   FIXME("(%p, %p): stub\n", This, pcbSize);
1269   return E_NOTIMPL;
1270 }
1271
1272 ICOM_VTABLE(IPersistStream) DirectMusicSegment8_PersistStream_Vtbl = {
1273   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1274   IDirectMusicSegment8Impl_IPersistStream_QueryInterface,
1275   IDirectMusicSegment8Impl_IPersistStream_AddRef,
1276   IDirectMusicSegment8Impl_IPersistStream_Release,
1277   IDirectMusicSegment8Impl_IPersistStream_GetClassID,
1278   IDirectMusicSegment8Impl_IPersistStream_IsDirty,
1279   IDirectMusicSegment8Impl_IPersistStream_Load,
1280   IDirectMusicSegment8Impl_IPersistStream_Save,
1281   IDirectMusicSegment8Impl_IPersistStream_GetSizeMax
1282 };
1283
1284 /* for ClassFactory */
1285 HRESULT WINAPI DMUSIC_CreateDirectMusicSegmentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
1286   IDirectMusicSegment8Impl* obj;
1287   
1288   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicSegment8Impl));
1289   if (NULL == obj) {
1290     *ppobj = (LPVOID) NULL;
1291     return E_OUTOFMEMORY;
1292   }
1293   obj->UnknownVtbl = &DirectMusicSegment8_Unknown_Vtbl;
1294   obj->SegmentVtbl = &DirectMusicSegment8_Segment_Vtbl;
1295   obj->ObjectVtbl = &DirectMusicSegment8_Object_Vtbl;
1296   obj->PersistStreamVtbl = &DirectMusicSegment8_PersistStream_Vtbl;
1297   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
1298   DM_STRUCT_INIT(obj->pDesc);
1299   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
1300   memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicSegment, sizeof (CLSID));
1301   obj->ref = 0; /* will be inited by QueryInterface */
1302   list_init (&obj->Tracks);
1303   
1304   return IDirectMusicSegment8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
1305 }