Handle case where partial structure update requested.
[wine] / dlls / dmime / performance.c
1 /* IDirectMusicPerformance 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
25 typedef struct DMUS_PMSGItem DMUS_PMSGItem;
26 struct DMUS_PMSGItem {
27   DMUS_PMSGItem* next;
28   DMUS_PMSGItem* prev;
29
30   REFERENCE_TIME rtItemTime;
31   BOOL bInUse;
32   DWORD cb;
33   DMUS_PMSG pMsg;
34 };
35
36 #define DMUS_PMSGToItem(pMSG)   ((DMUS_PMSGItem*) (((unsigned char*) pPMSG) -  offsetof(DMUS_PMSGItem, pMsg)))
37 #define DMUS_ItemToPMSG(pItem)  (&(pItem->pMsg))
38 #define DMUS_ItemRemoveFromQueue(This,pItem) \
39 {\
40   if (pItem->prev) pItem->prev->next = pItem->next;\
41   if (pItem->next) pItem->next->prev = pItem->prev;\
42   if (This->head == pItem) This->head = pItem->next;\
43   if (This->imm_head == pItem) This->imm_head = pItem->next;\
44   pItem->bInUse = FALSE;\
45 }
46
47 #define PROCESSMSG_START           (WM_APP + 0)
48 #define PROCESSMSG_EXIT            (WM_APP + 1)
49 #define PROCESSMSG_REMOVE          (WM_APP + 2)
50 #define PROCESSMSG_ADD             (WM_APP + 4)
51
52
53 static DMUS_PMSGItem* ProceedMsg(IDirectMusicPerformance8Impl* This, DMUS_PMSGItem* cur) {
54   if (cur->pMsg.dwType == DMUS_PMSGT_NOTIFICATION) {
55     SetEvent(This->hNotification);
56   }     
57   DMUS_ItemRemoveFromQueue(This, cur);
58   switch (cur->pMsg.dwType) {
59   case DMUS_PMSGT_WAVE:
60   case DMUS_PMSGT_TEMPO:   
61   case DMUS_PMSGT_STOP:
62   default:
63     FIXME("Unhandled PMsg Type: 0x%lx\n", cur->pMsg.dwType);
64     break;
65   }
66   return cur;
67 }
68
69 static DWORD WINAPI ProcessMsgThread(LPVOID lpParam) {
70   IDirectMusicPerformance8Impl* This = (IDirectMusicPerformance8Impl*) lpParam;
71   DWORD timeOut = INFINITE;
72   MSG msg;
73   HRESULT hr;
74   REFERENCE_TIME rtLastTime;
75   REFERENCE_TIME rtCurTime;
76   DMUS_PMSGItem* it = NULL;
77   DMUS_PMSGItem* cur = NULL;
78   DMUS_PMSGItem* it_next = NULL;
79
80   while (TRUE) {
81     DWORD dwDec = This->rtLatencyTime + This->dwBumperLength;
82
83     if (timeOut > 0) MsgWaitForMultipleObjects(0, NULL, FALSE, timeOut, QS_POSTMESSAGE|QS_SENDMESSAGE|QS_TIMER);
84     timeOut = INFINITE;
85
86     EnterCriticalSection(&This->safe);
87     rtLastTime = rtCurTime;
88     hr = IDirectMusicPerformance8Impl_GetTime((IDirectMusicPerformance8*) This, &rtCurTime, NULL);
89     if (FAILED(hr)) {
90       goto outrefresh;
91     }
92     
93     for (it = This->imm_head; NULL != it; ) {
94       it_next = it->next;
95       cur = ProceedMsg(This, it);  
96       if (NULL != cur) {
97         HeapFree(GetProcessHeap(), 0, cur); 
98       }
99       it = it_next;
100     }
101
102     for (it = This->head; NULL != it && it->rtItemTime < rtCurTime + dwDec; ) {
103       it_next = it->next;
104       cur = ProceedMsg(This, it);
105       if (NULL != cur) {
106         HeapFree(GetProcessHeap(), 0, cur);
107       }
108       it = it_next;
109     }
110     if (NULL != it) {
111       timeOut = ( it->rtItemTime - rtCurTime ) + This->rtLatencyTime;
112     }
113
114 outrefresh:
115     LeaveCriticalSection(&This->safe);
116     
117     while (TRUE == PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
118       /** if hwnd we suppose that is a windows event ... */
119       if  (NULL != msg.hwnd) {
120         TranslateMessage(&msg);
121         DispatchMessageA(&msg);
122       } else {
123         switch (msg.message) {      
124         case WM_QUIT:
125         case PROCESSMSG_EXIT:
126           goto outofthread;
127           break;
128         case PROCESSMSG_START:
129           break;
130         case PROCESSMSG_ADD:
131           break;
132         case PROCESSMSG_REMOVE:
133           break;
134         default:
135           ERR("Unhandled message %u. Critical Path\n", msg.message);
136           break;
137         }
138       }
139     }
140
141     /** here we should run a little of current AudioPath */
142
143   }
144
145 outofthread:
146   TRACE("(%p): Exiting\n", This);
147   
148   return 0;
149 }
150
151 static BOOL PostMessageToProcessMsgThread(IDirectMusicPerformance8Impl* This, UINT iMsg) {
152   if (FALSE == This->procThreadTicStarted && PROCESSMSG_EXIT != iMsg) {
153     This->procThread = CreateThread(NULL, 0, ProcessMsgThread, This, 0, &This->procThreadId);
154     if (NULL == This->procThread) return FALSE;
155     SetThreadPriority(This->procThread, THREAD_PRIORITY_TIME_CRITICAL);
156     This->procThreadTicStarted = TRUE;
157   }
158   return PostThreadMessageA(This->procThreadId, iMsg, 0, 0);
159 }
160
161 /* IDirectMusicPerformance8 IUnknown part: */
162 HRESULT WINAPI IDirectMusicPerformance8Impl_QueryInterface (LPDIRECTMUSICPERFORMANCE8 iface, REFIID riid, LPVOID *ppobj) {
163   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
164   TRACE("(%p, %s,%p)\n", This, debugstr_dmguid(riid), ppobj);
165
166   if (IsEqualIID (riid, &IID_IUnknown) || 
167       IsEqualIID (riid, &IID_IDirectMusicPerformance) ||
168       IsEqualIID (riid, &IID_IDirectMusicPerformance8)) {
169     IDirectMusicPerformance8Impl_AddRef(iface);
170     *ppobj = This;
171     return S_OK;
172   }
173         
174   WARN("(%p, %s,%p): not found\n", This, debugstr_dmguid(riid), ppobj);
175   return E_NOINTERFACE;
176 }
177
178 ULONG WINAPI IDirectMusicPerformance8Impl_AddRef (LPDIRECTMUSICPERFORMANCE8 iface) {
179   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
180   TRACE("(%p): AddRef from %ld\n", This, This->ref);
181   return ++(This->ref);
182 }
183
184 ULONG WINAPI IDirectMusicPerformance8Impl_Release (LPDIRECTMUSICPERFORMANCE8 iface) {
185   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
186   ULONG ref = --This->ref;
187   TRACE("(%p): ReleaseRef to %ld\n", This, This->ref);
188   if (ref == 0) {
189     DeleteCriticalSection(&This->safe);
190     HeapFree(GetProcessHeap(), 0, This);
191   }
192   return ref;
193 }
194
195 /* IDirectMusicPerformanceImpl IDirectMusicPerformance Interface part: */
196 HRESULT WINAPI IDirectMusicPerformance8Impl_Init (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusic** ppDirectMusic, LPDIRECTSOUND pDirectSound, HWND hWnd) {
197         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
198
199         FIXME("(iface = %p, dmusic = %p, dsound = %p, hwnd = %p)\n", This, ppDirectMusic, pDirectSound, hWnd);
200         if (This->pDirectMusic || This->pDirectSound)
201           return DMUS_E_ALREADY_INITED;
202         
203         if (NULL == hWnd) {
204           hWnd = GetForegroundWindow();
205         }
206
207         if (NULL != pDirectSound) {
208           This->pDirectSound = (IDirectSound*) pDirectSound;
209           IDirectSound_AddRef((LPDIRECTSOUND) This->pDirectSound);
210         } else {
211           DirectSoundCreate8(NULL, (LPDIRECTSOUND8*) &This->pDirectSound, NULL);
212           /** 
213            * as seen in msdn
214            * 
215            *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/idirectmusicperformance8initaudio.asp
216            */
217           if (NULL != hWnd) {
218             IDirectSound8_SetCooperativeLevel(This->pDirectSound, hWnd, DSSCL_PRIORITY);
219           } else {
220             /* how to get the ForeGround window handle ? */
221             /*IDirectSound8_SetCooperativeLevel(This->pDirectSound, hWnd, DSSCL_PRIORITY);*/
222           }
223           if (!This->pDirectSound)
224             return DSERR_NODRIVER;
225         }
226
227         if (NULL != ppDirectMusic && NULL != *ppDirectMusic) {
228           /* app creates it's own dmusic object and gives it to performance */
229           This->pDirectMusic = (IDirectMusic8*) *ppDirectMusic;
230           IDirectMusic8_AddRef((LPDIRECTMUSIC8) This->pDirectMusic);
231         } else {
232           /* app allows the performance to initialise itfself and needs a pointer to object*/
233           CoCreateInstance (&CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusic8, (void**)&This->pDirectMusic);
234           if (ppDirectMusic) {
235             *ppDirectMusic = (LPDIRECTMUSIC) This->pDirectMusic;
236             IDirectMusic8_AddRef((LPDIRECTMUSIC8) *ppDirectMusic);
237           }
238         }
239         
240         return S_OK;
241 }
242
243 HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegment (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, DWORD dwFlags, __int64 i64StartTime, IDirectMusicSegmentState** ppSegmentState) {
244         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
245         FIXME("(%p, %p, %ld, %lli, %p): stub\n", This, pSegment, dwFlags, i64StartTime, ppSegmentState);
246         return S_OK;
247 }
248
249 HRESULT WINAPI IDirectMusicPerformance8Impl_Stop (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, IDirectMusicSegmentState* pSegmentState, MUSIC_TIME mtTime, DWORD dwFlags) {
250         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
251         FIXME("(%p, %p, %p, %ld, %ld): stub\n", This, pSegment, pSegmentState, mtTime, dwFlags);
252         return S_OK;
253 }
254
255 HRESULT WINAPI IDirectMusicPerformance8Impl_GetSegmentState (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegmentState** ppSegmentState, MUSIC_TIME mtTime) {
256         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
257         FIXME("(%p,%p, %ld): stub\n", This, ppSegmentState, mtTime);
258         return S_OK;
259 }
260
261 HRESULT WINAPI IDirectMusicPerformance8Impl_SetPrepareTime (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwMilliSeconds) {
262   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
263   TRACE("(%p, %ld)\n", This, dwMilliSeconds);
264   This->dwPrepareTime = dwMilliSeconds;
265   return S_OK;
266 }
267
268 HRESULT WINAPI IDirectMusicPerformance8Impl_GetPrepareTime (LPDIRECTMUSICPERFORMANCE8 iface, DWORD* pdwMilliSeconds) {
269   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
270   TRACE("(%p, %p)\n", This, pdwMilliSeconds);
271   if (NULL == pdwMilliSeconds) {
272     return E_POINTER;
273   }
274   *pdwMilliSeconds = This->dwPrepareTime;
275   return S_OK;
276 }
277
278 HRESULT WINAPI IDirectMusicPerformance8Impl_SetBumperLength (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwMilliSeconds) {
279   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
280   TRACE("(%p, %ld)\n", This, dwMilliSeconds);
281   This->dwBumperLength =  dwMilliSeconds;
282   return S_OK;
283 }
284
285 HRESULT WINAPI IDirectMusicPerformance8Impl_GetBumperLength (LPDIRECTMUSICPERFORMANCE8 iface, DWORD* pdwMilliSeconds) {
286   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
287   TRACE("(%p, %p)\n", This, pdwMilliSeconds);
288   if (NULL == pdwMilliSeconds) {
289     return E_POINTER;
290   }
291   *pdwMilliSeconds = This->dwBumperLength;
292   return S_OK;
293 }
294
295 HRESULT WINAPI IDirectMusicPerformance8Impl_SendPMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pPMSG) {
296   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
297   DMUS_PMSGItem* pItem = NULL;
298   DMUS_PMSGItem* it = NULL;
299   DMUS_PMSGItem* prev_it = NULL;
300   DMUS_PMSGItem** queue = NULL;
301
302   FIXME("(%p, %p): stub\n", This, pPMSG);
303          
304   if (NULL == pPMSG) {
305     return E_POINTER;
306   }
307   pItem = DMUS_PMSGToItem(pPMSG);
308   if (NULL == pItem) {
309     return E_POINTER;
310   }
311   if (TRUE == pItem->bInUse) {
312     return DMUS_E_ALREADY_SENT;
313   }
314   
315   /* TODO: Valid Flags */
316   /* TODO: DMUS_PMSGF_MUSICTIME */
317   pItem->rtItemTime = pPMSG->rtTime;
318
319   if (pPMSG->dwFlags & DMUS_PMSGF_TOOL_IMMEDIATE) {
320     queue = &This->imm_head;
321   } else {
322     queue = &This->head;
323   }
324
325   EnterCriticalSection(&This->safe);
326   for (it = *queue; NULL != it && it->rtItemTime < pItem->rtItemTime; it = it->next) {
327     prev_it = it;
328   }
329   if (NULL == prev_it) {
330     pItem->prev = NULL;
331     if (NULL != *queue) pItem->next = (*queue)->next;
332     /*assert( NULL == pItem->next->prev );*/
333     if (NULL != pItem->next) pItem->next->prev = pItem;
334     *queue = pItem;
335   } else {
336     pItem->prev = prev_it;
337     pItem->next = prev_it->next;
338     prev_it->next = pItem;
339     if (NULL != pItem->next) pItem->next->prev = pItem;
340   } 
341   LeaveCriticalSection(&This->safe);
342
343   /** now in use, prevent from stupid Frees */
344   pItem->bInUse = TRUE;
345   return S_OK;
346 }
347
348 HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToReferenceTime (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, REFERENCE_TIME* prtTime) {
349         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
350         FIXME("(%p, %ld, %p): stub\n", This, mtTime, prtTime);
351         return S_OK;
352 }
353
354 HRESULT WINAPI IDirectMusicPerformance8Impl_ReferenceToMusicTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtTime, MUSIC_TIME* pmtTime) {
355         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
356         FIXME("(%p, %lli, %p): stub\n", This, rtTime, pmtTime);
357         return S_OK;
358 }
359
360 HRESULT WINAPI IDirectMusicPerformance8Impl_IsPlaying (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, IDirectMusicSegmentState* pSegState) {
361         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
362         FIXME("(%p, %p, %p): stub\n", This, pSegment, pSegState);
363         return S_FALSE;
364 }
365
366 HRESULT WINAPI IDirectMusicPerformance8Impl_GetTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtNow, MUSIC_TIME* pmtNow) {
367   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
368   HRESULT hr = S_OK;
369   REFERENCE_TIME rtCur = 0;
370
371   /*TRACE("(%p, %p, %p)\n", This, prtNow, pmtNow); */
372   if (TRUE == This->procThreadTicStarted) {
373     rtCur = ((REFERENCE_TIME) GetTickCount() * 10000) - This->procThreadStartTime;
374   } else {
375     /*return DMUS_E_NO_MASTER_CLOCK;*/
376   }
377   if (NULL != prtNow) {
378     *prtNow = rtCur;
379   }
380   if (NULL != pmtNow) {
381     hr = IDirectMusicPerformance8_ReferenceToMusicTime(iface, rtCur, pmtNow);
382   }
383   return hr;
384 }
385
386 HRESULT WINAPI IDirectMusicPerformance8Impl_AllocPMsg (LPDIRECTMUSICPERFORMANCE8 iface, ULONG cb, DMUS_PMSG** ppPMSG) {
387   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
388   DMUS_PMSGItem* pItem = NULL;
389   
390   FIXME("(%p, %ld, %p): stub\n", This, cb, ppPMSG);
391         
392   if (sizeof(DMUS_PMSG) > cb) {
393     return E_INVALIDARG;
394   }
395   if (NULL == ppPMSG) {
396     return E_POINTER;
397   }
398   pItem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb - sizeof(DMUS_PMSG)  + sizeof(DMUS_PMSGItem));
399   if (NULL == pItem) {
400     return E_OUTOFMEMORY;
401   }
402   pItem->pMsg.dwSize = cb;
403   *ppPMSG = DMUS_ItemToPMSG(pItem);
404   return S_OK;
405 }
406
407 HRESULT WINAPI IDirectMusicPerformance8Impl_FreePMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pPMSG) {
408   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
409   DMUS_PMSGItem* pItem = NULL;
410   
411   FIXME("(%p, %p): stub\n", This, pPMSG);
412   
413   if (NULL == pPMSG) {
414     return E_POINTER;
415   }
416   pItem = DMUS_PMSGToItem(pPMSG);
417   if (NULL == pItem) {
418     return E_POINTER;
419   }
420   if (TRUE == pItem->bInUse) {
421     /** prevent for freeing PMsg in queue (ie to be processed) */
422     return DMUS_E_CANNOT_FREE;
423   }
424   /** now we can remove it safely */
425   EnterCriticalSection(&This->safe);
426   DMUS_ItemRemoveFromQueue( This, pItem );
427   LeaveCriticalSection(&This->safe);
428
429   /** TODO: see if we should Release the pItem->pMsg->punkUser and others Interfaces */
430   HeapFree(GetProcessHeap(), 0, pItem);  
431   return S_OK;
432 }
433
434 HRESULT WINAPI IDirectMusicPerformance8Impl_GetGraph (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicGraph** ppGraph) {
435   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
436   FIXME("(%p, %p): to check\n", This, ppGraph);
437   if (NULL != This->pToolGraph) {
438     *ppGraph = (LPDIRECTMUSICGRAPH) This->pToolGraph; 
439     IDirectMusicGraph_AddRef((LPDIRECTMUSICGRAPH) *ppGraph);
440   } else {
441     return E_FAIL;
442   }
443   return S_OK;
444 }
445
446 HRESULT WINAPI IDirectMusicPerformance8Impl_SetGraph (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicGraph* pGraph) {
447   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
448   
449   FIXME("(%p, %p): to check\n", This, pGraph);
450   
451   if (NULL != This->pToolGraph) {
452     /* Todo clean buffers and tools before */
453     IDirectMusicGraph_Release((LPDIRECTMUSICGRAPH) This->pToolGraph);
454   }
455   This->pToolGraph = pGraph;
456   if (NULL != This->pToolGraph) {
457     IDirectMusicGraph_AddRef((LPDIRECTMUSICGRAPH) This->pToolGraph);
458   }
459   return S_OK;
460 }
461
462 HRESULT WINAPI IDirectMusicPerformance8Impl_SetNotificationHandle (LPDIRECTMUSICPERFORMANCE8 iface, HANDLE hNotification, REFERENCE_TIME rtMinimum) {
463   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
464   FIXME("(%p, %p, %lli): stub\n", This, hNotification, rtMinimum);
465   This->hNotification = hNotification;
466   if (rtMinimum) This->rtMinimum = rtMinimum;
467   return S_OK;
468 }
469
470 HRESULT WINAPI IDirectMusicPerformance8Impl_GetNotificationPMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_NOTIFICATION_PMSG** ppNotificationPMsg) {
471   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
472   
473   
474   FIXME("(%p, %p): stub\n", This, ppNotificationPMsg);
475   if (NULL == ppNotificationPMsg) {
476     return E_POINTER;
477   }
478   
479   
480
481   return S_FALSE;
482   /*return S_OK;*/
483 }
484
485 HRESULT WINAPI IDirectMusicPerformance8Impl_AddNotificationType (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidNotificationType) {
486         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
487         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
488         return S_OK;
489 }
490
491 HRESULT WINAPI IDirectMusicPerformance8Impl_RemoveNotificationType (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidNotificationType) {
492         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
493         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
494         return S_OK;
495 }
496
497 HRESULT WINAPI IDirectMusicPerformance8Impl_AddPort (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicPort* pPort) {
498         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
499         FIXME("(%p, %p): stub\n", This, pPort);
500         IDirectMusicPort_AddRef (pPort);
501         return S_OK;
502 }
503
504 HRESULT WINAPI IDirectMusicPerformance8Impl_RemovePort (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicPort* pPort) {
505         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
506         FIXME("(%p, %p): stub\n", This, pPort);
507         IDirectMusicPort_Release (pPort);
508         return S_OK;
509 }
510
511 HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannelBlock (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwBlockNum, IDirectMusicPort* pPort, DWORD dwGroup) {
512         int i, j, range /* min value in range */;
513         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
514
515         FIXME("(%p, %ld, %p, %ld): semi-stub\n", This, dwBlockNum, pPort, dwGroup-1);
516         range = 16 * dwBlockNum;
517         j = 0;
518                 
519         for (i = range; i < range+16; i++) {
520                 /*TRACE("Setting PChannel[%i] to port %p, group %ld, MIDI port %i\n", i, pPort, dwGroup-1, j); */
521                 This->PChannel[i].port = pPort; 
522                 This->PChannel[i].group = dwGroup - 1; /* first index is always zero */
523                 This->PChannel[i].channel = j; /* FIXME: should this be assigned? */
524                 j++;
525         }
526
527         return S_OK;
528 }
529
530 HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannel (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwPChannel, IDirectMusicPort* pPort, DWORD dwGroup, DWORD dwMChannel) {
531         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
532
533         TRACE("(%p, %ld, %p, %ld, %ld)\n", This, dwPChannel, pPort, dwGroup, dwMChannel);
534         This->PChannel[dwPChannel].port = pPort; 
535         This->PChannel[dwPChannel].group = dwGroup; 
536         This->PChannel[dwPChannel].channel = dwMChannel;
537
538         return S_OK;
539 }
540
541 HRESULT WINAPI IDirectMusicPerformance8Impl_PChannelInfo (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwPChannel, IDirectMusicPort** ppPort, DWORD* pdwGroup, DWORD* pdwMChannel) {
542         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
543         FIXME("(%p, %ld, %p, %p, %p): stub\n", This, dwPChannel, ppPort, pdwGroup, pdwMChannel);
544         return S_OK;
545 }
546
547 HRESULT WINAPI IDirectMusicPerformance8Impl_DownloadInstrument (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicInstrument* pInst, DWORD dwPChannel, IDirectMusicDownloadedInstrument** ppDownInst, DMUS_NOTERANGE* pNoteRanges, DWORD dwNumNoteRanges, IDirectMusicPort** ppPort, DWORD* pdwGroup, DWORD* pdwMChannel) {
548         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
549         FIXME("(%p, %p, %ld, %p, %p, %ld, %p, %p, %p): stub\n", This, pInst, dwPChannel, ppDownInst, pNoteRanges, dwNumNoteRanges, ppPort, pdwGroup, pdwMChannel);
550         return S_OK;
551 }
552
553 HRESULT WINAPI IDirectMusicPerformance8Impl_Invalidate (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, DWORD dwFlags) {
554         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
555         FIXME("(%p, %ld, %ld): stub\n", This, mtTime, dwFlags);
556         return S_OK;
557 }
558
559 HRESULT WINAPI IDirectMusicPerformance8Impl_GetParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
560         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
561         FIXME("(%p, %s, %ld, %ld, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
562         return S_OK;
563 }
564
565 HRESULT WINAPI IDirectMusicPerformance8Impl_SetParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void* pParam) {
566         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
567         FIXME("(%p, %s, %ld, %ld, %ld, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
568         return S_OK;
569 }
570
571 HRESULT WINAPI IDirectMusicPerformance8Impl_GetGlobalParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, void* pParam, DWORD dwSize) {
572         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
573
574         TRACE("(%p, %s, %p, %ld): stub\n", This, debugstr_dmguid(rguidType), pParam, dwSize);
575         
576         if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload))
577                 memcpy(pParam, &This->fAutoDownload, sizeof(&This->fAutoDownload));
578         if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel))
579                 memcpy(pParam, &This->cMasterGrooveLevel, sizeof(&This->cMasterGrooveLevel));
580         if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo))
581                 memcpy(pParam, &This->fMasterTempo, sizeof(&This->fMasterTempo));
582         if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume))
583                 memcpy(pParam, &This->lMasterVolume, sizeof(&This->lMasterVolume));
584
585         return S_OK;
586 }
587
588 HRESULT WINAPI IDirectMusicPerformance8Impl_SetGlobalParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, void* pParam, DWORD dwSize) {
589         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
590
591         TRACE("(%p, %s, %p, %ld)\n", This, debugstr_dmguid(rguidType), pParam, dwSize);
592         
593         if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload)) {
594                 memcpy(&This->fAutoDownload, pParam, dwSize);
595                 TRACE("=> AutoDownload set to %d\n", This->fAutoDownload);
596         }
597         if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel)) {
598                 memcpy(&This->cMasterGrooveLevel, pParam, dwSize);
599                 TRACE("=> MasterGrooveLevel set to %i\n", This->cMasterGrooveLevel);
600         }
601         if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo)) {
602                 memcpy(&This->fMasterTempo, pParam, dwSize);
603                 TRACE("=> MasterTempo set to %f\n", This->fMasterTempo);
604         }
605         if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume)) {
606                 memcpy(&This->lMasterVolume, pParam, dwSize);
607                 TRACE("=> MasterVolume set to %li\n", This->lMasterVolume);
608         }
609
610         return S_OK;
611 }
612
613 HRESULT WINAPI IDirectMusicPerformance8Impl_GetLatencyTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtTime) {
614         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
615         TRACE("(%p, %p): stub\n", This, prtTime);
616         *prtTime = This->rtLatencyTime;
617         return S_OK;
618 }
619
620 HRESULT WINAPI IDirectMusicPerformance8Impl_GetQueueTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtTime) {
621         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
622         FIXME("(%p, %p): stub\n", This, prtTime);
623         return S_OK;
624 }
625
626 HRESULT WINAPI IDirectMusicPerformance8Impl_AdjustTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtAmount) {
627         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
628         FIXME("(%p, %lli): stub\n", This, rtAmount);
629         return S_OK;
630 }
631
632 HRESULT WINAPI IDirectMusicPerformance8Impl_CloseDown (LPDIRECTMUSICPERFORMANCE8 iface) {
633   ICOM_THIS(IDirectMusicPerformance8Impl,iface);
634   FIXME("(%p): stub\n", This);
635   if (PostMessageToProcessMsgThread(This, PROCESSMSG_EXIT)) {
636     WaitForSingleObject(This->procThread, INFINITE);
637     This->procThreadTicStarted = FALSE;
638     CloseHandle(This->procThread);
639   }
640   if (NULL != This->pDirectSound) {
641     IDirectSound_Release((LPDIRECTSOUND) This->pDirectSound);
642     This->pDirectSound = NULL;
643   }
644   if (NULL != This->pDirectMusic) {
645     IDirectMusic8_Release((LPDIRECTMUSIC8) This->pDirectMusic);
646     This->pDirectMusic = NULL;
647   }
648   return S_OK;
649 }
650
651 HRESULT WINAPI IDirectMusicPerformance8Impl_GetResolvedTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtTime, REFERENCE_TIME* prtResolved, DWORD dwTimeResolveFlags) {
652         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
653         FIXME("(%p, %lli, %p, %ld): stub\n", This, rtTime, prtResolved, dwTimeResolveFlags);
654         return S_OK;
655 }
656
657 HRESULT WINAPI IDirectMusicPerformance8Impl_MIDIToMusic (LPDIRECTMUSICPERFORMANCE8 iface, BYTE bMIDIValue, DMUS_CHORD_KEY* pChord, BYTE bPlayMode, BYTE bChordLevel, WORD* pwMusicValue) {
658         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
659         FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, bMIDIValue, pChord, bPlayMode, bChordLevel, pwMusicValue);
660         return S_OK;
661 }
662
663 HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToMIDI (LPDIRECTMUSICPERFORMANCE8 iface, WORD wMusicValue, DMUS_CHORD_KEY* pChord, BYTE bPlayMode, BYTE bChordLevel, BYTE* pbMIDIValue) {
664         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
665         FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, wMusicValue, pChord, bPlayMode, bChordLevel, pbMIDIValue);
666         return S_OK;
667 }
668
669 HRESULT WINAPI IDirectMusicPerformance8Impl_TimeToRhythm (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, DMUS_TIMESIGNATURE* pTimeSig, WORD* pwMeasure, BYTE* pbBeat, BYTE* pbGrid, short* pnOffset) {
670         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
671         FIXME("(%p, %ld, %p, %p, %p, %p, %p): stub\n", This, mtTime, pTimeSig, pwMeasure, pbBeat, pbGrid, pnOffset);
672         return S_OK;
673 }
674
675 HRESULT WINAPI IDirectMusicPerformance8Impl_RhythmToTime (LPDIRECTMUSICPERFORMANCE8 iface, WORD wMeasure, BYTE bBeat, BYTE bGrid, short nOffset, DMUS_TIMESIGNATURE* pTimeSig, MUSIC_TIME* pmtTime) {
676         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
677         FIXME("(%p, %d, %d, %d, %i, %p, %p): stub\n", This, wMeasure, bBeat, bGrid, nOffset, pTimeSig, pmtTime);
678         return S_OK;
679 }
680
681 /* IDirectMusicPerformance8 Interface part follow: */
682 HRESULT WINAPI IDirectMusicPerformance8Impl_InitAudio (LPDIRECTMUSICPERFORMANCE8 iface, 
683                                                       IDirectMusic** ppDirectMusic, 
684                                                       IDirectSound** ppDirectSound, 
685                                                       HWND hWnd, 
686                                                       DWORD dwDefaultPathType, 
687                                                       DWORD dwPChannelCount, 
688                                                       DWORD dwFlags, 
689                                                       DMUS_AUDIOPARAMS* pParams) {
690
691         IDirectSound* dsound = NULL;
692         HRESULT hr = S_OK;
693         
694         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
695         FIXME("(%p, %p, %p, %p, %lx, %lu, %lx, %p): to check\n", This, ppDirectMusic, ppDirectSound, hWnd, dwDefaultPathType, dwPChannelCount, dwFlags, pParams);
696
697         if (This->pDirectMusic || This->pDirectSound)
698           return DMUS_E_ALREADY_INITED;
699
700         if (NULL != ppDirectSound && NULL != *ppDirectSound) {
701           dsound = *ppDirectSound;
702         } else {
703           hr = DirectSoundCreate8 (NULL, (LPDIRECTSOUND8*) &dsound, NULL);
704           FIXME("return dsound(%p,%ld)\n", dsound, hr);
705           if (FAILED(hr) || !dsound)
706             return DSERR_NODRIVER;
707           if (ppDirectSound)
708             *ppDirectSound = dsound;  
709         }
710         
711         IDirectMusicPerformance8Impl_Init(iface, ppDirectMusic, dsound, hWnd);
712
713         /* Init increases the ref count of the dsound object. Decremente it if the app don't want a pointer to the object. */
714         if (NULL == ppDirectSound) {
715           IDirectSound_Release(This->pDirectSound);
716         }
717
718         /* as seen in msdn we need params init before audio path creation */
719         if (NULL != pParams) {
720           memcpy(&This->pParams, pParams, sizeof(DMUS_AUDIOPARAMS));
721         } else {
722           /**
723            * TODO, how can i fill the struct 
724            * as seen at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/dmusaudioparams.asp
725            */
726           memset(&This->pParams, 0, sizeof(DMUS_AUDIOPARAMS));
727           This->pParams.dwSize = sizeof(DMUS_AUDIOPARAMS);
728           This->pParams.fInitNow = FALSE;
729           This->pParams.dwValidData = DMUS_AUDIOPARAMS_FEATURES | DMUS_AUDIOPARAMS_VOICES | DMUS_AUDIOPARAMS_SAMPLERATE | DMUS_AUDIOPARAMS_DEFAULTSYNTH;
730           This->pParams.dwVoices = 64;
731           This->pParams.dwSampleRate = (DWORD) 22.050; 
732           This->pParams.dwFeatures = dwFlags;
733           This->pParams.clsidDefaultSynth = CLSID_DirectMusicSynthSink;
734         }
735         hr = IDirectMusicPerformance8Impl_CreateStandardAudioPath(iface, dwDefaultPathType, dwPChannelCount, FALSE, (IDirectMusicAudioPath**) &This->pDefaultPath);
736
737         PostMessageToProcessMsgThread(This, PROCESSMSG_START);
738
739         return hr;
740 }
741
742 HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegmentEx (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pSource, WCHAR* pwzSegmentName, IUnknown* pTransition, DWORD dwFlags, __int64 i64StartTime, IDirectMusicSegmentState** ppSegmentState, IUnknown* pFrom, IUnknown* pAudioPath) {
743         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
744         FIXME("(%p, %p, %p, %p, %ld, %lli, %p, %p, %p): stub\n", This, pSource, pwzSegmentName, pTransition, dwFlags, i64StartTime, ppSegmentState, pFrom, pAudioPath);
745         return S_OK;
746 }
747
748 HRESULT WINAPI IDirectMusicPerformance8Impl_StopEx (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pObjectToStop, __int64 i64StopTime, DWORD dwFlags) {
749         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
750         FIXME("(%p, %p, %lli, %ld): stub\n", This, pObjectToStop, i64StopTime, dwFlags);
751         return S_OK;
752 }
753
754 HRESULT WINAPI IDirectMusicPerformance8Impl_ClonePMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pSourcePMSG, DMUS_PMSG** ppCopyPMSG) {
755         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
756         FIXME("(%p, %p, %p): stub\n", This, pSourcePMSG, ppCopyPMSG);
757         return S_OK;
758 }
759
760 HRESULT WINAPI IDirectMusicPerformance8Impl_CreateAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pSourceConfig, BOOL fActivate, IDirectMusicAudioPath** ppNewPath) {
761         IDirectMusicAudioPathImpl *default_path;
762         IDirectMusicAudioPath *pPath;
763
764         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
765         FIXME("(%p, %p, %d, %p): stub\n", This, pSourceConfig, fActivate, ppNewPath);
766
767         if (NULL == ppNewPath) {
768           return E_POINTER;
769         }
770
771         DMUSIC_CreateDirectMusicAudioPathImpl (&IID_IDirectMusicAudioPath, (LPVOID*)&pPath, NULL);
772         default_path = (IDirectMusicAudioPathImpl*)((char*)(pPath) - offsetof(IDirectMusicAudioPathImpl,AudioPathVtbl));
773         default_path->pPerf = (IDirectMusicPerformance8*) This;
774
775         /** TODO */
776         
777         *ppNewPath = (LPDIRECTMUSICAUDIOPATH) pPath;
778
779         return IDirectMusicAudioPathImpl_IDirectMusicAudioPath_Activate(*ppNewPath, fActivate);
780 }
781
782 /**
783  * see  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/standardaudiopaths.asp
784  */
785 HRESULT WINAPI IDirectMusicPerformance8Impl_CreateStandardAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwType, DWORD dwPChannelCount, BOOL fActivate, IDirectMusicAudioPath** ppNewPath) {
786         IDirectMusicAudioPathImpl *default_path;
787         IDirectMusicAudioPath *pPath;
788         DSBUFFERDESC desc;
789         WAVEFORMATEX format;
790         LPDIRECTSOUNDBUFFER8 buffer;
791         HRESULT hr = S_OK;
792
793         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
794         
795         FIXME("(%p)->(%ld, %ld, %d, %p): semi-stub\n", This, dwType, dwPChannelCount, fActivate, ppNewPath);
796
797         if (NULL == ppNewPath) {
798           return E_POINTER;
799         }
800         
801         DMUSIC_CreateDirectMusicAudioPathImpl (&IID_IDirectMusicAudioPath, (LPVOID*)&pPath, NULL);
802         default_path = (IDirectMusicAudioPathImpl*)((char*)(pPath) - offsetof(IDirectMusicAudioPathImpl,AudioPathVtbl));
803         default_path->pPerf = (IDirectMusicPerformance8*) This;
804         
805         /* Secondary buffer description */
806         memset(&format, 0, sizeof(format));
807         format.wFormatTag = WAVE_FORMAT_PCM;
808         format.nChannels = 1;
809         format.nSamplesPerSec = 44000;
810         format.nAvgBytesPerSec = 44000*2;
811         format.nBlockAlign = 2;
812         format.wBitsPerSample = 16;
813         format.cbSize = 0;
814         
815         memset(&desc, 0, sizeof(desc));
816         desc.dwSize = sizeof(desc);
817         desc.dwFlags = DSBCAPS_CTRLFX | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS;
818         desc.dwBufferBytes = DSBSIZE_MIN;
819         desc.dwReserved = 0;
820         desc.lpwfxFormat = &format;
821         desc.guid3DAlgorithm = GUID_NULL;
822         
823         switch(dwType) {
824         case DMUS_APATH_DYNAMIC_3D:
825                 desc.dwFlags |= DSBCAPS_CTRL3D | DSBCAPS_CTRLFREQUENCY | DSBCAPS_MUTE3DATMAXDISTANCE;
826                 break;
827         case DMUS_APATH_DYNAMIC_MONO:
828                 desc.dwFlags |= DSBCAPS_CTRLFREQUENCY;
829                 break;
830         case DMUS_APATH_SHARED_STEREOPLUSREVERB:
831                 /* normally we havet to create 2 buffers (one for music other for reverb) 
832                  * in this case. See msdn
833                  */
834         case DMUS_APATH_DYNAMIC_STEREO:
835                 desc.dwFlags |= DSBCAPS_CTRLFREQUENCY;
836                 format.nChannels = 2;
837                 format.nBlockAlign *= 2;
838                 format.nAvgBytesPerSec *=2;
839                 break;
840         default:
841                 HeapFree(GetProcessHeap(), 0, default_path); 
842                 *ppNewPath = NULL;
843                 return E_INVALIDARG;
844                 break;
845         }
846
847         /* FIXME: Should we create one secondary buffer for each PChannel? */
848         hr = IDirectSound8_CreateSoundBuffer ((LPDIRECTSOUND8) This->pDirectSound, &desc, &buffer, NULL);
849         if (FAILED(hr)) {
850                 HeapFree(GetProcessHeap(), 0, default_path); 
851                 *ppNewPath = NULL;
852                 return DSERR_BUFFERLOST;
853         }
854         default_path->pDSBuffer = (IDirectSoundBuffer*) buffer;
855
856         /* Update description for creating primary buffer */
857         desc.dwFlags |= DSBCAPS_PRIMARYBUFFER;
858         desc.dwBufferBytes = 0;
859         desc.lpwfxFormat = NULL;
860
861         hr = IDirectSound8_CreateSoundBuffer ((LPDIRECTSOUND8) This->pDirectSound, &desc, &buffer, NULL);
862         if (FAILED(hr)) {
863                 IDirectSoundBuffer_Release(default_path->pDSBuffer);
864                 HeapFree(GetProcessHeap(), 0, default_path); 
865                 *ppNewPath = NULL;
866                 return DSERR_BUFFERLOST;
867         }
868         default_path->pPrimary = (IDirectSoundBuffer*) buffer;
869
870         *ppNewPath = (LPDIRECTMUSICAUDIOPATH) pPath;
871         
872         TRACE(" returning IDirectMusicPerformance interface at %p.\n", *ppNewPath);
873
874         return IDirectMusicAudioPathImpl_IDirectMusicAudioPath_Activate(*ppNewPath, fActivate);
875 }
876
877 HRESULT WINAPI IDirectMusicPerformance8Impl_SetDefaultAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicAudioPath* pAudioPath) {
878         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
879
880         FIXME("(%p, %p): semi-stub\n", This, pAudioPath);
881         if (NULL != This->pDefaultPath) {
882                 IDirectMusicAudioPath_Release((LPDIRECTMUSICAUDIOPATH) This->pDefaultPath);
883                 ((IDirectMusicAudioPathImpl*) This->pDefaultPath)->pPerf = NULL;
884                 This->pDefaultPath = NULL;
885         }
886         This->pDefaultPath = pAudioPath;
887         if (NULL != This->pDefaultPath) {
888                 IDirectMusicAudioPath_AddRef((LPDIRECTMUSICAUDIOPATH) This->pDefaultPath);
889                 ((IDirectMusicAudioPathImpl*) This->pDefaultPath)->pPerf = (IDirectMusicPerformance8*) This;    
890         }
891         
892         return S_OK;
893 }
894
895 HRESULT WINAPI IDirectMusicPerformance8Impl_GetDefaultAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicAudioPath** ppAudioPath) {
896     ICOM_THIS(IDirectMusicPerformance8Impl,iface);
897
898         FIXME("(%p, %p): semi-stub (%p)\n", This, ppAudioPath, This->pDefaultPath);
899
900         if (NULL != This->pDefaultPath) {
901           *ppAudioPath = (LPDIRECTMUSICAUDIOPATH) This->pDefaultPath;
902           IDirectMusicAudioPath_AddRef(*ppAudioPath);
903         } else {
904           *ppAudioPath = NULL;
905         }
906         return S_OK;
907 }
908
909 HRESULT WINAPI IDirectMusicPerformance8Impl_GetParamEx (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwTrackID, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
910         ICOM_THIS(IDirectMusicPerformance8Impl,iface);
911
912         FIXME("(%p, %s, %ld, %ld, %ld, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), dwTrackID, dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
913
914         return S_OK;
915 }
916
917 ICOM_VTABLE(IDirectMusicPerformance8) DirectMusicPerformance8_Vtbl = {
918     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
919         IDirectMusicPerformance8Impl_QueryInterface,
920         IDirectMusicPerformance8Impl_AddRef,
921         IDirectMusicPerformance8Impl_Release,
922         IDirectMusicPerformance8Impl_Init,
923         IDirectMusicPerformance8Impl_PlaySegment,
924         IDirectMusicPerformance8Impl_Stop,
925         IDirectMusicPerformance8Impl_GetSegmentState,
926         IDirectMusicPerformance8Impl_SetPrepareTime,
927         IDirectMusicPerformance8Impl_GetPrepareTime,
928         IDirectMusicPerformance8Impl_SetBumperLength,
929         IDirectMusicPerformance8Impl_GetBumperLength,
930         IDirectMusicPerformance8Impl_SendPMsg,
931         IDirectMusicPerformance8Impl_MusicToReferenceTime,
932         IDirectMusicPerformance8Impl_ReferenceToMusicTime,
933         IDirectMusicPerformance8Impl_IsPlaying,
934         IDirectMusicPerformance8Impl_GetTime,
935         IDirectMusicPerformance8Impl_AllocPMsg,
936         IDirectMusicPerformance8Impl_FreePMsg,
937         IDirectMusicPerformance8Impl_GetGraph,
938         IDirectMusicPerformance8Impl_SetGraph,
939         IDirectMusicPerformance8Impl_SetNotificationHandle,
940         IDirectMusicPerformance8Impl_GetNotificationPMsg,
941         IDirectMusicPerformance8Impl_AddNotificationType,
942         IDirectMusicPerformance8Impl_RemoveNotificationType,
943         IDirectMusicPerformance8Impl_AddPort,
944         IDirectMusicPerformance8Impl_RemovePort,
945         IDirectMusicPerformance8Impl_AssignPChannelBlock,
946         IDirectMusicPerformance8Impl_AssignPChannel,
947         IDirectMusicPerformance8Impl_PChannelInfo,
948         IDirectMusicPerformance8Impl_DownloadInstrument,
949         IDirectMusicPerformance8Impl_Invalidate,
950         IDirectMusicPerformance8Impl_GetParam,
951         IDirectMusicPerformance8Impl_SetParam,
952         IDirectMusicPerformance8Impl_GetGlobalParam,
953         IDirectMusicPerformance8Impl_SetGlobalParam,
954         IDirectMusicPerformance8Impl_GetLatencyTime,
955         IDirectMusicPerformance8Impl_GetQueueTime,
956         IDirectMusicPerformance8Impl_AdjustTime,
957         IDirectMusicPerformance8Impl_CloseDown,
958         IDirectMusicPerformance8Impl_GetResolvedTime,
959         IDirectMusicPerformance8Impl_MIDIToMusic,
960         IDirectMusicPerformance8Impl_MusicToMIDI,
961         IDirectMusicPerformance8Impl_TimeToRhythm,
962         IDirectMusicPerformance8Impl_RhythmToTime,
963         IDirectMusicPerformance8Impl_InitAudio,
964         IDirectMusicPerformance8Impl_PlaySegmentEx,
965         IDirectMusicPerformance8Impl_StopEx,
966         IDirectMusicPerformance8Impl_ClonePMsg,
967         IDirectMusicPerformance8Impl_CreateAudioPath,
968         IDirectMusicPerformance8Impl_CreateStandardAudioPath,
969         IDirectMusicPerformance8Impl_SetDefaultAudioPath,
970         IDirectMusicPerformance8Impl_GetDefaultAudioPath,
971         IDirectMusicPerformance8Impl_GetParamEx
972 };
973
974 /* for ClassFactory */
975 HRESULT WINAPI DMUSIC_CreateDirectMusicPerformanceImpl (LPCGUID lpcGUID, LPVOID *ppobj, LPUNKNOWN pUnkOuter) {
976         IDirectMusicPerformance8Impl *obj;
977
978         TRACE("(%p,%p,%p)\n", lpcGUID, ppobj, pUnkOuter);
979
980         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicPerformance8Impl));
981         if (NULL == obj)        {
982                 *ppobj = (LPDIRECTMUSICPERFORMANCE8)NULL;
983                 return E_OUTOFMEMORY;
984         }
985         obj->lpVtbl = &DirectMusicPerformance8_Vtbl;
986         obj->ref = 0;  /* will be inited by QueryInterface */
987         obj->pDirectMusic = NULL;
988         obj->pDirectSound = NULL;
989         obj->pDefaultPath = NULL;
990         InitializeCriticalSection(&obj->safe);
991
992         /**
993          * @see http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/htm/latencyandbumpertime.asp
994          */
995         obj->rtLatencyTime  = 100;  /* 100ms TO FIX */
996         obj->dwBumperLength =   50; /* 50ms default */
997         obj->dwPrepareTime  = 1000; /* 1000ms default */
998         return IDirectMusicPerformance8Impl_QueryInterface ((LPDIRECTMUSICPERFORMANCE8)obj, lpcGUID, ppobj);
999 }