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