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