oleaut32: Use a saner calling convention for the marshaller asm thunks.
[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
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License 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 = &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 = &This->SegmentVtbl;
42                 IUnknown_AddRef (iface);
43                 return S_OK;
44         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
45                 *ppobj = &This->ObjectVtbl;
46                 IUnknown_AddRef (iface);
47                 return S_OK;
48         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
49                 *ppobj = &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 %d\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 %d\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, %d)\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, %d)\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, %d)\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, %d, 0x%x, %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%x,%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 unique way to get it is 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 -> %d,%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, %d)\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(" - #%u: %p -> %d,%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, %d): 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    * "...The segment object implements IDirectMusicGraph directly..."
314    */
315   *ppGraph = This->pGraph;
316   IDirectMusicGraph_AddRef(This->pGraph);
317   return S_OK;
318 }
319
320 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetGraph (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicGraph* pGraph) {
321   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
322   FIXME("(%p, %p): to complete\n", This, pGraph);
323   if (NULL != This->pGraph) {
324     IDirectMusicGraph_Release(This->pGraph);
325   }
326   This->pGraph = pGraph;
327   if (NULL != This->pGraph) {
328     IDirectMusicGraph_AddRef(This->pGraph);
329   }
330   return S_OK;
331 }
332
333 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_AddNotificationType (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidNotificationType) {
334   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
335   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
336   return S_OK;
337 }
338
339 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveNotificationType (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidNotificationType) {
340   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
341   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
342   return S_OK;
343 }
344
345 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetParam (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
346   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
347
348   CLSID pIt_clsid;
349   struct list* pEntry = NULL;
350   IDirectMusicTrack* pTrack = NULL;
351   IPersistStream* pCLSIDStream = NULL;
352   LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
353   HRESULT hr = S_OK;
354
355   FIXME("(%p, %s, 0x%x, %d, %d, %p, %p)\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
356   
357   if (DMUS_SEG_ANYTRACK == dwIndex) {
358     
359     LIST_FOR_EACH (pEntry, &This->Tracks) {
360       pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
361
362       hr = IDirectMusicTrack_QueryInterface(pIt->pTrack, &IID_IPersistStream, (void**) &pCLSIDStream);
363       if (FAILED(hr)) {
364         ERR("(%p): object %p don't implement IPersistStream Interface. Expect a crash (critical problem)\n", This, pIt->pTrack);
365         continue ;
366       }
367
368       TRACE(" - %p -> 0x%x,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
369
370       if (0xFFFFFFFF != dwGroupBits && 0 == (pIt->dwGroupBits & dwGroupBits)) continue ;
371       hr = IPersistStream_GetClassID(pCLSIDStream, &pIt_clsid);
372       IPersistStream_Release(pCLSIDStream); pCLSIDStream = NULL;
373       if (FAILED(hr)) {
374         ERR("(%p): non-implemented GetClassID for object %p\n", This, pIt->pTrack);
375         continue ;
376       }
377       if (FALSE == IsEqualGUID(&pIt_clsid, rguidType)) continue ;
378       if (FAILED(IDirectMusicTrack_IsParamSupported(pIt->pTrack, rguidType))) continue ;
379       hr = IDirectMusicTrack_GetParam(pIt->pTrack, rguidType, mtTime, pmtNext, pParam);
380       if (SUCCEEDED(hr)) return hr;
381     }
382     ERR("(%p): not found\n", This);
383     return DMUS_E_TRACK_NOT_FOUND;
384   } 
385
386   hr = IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack(iface, &GUID_NULL, dwGroupBits, dwIndex, &pTrack);
387   if (FAILED(hr)) {
388     ERR("(%p): not found\n", This);
389     return DMUS_E_TRACK_NOT_FOUND;
390   }
391
392   hr = IDirectMusicTrack_GetParam(pTrack, rguidType, mtTime, pmtNext, pParam);
393   IDirectMusicTrack_Release(pTrack); pTrack = NULL;
394
395   return hr;
396 }
397
398 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetParam (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void* pParam) {
399   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
400   FIXME("(%p, %s, %d, %d, %d, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
401   return S_OK;
402 }
403
404 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Clone (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, IDirectMusicSegment** ppSegment) {
405   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
406   FIXME("(%p, %d, %d, %p): stub\n", This, mtStart, mtEnd, ppSegment);
407   return S_OK;
408 }
409
410 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetStartPoint (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart) {
411   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
412   TRACE("(%p, %d)\n", This, mtStart);
413   if (mtStart >= This->header.mtLength) {
414     return DMUS_E_OUT_OF_RANGE;
415   }
416   This->header.mtPlayStart = mtStart;
417   return S_OK;
418 }
419
420 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetStartPoint (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtStart) {
421   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
422   TRACE("(%p, %p)\n", This, pmtStart);
423   if (NULL == pmtStart) {
424     return E_POINTER;
425   }
426   *pmtStart = This->header.mtPlayStart;
427   return S_OK;
428 }
429
430 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLoopPoints (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd) {
431   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
432   TRACE("(%p, %d, %d)\n", This, mtStart, mtEnd);
433   if (mtStart >= This->header.mtLength || mtEnd > This->header.mtLength || mtStart > mtEnd) {
434     return DMUS_E_OUT_OF_RANGE;
435   }
436   This->header.mtLoopStart = mtStart;
437   This->header.mtLoopEnd   = mtEnd;
438   return S_OK;
439 }
440
441 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLoopPoints (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtStart, MUSIC_TIME* pmtEnd) {
442   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
443   TRACE("(%p, %p, %p)\n", This, pmtStart, pmtEnd);
444   if (NULL == pmtStart || NULL == pmtEnd) {
445     return E_POINTER;
446   }
447   *pmtStart = This->header.mtLoopStart;
448   *pmtEnd   = This->header.mtLoopEnd;
449   return S_OK;
450 }
451
452 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetPChannelsUsed (LPDIRECTMUSICSEGMENT8 iface, DWORD dwNumPChannels, DWORD* paPChannels) {
453         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
454         FIXME("(%p, %d, %p): stub\n", This, dwNumPChannels, paPChannels);
455         return S_OK;
456 }
457
458 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetTrackConfig (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidTrackClassID, DWORD dwGroupBits, DWORD dwIndex, DWORD dwFlagsOn, DWORD dwFlagsOff) {
459         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
460         FIXME("(%p, %s, %d, %d, %d, %d): stub\n", This, debugstr_dmguid(rguidTrackClassID), dwGroupBits, dwIndex, dwFlagsOn, dwFlagsOff);
461         return S_OK;
462 }
463
464 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetAudioPathConfig (LPDIRECTMUSICSEGMENT8 iface, IUnknown** ppAudioPathConfig){
465         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
466         FIXME("(%p, %p): stub\n", This, ppAudioPathConfig);
467         return S_OK;
468 }
469
470 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Compose (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtTime, IDirectMusicSegment* pFromSegment, IDirectMusicSegment* pToSegment, IDirectMusicSegment** ppComposedSegment) {
471         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
472         FIXME("(%p, %d, %p, %p, %p): stub\n", This, mtTime, pFromSegment, pToSegment, ppComposedSegment);
473         return S_OK;
474 }
475
476 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Download (LPDIRECTMUSICSEGMENT8 iface, IUnknown *pAudioPath) {
477         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
478         FIXME("(%p, %p): stub\n", This, pAudioPath);
479         return S_OK;
480 }
481
482 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Unload (LPDIRECTMUSICSEGMENT8 iface, IUnknown *pAudioPath) {
483         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
484         FIXME("(%p, %p): stub\n", This, pAudioPath);
485         return S_OK;
486 }
487
488 static const IDirectMusicSegment8Vtbl DirectMusicSegment8_Segment_Vtbl = {
489         IDirectMusicSegment8Impl_IDirectMusicSegment8_QueryInterface,
490         IDirectMusicSegment8Impl_IDirectMusicSegment8_AddRef,
491         IDirectMusicSegment8Impl_IDirectMusicSegment8_Release,
492         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLength,
493         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLength,
494         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetRepeats,
495         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetRepeats,
496         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetDefaultResolution,
497         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetDefaultResolution,
498         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack,
499         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrackGroup,
500         IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack,
501         IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveTrack,
502         IDirectMusicSegment8Impl_IDirectMusicSegment8_InitPlay,
503         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetGraph,
504         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetGraph,
505         IDirectMusicSegment8Impl_IDirectMusicSegment8_AddNotificationType,
506         IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveNotificationType,
507         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetParam,
508         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetParam,
509         IDirectMusicSegment8Impl_IDirectMusicSegment8_Clone,
510         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetStartPoint,
511         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetStartPoint,
512         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLoopPoints,
513         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLoopPoints,
514         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetPChannelsUsed,
515         IDirectMusicSegment8Impl_IDirectMusicSegment8_SetTrackConfig,
516         IDirectMusicSegment8Impl_IDirectMusicSegment8_GetAudioPathConfig,
517         IDirectMusicSegment8Impl_IDirectMusicSegment8_Compose,
518         IDirectMusicSegment8Impl_IDirectMusicSegment8_Download,
519         IDirectMusicSegment8Impl_IDirectMusicSegment8_Unload
520 };
521
522 /* IDirectMusicSegment8Impl IDirectMusicObject part: */
523 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
524         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
525         return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
526 }
527
528 static ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
529         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
530         return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
531 }
532
533 static ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
534         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
535         return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
536 }
537
538 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
539         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
540         TRACE("(%p, %p)\n", This, pDesc);
541         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
542         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
543         return S_OK;
544 }
545
546 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
547         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
548         TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
549
550         /* According to MSDN, we should copy only given values, not whole struct */     
551         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
552                 This->pDesc->guidObject = pDesc->guidObject;
553         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
554                 This->pDesc->guidClass = pDesc->guidClass;
555         if (pDesc->dwValidData & DMUS_OBJ_NAME)
556                 lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
557         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
558                 lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
559         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
560                 lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
561         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
562                 This->pDesc->vVersion = pDesc->vVersion;
563         if (pDesc->dwValidData & DMUS_OBJ_DATE)
564                 This->pDesc->ftDate = pDesc->ftDate;
565         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
566                 This->pDesc->llMemLength = pDesc->llMemLength;
567                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, pDesc->llMemLength);
568         }
569         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
570                 /* according to MSDN, we copy the stream */
571                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
572         }
573         
574         /* add new flags */
575         This->pDesc->dwValidData |= pDesc->dwValidData;
576
577         return S_OK;
578 }
579
580 static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
581         ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
582         DMUS_PRIVATE_CHUNK Chunk;
583         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
584         LARGE_INTEGER liMove; /* used when skipping chunks */
585
586         TRACE("(%p,%p, %p)\n", This, pStream, pDesc);
587
588         /* FIXME: should this be determined from stream? */
589         pDesc->dwValidData |= DMUS_OBJ_CLASS;
590         pDesc->guidClass = CLSID_DirectMusicSegment;
591
592         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
593         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
594         switch (Chunk.fccID) {  
595                 case FOURCC_RIFF: {
596                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
597                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
598                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
599                         StreamCount = 0;
600                         if (Chunk.fccID == DMUS_FOURCC_SEGMENT_FORM) {
601                                 TRACE_(dmfile)(": segment form\n");
602                                 do {
603                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
604                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
605                                         TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
606                                         switch (Chunk.fccID) {
607                                                 case DMUS_FOURCC_GUID_CHUNK: {
608                                                         TRACE_(dmfile)(": GUID chunk\n");
609                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
610                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
611                                                         break;
612                                                 }
613                                                 case DMUS_FOURCC_VERSION_CHUNK: {
614                                                         TRACE_(dmfile)(": version chunk\n");
615                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
616                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
617                                                         break;
618                                                 }
619                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
620                                                         TRACE_(dmfile)(": category chunk\n");
621                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
622                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
623                                                         break;
624                                                 }
625                                                 case FOURCC_LIST: {
626                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
627                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
628                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
629                                                         ListCount[0] = 0;
630                                                         switch (Chunk.fccID) {
631                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
632                                                                 case DMUS_FOURCC_UNFO_LIST: {
633                                                                         TRACE_(dmfile)(": UNFO list\n");
634                                                                         do {
635                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
636                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
637                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
638                                                                                 switch (Chunk.fccID) {
639                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
640                                                                                            (though strings seem to be valid unicode) */
641                                                                                         case mmioFOURCC('I','N','A','M'):
642                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
643                                                                                                 TRACE_(dmfile)(": name chunk\n");
644                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
645                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
646                                                                                                 break;
647                                                                                         }
648                                                                                         case mmioFOURCC('I','A','R','T'):
649                                                                                         case DMUS_FOURCC_UART_CHUNK: {
650                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
651                                                                                                 liMove.QuadPart = Chunk.dwSize;
652                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
653                                                                                                 break;
654                                                                                         }
655                                                                                         case mmioFOURCC('I','C','O','P'):
656                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
657                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
658                                                                                                 liMove.QuadPart = Chunk.dwSize;
659                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
660                                                                                                 break;
661                                                                                         }
662                                                                                         case mmioFOURCC('I','S','B','J'):
663                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
664                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
665                                                                                                 liMove.QuadPart = Chunk.dwSize;
666                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
667                                                                                                 break;
668                                                                                         }
669                                                                                         case mmioFOURCC('I','C','M','T'):
670                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
671                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
672                                                                                                 liMove.QuadPart = Chunk.dwSize;
673                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
674                                                                                                 break;
675                                                                                         }
676                                                                                         default: {
677                                                                                                 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
678                                                                                                 liMove.QuadPart = Chunk.dwSize;
679                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
680                                                                                                 break;                                          
681                                                                                         }
682                                                                                 }
683                                                                                 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
684                                                                         } while (ListCount[0] < ListSize[0]);
685                                                                         break;
686                                                                 }
687                                                                 case DMUS_FOURCC_TRACK_LIST: {
688                                                                   TRACE_(dmfile)(": TRACK list\n");
689                                                                   do {
690                                                                     IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
691                                                                     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
692                                                                     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
693                                                                     switch (Chunk.fccID) {
694                                                                     default: {
695                                                                       TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
696                                                                       liMove.QuadPart = Chunk.dwSize;
697                                                                       IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
698                                                                       break;                                            
699                                                                     }
700                                                                     }
701                                                                     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
702                                                                   } while (ListCount[0] < ListSize[0]);
703                                                                   break;
704                                                                 }
705                                                                 default: {
706                                                                   TRACE_(dmfile)(": unknown (skipping)\n");
707                                                                   liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
708                                                                   IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
709                                                                   break;                                                
710                                                                 }
711                                                         }
712                                                         break;
713                                                 }       
714                                                 default: {
715                                                         TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
716                                                         liMove.QuadPart = Chunk.dwSize;
717                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
718                                                         break;                                          
719                                                 }
720                                         }
721                                         TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
722                                 } while (StreamCount < StreamSize);
723                                 break;
724                         } else if (Chunk.fccID == mmioFOURCC('W','A','V','E')) {
725                                 TRACE_(dmfile)(": wave form (loading not yet implemented)\n");
726                                 liMove.QuadPart = StreamSize;
727                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
728                         } else {
729                                 TRACE_(dmfile)(": unexpected chunk (loading failed)\n");
730                                 liMove.QuadPart = StreamSize;
731                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
732                                 return E_FAIL;
733                         }
734                 
735                         TRACE_(dmfile)(": reading finished\n");
736                         break;
737                 }
738                 default: {
739                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
740                         liMove.QuadPart = Chunk.dwSize;
741                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
742                         return DMUS_E_INVALIDFILE;
743                 }
744         }       
745         
746         TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
747         
748         return S_OK;
749 }
750
751 static const IDirectMusicObjectVtbl DirectMusicSegment8_Object_Vtbl = {
752   IDirectMusicSegment8Impl_IDirectMusicObject_QueryInterface,
753   IDirectMusicSegment8Impl_IDirectMusicObject_AddRef,
754   IDirectMusicSegment8Impl_IDirectMusicObject_Release,
755   IDirectMusicSegment8Impl_IDirectMusicObject_GetDescriptor,
756   IDirectMusicSegment8Impl_IDirectMusicObject_SetDescriptor,
757   IDirectMusicSegment8Impl_IDirectMusicObject_ParseDescriptor
758 };
759
760 /* IDirectMusicSegment8Impl IPersistStream part: */
761 static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
762   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
763   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
764 }
765
766 static ULONG WINAPI IDirectMusicSegment8Impl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
767   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
768   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
769 }
770
771 static ULONG WINAPI IDirectMusicSegment8Impl_IPersistStream_Release (LPPERSISTSTREAM iface) {
772   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
773   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
774 }
775
776 static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
777   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
778   TRACE("(%p, %p)\n", This, pClassID);
779   *pClassID = CLSID_DirectMusicSegment;
780   return S_OK;
781 }
782
783 static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
784   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
785   FIXME("(%p): stub, always S_FALSE\n", This);
786   return S_FALSE;
787 }
788
789 static HRESULT IDirectMusicSegment8Impl_IPersistStream_LoadTrack (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicTrack** ppTrack, 
790                                                                   DMUS_IO_TRACK_HEADER* pTrack_hdr) {
791
792   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
793   HRESULT hr = E_FAIL;
794   IPersistStream* pPersistStream = NULL;
795   
796   hr = CoCreateInstance (&pTrack_hdr->guidClassID, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicTrack, (LPVOID*) ppTrack);
797   if (FAILED(hr)) {
798     ERR(": could not create object\n");
799     return hr;
800   }
801   /* acquire PersistStream interface */
802   hr = IDirectMusicTrack_QueryInterface (*ppTrack, &IID_IPersistStream, (LPVOID*) &pPersistStream);
803   if (FAILED(hr)) {
804     ERR(": could not acquire IPersistStream\n");
805     return hr;
806   }
807   /* load */
808   hr = IPersistStream_Load (pPersistStream, pClonedStream);
809   if (FAILED(hr)) {
810     ERR(": failed to load object\n");
811     return hr;
812   }
813   
814   /* release all loading-related stuff */
815   IPersistStream_Release (pPersistStream);
816
817   hr = IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack ((LPDIRECTMUSICSEGMENT8)&This->SegmentVtbl, *ppTrack, pTrack_hdr->dwGroup); /* at dsPosition */
818   if (FAILED(hr)) {
819     ERR(": could not insert track\n");
820     return hr;
821   }
822
823   return S_OK;
824 }
825
826 static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseTrackForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
827
828   /*ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);*/
829   HRESULT hr = E_FAIL;
830   DMUS_PRIVATE_CHUNK Chunk;
831   DWORD StreamSize, StreamCount, ListSize[3];
832   LARGE_INTEGER liMove; /* used when skipping chunks */
833
834   DMUS_IO_TRACK_HEADER        track_hdr;
835   DMUS_IO_TRACK_EXTRAS_HEADER track_xhdr;
836   IDirectMusicTrack*          pTrack = NULL;
837
838   if (pChunk->fccID != DMUS_FOURCC_TRACK_FORM) {
839     ERR_(dmfile)(": %s chunk should be a TRACK form\n", debugstr_fourcc (pChunk->fccID));
840     return E_FAIL;
841   }  
842
843   StreamSize = pChunk->dwSize - sizeof(FOURCC);
844   StreamCount = 0;
845
846   do {
847     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
848     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
849     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
850     
851     switch (Chunk.fccID) {
852     case DMUS_FOURCC_TRACK_CHUNK: {
853       TRACE_(dmfile)(": track chunk\n");
854       IStream_Read (pStm, &track_hdr, sizeof(DMUS_IO_TRACK_HEADER), NULL);
855       TRACE_(dmfile)(" - class: %s\n", debugstr_guid (&track_hdr.guidClassID));
856       TRACE_(dmfile)(" - dwGroup: %d\n", track_hdr.dwGroup);
857       TRACE_(dmfile)(" - ckid: %s\n", debugstr_fourcc (track_hdr.ckid));
858       TRACE_(dmfile)(" - fccType: %s\n", debugstr_fourcc (track_hdr.fccType));
859       break;
860     }
861     case DMUS_FOURCC_TRACK_EXTRAS_CHUNK: {
862       TRACE_(dmfile)(": track extras chunk\n");
863       IStream_Read (pStm, &track_xhdr, sizeof(DMUS_IO_TRACK_EXTRAS_HEADER), NULL);
864       break;
865     }
866
867     case DMUS_FOURCC_COMMANDTRACK_CHUNK: {
868       TRACE_(dmfile)(": COMMANDTRACK track\n");
869       liMove.QuadPart = Chunk.dwSize;
870       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
871       break;
872     }
873
874     case FOURCC_LIST: {
875       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
876       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
877       ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
878       if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {
879         LPSTREAM pClonedStream = NULL;
880
881         TRACE_(dmfile)(": TRACK list\n");
882
883         IStream_Clone (pStm, &pClonedStream);
884
885         liMove.QuadPart = 0;
886         liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
887         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
888         
889         hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
890         if (FAILED(hr)) {
891           ERR(": could not load track\n");
892           return hr;
893         }
894         IStream_Release (pClonedStream);
895         
896         IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
897
898         liMove.QuadPart = ListSize[0];
899         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
900         
901       } else {
902         TRACE_(dmfile)(": unknown (skipping)\n");
903         liMove.QuadPart = Chunk.dwSize;
904         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
905       }
906       break;
907     }
908
909     case FOURCC_RIFF: {
910       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
911       TRACE_(dmfile)(": RIFF chunk of type %s\n", debugstr_fourcc(Chunk.fccID));
912
913       ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
914
915       if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {    
916         LPSTREAM pClonedStream = NULL;
917
918         TRACE_(dmfile)(": TRACK RIFF\n");
919
920         IStream_Clone (pStm, &pClonedStream);
921         
922         liMove.QuadPart = 0;
923         liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
924         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
925         
926         hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
927         if (FAILED(hr)) {
928           ERR(": could not load track\n");
929           return hr;
930         }
931         IStream_Release (pClonedStream);
932         
933         IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
934
935         /** now safe move the cursor */
936         liMove.QuadPart = ListSize[0];
937         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
938          
939       } else {
940         TRACE_(dmfile)(": unknown RIFF fmt (skipping)\n");
941         liMove.QuadPart = ListSize[0];
942         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
943       }
944       break;
945     }
946
947     default: {
948       if (0 == track_hdr.fccType && Chunk.fccID == track_hdr.ckid) {
949         LPSTREAM pClonedStream = NULL;
950
951         TRACE_(dmfile)(": TRACK solo\n");
952
953         IStream_Clone (pStm, &pClonedStream);
954         
955         liMove.QuadPart = 0;
956         liMove.QuadPart -= (sizeof(FOURCC) + sizeof(DWORD));
957         IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
958
959         hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
960         if (FAILED(hr)) {
961           ERR(": could not load track\n");
962           return hr;
963         }
964         IStream_Release (pClonedStream);
965         
966         IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */
967
968         liMove.QuadPart = Chunk.dwSize;
969         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
970
971         break;
972       }
973
974       TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
975       liMove.QuadPart = Chunk.dwSize;
976       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
977       break;                                            
978     }
979     }
980     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
981   } while (StreamCount < StreamSize);  
982
983   return S_OK;
984 }
985
986 static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseTrackList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, IDirectMusicSegment8Impl* This) {
987
988   HRESULT hr = E_FAIL;
989   DMUS_PRIVATE_CHUNK Chunk;
990   DWORD StreamSize, ListSize[3], ListCount[3];
991   LARGE_INTEGER liMove; /* used when skipping chunks */
992
993   if (pChunk->fccID != DMUS_FOURCC_TRACK_LIST) {
994     ERR_(dmfile)(": %s chunk should be a TRACK list\n", debugstr_fourcc (pChunk->fccID));
995     return E_FAIL;
996   }  
997
998   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
999   ListCount[0] = 0;
1000
1001   do {
1002     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1003     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1004     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1005     switch (Chunk.fccID) { 
1006     case FOURCC_RIFF: {
1007       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
1008       TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
1009       StreamSize = Chunk.dwSize - sizeof(FOURCC);
1010       switch (Chunk.fccID) {
1011       case  DMUS_FOURCC_TRACK_FORM: {
1012         TRACE_(dmfile)(": TRACK form\n");
1013         hr = IDirectMusicSegment8Impl_IPersistStream_ParseTrackForm (iface, &Chunk, pStm);
1014         if (FAILED(hr)) return hr;      
1015         break;
1016       }
1017       default: {
1018         TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1019         liMove.QuadPart = StreamSize;
1020         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1021         break;
1022       }
1023       }
1024       break;
1025     }
1026     default: {
1027       TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1028       liMove.QuadPart = Chunk.dwSize;
1029       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1030       break;                                            
1031     }
1032     }
1033     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
1034   } while (ListCount[0] < ListSize[0]);
1035
1036   return S_OK;
1037 }
1038
1039 static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseSegmentForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, IDirectMusicSegment8Impl* This) {
1040
1041   HRESULT hr = E_FAIL;
1042   DMUS_PRIVATE_CHUNK Chunk;
1043   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
1044   LARGE_INTEGER liMove; /* used when skipping chunks */
1045
1046   if (pChunk->fccID != DMUS_FOURCC_SEGMENT_FORM) {
1047     ERR_(dmfile)(": %s chunk should be a segment form\n", debugstr_fourcc (pChunk->fccID));
1048     return E_FAIL;
1049   }
1050
1051   StreamSize = pChunk->dwSize - sizeof(FOURCC);
1052   StreamCount = 0;
1053
1054   do {
1055     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1056     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1057     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1058     
1059     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
1060     if (FAILED(hr)) return hr;
1061     
1062     if (hr == S_FALSE) {
1063       switch (Chunk.fccID) {
1064       case DMUS_FOURCC_SEGMENT_CHUNK: {
1065         DWORD checkSz = sizeof(FOURCC);
1066         TRACE_(dmfile)(": segment chunk\n");
1067 #if 1
1068         /** DX 7 */
1069         IStream_Read (pStm, &This->header.dwRepeats,    sizeof(This->header.dwRepeats), NULL);
1070         checkSz += sizeof(This->header.dwRepeats);
1071         IStream_Read (pStm, &This->header.mtLength,     sizeof(This->header.mtLength), NULL);
1072         checkSz += sizeof(This->header.mtLength);
1073         IStream_Read (pStm, &This->header.mtPlayStart,  sizeof(This->header.mtPlayStart), NULL);
1074         checkSz += sizeof(This->header.mtPlayStart);
1075         IStream_Read (pStm, &This->header.mtLoopStart,  sizeof(This->header.mtLoopStart), NULL);
1076         checkSz += sizeof(This->header.mtLoopStart);
1077         IStream_Read (pStm, &This->header.mtLoopEnd,    sizeof(This->header.mtLoopEnd), NULL);
1078         checkSz += sizeof(This->header.mtLoopEnd);
1079         IStream_Read (pStm, &This->header.dwResolution, sizeof(This->header.dwResolution), NULL);
1080         checkSz += sizeof(This->header.dwResolution);
1081         TRACE_(dmfile)("dwRepeats: %u\n", This->header.dwRepeats);
1082         TRACE_(dmfile)("mtLength: %u\n",  This->header.mtLength);
1083         TRACE_(dmfile)("mtPlayStart: %u\n",  This->header.mtPlayStart);
1084         TRACE_(dmfile)("mtLoopStart: %u\n",  This->header.mtLoopStart);
1085         TRACE_(dmfile)("mtLoopEnd: %u\n",  This->header.mtLoopEnd);
1086         TRACE_(dmfile)("dwResolution: %u\n", This->header.dwResolution);
1087         /** DX 8 */
1088         if (Chunk.dwSize > checkSz) {
1089           IStream_Read (pStm, &This->header.rtLength,    sizeof(This->header.rtLength), NULL);
1090           checkSz += sizeof(This->header.rtLength);
1091           IStream_Read (pStm, &This->header.dwFlags,     sizeof(This->header.dwFlags), NULL);
1092           checkSz += sizeof(This->header.dwFlags);
1093         }
1094         /** DX 9 */
1095         if (Chunk.dwSize > checkSz) {
1096           IStream_Read (pStm, &This->header.rtLoopStart,  sizeof(This->header.rtLoopStart), NULL);
1097           checkSz += sizeof(This->header.rtLoopStart);
1098           IStream_Read (pStm, &This->header.rtLoopEnd,    sizeof(This->header.rtLoopEnd), NULL);
1099           checkSz += sizeof(This->header.rtLoopEnd);
1100           IStream_Read (pStm, &This->header.rtPlayStart,  sizeof(This->header.rtPlayStart), NULL);
1101           checkSz += sizeof(This->header.rtPlayStart);
1102         }
1103 #endif
1104         liMove.QuadPart = Chunk.dwSize - checkSz + sizeof(FOURCC);
1105         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1106         break;
1107       }
1108       case FOURCC_LIST: {
1109         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
1110         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
1111         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
1112         ListCount[0] = 0;
1113         switch (Chunk.fccID) {
1114         case DMUS_FOURCC_UNFO_LIST: {
1115           TRACE_(dmfile)(": UNFO list\n");
1116           do {
1117             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1118             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1119             TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1120
1121             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
1122             if (FAILED(hr)) return hr;
1123             
1124             if (hr == S_FALSE) {
1125               switch (Chunk.fccID) {
1126               default: {
1127                 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1128                 liMove.QuadPart = Chunk.dwSize;
1129                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1130                 break;                                          
1131               }
1132               }
1133             }
1134
1135             TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
1136           } while (ListCount[0] < ListSize[0]);
1137           break;
1138         }
1139         case DMUS_FOURCC_TRACK_LIST: {
1140           TRACE_(dmfile)(": TRACK list\n");
1141           hr = IDirectMusicSegment8Impl_IPersistStream_ParseTrackList (iface, &Chunk, pStm, This);
1142           if (FAILED(hr)) return hr;
1143           break;
1144         }
1145         default: {
1146           TRACE_(dmfile)(": unknown (skipping)\n");
1147           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
1148           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1149           break;                                                
1150         }
1151         }
1152         break;
1153       }
1154       default: {
1155         TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1156         liMove.QuadPart = Chunk.dwSize;
1157         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
1158         break;                                          
1159       }
1160       }
1161     }
1162     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
1163   } while (StreamCount < StreamSize);
1164
1165   return S_OK;
1166 }
1167
1168 static HRESULT IDirectMusicSegment8Impl_IPersistStream_LoadWave (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicObject** ppWaveObject) {
1169
1170   HRESULT hr = E_FAIL;
1171   IPersistStream* pPersistStream = NULL;
1172   
1173   hr = CoCreateInstance (&CLSID_DirectSoundWave, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*) ppWaveObject);
1174   if (FAILED(hr)) {
1175     ERR(": could not create object\n");
1176     return hr;
1177   }
1178   /* acquire PersistStream interface */
1179   hr = IDirectMusicObject_QueryInterface (*ppWaveObject, &IID_IPersistStream, (LPVOID*) &pPersistStream);
1180   if (FAILED(hr)) {
1181     ERR(": could not acquire IPersistStream\n");
1182     return hr;
1183   }
1184   /* load */
1185   hr = IPersistStream_Load (pPersistStream, pClonedStream);
1186   if (FAILED(hr)) {
1187     ERR(": failed to load object\n");
1188     return hr;
1189   }
1190   
1191   /* release all loading-related stuff */
1192   IPersistStream_Release (pPersistStream);
1193
1194   return S_OK;
1195 }
1196
1197 static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
1198   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
1199
1200   HRESULT hr;
1201   DMUS_PRIVATE_CHUNK Chunk;
1202   DWORD StreamSize;
1203   /*DWORD ListSize[3], ListCount[3];*/
1204   LARGE_INTEGER liMove; /* used when skipping chunks */
1205
1206   TRACE("(%p, %p): Loading\n", This, pStm);
1207   hr = IStream_Read (pStm, &Chunk, sizeof(Chunk), NULL);
1208   if(hr != S_OK){
1209     WARN("IStream_Read failed: %08x\n", hr);
1210     return DMUS_E_UNSUPPORTED_STREAM;
1211   }
1212   TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1213   switch (Chunk.fccID) {        
1214   case FOURCC_RIFF: {
1215     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);                            
1216     TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
1217     StreamSize = Chunk.dwSize - sizeof(FOURCC);
1218     switch (Chunk.fccID) {
1219     case DMUS_FOURCC_SEGMENT_FORM: {
1220       TRACE_(dmfile)(": segment form\n");
1221       hr = IDirectMusicSegment8Impl_IPersistStream_ParseSegmentForm (iface, &Chunk, pStm, This);
1222       if (FAILED(hr)) return hr;
1223       break;
1224     }
1225     case mmioFOURCC('W','A','V','E'): {
1226       LPSTREAM pClonedStream = NULL;    
1227       IDirectMusicObject* pWave = NULL;
1228
1229       FIXME_(dmfile)(": WAVE form (loading to be checked)\n");
1230
1231       IStream_Clone (pStm, &pClonedStream);
1232         
1233       liMove.QuadPart = - (LONGLONG)(sizeof(FOURCC) * 2 + sizeof(DWORD));
1234       IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
1235         
1236       hr = IDirectMusicSegment8Impl_IPersistStream_LoadWave (iface, pClonedStream, &pWave);
1237       if (FAILED(hr)) {
1238         ERR(": could not load track\n");
1239         return hr;
1240       }
1241       IStream_Release (pClonedStream);
1242       
1243       IDirectMusicTrack_Release(pWave); pWave = NULL; /* now we can release at as it inserted */
1244
1245       liMove.QuadPart = StreamSize;
1246       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1247       break;      
1248     }
1249     default: {
1250       TRACE_(dmfile)(": unexpected chunk (loading failed)\n");
1251       liMove.QuadPart = StreamSize;
1252       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
1253       return DMUS_E_UNSUPPORTED_STREAM;
1254     }
1255     }
1256     TRACE_(dmfile)(": reading finished\n");
1257     break;
1258   }
1259   default: {
1260     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
1261     return DMUS_E_UNSUPPORTED_STREAM;
1262   }
1263   }
1264   
1265   return S_OK;
1266 }
1267
1268 static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
1269   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
1270   FIXME("(%p): Saving not implemented yet\n", This);
1271   return E_NOTIMPL;
1272 }
1273
1274 static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
1275   ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
1276   FIXME("(%p, %p): stub\n", This, pcbSize);
1277   return E_NOTIMPL;
1278 }
1279
1280 static const IPersistStreamVtbl DirectMusicSegment8_PersistStream_Vtbl = {
1281   IDirectMusicSegment8Impl_IPersistStream_QueryInterface,
1282   IDirectMusicSegment8Impl_IPersistStream_AddRef,
1283   IDirectMusicSegment8Impl_IPersistStream_Release,
1284   IDirectMusicSegment8Impl_IPersistStream_GetClassID,
1285   IDirectMusicSegment8Impl_IPersistStream_IsDirty,
1286   IDirectMusicSegment8Impl_IPersistStream_Load,
1287   IDirectMusicSegment8Impl_IPersistStream_Save,
1288   IDirectMusicSegment8Impl_IPersistStream_GetSizeMax
1289 };
1290
1291 /* for ClassFactory */
1292 HRESULT WINAPI DMUSIC_CreateDirectMusicSegmentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
1293   IDirectMusicSegment8Impl* obj;
1294
1295   if (pUnkOuter) {
1296     *ppobj = NULL;
1297     return CLASS_E_NOAGGREGATION;
1298   }
1299
1300   obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicSegment8Impl));
1301   if (NULL == obj) {
1302     *ppobj = NULL;
1303     return E_OUTOFMEMORY;
1304   }
1305   obj->UnknownVtbl = &DirectMusicSegment8_Unknown_Vtbl;
1306   obj->SegmentVtbl = &DirectMusicSegment8_Segment_Vtbl;
1307   obj->ObjectVtbl = &DirectMusicSegment8_Object_Vtbl;
1308   obj->PersistStreamVtbl = &DirectMusicSegment8_PersistStream_Vtbl;
1309   obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
1310   DM_STRUCT_INIT(obj->pDesc);
1311   obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
1312   obj->pDesc->guidClass = CLSID_DirectMusicSegment;
1313   obj->ref = 0; /* will be inited by QueryInterface */
1314   list_init (&obj->Tracks);
1315
1316   return IDirectMusicSegment8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
1317 }