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