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