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