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