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