advapi32: Plug a couple of memory leaks.
[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., 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 = (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 = IDirectMusicPerformance8_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 static 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_IDirectMusicPerformance2) ||
174       IsEqualIID (riid, &IID_IDirectMusicPerformance8)) {
175     IUnknown_AddRef(iface);
176     *ppobj = This;
177     return S_OK;
178   }
179         
180   WARN("(%p, %s,%p): not found\n", This, debugstr_dmguid(riid), ppobj);
181   return E_NOINTERFACE;
182 }
183
184 static ULONG WINAPI IDirectMusicPerformance8Impl_AddRef (LPDIRECTMUSICPERFORMANCE8 iface) {
185   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
186   ULONG ref = InterlockedIncrement(&This->ref);
187
188   TRACE("(%p): AddRef from %d\n", This, ref - 1);
189
190   DMIME_LockModule();
191
192   return ref;
193 }
194
195 static ULONG WINAPI IDirectMusicPerformance8Impl_Release (LPDIRECTMUSICPERFORMANCE8 iface) {
196   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
197   ULONG ref = InterlockedDecrement(&This->ref);
198   TRACE("(%p): ReleaseRef to %d\n", This, ref);
199   
200   if (ref == 0) {
201     DeleteCriticalSection(&This->safe);
202     HeapFree(GetProcessHeap(), 0, This);
203   }
204
205   DMIME_UnlockModule();
206
207   return ref;
208 }
209
210 /* IDirectMusicPerformanceImpl IDirectMusicPerformance Interface part: */
211 static HRESULT WINAPI IDirectMusicPerformance8Impl_Init (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusic** ppDirectMusic, LPDIRECTSOUND pDirectSound, HWND hWnd) {
212         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
213
214         FIXME("(iface = %p, dmusic = %p, dsound = %p, hwnd = %p)\n", This, ppDirectMusic, pDirectSound, hWnd);
215         if (This->pDirectMusic || This->pDirectSound)
216           return DMUS_E_ALREADY_INITED;
217         
218         if (NULL == hWnd) {
219           hWnd = GetForegroundWindow();
220         }
221
222         if (NULL != pDirectSound) {
223           This->pDirectSound = (IDirectSound*) pDirectSound;
224           IDirectSound_AddRef((LPDIRECTSOUND) This->pDirectSound);
225         } else {
226           HRESULT hr;
227           hr = DirectSoundCreate8(NULL, (LPDIRECTSOUND8*) &This->pDirectSound, NULL);
228           if (!This->pDirectSound) return DSERR_NODRIVER;
229           
230           /** 
231            * as seen in msdn
232            * 
233            *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/idirectmusicperformance8initaudio.asp
234            */
235           if (NULL != hWnd) {
236             IDirectSound8_SetCooperativeLevel(This->pDirectSound, hWnd, DSSCL_PRIORITY);
237           } else {
238             /* how to get the ForeGround window handle ? */
239             /*IDirectSound8_SetCooperativeLevel(This->pDirectSound, hWnd, DSSCL_PRIORITY);*/
240           }
241         }
242
243         if (NULL != ppDirectMusic && NULL != *ppDirectMusic) {
244           /* app creates it's own dmusic object and gives it to performance */
245           This->pDirectMusic = (IDirectMusic8*) *ppDirectMusic;
246           IDirectMusic8_AddRef((LPDIRECTMUSIC8) This->pDirectMusic);
247         } else {
248           /* app allows the performance to initialise itfself and needs a pointer to object*/
249           CoCreateInstance (&CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusic8, (void**)&This->pDirectMusic);
250           if (ppDirectMusic) {
251             *ppDirectMusic = (LPDIRECTMUSIC) This->pDirectMusic;
252             IDirectMusic8_AddRef((LPDIRECTMUSIC8) *ppDirectMusic);
253           }
254         }
255         
256         return S_OK;
257 }
258
259 static HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegment (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, DWORD dwFlags, __int64 i64StartTime, IDirectMusicSegmentState** ppSegmentState) {
260         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
261         FIXME("(%p, %p, %d, 0x%s, %p): stub\n", This, pSegment, dwFlags,
262             wine_dbgstr_longlong(i64StartTime), ppSegmentState);
263         return S_OK;
264 }
265
266 static HRESULT WINAPI IDirectMusicPerformance8Impl_Stop (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, IDirectMusicSegmentState* pSegmentState, MUSIC_TIME mtTime, DWORD dwFlags) {
267         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
268         FIXME("(%p, %p, %p, %ld, %d): stub\n", This, pSegment, pSegmentState, mtTime, dwFlags);
269         return S_OK;
270 }
271
272 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetSegmentState (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegmentState** ppSegmentState, MUSIC_TIME mtTime) {
273         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
274         FIXME("(%p,%p, %ld): stub\n", This, ppSegmentState, mtTime);
275         return S_OK;
276 }
277
278 static HRESULT WINAPI IDirectMusicPerformance8Impl_SetPrepareTime (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwMilliSeconds) {
279   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
280   TRACE("(%p, %d)\n", This, dwMilliSeconds);
281   This->dwPrepareTime = dwMilliSeconds;
282   return S_OK;
283 }
284
285 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetPrepareTime (LPDIRECTMUSICPERFORMANCE8 iface, DWORD* pdwMilliSeconds) {
286   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
287   TRACE("(%p, %p)\n", This, pdwMilliSeconds);
288   if (NULL == pdwMilliSeconds) {
289     return E_POINTER;
290   }
291   *pdwMilliSeconds = This->dwPrepareTime;
292   return S_OK;
293 }
294
295 static HRESULT WINAPI IDirectMusicPerformance8Impl_SetBumperLength (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwMilliSeconds) {
296   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
297   TRACE("(%p, %d)\n", This, dwMilliSeconds);
298   This->dwBumperLength =  dwMilliSeconds;
299   return S_OK;
300 }
301
302 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetBumperLength (LPDIRECTMUSICPERFORMANCE8 iface, DWORD* pdwMilliSeconds) {
303   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
304   TRACE("(%p, %p)\n", This, pdwMilliSeconds);
305   if (NULL == pdwMilliSeconds) {
306     return E_POINTER;
307   }
308   *pdwMilliSeconds = This->dwBumperLength;
309   return S_OK;
310 }
311
312 static HRESULT WINAPI IDirectMusicPerformance8Impl_SendPMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pPMSG) {
313   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
314   DMUS_PMSGItem* pItem = NULL;
315   DMUS_PMSGItem* it = NULL;
316   DMUS_PMSGItem* prev_it = NULL;
317   DMUS_PMSGItem** queue = NULL;
318
319   FIXME("(%p, %p): stub\n", This, pPMSG);
320          
321   if (NULL == pPMSG) {
322     return E_POINTER;
323   }
324   pItem = DMUS_PMSGToItem(pPMSG);
325   if (NULL == pItem) {
326     return E_POINTER;
327   }
328   if (pItem->bInUse) {
329     return DMUS_E_ALREADY_SENT;
330   }
331   
332   /* TODO: Valid Flags */
333   /* TODO: DMUS_PMSGF_MUSICTIME */
334   pItem->rtItemTime = pPMSG->rtTime;
335
336   if (pPMSG->dwFlags & DMUS_PMSGF_TOOL_IMMEDIATE) {
337     queue = &This->imm_head;
338   } else {
339     queue = &This->head;
340   }
341
342   EnterCriticalSection(&This->safe);
343   for (it = *queue; NULL != it && it->rtItemTime < pItem->rtItemTime; it = it->next) {
344     prev_it = it;
345   }
346   if (NULL == prev_it) {
347     pItem->prev = NULL;
348     if (NULL != *queue) pItem->next = (*queue)->next;
349     /*assert( NULL == pItem->next->prev );*/
350     if (NULL != pItem->next) pItem->next->prev = pItem;
351     *queue = pItem;
352   } else {
353     pItem->prev = prev_it;
354     pItem->next = prev_it->next;
355     prev_it->next = pItem;
356     if (NULL != pItem->next) pItem->next->prev = pItem;
357   } 
358   LeaveCriticalSection(&This->safe);
359
360   /** now in use, prevent from stupid Frees */
361   pItem->bInUse = TRUE;
362   return S_OK;
363 }
364
365 static HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToReferenceTime (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, REFERENCE_TIME* prtTime) {
366         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
367         FIXME("(%p, %ld, %p): stub\n", This, mtTime, prtTime);
368         return S_OK;
369 }
370
371 static HRESULT WINAPI IDirectMusicPerformance8Impl_ReferenceToMusicTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtTime, MUSIC_TIME* pmtTime) {
372         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
373         FIXME("(%p, 0x%s, %p): stub\n", This, wine_dbgstr_longlong(rtTime), pmtTime);
374         return S_OK;
375 }
376
377 static HRESULT WINAPI IDirectMusicPerformance8Impl_IsPlaying (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicSegment* pSegment, IDirectMusicSegmentState* pSegState) {
378         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
379         FIXME("(%p, %p, %p): stub\n", This, pSegment, pSegState);
380         return S_FALSE;
381 }
382
383 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtNow, MUSIC_TIME* pmtNow) {
384   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
385   HRESULT hr = S_OK;
386   REFERENCE_TIME rtCur = 0;
387
388   /*TRACE("(%p, %p, %p)\n", This, prtNow, pmtNow); */
389   if (This->procThreadTicStarted) {
390     rtCur = ((REFERENCE_TIME) GetTickCount() * 10000) - This->procThreadStartTime;
391   } else {
392     /*return DMUS_E_NO_MASTER_CLOCK;*/
393   }
394   if (NULL != prtNow) {
395     *prtNow = rtCur;
396   }
397   if (NULL != pmtNow) {
398     hr = IDirectMusicPerformance8_ReferenceToMusicTime(iface, rtCur, pmtNow);
399   }
400   return hr;
401 }
402
403 static HRESULT WINAPI IDirectMusicPerformance8Impl_AllocPMsg (LPDIRECTMUSICPERFORMANCE8 iface, ULONG cb, DMUS_PMSG** ppPMSG) {
404   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
405   DMUS_PMSGItem* pItem = NULL;
406   
407   FIXME("(%p, %d, %p): stub\n", This, cb, ppPMSG);
408         
409   if (sizeof(DMUS_PMSG) > cb) {
410     return E_INVALIDARG;
411   }
412   if (NULL == ppPMSG) {
413     return E_POINTER;
414   }
415   pItem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb - sizeof(DMUS_PMSG)  + sizeof(DMUS_PMSGItem));
416   if (NULL == pItem) {
417     return E_OUTOFMEMORY;
418   }
419   pItem->pMsg.dwSize = cb;
420   *ppPMSG = DMUS_ItemToPMSG(pItem);
421   return S_OK;
422 }
423
424 static HRESULT WINAPI IDirectMusicPerformance8Impl_FreePMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pPMSG) {
425   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
426   DMUS_PMSGItem* pItem = NULL;
427   
428   FIXME("(%p, %p): stub\n", This, pPMSG);
429   
430   if (NULL == pPMSG) {
431     return E_POINTER;
432   }
433   pItem = DMUS_PMSGToItem(pPMSG);
434   if (NULL == pItem) {
435     return E_POINTER;
436   }
437   if (pItem->bInUse) {
438     /** prevent for freeing PMsg in queue (ie to be processed) */
439     return DMUS_E_CANNOT_FREE;
440   }
441   /** now we can remove it safely */
442   EnterCriticalSection(&This->safe);
443   DMUS_ItemRemoveFromQueue( This, pItem );
444   LeaveCriticalSection(&This->safe);
445
446   /** TODO: see if we should Release the pItem->pMsg->punkUser and others Interfaces */
447   HeapFree(GetProcessHeap(), 0, pItem);  
448   return S_OK;
449 }
450
451 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetGraph (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicGraph** ppGraph) {
452   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
453   FIXME("(%p, %p): to check\n", This, ppGraph);
454   if (NULL != This->pToolGraph) {
455     *ppGraph = (LPDIRECTMUSICGRAPH) This->pToolGraph; 
456     IDirectMusicGraph_AddRef((LPDIRECTMUSICGRAPH) *ppGraph);
457   } else {
458     return E_FAIL;
459   }
460   return S_OK;
461 }
462
463 static HRESULT WINAPI IDirectMusicPerformance8Impl_SetGraph (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicGraph* pGraph) {
464   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
465   
466   FIXME("(%p, %p): to check\n", This, pGraph);
467   
468   if (NULL != This->pToolGraph) {
469     /* Todo clean buffers and tools before */
470     IDirectMusicGraph_Release((LPDIRECTMUSICGRAPH) This->pToolGraph);
471   }
472   This->pToolGraph = pGraph;
473   if (NULL != This->pToolGraph) {
474     IDirectMusicGraph_AddRef((LPDIRECTMUSICGRAPH) This->pToolGraph);
475   }
476   return S_OK;
477 }
478
479 static HRESULT WINAPI IDirectMusicPerformance8Impl_SetNotificationHandle (LPDIRECTMUSICPERFORMANCE8 iface, HANDLE hNotification, REFERENCE_TIME rtMinimum) {
480   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
481   FIXME("(%p, %p, 0x%s): stub\n", This, hNotification, wine_dbgstr_longlong(rtMinimum));
482   This->hNotification = hNotification;
483   if (rtMinimum) This->rtMinimum = rtMinimum;
484   return S_OK;
485 }
486
487 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetNotificationPMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_NOTIFICATION_PMSG** ppNotificationPMsg) {
488   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
489   
490   
491   FIXME("(%p, %p): stub\n", This, ppNotificationPMsg);
492   if (NULL == ppNotificationPMsg) {
493     return E_POINTER;
494   }
495   
496   
497
498   return S_FALSE;
499   /*return S_OK;*/
500 }
501
502 static HRESULT WINAPI IDirectMusicPerformance8Impl_AddNotificationType (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidNotificationType) {
503         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
504         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
505         return S_OK;
506 }
507
508 static HRESULT WINAPI IDirectMusicPerformance8Impl_RemoveNotificationType (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidNotificationType) {
509         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
510         FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
511         return S_OK;
512 }
513
514 static HRESULT WINAPI IDirectMusicPerformance8Impl_AddPort (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicPort* pPort) {
515         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
516         HRESULT hr = E_FAIL;
517         FIXME("(%p, %p): stub\n", This, pPort);
518         if (!This->pDirectMusic || !This->pDirectSound) return DMUS_E_NOT_INIT;
519         if (NULL == pPort) {
520           GUID port_guid;
521           IDirectMusicPort* pDefaultPort = NULL;
522           DMUS_PORTPARAMS params;
523           int i, j;
524           hr = IDirectMusic8_GetDefaultPort(This->pDirectMusic, &port_guid);
525           if (FAILED(hr)) return hr;
526           ZeroMemory(&params, sizeof(params)); 
527           params.dwSize = sizeof(params);
528           params.dwValidParams = DMUS_PORTPARAMS_CHANNELGROUPS | DMUS_PORTPARAMS_SHARE;
529           params.dwChannelGroups = 1;
530           params.fShare = TRUE;
531           hr = IDirectMusic8_CreatePort(This->pDirectMusic, &port_guid, &params, &pDefaultPort, NULL);
532           if (FAILED(hr)) return hr;
533           hr = IDirectMusicPort_Activate(pDefaultPort, TRUE);
534           if (FAILED(hr)) { IDirectMusicPort_Release(pDefaultPort); return hr; }
535           j = 0;
536           for (i = 0; i < 16; ++i) {
537             if (NULL == This->PChannel[i].port) {
538               This->PChannel[i].port = pPort; 
539               This->PChannel[i].group = 0; 
540               This->PChannel[i].channel = j; /* FIXME: should this be assigned? */
541               j++;
542             }
543           }
544         } else {
545           IDirectMusicPort_AddRef(pPort);         
546         }
547         /**
548          * We should remember added Ports (for example using a list)
549          * and control if Port is registered for each api who use ports
550          */
551         return S_OK;
552 }
553
554 static HRESULT WINAPI IDirectMusicPerformance8Impl_RemovePort (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicPort* pPort) {
555         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
556         FIXME("(%p, %p): stub\n", This, pPort);
557         IDirectMusicPort_Release (pPort);
558         return S_OK;
559 }
560
561 static HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannelBlock (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwBlockNum, IDirectMusicPort* pPort, DWORD dwGroup) {
562         int i, j, range /* min value in range */;
563         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
564
565         FIXME("(%p, %d, %p, %d): semi-stub\n", This, dwBlockNum, pPort, dwGroup-1);
566         if (NULL == pPort) return E_POINTER;
567
568         range = 16 * dwBlockNum;
569         j = 0;
570         for (i = range; i < range+16; i++) {
571                 /*TRACE("Setting PChannel[%i] to port %p, group %ld, MIDI port %i\n", i, pPort, dwGroup-1, j); */
572                 This->PChannel[i].port = pPort; 
573                 This->PChannel[i].group = dwGroup - 1; /* first index is always zero */
574                 This->PChannel[i].channel = j; /* FIXME: should this be assigned? */
575                 j++;
576         }
577         /*if (dwGroup > 2) return S_FALSE;*/
578
579         return S_OK;
580 }
581
582 static HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannel (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwPChannel, IDirectMusicPort* pPort, DWORD dwGroup, DWORD dwMChannel) {
583         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
584
585         TRACE("(%p, %d, %p, %d, %d)\n", This, dwPChannel, pPort, dwGroup, dwMChannel);
586         if (NULL == pPort) return E_POINTER;
587         This->PChannel[dwPChannel].port = pPort; 
588         This->PChannel[dwPChannel].group = dwGroup; 
589         This->PChannel[dwPChannel].channel = dwMChannel;
590
591         return S_OK;
592 }
593
594 static HRESULT WINAPI IDirectMusicPerformance8Impl_PChannelInfo (LPDIRECTMUSICPERFORMANCE8 iface, DWORD dwPChannel, IDirectMusicPort** ppPort, DWORD* pdwGroup, DWORD* pdwMChannel) {
595         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
596         FIXME("(%p, %d, %p, %p, %p): stub\n", This, dwPChannel, ppPort, pdwGroup, pdwMChannel);
597         return S_OK;
598 }
599
600 static HRESULT WINAPI IDirectMusicPerformance8Impl_DownloadInstrument (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusicInstrument* pInst, DWORD dwPChannel, IDirectMusicDownloadedInstrument** ppDownInst, DMUS_NOTERANGE* pNoteRanges, DWORD dwNumNoteRanges, IDirectMusicPort** ppPort, DWORD* pdwGroup, DWORD* pdwMChannel) {
601         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
602         FIXME("(%p, %p, %d, %p, %p, %d, %p, %p, %p): stub\n", This, pInst, dwPChannel, ppDownInst, pNoteRanges, dwNumNoteRanges, ppPort, pdwGroup, pdwMChannel);
603         return S_OK;
604 }
605
606 static HRESULT WINAPI IDirectMusicPerformance8Impl_Invalidate (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, DWORD dwFlags) {
607         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
608         FIXME("(%p, %ld, %d): stub\n", This, mtTime, dwFlags);
609         return S_OK;
610 }
611
612 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
613         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
614         FIXME("(%p, %s, %d, %d, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
615         return S_OK;
616 }
617
618 static HRESULT WINAPI IDirectMusicPerformance8Impl_SetParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void* pParam) {
619         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
620         FIXME("(%p, %s, %d, %d, %ld, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
621         return S_OK;
622 }
623
624 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetGlobalParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, void* pParam, DWORD dwSize) {
625         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
626
627         TRACE("(%p, %s, %p, %d): stub\n", This, debugstr_dmguid(rguidType), pParam, dwSize);
628         
629         if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload))
630                 memcpy(pParam, &This->fAutoDownload, sizeof(&This->fAutoDownload));
631         if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel))
632                 memcpy(pParam, &This->cMasterGrooveLevel, sizeof(&This->cMasterGrooveLevel));
633         if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo))
634                 memcpy(pParam, &This->fMasterTempo, sizeof(&This->fMasterTempo));
635         if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume))
636                 memcpy(pParam, &This->lMasterVolume, sizeof(&This->lMasterVolume));
637
638         return S_OK;
639 }
640
641 static HRESULT WINAPI IDirectMusicPerformance8Impl_SetGlobalParam (LPDIRECTMUSICPERFORMANCE8 iface, REFGUID rguidType, void* pParam, DWORD dwSize) {
642         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
643
644         TRACE("(%p, %s, %p, %d)\n", This, debugstr_dmguid(rguidType), pParam, dwSize);
645         
646         if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload)) {
647                 memcpy(&This->fAutoDownload, pParam, dwSize);
648                 TRACE("=> AutoDownload set to %d\n", This->fAutoDownload);
649         }
650         if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel)) {
651                 memcpy(&This->cMasterGrooveLevel, pParam, dwSize);
652                 TRACE("=> MasterGrooveLevel set to %i\n", This->cMasterGrooveLevel);
653         }
654         if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo)) {
655                 memcpy(&This->fMasterTempo, pParam, dwSize);
656                 TRACE("=> MasterTempo set to %f\n", This->fMasterTempo);
657         }
658         if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume)) {
659                 memcpy(&This->lMasterVolume, pParam, dwSize);
660                 TRACE("=> MasterVolume set to %li\n", This->lMasterVolume);
661         }
662
663         return S_OK;
664 }
665
666 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetLatencyTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtTime) {
667         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
668         TRACE("(%p, %p): stub\n", This, prtTime);
669         *prtTime = This->rtLatencyTime;
670         return S_OK;
671 }
672
673 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetQueueTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME* prtTime) {
674         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
675         FIXME("(%p, %p): stub\n", This, prtTime);
676         return S_OK;
677 }
678
679 static HRESULT WINAPI IDirectMusicPerformance8Impl_AdjustTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtAmount) {
680         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
681         FIXME("(%p, 0x%s): stub\n", This, wine_dbgstr_longlong(rtAmount));
682         return S_OK;
683 }
684
685 static HRESULT WINAPI IDirectMusicPerformance8Impl_CloseDown (LPDIRECTMUSICPERFORMANCE8 iface) {
686   IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
687   FIXME("(%p): stub\n", This);
688   if (PostMessageToProcessMsgThread(This, PROCESSMSG_EXIT)) {
689     WaitForSingleObject(This->procThread, INFINITE);
690     This->procThreadTicStarted = FALSE;
691     CloseHandle(This->procThread);
692   }
693   if (NULL != This->pDirectSound) {
694     IDirectSound_Release((LPDIRECTSOUND) This->pDirectSound);
695     This->pDirectSound = NULL;
696   }
697   if (NULL != This->pDirectMusic) {
698     IDirectMusic8_Release((LPDIRECTMUSIC8) This->pDirectMusic);
699     This->pDirectMusic = NULL;
700   }
701   return S_OK;
702 }
703
704 static HRESULT WINAPI IDirectMusicPerformance8Impl_GetResolvedTime (LPDIRECTMUSICPERFORMANCE8 iface, REFERENCE_TIME rtTime, REFERENCE_TIME* prtResolved, DWORD dwTimeResolveFlags) {
705         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
706         FIXME("(%p, 0x%s, %p, %d): stub\n", This, wine_dbgstr_longlong(rtTime),
707             prtResolved, dwTimeResolveFlags);
708         return S_OK;
709 }
710
711 static HRESULT WINAPI IDirectMusicPerformance8Impl_MIDIToMusic (LPDIRECTMUSICPERFORMANCE8 iface, BYTE bMIDIValue, DMUS_CHORD_KEY* pChord, BYTE bPlayMode, BYTE bChordLevel, WORD* pwMusicValue) {
712         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
713         FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, bMIDIValue, pChord, bPlayMode, bChordLevel, pwMusicValue);
714         return S_OK;
715 }
716
717 static HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToMIDI (LPDIRECTMUSICPERFORMANCE8 iface, WORD wMusicValue, DMUS_CHORD_KEY* pChord, BYTE bPlayMode, BYTE bChordLevel, BYTE* pbMIDIValue) {
718         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
719         FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, wMusicValue, pChord, bPlayMode, bChordLevel, pbMIDIValue);
720         return S_OK;
721 }
722
723 static HRESULT WINAPI IDirectMusicPerformance8Impl_TimeToRhythm (LPDIRECTMUSICPERFORMANCE8 iface, MUSIC_TIME mtTime, DMUS_TIMESIGNATURE* pTimeSig, WORD* pwMeasure, BYTE* pbBeat, BYTE* pbGrid, short* pnOffset) {
724         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
725         FIXME("(%p, %ld, %p, %p, %p, %p, %p): stub\n", This, mtTime, pTimeSig, pwMeasure, pbBeat, pbGrid, pnOffset);
726         return S_OK;
727 }
728
729 static HRESULT WINAPI IDirectMusicPerformance8Impl_RhythmToTime (LPDIRECTMUSICPERFORMANCE8 iface, WORD wMeasure, BYTE bBeat, BYTE bGrid, short nOffset, DMUS_TIMESIGNATURE* pTimeSig, MUSIC_TIME* pmtTime) {
730         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
731         FIXME("(%p, %d, %d, %d, %i, %p, %p): stub\n", This, wMeasure, bBeat, bGrid, nOffset, pTimeSig, pmtTime);
732         return S_OK;
733 }
734
735 /* IDirectMusicPerformance8 Interface part follow: */
736 static HRESULT WINAPI IDirectMusicPerformance8Impl_InitAudio (LPDIRECTMUSICPERFORMANCE8 iface, 
737                                                       IDirectMusic** ppDirectMusic, 
738                                                       IDirectSound** ppDirectSound, 
739                                                       HWND hWnd, 
740                                                       DWORD dwDefaultPathType, 
741                                                       DWORD dwPChannelCount, 
742                                                       DWORD dwFlags, 
743                                                       DMUS_AUDIOPARAMS* pParams) {
744
745         IDirectSound* dsound = NULL;
746         HRESULT hr = S_OK;
747         
748         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
749         FIXME("(%p, %p, %p, %p, %x, %u, %x, %p): to check\n", This, ppDirectMusic, ppDirectSound, hWnd, dwDefaultPathType, dwPChannelCount, dwFlags, pParams);
750
751         if (This->pDirectMusic || This->pDirectSound)
752           return DMUS_E_ALREADY_INITED;
753
754         if (NULL != ppDirectSound && NULL != *ppDirectSound) {
755           dsound = *ppDirectSound;
756         } else {
757           hr = DirectSoundCreate8 (NULL, (LPDIRECTSOUND8*) &dsound, NULL);
758           FIXME("return dsound(%p,%d)\n", dsound, hr);
759           if (FAILED(hr) || !dsound)
760             return DSERR_NODRIVER;
761           if (ppDirectSound)
762             *ppDirectSound = dsound;  
763         }
764         
765         IDirectMusicPerformance8Impl_Init(iface, ppDirectMusic, dsound, hWnd);
766
767         /* Init increases the ref count of the dsound object. Decremente it if the app don't want a pointer to the object. */
768         if (NULL == ppDirectSound) {
769           IDirectSound_Release(This->pDirectSound);
770         }
771
772         /* as seen in msdn we need params init before audio path creation */
773         if (NULL != pParams) {
774           memcpy(&This->pParams, pParams, sizeof(DMUS_AUDIOPARAMS));
775         } else {
776           /**
777            * TODO, how can i fill the struct 
778            * as seen at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/dmusaudioparams.asp
779            */
780           memset(&This->pParams, 0, sizeof(DMUS_AUDIOPARAMS));
781           This->pParams.dwSize = sizeof(DMUS_AUDIOPARAMS);
782           This->pParams.fInitNow = FALSE;
783           This->pParams.dwValidData = DMUS_AUDIOPARAMS_FEATURES | DMUS_AUDIOPARAMS_VOICES | DMUS_AUDIOPARAMS_SAMPLERATE | DMUS_AUDIOPARAMS_DEFAULTSYNTH;
784           This->pParams.dwVoices = 64;
785           This->pParams.dwSampleRate = (DWORD) 22.050; 
786           This->pParams.dwFeatures = dwFlags;
787           This->pParams.clsidDefaultSynth = CLSID_DirectMusicSynthSink;
788         }
789         hr = IDirectMusicPerformance8_CreateStandardAudioPath(iface, dwDefaultPathType, dwPChannelCount, FALSE, (IDirectMusicAudioPath**) &This->pDefaultPath);
790
791         PostMessageToProcessMsgThread(This, PROCESSMSG_START);
792
793         return hr;
794 }
795
796 static HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegmentEx (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pSource, WCHAR* pwzSegmentName, IUnknown* pTransition, DWORD dwFlags, __int64 i64StartTime, IDirectMusicSegmentState** ppSegmentState, IUnknown* pFrom, IUnknown* pAudioPath) {
797         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
798         FIXME("(%p, %p, %p, %p, %d, 0x%s, %p, %p, %p): stub\n", This, pSource, pwzSegmentName,
799             pTransition, dwFlags, wine_dbgstr_longlong(i64StartTime), ppSegmentState, pFrom, pAudioPath);
800         return S_OK;
801 }
802
803 static HRESULT WINAPI IDirectMusicPerformance8Impl_StopEx (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pObjectToStop, __int64 i64StopTime, DWORD dwFlags) {
804         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
805         FIXME("(%p, %p, 0x%s, %d): stub\n", This, pObjectToStop,
806             wine_dbgstr_longlong(i64StopTime), dwFlags);
807         return S_OK;
808 }
809
810 static HRESULT WINAPI IDirectMusicPerformance8Impl_ClonePMsg (LPDIRECTMUSICPERFORMANCE8 iface, DMUS_PMSG* pSourcePMSG, DMUS_PMSG** ppCopyPMSG) {
811         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
812         FIXME("(%p, %p, %p): stub\n", This, pSourcePMSG, ppCopyPMSG);
813         return S_OK;
814 }
815
816 static HRESULT WINAPI IDirectMusicPerformance8Impl_CreateAudioPath (LPDIRECTMUSICPERFORMANCE8 iface, IUnknown* pSourceConfig, BOOL fActivate, IDirectMusicAudioPath** ppNewPath) {
817         IDirectMusicAudioPathImpl *default_path;
818         IDirectMusicAudioPath *pPath;
819
820         IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface;
821         FIXME("(%p, %p, %d, %p): stub\n", This, pSourceConfig, fActivate, ppNewPath);
822
823         if (NULL == ppNewPath) {
824           return E_POINTER;
825         }
826
827         DMUSIC_CreateDirectMusicAudioPathImpl (&IID_IDirectMusicAudioPath, (LPVOID*)&pPath, NULL);
828         default_path = (IDirectMusicAudioPathImpl*)((char*)(pPath) - offsetof(IDirectMusicAudioPathImpl,AudioPathVtbl));
829         default_path->pPerf = (IDirectMusicPerformance8*) This;
830
831         /** TODO */
832         
833         *ppNewPath = (LPDIRECTMUSICAUDIOPATH) pPath;
834
835         return IDirectMusicAudioPath_Activate(*ppNewPath, fActivate);
836 }
837
838 /**
839  * see  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directX/htm/standardaudiopaths.asp
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 havet 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 = (LPDIRECTMUSICAUDIOPATH) 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((LPDIRECTMUSICAUDIOPATH) 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((LPDIRECTMUSICAUDIOPATH) 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 = (LPDIRECTMUSICAUDIOPATH) 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, %ld, %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 = (LPDIRECTMUSICPERFORMANCE8)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
1046         /**
1047          * @see http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/htm/latencyandbumpertime.asp
1048          */
1049         obj->rtLatencyTime  = 100;  /* 100ms TO FIX */
1050         obj->dwBumperLength =   50; /* 50ms default */
1051         obj->dwPrepareTime  = 1000; /* 1000ms default */
1052         return IDirectMusicPerformance8Impl_QueryInterface ((LPDIRECTMUSICPERFORMANCE8)obj, lpcGUID, ppobj);
1053 }