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