- fix DirectSound Creation by dmusic (fix recent Unreal2 regression)
[wine] / dlls / dmime / performance.c
1 /* IDirectMusicPerformance Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include "dmime_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
23
24 typedef struct DMUS_PMSGItem DMUS_PMSGItem;
25 struct DMUS_PMSGItem {
26   DMUS_PMSGItem* next;
27   DMUS_PMSGItem* prev;
28
29   BOOL bInUse;
30   DWORD cb;
31   DMUS_PMSG pMsg;
32 };
33
34 /* IDirectMusicPerformance8 IUnknown part: */
35 HRESULT WINAPI IDirectMusicPerformance8Impl_QueryInterface (LPDIRECTMUSICPERFORMANCE8 iface, REFIID riid, LPVOID *ppobj) {
36         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
37         TRACE("(%p, %s,%p)\n", This, debugstr_dmguid(riid), ppobj);
38
39         if (IsEqualIID (riid, &IID_IUnknown) || 
40             IsEqualIID (riid, &IID_IDirectMusicPerformance) ||
41             IsEqualIID (riid, &IID_IDirectMusicPerformance8)) {
42                 IDirectMusicPerformance8Impl_AddRef(iface);
43                 *ppobj = This;
44                 return S_OK;
45         }
46         
47         WARN("(%p, %s,%p): not found\n", This, debugstr_dmguid(riid), ppobj);
48         return E_NOINTERFACE;
49 }
50
51 ULONG WINAPI IDirectMusicPerformance8Impl_AddRef (LPDIRECTMUSICPERFORMANCE8 iface) {
52         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
53         TRACE("(%p): AddRef from %ld\n", This, This->ref);
54         return ++(This->ref);
55 }
56
57 ULONG WINAPI IDirectMusicPerformance8Impl_Release (LPDIRECTMUSICPERFORMANCE8 iface) {
58         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
59         ULONG ref = --This->ref;
60         TRACE("(%p): ReleaseRef to %ld\n", This, This->ref);
61         if (ref == 0) {
62                 HeapFree(GetProcessHeap(), 0, This);
63         }
64         return ref;
65 }
66
67 /* IDirectMusicPerformanceImpl IDirectMusicPerformance Interface part: */
68 HRESULT WINAPI IDirectMusicPerformance8Impl_Init (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusic** ppDirectMusic, LPDIRECTSOUND pDirectSound, HWND hWnd) {
69         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
70
71         FIXME("(iface = %p, dmusic = %p, dsound = %p, hwnd = %p)\n", This, ppDirectMusic, pDirectSound, hWnd);
72         if (This->pDirectMusic || This->pDirectSound)
73           return DMUS_E_ALREADY_INITED;
74         
75         if (NULL == hWnd) {
76           hWnd = GetForegroundWindow();
77         }
78
79         if (NULL != pDirectSound) {
80           This->pDirectSound = (IDirectSound*) pDirectSound;
81           IDirectSound_AddRef((LPDIRECTSOUND) This->pDirectSound);
82         } else {
83           DirectSoundCreate8(NULL, (LPDIRECTSOUND8*) &This->pDirectSound, NULL);
84           /** 
85            * as seen in msdn
86            * 
87            *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/idirectmusicperformance8initaudio.asp
88            */
89           if (NULL != hWnd) {
90             IDirectSound8_SetCooperativeLevel(This->pDirectSound, hWnd, DSSCL_PRIORITY);
91           } else {
92             /* how to get the ForeGround window handle ? */
93             /*IDirectSound8_SetCooperativeLevel(This->pDirectSound, hWnd, DSSCL_PRIORITY);*/
94           }
95           if (!This->pDirectSound)
96             return DSERR_NODRIVER;
97         }
98
99         if (NULL != ppDirectMusic && NULL != *ppDirectMusic) {
100           /* app creates it's own dmusic object and gives it to performance */
101           This->pDirectMusic = (IDirectMusic8*) *ppDirectMusic;
102           IDirectMusic8_AddRef((LPDIRECTMUSIC8) This->pDirectMusic);
103         } else {
104           /* app allows the performance to initialise itfself and needs a pointer to object*/
105           CoCreateInstance (&CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusic8, (void**)&This->pDirectMusic);
106           if (ppDirectMusic) {
107             *ppDirectMusic = (LPDIRECTMUSIC) This->pDirectMusic;
108             IDirectMusic8_AddRef((LPDIRECTMUSIC8) *ppDirectMusic);
109           }
110         }
111         
112         return S_OK;
113 }
114
115 HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegment (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, DWORD dwFlags, __int64 i64StartTime, IDirectMusicSegmentState** ppSegmentState) {
116         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
117         FIXME("(%p, %p, %ld, %lli, %p): stub\n", This, pSegment, dwFlags, i64StartTime, ppSegmentState);
118         return S_OK;
119 }
120
121 HRESULT WINAPI IDirectMusicPerformance8Impl_Stop (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, IDirectMusicSegmentState* pSegmentState, MUSIC_TIME mtTime, DWORD dwFlags) {
122         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
123         FIXME("(%p, %p, %p, %ld, %ld): stub\n", This, pSegment, pSegmentState, mtTime, dwFlags);
124         return S_OK;
125 }
126
127 HRESULT WINAPI IDirectMusicPerformance8Impl_GetSegmentState (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegmentState** ppSegmentState, MUSIC_TIME mtTime) {
128         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
129         FIXME("(%p,%p, %ld): stub\n", This, ppSegmentState, mtTime);
130         return S_OK;
131 }
132
133 HRESULT WINAPI IDirectMusicPerformance8Impl_SetPrepareTime (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwMilliSeconds) {
134         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
135         FIXME("(%p, %ld): stub\n", This, dwMilliSeconds);
136         return S_OK;
137 }
138
139 HRESULT WINAPI IDirectMusicPerformance8Impl_GetPrepareTime (LPDIRECTMUSICPERFORMANCE8 iface, DWORD* pdwMilliSeconds) {
140         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
141         FIXME("(%p, %p): stub\n", This, pdwMilliSeconds);
142         return S_OK;
143 }
144
145 HRESULT WINAPI IDirectMusicPerformance8Impl_SetBumperLength (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwMilliSeconds) {
146         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
147         FIXME("(%p, %ld): stub\n", This, dwMilliSeconds);
148         return S_OK;
149 }
150
151 HRESULT WINAPI IDirectMusicPerformance8Impl_GetBumperLength (LPDIRECTMUSICPERFORMANCE8 iface, DWORD* pdwMilliSeconds) {
152         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
153         FIXME("(%p, %p): stub\n", This, pdwMilliSeconds);
154         return S_OK;
155 }
156
157 HRESULT WINAPI IDirectMusicPerformance8Impl_SendPMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pPMSG) {
158         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
159         FIXME("(%p, %p): stub\n", This, pPMSG);
160         return S_OK;
161 }
162
163 HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToReferenceTime (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, REFERENCE_TIME* prtTime) {
164         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
165         FIXME("(%p, %ld, %p): stub\n", This, mtTime, prtTime);
166         return S_OK;
167 }
168
169 HRESULT WINAPI IDirectMusicPerformance8Impl_ReferenceToMusicTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtTime, MUSIC_TIME* pmtTime) {
170         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
171         FIXME("(%p, %lli, %p): stub\n", This, rtTime, pmtTime);
172         return S_OK;
173 }
174
175 HRESULT WINAPI IDirectMusicPerformance8Impl_IsPlaying (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, IDirectMusicSegmentState* pSegState) {
176         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
177         FIXME("(%p, %p, %p): stub\n", This, pSegment, pSegState);
178         return S_FALSE;
179 }
180
181 HRESULT WINAPI IDirectMusicPerformance8Impl_GetTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtNow, MUSIC_TIME* pmtNow) {
182         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
183         FIXME("(%p, %p, %p): stub\n", This, prtNow, pmtNow);    
184         return S_OK;
185 }
186
187 HRESULT WINAPI IDirectMusicPerformance8Impl_AllocPMsg (LPDIRECTMUSICPERFORMANCE8 iface, ULONG cb, DMUS_PMSG** ppPMSG) {
188   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
189   DMUS_PMSGItem* pItem = NULL;
190   
191   FIXME("(%p, %ld, %p): stub\n", This, cb, ppPMSG);
192         
193   if (sizeof(DMUS_PMSG) > cb) {
194     return E_INVALIDARG;
195   }
196   if (NULL == ppPMSG) {
197     return E_POINTER;
198   }
199   pItem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb - sizeof(DMUS_PMSG)  + sizeof(DMUS_PMSGItem));
200   if (NULL == pItem) {
201     return E_OUTOFMEMORY;
202   }
203   pItem->pMsg.dwSize = cb;
204   *ppPMSG = &(pItem->pMsg);
205   return S_OK;
206 }
207
208 HRESULT WINAPI IDirectMusicPerformance8Impl_FreePMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pPMSG) {
209   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
210   DMUS_PMSGItem* pItem = NULL;
211   
212   FIXME("(%p, %p): stub\n", This, pPMSG);
213   
214   if (NULL == pPMSG) {
215     return E_POINTER;
216   }
217   pItem = (DMUS_PMSGItem*) (((unsigned char*) pPMSG) - sizeof(DMUS_PMSG) - sizeof(DMUS_PMSGItem));
218   if (NULL == pItem) {
219     return E_POINTER;
220   }
221   if (TRUE == pItem->bInUse) {
222     return DMUS_E_CANNOT_FREE;
223   }     
224   HeapFree(GetProcessHeap(), 0, pItem);  
225   return S_OK;
226 }
227
228 HRESULT WINAPI IDirectMusicPerformance8Impl_GetGraph (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicGraph** ppGraph) {
229         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
230         FIXME("(%p, %p): to check\n", This, ppGraph);
231         if (NULL != This->pToolGraph) {
232           *ppGraph = (LPDIRECTMUSICGRAPH) This->pToolGraph; 
233           IDirectMusicGraph_AddRef((LPDIRECTMUSICGRAPH) *ppGraph);
234         }
235         return S_OK;
236 }
237
238 HRESULT WINAPI IDirectMusicPerformance8Impl_SetGraph (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicGraph* pGraph) {
239         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
240
241         FIXME("(%p, %p): to check\n", This, pGraph);
242
243         if (NULL != This->pToolGraph) {
244           /* Todo clean buffers and tools before */
245           IDirectMusicGraph_Release((LPDIRECTMUSICGRAPH) This->pToolGraph);
246         }
247         This->pToolGraph = pGraph;
248         if (NULL != This->pToolGraph) {
249           IDirectMusicGraph_AddRef((LPDIRECTMUSICGRAPH) This->pToolGraph);
250         }
251         return S_OK;
252 }
253
254 HRESULT WINAPI IDirectMusicPerformance8Impl_SetNotificationHandle (LPDIRECTMUSICPERFORMANCE8 iface, HANDLE hNotification, REFERENCE_TIME rtMinimum) {
255   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
256   FIXME("(%p, %p, %lli): stub\n", This, hNotification, rtMinimum);
257   This->hNotification = hNotification;
258   if (rtMinimum) This->rtMinimum = rtMinimum;
259   return S_OK;
260 }
261
262 HRESULT WINAPI IDirectMusicPerformance8Impl_GetNotificationPMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_NOTIFICATION_PMSG** ppNotificationPMsg) {
263   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
264   FIXME("(%p, %p): stub\n", This, ppNotificationPMsg);
265   if (NULL == ppNotificationPMsg) {
266     return E_POINTER;
267   }
268   return S_FALSE;
269   /*return S_OK;*/
270 }
271
272 HRESULT WINAPI IDirectMusicPerformance8Impl_AddNotificationType (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidNotificationType) {
273         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
274         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
275         return S_OK;
276 }
277
278 HRESULT WINAPI IDirectMusicPerformance8Impl_RemoveNotificationType (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidNotificationType) {
279         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
280         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
281         return S_OK;
282 }
283
284 HRESULT WINAPI IDirectMusicPerformance8Impl_AddPort (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicPort* pPort) {
285         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
286         FIXME("(%p, %p): stub\n", This, pPort);
287         IDirectMusicPort_AddRef (pPort);
288         return S_OK;
289 }
290
291 HRESULT WINAPI IDirectMusicPerformance8Impl_RemovePort (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicPort* pPort) {
292         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
293         FIXME("(%p, %p): stub\n", This, pPort);
294         IDirectMusicPort_Release (pPort);
295         return S_OK;
296 }
297
298 HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannelBlock (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwBlockNum, IDirectMusicPort* pPort, DWORD dwGroup) {
299         int i, j, range /* min value in range */;
300         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
301
302         FIXME("(%p, %ld, %p, %ld): semi-stub\n", This, dwBlockNum, pPort, dwGroup-1);
303         range = 16 * dwBlockNum;
304         j = 0;
305                 
306         for (i = range; i < range+16; i++) {
307                 /*TRACE("Setting PChannel[%i] to port %p, group %ld, MIDI port %i\n", i, pPort, dwGroup-1, j); */
308                 This->PChannel[i].port = pPort; 
309                 This->PChannel[i].group = dwGroup - 1; /* first index is always zero */
310                 This->PChannel[i].channel = j; /* FIXME: should this be assigned? */
311                 j++;
312         }
313
314         return S_OK;
315 }
316
317 HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannel (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwPChannel, IDirectMusicPort* pPort, DWORD dwGroup, DWORD dwMChannel) {
318         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
319
320         TRACE("(%p, %ld, %p, %ld, %ld)\n", This, dwPChannel, pPort, dwGroup, dwMChannel);
321         This->PChannel[dwPChannel].port = pPort; 
322         This->PChannel[dwPChannel].group = dwGroup; 
323         This->PChannel[dwPChannel].channel = dwMChannel;
324
325         return S_OK;
326 }
327
328 HRESULT WINAPI IDirectMusicPerformance8Impl_PChannelInfo (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwPChannel, IDirectMusicPort** ppPort, DWORD* pdwGroup, DWORD* pdwMChannel) {
329         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
330         FIXME("(%p, %ld, %p, %p, %p): stub\n", This, dwPChannel, ppPort, pdwGroup, pdwMChannel);
331         return S_OK;
332 }
333
334 HRESULT WINAPI IDirectMusicPerformance8Impl_DownloadInstrument (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicInstrument* pInst, DWORD dwPChannel, IDirectMusicDownloadedInstrument** ppDownInst, DMUS_NOTERANGE* pNoteRanges, DWORD dwNumNoteRanges, IDirectMusicPort** ppPort, DWORD* pdwGroup, DWORD* pdwMChannel) {
335         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
336         FIXME("(%p, %p, %ld, %p, %p, %ld, %p, %p, %p): stub\n", This, pInst, dwPChannel, ppDownInst, pNoteRanges, dwNumNoteRanges, ppPort, pdwGroup, pdwMChannel);
337         return S_OK;
338 }
339
340 HRESULT WINAPI IDirectMusicPerformance8Impl_Invalidate (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, DWORD dwFlags) {
341         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
342         FIXME("(%p, %ld, %ld): stub\n", This, mtTime, dwFlags);
343         return S_OK;
344 }
345
346 HRESULT WINAPI IDirectMusicPerformance8Impl_GetParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
347         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
348         FIXME("(%p, %s, %ld, %ld, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
349         return S_OK;
350 }
351
352 HRESULT WINAPI IDirectMusicPerformance8Impl_SetParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void* pParam) {
353         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
354         FIXME("(%p, %s, %ld, %ld, %ld, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
355         return S_OK;
356 }
357
358 HRESULT WINAPI IDirectMusicPerformance8Impl_GetGlobalParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, void* pParam, DWORD dwSize) {
359         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
360
361         TRACE("(%p, %s, %p, %ld): stub\n", This, debugstr_dmguid(rguidType), pParam, dwSize);
362         
363         if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload))
364                 memcpy(pParam, &This->fAutoDownload, sizeof(&This->fAutoDownload));
365         if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel))
366                 memcpy(pParam, &This->cMasterGrooveLevel, sizeof(&This->cMasterGrooveLevel));
367         if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo))
368                 memcpy(pParam, &This->fMasterTempo, sizeof(&This->fMasterTempo));
369         if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume))
370                 memcpy(pParam, &This->lMasterVolume, sizeof(&This->lMasterVolume));
371
372         return S_OK;
373 }
374
375 HRESULT WINAPI IDirectMusicPerformance8Impl_SetGlobalParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, void* pParam, DWORD dwSize) {
376         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
377
378         TRACE("(%p, %s, %p, %ld)\n", This, debugstr_dmguid(rguidType), pParam, dwSize);
379         
380         if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload)) {
381                 memcpy(&This->fAutoDownload, pParam, dwSize);
382                 TRACE("=> AutoDownload set to %d\n", This->fAutoDownload);
383         }
384         if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel)) {
385                 memcpy(&This->cMasterGrooveLevel, pParam, dwSize);
386                 TRACE("=> MasterGrooveLevel set to %i\n", This->cMasterGrooveLevel);
387         }
388         if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo)) {
389                 memcpy(&This->fMasterTempo, pParam, dwSize);
390                 TRACE("=> MasterTempo set to %f\n", This->fMasterTempo);
391         }
392         if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume)) {
393                 memcpy(&This->lMasterVolume, pParam, dwSize);
394                 TRACE("=> MasterVolume set to %li\n", This->lMasterVolume);
395         }
396
397         return S_OK;
398 }
399
400 HRESULT WINAPI IDirectMusicPerformance8Impl_GetLatencyTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtTime) {
401         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
402         FIXME("(%p, %p): stub\n", This, prtTime);
403         return S_OK;
404 }
405
406 HRESULT WINAPI IDirectMusicPerformance8Impl_GetQueueTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtTime) {
407         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
408         FIXME("(%p, %p): stub\n", This, prtTime);
409         return S_OK;
410 }
411
412 HRESULT WINAPI IDirectMusicPerformance8Impl_AdjustTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtAmount) {
413         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
414         FIXME("(%p, %lli): stub\n", This, rtAmount);
415         return S_OK;
416 }
417
418 HRESULT WINAPI IDirectMusicPerformance8Impl_CloseDown (LPDIRECTMUSICPERFORMANCE8 iface) {
419         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
420         FIXME("(%p): stub\n", This);
421         return S_OK;
422 }
423
424 HRESULT WINAPI IDirectMusicPerformance8Impl_GetResolvedTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtTime, REFERENCE_TIME* prtResolved, DWORD dwTimeResolveFlags) {
425         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
426         FIXME("(%p, %lli, %p, %ld): stub\n", This, rtTime, prtResolved, dwTimeResolveFlags);
427         return S_OK;
428 }
429
430 HRESULT WINAPI IDirectMusicPerformance8Impl_MIDIToMusic (LPDIRECTMUSICPERFORMANCE8 iface, BYTE bMIDIValue, DMUS_CHORD_KEY* pChord, BYTE bPlayMode, BYTE bChordLevel, WORD* pwMusicValue) {
431         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
432         FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, bMIDIValue, pChord, bPlayMode, bChordLevel, pwMusicValue);
433         return S_OK;
434 }
435
436 HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToMIDI (LPDIRECTMUSICPERFORMANCE8 iface, WORD wMusicValue, DMUS_CHORD_KEY* pChord, BYTE bPlayMode, BYTE bChordLevel, BYTE* pbMIDIValue) {
437         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
438         FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, wMusicValue, pChord, bPlayMode, bChordLevel, pbMIDIValue);
439         return S_OK;
440 }
441
442 HRESULT WINAPI IDirectMusicPerformance8Impl_TimeToRhythm (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, DMUS_TIMESIGNATURE* pTimeSig, WORD* pwMeasure, BYTE* pbBeat, BYTE* pbGrid, short* pnOffset) {
443         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
444         FIXME("(%p, %ld, %p, %p, %p, %p, %p): stub\n", This, mtTime, pTimeSig, pwMeasure, pbBeat, pbGrid, pnOffset);
445         return S_OK;
446 }
447
448 HRESULT WINAPI IDirectMusicPerformance8Impl_RhythmToTime (LPDIRECTMUSICPERFORMANCE8 iface, WORD wMeasure, BYTE bBeat, BYTE bGrid, short nOffset, DMUS_TIMESIGNATURE* pTimeSig, MUSIC_TIME* pmtTime) {
449         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
450         FIXME("(%p, %d, %d, %d, %i, %p, %p): stub\n", This, wMeasure, bBeat, bGrid, nOffset, pTimeSig, pmtTime);
451         return S_OK;
452 }
453
454 /* IDirectMusicPerformance8 Interface part follow: */
455 HRESULT WINAPI IDirectMusicPerformance8Impl_InitAudio (LPDIRECTMUSICPERFORMANCE8 iface, 
456                                                       IDirectMusic** ppDirectMusic, 
457                                                       IDirectSound** ppDirectSound, 
458                                                       HWND hWnd, 
459                                                       DWORD dwDefaultPathType, 
460                                                       DWORD dwPChannelCount, 
461                                                       DWORD dwFlags, 
462                                                       DMUS_AUDIOPARAMS* pParams) {
463
464         IDirectSound* dsound = NULL;
465         HRESULT hr = S_OK;
466         
467         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
468         FIXME("(%p, %p, %p, %p, %lx, %lu, %lx, %p): to check\n", This, ppDirectMusic, ppDirectSound, hWnd, dwDefaultPathType, dwPChannelCount, dwFlags, pParams);
469
470         if (This->pDirectMusic || This->pDirectSound)
471           return DMUS_E_ALREADY_INITED;
472
473         if (NULL != ppDirectSound && NULL != *ppDirectSound) {
474           dsound = *ppDirectSound;
475         } else {
476           hr = DirectSoundCreate8 (NULL, (LPDIRECTSOUND8*) &dsound, NULL);
477           FIXME("return dsound(%p,%ld)\n", dsound, hr);
478           if (FAILED(hr) || !dsound)
479             return DSERR_NODRIVER;
480           if (ppDirectSound)
481             *ppDirectSound = dsound;  
482         }
483         
484         IDirectMusicPerformance8Impl_Init(iface, ppDirectMusic, dsound, hWnd);
485
486         /* Init increases the ref count of the dsound object. Decremente it if the app don't want a pointer to the object. */
487         if (NULL == ppDirectSound) {
488           IDirectSound_Release(This->pDirectSound);
489         }
490
491         /* as seen in msdn we need params init before audio path creation */
492         if (NULL != pParams) {
493           memcpy(&This->pParams, pParams, sizeof(DMUS_AUDIOPARAMS));
494         } else {
495           /**
496            * TODO, how can i fill the struct 
497            * as seen at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/dmusaudioparams.asp
498            */
499           This->pParams.dwSize = sizeof(DMUS_AUDIOPARAMS);
500           This->pParams.fInitNow = FALSE;
501           This->pParams.dwValidData = DMUS_AUDIOPARAMS_FEATURES | DMUS_AUDIOPARAMS_VOICES | DMUS_AUDIOPARAMS_SAMPLERATE | DMUS_AUDIOPARAMS_DEFAULTSYNTH;
502           This->pParams.dwVoices = 64;
503           This->pParams.dwSampleRate = (DWORD) 22.050; 
504           This->pParams.dwFeatures = dwFlags;
505           This->pParams.clsidDefaultSynth = CLSID_DirectMusicSynthSink;
506         }
507         hr = IDirectMusicPerformance8Impl_CreateStandardAudioPath(iface, dwDefaultPathType, dwPChannelCount, FALSE, (IDirectMusicAudioPath**) &This->pDefaultPath);
508
509         return hr;
510 }
511
512 HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegmentEx (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pSource, WCHAR* pwzSegmentName, IUnknown* pTransition, DWORD dwFlags, __int64 i64StartTime, IDirectMusicSegmentState** ppSegmentState, IUnknown* pFrom, IUnknown* pAudioPath) {
513         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
514         FIXME("(%p, %p, %p, %p, %ld, %lli, %p, %p, %p): stub\n", This, pSource, pwzSegmentName, pTransition, dwFlags, i64StartTime, ppSegmentState, pFrom, pAudioPath);
515         return S_OK;
516 }
517
518 HRESULT WINAPI IDirectMusicPerformance8Impl_StopEx (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pObjectToStop, __int64 i64StopTime, DWORD dwFlags) {
519         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
520         FIXME("(%p, %p, %lli, %ld): stub\n", This, pObjectToStop, i64StopTime, dwFlags);
521         return S_OK;
522 }
523
524 HRESULT WINAPI IDirectMusicPerformance8Impl_ClonePMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pSourcePMSG, DMUS_PMSG** ppCopyPMSG) {
525         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
526         FIXME("(%p, %p, %p): stub\n", This, pSourcePMSG, ppCopyPMSG);
527         return S_OK;
528 }
529
530 HRESULT WINAPI IDirectMusicPerformance8Impl_CreateAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pSourceConfig, BOOL fActivate, IDirectMusicAudioPath** ppNewPath) {
531         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
532         FIXME("(%p, %p, %d, %p): stub\n", This, pSourceConfig, fActivate, ppNewPath);
533         return S_OK;
534 }
535
536 /**
537  * see  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/standardaudiopaths.asp
538  */
539 HRESULT WINAPI IDirectMusicPerformance8Impl_CreateStandardAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwType, DWORD dwPChannelCount, BOOL fActivate, IDirectMusicAudioPath** ppNewPath) {
540         IDirectMusicAudioPathImpl *default_path;
541         IDirectMusicAudioPath *pPath;
542         DSBUFFERDESC desc;
543         WAVEFORMATEX format;
544         LPDIRECTSOUNDBUFFER8 buffer;
545         HRESULT hr = S_OK;
546
547         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
548         
549         FIXME("(%p)->(%ld, %ld, %d, %p): semi-stub\n", This, dwType, dwPChannelCount, fActivate, ppNewPath);
550
551         if (NULL == ppNewPath) {
552           return E_POINTER;
553         }
554         
555         DMUSIC_CreateDirectMusicAudioPathImpl (&IID_IDirectMusicAudioPath, (LPVOID*)&pPath, NULL);
556         default_path = (IDirectMusicAudioPathImpl*)((char*)(pPath) - offsetof(IDirectMusicAudioPathImpl,AudioPathVtbl));
557         default_path->pPerf = (IDirectMusicPerformance8*) This;
558         
559         /* Secondary buffer description */
560         format.wFormatTag = WAVE_FORMAT_PCM;
561         format.nChannels = 1;
562         format.nSamplesPerSec = 44000;
563         format.nAvgBytesPerSec = 44000*2;
564         format.nBlockAlign = 2;
565         format.wBitsPerSample = 16;
566         format.cbSize = 0;
567         
568         desc.dwSize = sizeof(desc);
569         desc.dwFlags = DSBCAPS_CTRLFX | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS;
570         desc.dwBufferBytes = DSBSIZE_MIN;
571         desc.dwReserved = 0;
572         desc.lpwfxFormat = &format;
573         desc.guid3DAlgorithm = GUID_NULL;
574         
575         switch(dwType) {
576         case DMUS_APATH_DYNAMIC_3D:
577                 desc.dwFlags |= DSBCAPS_CTRL3D | DSBCAPS_CTRLFREQUENCY | DSBCAPS_MUTE3DATMAXDISTANCE;
578                 break;
579         case DMUS_APATH_DYNAMIC_MONO:
580                 desc.dwFlags |= DSBCAPS_CTRLFREQUENCY;
581                 break;
582         case DMUS_APATH_SHARED_STEREOPLUSREVERB:
583                 /* normally we havet to create 2 buffers (one for music other for reverb) 
584                  * in this case. See msdn
585                  */
586         case DMUS_APATH_DYNAMIC_STEREO:
587                 desc.dwFlags |= DSBCAPS_CTRLFREQUENCY;
588                 format.nChannels = 2;
589                 format.nBlockAlign *= 2;
590                 format.nAvgBytesPerSec *=2;
591                 break;
592         default:
593                 HeapFree(GetProcessHeap(), 0, default_path); 
594                 *ppNewPath = NULL;
595                 return E_INVALIDARG;
596                 break;
597         }
598
599         /* FIXME: Should we create one secondary buffer for each PChannel? */
600         hr = IDirectSound8_CreateSoundBuffer ((LPDIRECTSOUND8) This->pDirectSound, &desc, &buffer, NULL);
601         if (FAILED(hr)) {
602                 HeapFree(GetProcessHeap(), 0, default_path); 
603                 *ppNewPath = NULL;
604                 return DSERR_BUFFERLOST;
605         }
606         default_path->pDSBuffer = (IDirectSoundBuffer*) buffer;
607
608         /* Update description for creating primary buffer */
609         desc.dwFlags |= DSBCAPS_PRIMARYBUFFER;
610         desc.dwBufferBytes = 0;
611         desc.lpwfxFormat = NULL;
612
613         hr = IDirectSound8_CreateSoundBuffer ((LPDIRECTSOUND8) This->pDirectSound, &desc, &buffer, NULL);
614         if (FAILED(hr)) {
615                 IDirectSoundBuffer_Release(default_path->pDSBuffer);
616                 HeapFree(GetProcessHeap(), 0, default_path); 
617                 *ppNewPath = NULL;
618                 return DSERR_BUFFERLOST;
619         }
620         default_path->pPrimary = (IDirectSoundBuffer*) buffer;
621
622         *ppNewPath = (LPDIRECTMUSICAUDIOPATH) pPath;
623         
624         TRACE(" returning IDirectMusicPerformance interface at %p.\n", *ppNewPath);
625
626         return S_OK;
627 }
628
629 HRESULT WINAPI IDirectMusicPerformance8Impl_SetDefaultAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicAudioPath* pAudioPath) {
630         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
631
632         FIXME("(%p, %p): semi-stub\n", This, pAudioPath);
633         if (NULL != This->pDefaultPath) {
634                 IDirectMusicAudioPath_Release((LPDIRECTMUSICAUDIOPATH) This->pDefaultPath);
635                 ((IDirectMusicAudioPathImpl*) This->pDefaultPath)->pPerf = NULL;
636                 This->pDefaultPath = NULL;
637         }
638         This->pDefaultPath = pAudioPath;
639         if (NULL != This->pDefaultPath) {
640                 IDirectMusicAudioPath_AddRef((LPDIRECTMUSICAUDIOPATH) This->pDefaultPath);
641                 ((IDirectMusicAudioPathImpl*) This->pDefaultPath)->pPerf = (IDirectMusicPerformance8*) This;    
642         }
643         
644         return S_OK;
645 }
646
647 HRESULT WINAPI IDirectMusicPerformance8Impl_GetDefaultAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicAudioPath** ppAudioPath) {
648     ICOM_THIS(IDirectMusicPerformance8Impl,iface);
649
650         FIXME("(%p, %p): semi-stub (%p)\n", This, ppAudioPath, This->pDefaultPath);
651
652         if (NULL != This->pDefaultPath) {
653           *ppAudioPath = (LPDIRECTMUSICAUDIOPATH) This->pDefaultPath;
654           IDirectMusicAudioPath_AddRef(*ppAudioPath);
655         } else {
656           *ppAudioPath = NULL;
657         }
658         return S_OK;
659 }
660
661 HRESULT WINAPI IDirectMusicPerformance8Impl_GetParamEx (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwTrackID, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
662         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
663
664         FIXME("(%p, %s, %ld, %ld, %ld, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), dwTrackID, dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
665
666         return S_OK;
667 }
668
669 ICOM_VTABLE(IDirectMusicPerformance8) DirectMusicPerformance8_Vtbl = {
670     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
671         IDirectMusicPerformance8Impl_QueryInterface,
672         IDirectMusicPerformance8Impl_AddRef,
673         IDirectMusicPerformance8Impl_Release,
674         IDirectMusicPerformance8Impl_Init,
675         IDirectMusicPerformance8Impl_PlaySegment,
676         IDirectMusicPerformance8Impl_Stop,
677         IDirectMusicPerformance8Impl_GetSegmentState,
678         IDirectMusicPerformance8Impl_SetPrepareTime,
679         IDirectMusicPerformance8Impl_GetPrepareTime,
680         IDirectMusicPerformance8Impl_SetBumperLength,
681         IDirectMusicPerformance8Impl_GetBumperLength,
682         IDirectMusicPerformance8Impl_SendPMsg,
683         IDirectMusicPerformance8Impl_MusicToReferenceTime,
684         IDirectMusicPerformance8Impl_ReferenceToMusicTime,
685         IDirectMusicPerformance8Impl_IsPlaying,
686         IDirectMusicPerformance8Impl_GetTime,
687         IDirectMusicPerformance8Impl_AllocPMsg,
688         IDirectMusicPerformance8Impl_FreePMsg,
689         IDirectMusicPerformance8Impl_GetGraph,
690         IDirectMusicPerformance8Impl_SetGraph,
691         IDirectMusicPerformance8Impl_SetNotificationHandle,
692         IDirectMusicPerformance8Impl_GetNotificationPMsg,
693         IDirectMusicPerformance8Impl_AddNotificationType,
694         IDirectMusicPerformance8Impl_RemoveNotificationType,
695         IDirectMusicPerformance8Impl_AddPort,
696         IDirectMusicPerformance8Impl_RemovePort,
697         IDirectMusicPerformance8Impl_AssignPChannelBlock,
698         IDirectMusicPerformance8Impl_AssignPChannel,
699         IDirectMusicPerformance8Impl_PChannelInfo,
700         IDirectMusicPerformance8Impl_DownloadInstrument,
701         IDirectMusicPerformance8Impl_Invalidate,
702         IDirectMusicPerformance8Impl_GetParam,
703         IDirectMusicPerformance8Impl_SetParam,
704         IDirectMusicPerformance8Impl_GetGlobalParam,
705         IDirectMusicPerformance8Impl_SetGlobalParam,
706         IDirectMusicPerformance8Impl_GetLatencyTime,
707         IDirectMusicPerformance8Impl_GetQueueTime,
708         IDirectMusicPerformance8Impl_AdjustTime,
709         IDirectMusicPerformance8Impl_CloseDown,
710         IDirectMusicPerformance8Impl_GetResolvedTime,
711         IDirectMusicPerformance8Impl_MIDIToMusic,
712         IDirectMusicPerformance8Impl_MusicToMIDI,
713         IDirectMusicPerformance8Impl_TimeToRhythm,
714         IDirectMusicPerformance8Impl_RhythmToTime,
715         IDirectMusicPerformance8Impl_InitAudio,
716         IDirectMusicPerformance8Impl_PlaySegmentEx,
717         IDirectMusicPerformance8Impl_StopEx,
718         IDirectMusicPerformance8Impl_ClonePMsg,
719         IDirectMusicPerformance8Impl_CreateAudioPath,
720         IDirectMusicPerformance8Impl_CreateStandardAudioPath,
721         IDirectMusicPerformance8Impl_SetDefaultAudioPath,
722         IDirectMusicPerformance8Impl_GetDefaultAudioPath,
723         IDirectMusicPerformance8Impl_GetParamEx
724 };
725
726 /* for ClassFactory */
727 HRESULT WINAPI DMUSIC_CreateDirectMusicPerformanceImpl (LPCGUID lpcGUID, LPVOID *ppobj, LPUNKNOWN pUnkOuter) {
728         IDirectMusicPerformance8Impl *obj;
729
730         TRACE("(%p,%p,%p)\n", lpcGUID, ppobj, pUnkOuter);
731
732         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicPerformance8Impl));
733         if (NULL == obj)        {
734                 *ppobj = (LPDIRECTMUSICPERFORMANCE8)NULL;
735                 return E_OUTOFMEMORY;
736         }
737         obj->lpVtbl = &DirectMusicPerformance8_Vtbl;
738         obj->ref = 1;
739         obj->pDirectMusic = NULL;
740         obj->pDirectSound = NULL;
741         obj->pDefaultPath = NULL;
742         
743         return IDirectMusicPerformance8Impl_QueryInterface ((LPDIRECTMUSICPERFORMANCE8)obj, lpcGUID, ppobj);
744 }