performance.c 42.8 KB
Newer Older
1 2
/* IDirectMusicPerformance Implementation
 *
3
 * Copyright (C) 2003-2004 Rok Mandeljc
4
 * Copyright (C) 2003-2004 Raphael Junqueira
5
 *
6 7 8 9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
10 11 12
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
15
 *
16 17 18
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include "dmime_private.h"
22 23
#include "wine/heap.h"
#include "wine/rbtree.h"
24
#include "dmobject.h"
25

26
WINE_DEFAULT_DEBUG_CHANNEL(dmime);
27

28 29
struct pchannel_block {
    DWORD block_num;   /* Block 0 is PChannels 0-15, Block 1 is PChannels 16-31, etc */
30 31 32 33 34
    struct {
       DWORD channel;  /* MIDI channel */
       DWORD group;    /* MIDI group */
       IDirectMusicPort *port;
    } pchannel[16];
35 36 37
    struct wine_rb_entry entry;
};

38 39 40
typedef struct IDirectMusicPerformance8Impl {
    IDirectMusicPerformance8 IDirectMusicPerformance8_iface;
    LONG ref;
41
    IDirectMusic8 *dmusic;
42
    IDirectSound *dsound;
43
    IDirectMusicGraph *pToolGraph;
44
    DMUS_AUDIOPARAMS params;
45 46 47 48 49
    BOOL fAutoDownload;
    char cMasterGrooveLevel;
    float fMasterTempo;
    long lMasterVolume;
    /* performance channels */
50
    struct wine_rb_tree pchannels;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    /* IDirectMusicPerformance8Impl fields */
    IDirectMusicAudioPath *pDefaultPath;
    HANDLE hNotification;
    REFERENCE_TIME rtMinimum;
    REFERENCE_TIME rtLatencyTime;
    DWORD dwBumperLength;
    DWORD dwPrepareTime;
    /** Message Processing */
    HANDLE procThread;
    DWORD procThreadId;
    REFERENCE_TIME procThreadStartTime;
    BOOL procThreadTicStarted;
    CRITICAL_SECTION safe;
    struct DMUS_PMSGItem *head;
    struct DMUS_PMSGItem *imm_head;
} IDirectMusicPerformance8Impl;

68 69 70 71 72 73 74 75 76 77
typedef struct DMUS_PMSGItem DMUS_PMSGItem;
struct DMUS_PMSGItem {
  DMUS_PMSGItem* next;
  DMUS_PMSGItem* prev;

  REFERENCE_TIME rtItemTime;
  BOOL bInUse;
  DWORD cb;
  DMUS_PMSG pMsg;
};
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#define DMUS_PMSGToItem(pMSG)   ((DMUS_PMSGItem*) (((unsigned char*) pPMSG) -  offsetof(DMUS_PMSGItem, pMsg)))
#define DMUS_ItemToPMSG(pItem)  (&(pItem->pMsg))
#define DMUS_ItemRemoveFromQueue(This,pItem) \
{\
  if (pItem->prev) pItem->prev->next = pItem->next;\
  if (pItem->next) pItem->next->prev = pItem->prev;\
  if (This->head == pItem) This->head = pItem->next;\
  if (This->imm_head == pItem) This->imm_head = pItem->next;\
  pItem->bInUse = FALSE;\
}

#define PROCESSMSG_START           (WM_APP + 0)
#define PROCESSMSG_EXIT            (WM_APP + 1)
#define PROCESSMSG_REMOVE          (WM_APP + 2)
#define PROCESSMSG_ADD             (WM_APP + 4)


96
static DMUS_PMSGItem* ProceedMsg(IDirectMusicPerformance8Impl* This, DMUS_PMSGItem* cur) {
97 98 99 100 101
  if (cur->pMsg.dwType == DMUS_PMSGT_NOTIFICATION) {
    SetEvent(This->hNotification);
  }	
  DMUS_ItemRemoveFromQueue(This, cur);
  switch (cur->pMsg.dwType) {
102 103
  case DMUS_PMSGT_WAVE:
  case DMUS_PMSGT_TEMPO:   
104 105
  case DMUS_PMSGT_STOP:
  default:
106
    FIXME("Unhandled PMsg Type: %#lx\n", cur->pMsg.dwType);
107 108
    break;
  }
109
  return cur;
110 111 112
}

static DWORD WINAPI ProcessMsgThread(LPVOID lpParam) {
113
  IDirectMusicPerformance8Impl* This = lpParam;
114
  DWORD timeOut = INFINITE;
115 116 117 118
  MSG msg;
  HRESULT hr;
  REFERENCE_TIME rtCurTime;
  DMUS_PMSGItem* it = NULL;
119
  DMUS_PMSGItem* cur = NULL;
120 121 122 123 124
  DMUS_PMSGItem* it_next = NULL;

  while (TRUE) {
    DWORD dwDec = This->rtLatencyTime + This->dwBumperLength;

125 126 127
    if (timeOut > 0) MsgWaitForMultipleObjects(0, NULL, FALSE, timeOut, QS_POSTMESSAGE|QS_SENDMESSAGE|QS_TIMER);
    timeOut = INFINITE;

128
    EnterCriticalSection(&This->safe);
129
    hr = IDirectMusicPerformance8_GetTime(&This->IDirectMusicPerformance8_iface, &rtCurTime, NULL);
130 131 132 133 134 135
    if (FAILED(hr)) {
      goto outrefresh;
    }
    
    for (it = This->imm_head; NULL != it; ) {
      it_next = it->next;
136
      cur = ProceedMsg(This, it);  
137
      HeapFree(GetProcessHeap(), 0, cur); 
138 139 140 141 142
      it = it_next;
    }

    for (it = This->head; NULL != it && it->rtItemTime < rtCurTime + dwDec; ) {
      it_next = it->next;
143
      cur = ProceedMsg(This, it);
144
      HeapFree(GetProcessHeap(), 0, cur);
145 146
      it = it_next;
    }
147 148 149
    if (NULL != it) {
      timeOut = ( it->rtItemTime - rtCurTime ) + This->rtLatencyTime;
    }
150 151 152 153

outrefresh:
    LeaveCriticalSection(&This->safe);
    
154
    while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      /** if hwnd we suppose that is a windows event ... */
      if  (NULL != msg.hwnd) {
	TranslateMessage(&msg);
	DispatchMessageA(&msg);
      } else {
	switch (msg.message) {	    
	case WM_QUIT:
	case PROCESSMSG_EXIT:
	  goto outofthread;
	case PROCESSMSG_START:
	  break;
	case PROCESSMSG_ADD:
	  break;
	case PROCESSMSG_REMOVE:
	  break;
	default:
	  ERR("Unhandled message %u. Critical Path\n", msg.message);
	  break;
173
	}
174 175
      }
    }
176 177 178

    /** here we should run a little of current AudioPath */

179 180 181 182 183 184 185 186 187 188
  }

outofthread:
  TRACE("(%p): Exiting\n", This);
  
  return 0;
}

static BOOL PostMessageToProcessMsgThread(IDirectMusicPerformance8Impl* This, UINT iMsg) {
  if (FALSE == This->procThreadTicStarted && PROCESSMSG_EXIT != iMsg) {
189
    BOOL res;
190 191 192 193
    This->procThread = CreateThread(NULL, 0, ProcessMsgThread, This, 0, &This->procThreadId);
    if (NULL == This->procThread) return FALSE;
    SetThreadPriority(This->procThread, THREAD_PRIORITY_TIME_CRITICAL);
    This->procThreadTicStarted = TRUE;
194 195 196 197 198 199 200 201 202
    while(1) {
      res = PostThreadMessageA(This->procThreadId, iMsg, 0, 0);
      /* Let the thread creates its message queue (with MsgWaitForMultipleObjects call) by yielding and retrying */
      if (!res && (GetLastError() == ERROR_INVALID_THREAD_ID))
	Sleep(0);
      else
	break;
    }
    return res;
203 204 205 206
  }
  return PostThreadMessageA(This->procThreadId, iMsg, 0, 0);
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static int pchannel_block_compare(const void *key, const struct wine_rb_entry *entry)
{
    const struct pchannel_block *b = WINE_RB_ENTRY_VALUE(entry, const struct pchannel_block, entry);

    return *(DWORD *)key - b->block_num;
}

static void pchannel_block_free(struct wine_rb_entry *entry, void *context)
{
    struct pchannel_block *b = WINE_RB_ENTRY_VALUE(entry, struct pchannel_block, entry);

    heap_free(b);
}

static struct pchannel_block *pchannel_block_set(struct wine_rb_tree *tree, DWORD block_num,
        IDirectMusicPort *port, DWORD group, BOOL only_set_new)
{
    struct pchannel_block *block;
    struct wine_rb_entry *entry;
    unsigned int i;

    entry = wine_rb_get(tree, &block_num);
    if (entry) {
        block = WINE_RB_ENTRY_VALUE(entry, struct pchannel_block, entry);
        if (only_set_new)
            return block;
    } else {
        if (!(block = heap_alloc(sizeof(*block))))
            return NULL;
        block->block_num = block_num;
    }

    for (i = 0; i < 16; ++i) {
        block->pchannel[i].port = port;
        block->pchannel[i].group = group;
        block->pchannel[i].channel = i;
    }
    if (!entry)
        wine_rb_put(tree, &block->block_num, &block->entry);

    return block;
}

250 251 252 253 254
static inline IDirectMusicPerformance8Impl *impl_from_IDirectMusicPerformance8(IDirectMusicPerformance8 *iface)
{
    return CONTAINING_RECORD(iface, IDirectMusicPerformance8Impl, IDirectMusicPerformance8_iface);
}

255
/* IDirectMusicPerformance8 IUnknown part: */
256 257 258 259
static HRESULT WINAPI IDirectMusicPerformance8Impl_QueryInterface(IDirectMusicPerformance8 *iface,
        REFIID riid, void **ppv)
{
  TRACE("(%p, %s,%p)\n", iface, debugstr_dmguid(riid), ppv);
260

261
  if (IsEqualIID (riid, &IID_IUnknown) ||
262
      IsEqualIID (riid, &IID_IDirectMusicPerformance) ||
263
      IsEqualIID (riid, &IID_IDirectMusicPerformance2) ||
264
      IsEqualIID (riid, &IID_IDirectMusicPerformance8)) {
265
    *ppv = iface;
266
    IUnknown_AddRef(iface);
267 268
    return S_OK;
  }
269 270

  WARN("(%p, %s,%p): not found\n", iface, debugstr_dmguid(riid), ppv);
271
  return E_NOINTERFACE;
272 273
}

274 275 276
static ULONG WINAPI IDirectMusicPerformance8Impl_AddRef(IDirectMusicPerformance8 *iface)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
277 278
  ULONG ref = InterlockedIncrement(&This->ref);

279
  TRACE("(%p): ref=%ld\n", This, ref);
280

281 282
  DMIME_LockModule();

283
  return ref;
284 285
}

286 287 288
static ULONG WINAPI IDirectMusicPerformance8Impl_Release(IDirectMusicPerformance8 *iface)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
289
  ULONG ref = InterlockedDecrement(&This->ref);
290

291
  TRACE("(%p): ref=%ld\n", This, ref);
292
  
293
  if (ref == 0) {
294
    wine_rb_destroy(&This->pchannels, pchannel_block_free, NULL);
295
    This->safe.DebugInfo->Spare[0] = 0;
296 297 298
    DeleteCriticalSection(&This->safe);
    HeapFree(GetProcessHeap(), 0, This);
  }
299 300 301

  DMIME_UnlockModule();

302
  return ref;
303 304
}

305
/* IDirectMusicPerformanceImpl IDirectMusicPerformance Interface part: */
306
static HRESULT WINAPI IDirectMusicPerformance8Impl_Init(IDirectMusicPerformance8 *iface,
307
        IDirectMusic **dmusic, IDirectSound *dsound, HWND hwnd)
308
{
309
    TRACE("(%p, %p, %p, %p)\n", iface, dmusic, dsound, hwnd);
310

311 312
    return IDirectMusicPerformance8_InitAudio(iface, dmusic, dsound ? &dsound : NULL, hwnd, 0, 0,
            0, NULL);
313 314
}

315 316 317 318 319 320
static HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegment(IDirectMusicPerformance8 *iface,
        IDirectMusicSegment *pSegment, DWORD dwFlags, __int64 i64StartTime,
        IDirectMusicSegmentState **ppSegmentState)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

321
	FIXME("(%p, %p, %ld, 0x%s, %p): stub\n", This, pSegment, dwFlags,
322
	    wine_dbgstr_longlong(i64StartTime), ppSegmentState);
323
	if (ppSegmentState)
324
          return create_dmsegmentstate(&IID_IDirectMusicSegmentState,(void**)ppSegmentState);
Rok Mandeljc's avatar
Rok Mandeljc committed
325
	return S_OK;
326 327
}

328 329 330 331 332 333
static HRESULT WINAPI IDirectMusicPerformance8Impl_Stop(IDirectMusicPerformance8 *iface,
        IDirectMusicSegment *pSegment, IDirectMusicSegmentState *pSegmentState, MUSIC_TIME mtTime,
        DWORD dwFlags)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

334
	FIXME("(%p, %p, %p, %ld, %ld): stub\n", This, pSegment, pSegmentState, mtTime, dwFlags);
Rok Mandeljc's avatar
Rok Mandeljc committed
335
	return S_OK;
336 337
}

338 339 340 341 342
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetSegmentState(IDirectMusicPerformance8 *iface,
        IDirectMusicSegmentState **ppSegmentState, MUSIC_TIME mtTime)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

343
	FIXME("(%p,%p, %ld): stub\n", This, ppSegmentState, mtTime);
Rok Mandeljc's avatar
Rok Mandeljc committed
344
	return S_OK;
345 346
}

347 348 349 350 351
static HRESULT WINAPI IDirectMusicPerformance8Impl_SetPrepareTime(IDirectMusicPerformance8 *iface,
        DWORD dwMilliSeconds)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

352
  TRACE("(%p, %ld)\n", This, dwMilliSeconds);
353 354
  This->dwPrepareTime = dwMilliSeconds;
  return S_OK;
355 356
}

357 358 359 360 361
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetPrepareTime(IDirectMusicPerformance8 *iface,
        DWORD *pdwMilliSeconds)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

362 363 364 365 366 367
  TRACE("(%p, %p)\n", This, pdwMilliSeconds);
  if (NULL == pdwMilliSeconds) {
    return E_POINTER;
  }
  *pdwMilliSeconds = This->dwPrepareTime;
  return S_OK;
368 369
}

370 371 372 373 374
static HRESULT WINAPI IDirectMusicPerformance8Impl_SetBumperLength(IDirectMusicPerformance8 *iface,
        DWORD dwMilliSeconds)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

375
  TRACE("(%p, %ld)\n", This, dwMilliSeconds);
376 377
  This->dwBumperLength =  dwMilliSeconds;
  return S_OK;
378 379
}

380 381 382 383 384
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetBumperLength(IDirectMusicPerformance8 *iface,
        DWORD *pdwMilliSeconds)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

385 386 387 388 389 390
  TRACE("(%p, %p)\n", This, pdwMilliSeconds);
  if (NULL == pdwMilliSeconds) {
    return E_POINTER;
  }
  *pdwMilliSeconds = This->dwBumperLength;
  return S_OK;
391 392
}

393 394 395 396
static HRESULT WINAPI IDirectMusicPerformance8Impl_SendPMsg(IDirectMusicPerformance8 *iface,
        DMUS_PMSG *pPMSG)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
397 398 399 400 401 402 403 404 405 406 407
  DMUS_PMSGItem* pItem = NULL;
  DMUS_PMSGItem* it = NULL;
  DMUS_PMSGItem* prev_it = NULL;
  DMUS_PMSGItem** queue = NULL;

  FIXME("(%p, %p): stub\n", This, pPMSG);
	 
  if (NULL == pPMSG) {
    return E_POINTER;
  }
  pItem = DMUS_PMSGToItem(pPMSG);
408
  if (pItem->bInUse) {
409 410 411 412 413 414 415 416 417 418 419 420 421
    return DMUS_E_ALREADY_SENT;
  }
  
  /* TODO: Valid Flags */
  /* TODO: DMUS_PMSGF_MUSICTIME */
  pItem->rtItemTime = pPMSG->rtTime;

  if (pPMSG->dwFlags & DMUS_PMSGF_TOOL_IMMEDIATE) {
    queue = &This->imm_head;
  } else {
    queue = &This->head;
  }

422
  EnterCriticalSection(&This->safe);
423 424 425 426 427
  for (it = *queue; NULL != it && it->rtItemTime < pItem->rtItemTime; it = it->next) {
    prev_it = it;
  }
  if (NULL == prev_it) {
    pItem->prev = NULL;
428
    if (NULL != *queue) pItem->next = (*queue)->next;
429 430 431 432 433 434 435 436
    /*assert( NULL == pItem->next->prev );*/
    if (NULL != pItem->next) pItem->next->prev = pItem;
    *queue = pItem;
  } else {
    pItem->prev = prev_it;
    pItem->next = prev_it->next;
    prev_it->next = pItem;
    if (NULL != pItem->next) pItem->next->prev = pItem;
437 438 439
  } 
  LeaveCriticalSection(&This->safe);

440 441 442
  /** now in use, prevent from stupid Frees */
  pItem->bInUse = TRUE;
  return S_OK;
443 444
}

445 446 447 448 449
static HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToReferenceTime(IDirectMusicPerformance8 *iface,
        MUSIC_TIME mtTime, REFERENCE_TIME *prtTime)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

450
	FIXME("(%p, %ld, %p): stub\n", This, mtTime, prtTime);
Rok Mandeljc's avatar
Rok Mandeljc committed
451
	return S_OK;
452 453
}

454 455 456 457 458
static HRESULT WINAPI IDirectMusicPerformance8Impl_ReferenceToMusicTime(IDirectMusicPerformance8 *iface,
        REFERENCE_TIME rtTime, MUSIC_TIME *pmtTime)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

459
	FIXME("(%p, 0x%s, %p): stub\n", This, wine_dbgstr_longlong(rtTime), pmtTime);
Rok Mandeljc's avatar
Rok Mandeljc committed
460
	return S_OK;
461 462
}

463 464 465 466 467
static HRESULT WINAPI IDirectMusicPerformance8Impl_IsPlaying(IDirectMusicPerformance8 *iface,
        IDirectMusicSegment *pSegment, IDirectMusicSegmentState *pSegState)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

Rok Mandeljc's avatar
Rok Mandeljc committed
468
	FIXME("(%p, %p, %p): stub\n", This, pSegment, pSegState);
469
	return S_FALSE;
470 471
}

472 473 474 475
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetTime(IDirectMusicPerformance8 *iface,
        REFERENCE_TIME *prtNow, MUSIC_TIME *pmtNow)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
476 477 478 479
  HRESULT hr = S_OK;
  REFERENCE_TIME rtCur = 0;

  /*TRACE("(%p, %p, %p)\n", This, prtNow, pmtNow); */
480
  if (This->procThreadTicStarted) {
481 482 483 484 485 486 487 488 489 490 491
    rtCur = ((REFERENCE_TIME) GetTickCount() * 10000) - This->procThreadStartTime;
  } else {
    /*return DMUS_E_NO_MASTER_CLOCK;*/
  }
  if (NULL != prtNow) {
    *prtNow = rtCur;
  }
  if (NULL != pmtNow) {
    hr = IDirectMusicPerformance8_ReferenceToMusicTime(iface, rtCur, pmtNow);
  }
  return hr;
492 493
}

494 495 496 497
static HRESULT WINAPI IDirectMusicPerformance8Impl_AllocPMsg(IDirectMusicPerformance8 *iface,
        ULONG cb, DMUS_PMSG **ppPMSG)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
498
  DMUS_PMSGItem* pItem = NULL;
499 500 501

  FIXME("(%p, %ld, %p): stub\n", This, cb, ppPMSG);

502 503 504 505 506 507 508 509 510 511 512
  if (sizeof(DMUS_PMSG) > cb) {
    return E_INVALIDARG;
  }
  if (NULL == ppPMSG) {
    return E_POINTER;
  }
  pItem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb - sizeof(DMUS_PMSG)  + sizeof(DMUS_PMSGItem));
  if (NULL == pItem) {
    return E_OUTOFMEMORY;
  }
  pItem->pMsg.dwSize = cb;
513
  *ppPMSG = DMUS_ItemToPMSG(pItem);
514
  return S_OK;
515 516
}

517 518 519 520
static HRESULT WINAPI IDirectMusicPerformance8Impl_FreePMsg(IDirectMusicPerformance8 *iface,
        DMUS_PMSG *pPMSG)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
521 522 523 524 525 526 527
  DMUS_PMSGItem* pItem = NULL;
  
  FIXME("(%p, %p): stub\n", This, pPMSG);
  
  if (NULL == pPMSG) {
    return E_POINTER;
  }
528
  pItem = DMUS_PMSGToItem(pPMSG);
529
  if (pItem->bInUse) {
530
    /** prevent for freeing PMsg in queue (ie to be processed) */
531
    return DMUS_E_CANNOT_FREE;
532 533
  }
  /** now we can remove it safely */
534
  EnterCriticalSection(&This->safe);
535
  DMUS_ItemRemoveFromQueue( This, pItem );
536 537
  LeaveCriticalSection(&This->safe);

538 539 540 541 542 543
  if (pPMSG->pTool)
    IDirectMusicTool_Release(pPMSG->pTool);

  if (pPMSG->pGraph)
    IDirectMusicGraph_Release(pPMSG->pGraph);

544 545 546
  if (pPMSG->punkUser)
    IUnknown_Release(pPMSG->punkUser);

547 548
  HeapFree(GetProcessHeap(), 0, pItem);  
  return S_OK;
549 550
}

551
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetGraph(IDirectMusicPerformance8 *iface,
552
        IDirectMusicGraph **graph)
553
{
554
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
555

556 557 558 559 560 561 562 563 564 565 566
    TRACE("(%p, %p)\n", This, graph);

    if (!graph)
        return E_POINTER;

    *graph = This->pToolGraph;
    if (This->pToolGraph) {
        IDirectMusicGraph_AddRef(*graph);
    }

    return *graph ? S_OK : DMUS_E_NOT_FOUND;
567 568
}

569 570 571 572 573
static HRESULT WINAPI IDirectMusicPerformance8Impl_SetGraph(IDirectMusicPerformance8 *iface,
        IDirectMusicGraph *pGraph)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

574 575 576 577
  FIXME("(%p, %p): to check\n", This, pGraph);
  
  if (NULL != This->pToolGraph) {
    /* Todo clean buffers and tools before */
578
    IDirectMusicGraph_Release(This->pToolGraph);
579 580 581
  }
  This->pToolGraph = pGraph;
  if (NULL != This->pToolGraph) {
582
    IDirectMusicGraph_AddRef(This->pToolGraph);
583 584
  }
  return S_OK;
585 586
}

587 588 589
static HRESULT WINAPI IDirectMusicPerformance8Impl_SetNotificationHandle(IDirectMusicPerformance8 *iface,
        HANDLE hNotification, REFERENCE_TIME rtMinimum)
{
590
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
591

592 593 594 595 596 597 598 599
    TRACE("(%p, %p, 0x%s)\n", This, hNotification, wine_dbgstr_longlong(rtMinimum));

    This->hNotification = hNotification;
    if (rtMinimum)
        This->rtMinimum = rtMinimum;
    else if (!This->rtMinimum)
        This->rtMinimum = 20000000; /* 2 seconds */
    return S_OK;
600 601
}

602 603 604 605 606
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetNotificationPMsg(IDirectMusicPerformance8 *iface,
        DMUS_NOTIFICATION_PMSG **ppNotificationPMsg)
{
  IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

607 608 609 610
  FIXME("(%p, %p): stub\n", This, ppNotificationPMsg);
  if (NULL == ppNotificationPMsg) {
    return E_POINTER;
  }
611 612 613
  
  

614 615
  return S_FALSE;
  /*return S_OK;*/
616 617
}

618 619 620 621 622
static HRESULT WINAPI IDirectMusicPerformance8Impl_AddNotificationType(IDirectMusicPerformance8 *iface,
        REFGUID rguidNotificationType)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

623
	FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
Rok Mandeljc's avatar
Rok Mandeljc committed
624
	return S_OK;
625 626
}

627 628 629 630 631
static HRESULT WINAPI IDirectMusicPerformance8Impl_RemoveNotificationType(IDirectMusicPerformance8 *iface,
        REFGUID rguidNotificationType)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

632
	FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
Rok Mandeljc's avatar
Rok Mandeljc committed
633
	return S_OK;
634 635
}

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
static HRESULT perf_dmport_create(IDirectMusicPerformance8Impl *perf, DMUS_PORTPARAMS *params)
{
    IDirectMusicPort *port;
    GUID guid;
    unsigned int i;
    HRESULT hr;

    if (FAILED(hr = IDirectMusic8_GetDefaultPort(perf->dmusic, &guid)))
        return hr;

    if (FAILED(hr = IDirectMusic8_CreatePort(perf->dmusic, &guid, params, &port, NULL)))
        return hr;
    if (FAILED(hr = IDirectMusicPort_Activate(port, TRUE))) {
        IDirectMusicPort_Release(port);
        return hr;
    }
    for (i = 0; i < params->dwChannelGroups; i++)
        pchannel_block_set(&perf->pchannels, i, port, i + 1, FALSE);

    return S_OK;
}

658
static HRESULT WINAPI IDirectMusicPerformance8Impl_AddPort(IDirectMusicPerformance8 *iface,
659
        IDirectMusicPort *port)
660
{
661
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
662

663 664 665 666 667 668 669
    FIXME("(%p, %p): semi-stub\n", This, port);

    if (!This->dmusic)
        return DMUS_E_NOT_INIT;

    if (!port) {
        DMUS_PORTPARAMS params = {
670
            .dwSize = sizeof(params),
671 672 673 674 675 676 677 678 679 680 681 682 683
            .dwValidParams = DMUS_PORTPARAMS_CHANNELGROUPS,
            .dwChannelGroups = 1
        };

        return perf_dmport_create(This, &params);
    }

    IDirectMusicPort_AddRef(port);
    /**
     * We should remember added Ports (for example using a list)
     * and control if Port is registered for each api who use ports
     */
    return S_OK;
684 685
}

686 687 688 689 690
static HRESULT WINAPI IDirectMusicPerformance8Impl_RemovePort(IDirectMusicPerformance8 *iface,
        IDirectMusicPort *pPort)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

Rok Mandeljc's avatar
Rok Mandeljc committed
691
	FIXME("(%p, %p): stub\n", This, pPort);
692
	IDirectMusicPort_Release (pPort);
Rok Mandeljc's avatar
Rok Mandeljc committed
693
	return S_OK;
694 695
}

696
static HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannelBlock(IDirectMusicPerformance8 *iface,
697
        DWORD block_num, IDirectMusicPort *port, DWORD group)
698
{
699
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
700

701
    FIXME("(%p, %ld, %p, %ld): semi-stub\n", This, block_num, port, group);
702 703 704 705 706 707 708 709 710

    if (!port)
        return E_POINTER;
    if (block_num > MAXDWORD / 16)
        return E_INVALIDARG;

    pchannel_block_set(&This->pchannels, block_num, port, group, FALSE);

    return S_OK;
711 712
}

713
static HRESULT WINAPI IDirectMusicPerformance8Impl_AssignPChannel(IDirectMusicPerformance8 *iface,
714
        DWORD pchannel, IDirectMusicPort *port, DWORD group, DWORD channel)
715
{
716
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
717
    struct pchannel_block *block;
Rok Mandeljc's avatar
Rok Mandeljc committed
718

719
    FIXME("(%p)->(%ld, %p, %ld, %ld) semi-stub\n", This, pchannel, port, group, channel);
Rok Mandeljc's avatar
Rok Mandeljc committed
720

721 722 723
    if (!port)
        return E_POINTER;

724 725 726 727 728
    block = pchannel_block_set(&This->pchannels, pchannel / 16, port, 0, TRUE);
    if (block) {
        block->pchannel[pchannel % 16].group = group;
        block->pchannel[pchannel % 16].channel = channel;
    }
729 730

    return S_OK;
731 732
}

733
static HRESULT WINAPI IDirectMusicPerformance8Impl_PChannelInfo(IDirectMusicPerformance8 *iface,
734
        DWORD pchannel, IDirectMusicPort **port, DWORD *group, DWORD *channel)
735
{
736
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
737 738 739 740 741
    struct pchannel_block *block;
    struct wine_rb_entry *entry;
    DWORD block_num = pchannel / 16;
    unsigned int index = pchannel % 16;

742
    TRACE("(%p)->(%ld, %p, %p, %p)\n", This, pchannel, port, group, channel);
743

744 745 746 747
    entry = wine_rb_get(&This->pchannels, &block_num);
    if (!entry)
        return E_INVALIDARG;
    block = WINE_RB_ENTRY_VALUE(entry, struct pchannel_block, entry);
748

749 750 751 752 753 754 755 756
    if (port) {
        *port = block->pchannel[index].port;
        IDirectMusicPort_AddRef(*port);
    }
    if (group)
        *group = block->pchannel[index].group;
    if (channel)
        *channel = block->pchannel[index].channel;
757

758
    return S_OK;
759 760
}

761 762 763 764 765 766 767
static HRESULT WINAPI IDirectMusicPerformance8Impl_DownloadInstrument(IDirectMusicPerformance8 *iface,
        IDirectMusicInstrument *pInst, DWORD dwPChannel,
        IDirectMusicDownloadedInstrument **ppDownInst, DMUS_NOTERANGE *pNoteRanges,
        DWORD dwNumNoteRanges, IDirectMusicPort **ppPort, DWORD *pdwGroup, DWORD *pdwMChannel)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

768
	FIXME("(%p, %p, %ld, %p, %p, %ld, %p, %p, %p): stub\n", This, pInst, dwPChannel, ppDownInst, pNoteRanges, dwNumNoteRanges, ppPort, pdwGroup, pdwMChannel);
Rok Mandeljc's avatar
Rok Mandeljc committed
769
	return S_OK;
770 771
}

772 773 774 775 776
static HRESULT WINAPI IDirectMusicPerformance8Impl_Invalidate(IDirectMusicPerformance8 *iface,
        MUSIC_TIME mtTime, DWORD dwFlags)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

777
	FIXME("(%p, %ld, %ld): stub\n", This, mtTime, dwFlags);
Rok Mandeljc's avatar
Rok Mandeljc committed
778
	return S_OK;
779 780
}

781 782 783 784 785 786
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetParam(IDirectMusicPerformance8 *iface,
        REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime,
        MUSIC_TIME *pmtNext, void *pParam)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

787
	FIXME("(%p, %s, %ld, %ld, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
Rok Mandeljc's avatar
Rok Mandeljc committed
788
	return S_OK;
789 790
}

791 792 793 794 795
static HRESULT WINAPI IDirectMusicPerformance8Impl_SetParam(IDirectMusicPerformance8 *iface,
        REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void *pParam)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

796
	FIXME("(%p, %s, %ld, %ld, %ld, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
Rok Mandeljc's avatar
Rok Mandeljc committed
797
	return S_OK;
798 799
}

800 801 802 803
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetGlobalParam(IDirectMusicPerformance8 *iface,
        REFGUID rguidType, void *pParam, DWORD dwSize)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
804

805 806
	TRACE("(%p, %s, %p, %ld): stub\n", This, debugstr_dmguid(rguidType), pParam, dwSize);

807
	if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload))
808
		memcpy(pParam, &This->fAutoDownload, sizeof(This->fAutoDownload));
809
	if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel))
810
		memcpy(pParam, &This->cMasterGrooveLevel, sizeof(This->cMasterGrooveLevel));
811
	if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo))
812
		memcpy(pParam, &This->fMasterTempo, sizeof(This->fMasterTempo));
813
	if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume))
814
		memcpy(pParam, &This->lMasterVolume, sizeof(This->lMasterVolume));
Rok Mandeljc's avatar
Rok Mandeljc committed
815 816

	return S_OK;
817 818
}

819 820 821 822
static HRESULT WINAPI IDirectMusicPerformance8Impl_SetGlobalParam(IDirectMusicPerformance8 *iface,
        REFGUID rguidType, void *pParam, DWORD dwSize)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
823

824 825
	TRACE("(%p, %s, %p, %ld)\n", This, debugstr_dmguid(rguidType), pParam, dwSize);

826
	if (IsEqualGUID (rguidType, &GUID_PerfAutoDownload)) {
827 828
		memcpy(&This->fAutoDownload, pParam, dwSize);
		TRACE("=> AutoDownload set to %d\n", This->fAutoDownload);
829
	}
830
	if (IsEqualGUID (rguidType, &GUID_PerfMasterGrooveLevel)) {
831 832
		memcpy(&This->cMasterGrooveLevel, pParam, dwSize);
		TRACE("=> MasterGrooveLevel set to %i\n", This->cMasterGrooveLevel);
833
	}
834
	if (IsEqualGUID (rguidType, &GUID_PerfMasterTempo)) {
835 836
		memcpy(&This->fMasterTempo, pParam, dwSize);
		TRACE("=> MasterTempo set to %f\n", This->fMasterTempo);
837
	}
838
	if (IsEqualGUID (rguidType, &GUID_PerfMasterVolume)) {
839 840
		memcpy(&This->lMasterVolume, pParam, dwSize);
		TRACE("=> MasterVolume set to %li\n", This->lMasterVolume);
841
	}
Rok Mandeljc's avatar
Rok Mandeljc committed
842 843

	return S_OK;
844 845
}

846 847 848 849 850
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetLatencyTime(IDirectMusicPerformance8 *iface,
        REFERENCE_TIME *prtTime)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

851 852
	TRACE("(%p, %p): stub\n", This, prtTime);
	*prtTime = This->rtLatencyTime;
Rok Mandeljc's avatar
Rok Mandeljc committed
853
	return S_OK;
854 855
}

856 857 858 859 860 861
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetQueueTime(IDirectMusicPerformance8 *iface,
        REFERENCE_TIME *prtTime)

{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

Rok Mandeljc's avatar
Rok Mandeljc committed
862 863
	FIXME("(%p, %p): stub\n", This, prtTime);
	return S_OK;
864 865
}

866 867 868 869 870
static HRESULT WINAPI IDirectMusicPerformance8Impl_AdjustTime(IDirectMusicPerformance8 *iface,
        REFERENCE_TIME rtAmount)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

871
	FIXME("(%p, 0x%s): stub\n", This, wine_dbgstr_longlong(rtAmount));
Rok Mandeljc's avatar
Rok Mandeljc committed
872
	return S_OK;
873 874
}

875 876
static HRESULT WINAPI IDirectMusicPerformance8Impl_CloseDown(IDirectMusicPerformance8 *iface)
{
877
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
878

879 880 881 882 883 884 885
    FIXME("(%p): semi-stub\n", This);

    if (PostMessageToProcessMsgThread(This, PROCESSMSG_EXIT)) {
        WaitForSingleObject(This->procThread, INFINITE);
        This->procThreadTicStarted = FALSE;
        CloseHandle(This->procThread);
    }
886 887 888 889
    if (This->dsound) {
        IDirectSound_Release(This->dsound);
        This->dsound = NULL;
    }
890
    if (This->dmusic) {
891
        IDirectMusic_SetDirectSound(This->dmusic, NULL, NULL);
892 893 894 895
        IDirectMusic8_Release(This->dmusic);
        This->dmusic = NULL;
    }
    return S_OK;
896 897
}

898 899 900 901 902
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetResolvedTime(IDirectMusicPerformance8 *iface,
        REFERENCE_TIME rtTime, REFERENCE_TIME *prtResolved, DWORD dwTimeResolveFlags)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

903
	FIXME("(%p, 0x%s, %p, %ld): stub\n", This, wine_dbgstr_longlong(rtTime),
904
	    prtResolved, dwTimeResolveFlags);
Rok Mandeljc's avatar
Rok Mandeljc committed
905
	return S_OK;
906 907
}

908 909 910 911 912 913
static HRESULT WINAPI IDirectMusicPerformance8Impl_MIDIToMusic(IDirectMusicPerformance8 *iface,
        BYTE bMIDIValue, DMUS_CHORD_KEY *pChord, BYTE bPlayMode, BYTE bChordLevel,
        WORD *pwMusicValue)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

Rok Mandeljc's avatar
Rok Mandeljc committed
914 915
	FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, bMIDIValue, pChord, bPlayMode, bChordLevel, pwMusicValue);
	return S_OK;
916 917
}

918 919 920 921 922 923
static HRESULT WINAPI IDirectMusicPerformance8Impl_MusicToMIDI(IDirectMusicPerformance8 *iface,
        WORD wMusicValue, DMUS_CHORD_KEY *pChord, BYTE bPlayMode, BYTE bChordLevel,
        BYTE *pbMIDIValue)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

Rok Mandeljc's avatar
Rok Mandeljc committed
924 925
	FIXME("(%p, %d, %p, %d, %d, %p): stub\n", This, wMusicValue, pChord, bPlayMode, bChordLevel, pbMIDIValue);
	return S_OK;
926 927
}

928 929 930 931 932 933
static HRESULT WINAPI IDirectMusicPerformance8Impl_TimeToRhythm(IDirectMusicPerformance8 *iface,
        MUSIC_TIME mtTime, DMUS_TIMESIGNATURE *pTimeSig, WORD *pwMeasure, BYTE *pbBeat,
        BYTE *pbGrid, short *pnOffset)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

934
	FIXME("(%p, %ld, %p, %p, %p, %p, %p): stub\n", This, mtTime, pTimeSig, pwMeasure, pbBeat, pbGrid, pnOffset);
Rok Mandeljc's avatar
Rok Mandeljc committed
935
	return S_OK;
936 937
}

938 939 940 941 942 943
static HRESULT WINAPI IDirectMusicPerformance8Impl_RhythmToTime(IDirectMusicPerformance8 *iface,
        WORD wMeasure, BYTE bBeat, BYTE bGrid, short nOffset, DMUS_TIMESIGNATURE *pTimeSig,
        MUSIC_TIME *pmtTime)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

Rok Mandeljc's avatar
Rok Mandeljc committed
944 945
	FIXME("(%p, %d, %d, %d, %i, %p, %p): stub\n", This, wMeasure, bBeat, bGrid, nOffset, pTimeSig, pmtTime);
	return S_OK;
946 947 948
}

/* IDirectMusicPerformance8 Interface part follow: */
949
static HRESULT WINAPI IDirectMusicPerformance8Impl_InitAudio(IDirectMusicPerformance8 *iface,
950 951
        IDirectMusic **dmusic, IDirectSound **dsound, HWND hwnd, DWORD default_path_type,
        DWORD num_channels, DWORD flags, DMUS_AUDIOPARAMS *params)
952
{
953 954
    IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
    HRESULT hr = S_OK;
955

956
    TRACE("(%p, %p, %p, %p, %lx, %lu, %lx, %p)\n", This, dmusic, dsound, hwnd, default_path_type,
957
            num_channels, flags, params);
958

959 960
    if (This->dmusic)
        return DMUS_E_ALREADY_INITED;
961

962 963 964 965 966
    if (!dmusic || !*dmusic) {
        hr = CoCreateInstance(&CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusic8,
                (void **)&This->dmusic);
        if (FAILED(hr))
            return hr;
967
    } else {
968 969
        This->dmusic = (IDirectMusic8 *)*dmusic;
        IDirectMusic8_AddRef(This->dmusic);
970
    }
971

972 973 974
    if (!dsound || !*dsound) {
        hr = DirectSoundCreate8(NULL, (IDirectSound8 **)&This->dsound, NULL);
        if (FAILED(hr))
975 976
            goto error;
        hr = IDirectSound_SetCooperativeLevel(This->dsound, hwnd ? hwnd : GetForegroundWindow(),
977
                DSSCL_PRIORITY);
978 979 980
        if (FAILED(hr))
            goto error;
    } else {
981
        This->dsound = *dsound;
982
        IDirectSound_AddRef(This->dsound);
983
    }
984

985 986 987 988
    hr = IDirectMusic8_SetDirectSound(This->dmusic, This->dsound, NULL);
    if (FAILED(hr))
        goto error;

989 990 991 992 993 994 995 996 997 998 999 1000
    if (!params) {
        This->params.dwSize = sizeof(DMUS_AUDIOPARAMS);
        This->params.fInitNow = FALSE;
        This->params.dwValidData = DMUS_AUDIOPARAMS_FEATURES | DMUS_AUDIOPARAMS_VOICES |
                DMUS_AUDIOPARAMS_SAMPLERATE | DMUS_AUDIOPARAMS_DEFAULTSYNTH;
        This->params.dwVoices = 64;
        This->params.dwSampleRate = 22050;
        This->params.dwFeatures = flags;
        This->params.clsidDefaultSynth = CLSID_DirectMusicSynthSink;
    } else
        This->params = *params;

1001
    if (default_path_type) {
1002 1003
        hr = IDirectMusicPerformance8_CreateStandardAudioPath(iface, default_path_type,
                num_channels, FALSE, &This->pDefaultPath);
1004 1005
        if (FAILED(hr)) {
            IDirectMusic8_SetDirectSound(This->dmusic, NULL, NULL);
1006
            goto error;
1007
        }
1008
    }
Rok Mandeljc's avatar
Rok Mandeljc committed
1009

1010 1011 1012 1013 1014 1015 1016 1017
    if (dsound && !*dsound) {
        *dsound = This->dsound;
        IDirectSound_AddRef(*dsound);
    }
    if (dmusic && !*dmusic) {
        *dmusic = (IDirectMusic *)This->dmusic;
        IDirectMusic_AddRef(*dmusic);
    }
1018
    PostMessageToProcessMsgThread(This, PROCESSMSG_START);
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    return S_OK;

error:
    if (This->dsound) {
        IDirectSound_Release(This->dsound);
        This->dsound = NULL;
    }
    if (This->dmusic) {
        IDirectMusic8_Release(This->dmusic);
        This->dmusic = NULL;
    }
1031
    return hr;
1032 1033
}

1034 1035 1036 1037 1038 1039 1040
static HRESULT WINAPI IDirectMusicPerformance8Impl_PlaySegmentEx(IDirectMusicPerformance8 *iface,
        IUnknown *pSource, WCHAR *pwzSegmentName, IUnknown *pTransition, DWORD dwFlags,
        __int64 i64StartTime, IDirectMusicSegmentState **ppSegmentState, IUnknown *pFrom,
        IUnknown *pAudioPath)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

1041
	FIXME("(%p, %p, %p, %p, %ld, 0x%s, %p, %p, %p): stub\n", This, pSource, pwzSegmentName,
1042
	    pTransition, dwFlags, wine_dbgstr_longlong(i64StartTime), ppSegmentState, pFrom, pAudioPath);
1043
	if (ppSegmentState)
1044
          return create_dmsegmentstate(&IID_IDirectMusicSegmentState,(void**)ppSegmentState);
1045
	return S_OK;
1046 1047
}

1048 1049 1050 1051 1052
static HRESULT WINAPI IDirectMusicPerformance8Impl_StopEx(IDirectMusicPerformance8 *iface,
        IUnknown *pObjectToStop, __int64 i64StopTime, DWORD dwFlags)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

1053
	FIXME("(%p, %p, 0x%s, %ld): stub\n", This, pObjectToStop,
1054
	    wine_dbgstr_longlong(i64StopTime), dwFlags);
Rok Mandeljc's avatar
Rok Mandeljc committed
1055
	return S_OK;
1056 1057
}

1058 1059 1060 1061 1062
static HRESULT WINAPI IDirectMusicPerformance8Impl_ClonePMsg(IDirectMusicPerformance8 *iface,
        DMUS_PMSG *pSourcePMSG, DMUS_PMSG **ppCopyPMSG)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);

Rok Mandeljc's avatar
Rok Mandeljc committed
1063 1064
	FIXME("(%p, %p, %p): stub\n", This, pSourcePMSG, ppCopyPMSG);
	return S_OK;
1065 1066
}

1067 1068 1069 1070
static HRESULT WINAPI IDirectMusicPerformance8Impl_CreateAudioPath(IDirectMusicPerformance8 *iface,
        IUnknown *pSourceConfig, BOOL fActivate, IDirectMusicAudioPath **ppNewPath)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
1071 1072
	IDirectMusicAudioPath *pPath;

Rok Mandeljc's avatar
Rok Mandeljc committed
1073
	FIXME("(%p, %p, %d, %p): stub\n", This, pSourceConfig, fActivate, ppNewPath);
1074 1075 1076 1077 1078

	if (NULL == ppNewPath) {
	  return E_POINTER;
	}

1079
        create_dmaudiopath(&IID_IDirectMusicAudioPath, (void**)&pPath);
1080
        set_audiopath_perf_pointer(pPath, iface);
1081 1082 1083

	/** TODO */
	
1084
	*ppNewPath = pPath;
1085

1086
	return IDirectMusicAudioPath_Activate(*ppNewPath, fActivate);
1087 1088
}

1089
static HRESULT WINAPI IDirectMusicPerformance8Impl_CreateStandardAudioPath(IDirectMusicPerformance8 *iface,
1090
        DWORD dwType, DWORD pchannel_count, BOOL fActivate, IDirectMusicAudioPath **ppNewPath)
1091 1092
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
1093
	IDirectMusicAudioPath *pPath;
1094 1095
	DSBUFFERDESC desc;
	WAVEFORMATEX format;
1096
        DMUS_PORTPARAMS params = {0};
1097
	IDirectSoundBuffer *buffer, *primary_buffer;
1098
	HRESULT hr = S_OK;
Rok Mandeljc's avatar
Rok Mandeljc committed
1099

1100
        FIXME("(%p)->(%ld, %ld, %d, %p): semi-stub\n", This, dwType, pchannel_count, fActivate, ppNewPath);
Rok Mandeljc's avatar
Rok Mandeljc committed
1101

1102 1103 1104
	if (NULL == ppNewPath) {
	  return E_POINTER;
	}
1105

1106
        *ppNewPath = NULL;
1107

1108
	/* Secondary buffer description */
1109
	memset(&format, 0, sizeof(format));
1110 1111 1112 1113 1114 1115 1116 1117
	format.wFormatTag = WAVE_FORMAT_PCM;
	format.nChannels = 1;
	format.nSamplesPerSec = 44000;
	format.nAvgBytesPerSec = 44000*2;
	format.nBlockAlign = 2;
	format.wBitsPerSample = 16;
	format.cbSize = 0;
	
1118
	memset(&desc, 0, sizeof(desc));
1119
	desc.dwSize = sizeof(desc);
1120
        desc.dwFlags = DSBCAPS_CTRLFX | DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS;
1121 1122 1123 1124 1125 1126
	desc.dwBufferBytes = DSBSIZE_MIN;
	desc.dwReserved = 0;
	desc.lpwfxFormat = &format;
	desc.guid3DAlgorithm = GUID_NULL;
	
	switch(dwType) {
1127
	case DMUS_APATH_DYNAMIC_3D:
1128
                desc.dwFlags |= DSBCAPS_CTRL3D | DSBCAPS_CTRLFREQUENCY | DSBCAPS_MUTE3DATMAXDISTANCE;
1129
		break;
1130
	case DMUS_APATH_DYNAMIC_MONO:
1131
                desc.dwFlags |= DSBCAPS_CTRLPAN | DSBCAPS_CTRLFREQUENCY;
1132
		break;
1133
	case DMUS_APATH_SHARED_STEREOPLUSREVERB:
Austin English's avatar
Austin English committed
1134
	        /* normally we have to create 2 buffers (one for music other for reverb)
1135 1136
		 * in this case. See msdn
                 */
1137
	case DMUS_APATH_DYNAMIC_STEREO:
1138
                desc.dwFlags |= DSBCAPS_CTRLPAN | DSBCAPS_CTRLFREQUENCY;
1139 1140 1141 1142
		format.nChannels = 2;
		format.nBlockAlign *= 2;
		format.nAvgBytesPerSec *=2;
		break;
1143
	default:
1144
	        return E_INVALIDARG;
1145
	}
1146

1147
        /* Create a port */
1148
        params.dwSize = sizeof(params);
1149 1150 1151 1152 1153 1154
        params.dwValidParams = DMUS_PORTPARAMS_CHANNELGROUPS | DMUS_PORTPARAMS_AUDIOCHANNELS;
        params.dwChannelGroups = (pchannel_count + 15) / 16;
        params.dwAudioChannels = format.nChannels;
        if (FAILED(hr = perf_dmport_create(This, &params)))
                return hr;

1155
        hr = IDirectSound_CreateSoundBuffer(This->dsound, &desc, &buffer, NULL);
1156
	if (FAILED(hr))
1157
	        return DSERR_BUFFERLOST;
1158

1159 1160
	/* Update description for creating primary buffer */
	desc.dwFlags |= DSBCAPS_PRIMARYBUFFER;
1161
	desc.dwFlags &= ~DSBCAPS_CTRLFX;
1162 1163 1164
	desc.dwBufferBytes = 0;
	desc.lpwfxFormat = NULL;

1165
        hr = IDirectSound_CreateSoundBuffer(This->dsound, &desc, &primary_buffer, NULL);
1166
	if (FAILED(hr)) {
1167
                IDirectSoundBuffer_Release(buffer);
1168 1169
	        return DSERR_BUFFERLOST;
	}
1170 1171 1172 1173

	create_dmaudiopath(&IID_IDirectMusicAudioPath, (void**)&pPath);
	set_audiopath_perf_pointer(pPath, iface);
	set_audiopath_dsound_buffer(pPath, buffer);
1174
	set_audiopath_primary_dsound_buffer(pPath, primary_buffer);
1175

1176
	*ppNewPath = pPath;
1177
	
1178
	TRACE(" returning IDirectMusicAudioPath interface at %p.\n", *ppNewPath);
1179

1180
	return IDirectMusicAudioPath_Activate(*ppNewPath, fActivate);
1181 1182
}

1183 1184 1185 1186
static HRESULT WINAPI IDirectMusicPerformance8Impl_SetDefaultAudioPath(IDirectMusicPerformance8 *iface,
        IDirectMusicAudioPath *pAudioPath)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
1187

1188
	FIXME("(%p, %p): semi-stub\n", This, pAudioPath);
1189 1190

	if (This->pDefaultPath) {
1191
		IDirectMusicAudioPath_Release(This->pDefaultPath);
1192 1193 1194
		This->pDefaultPath = NULL;
	}
	This->pDefaultPath = pAudioPath;
1195
	if (This->pDefaultPath) {
1196
		IDirectMusicAudioPath_AddRef(This->pDefaultPath);
1197
		set_audiopath_perf_pointer(This->pDefaultPath, iface);
1198
	}
1199

1200
	return S_OK;
1201 1202
}

1203 1204 1205 1206
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetDefaultAudioPath(IDirectMusicPerformance8 *iface,
        IDirectMusicAudioPath **ppAudioPath)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
1207

1208
	FIXME("(%p, %p): semi-stub (%p)\n", This, ppAudioPath, This->pDefaultPath);
Rok Mandeljc's avatar
Rok Mandeljc committed
1209

1210
	if (NULL != This->pDefaultPath) {
1211
	  *ppAudioPath = This->pDefaultPath;
1212
          IDirectMusicAudioPath_AddRef(*ppAudioPath);
1213 1214 1215
        } else {
	  *ppAudioPath = NULL;
        }
1216
	return S_OK;
1217 1218
}

1219 1220 1221 1222 1223
static HRESULT WINAPI IDirectMusicPerformance8Impl_GetParamEx(IDirectMusicPerformance8 *iface,
        REFGUID rguidType, DWORD dwTrackID, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime,
        MUSIC_TIME *pmtNext, void *pParam)
{
        IDirectMusicPerformance8Impl *This = impl_from_IDirectMusicPerformance8(iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
1224

1225
	FIXME("(%p, %s, %ld, %ld, %ld, %ld, %p, %p): stub\n", This, debugstr_dmguid(rguidType), dwTrackID, dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
Rok Mandeljc's avatar
Rok Mandeljc committed
1226 1227

	return S_OK;
1228 1229
}

1230
static const IDirectMusicPerformance8Vtbl DirectMusicPerformance8_Vtbl = {
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
	IDirectMusicPerformance8Impl_QueryInterface,
	IDirectMusicPerformance8Impl_AddRef,
	IDirectMusicPerformance8Impl_Release,
	IDirectMusicPerformance8Impl_Init,
	IDirectMusicPerformance8Impl_PlaySegment,
	IDirectMusicPerformance8Impl_Stop,
	IDirectMusicPerformance8Impl_GetSegmentState,
	IDirectMusicPerformance8Impl_SetPrepareTime,
	IDirectMusicPerformance8Impl_GetPrepareTime,
	IDirectMusicPerformance8Impl_SetBumperLength,
	IDirectMusicPerformance8Impl_GetBumperLength,
	IDirectMusicPerformance8Impl_SendPMsg,
	IDirectMusicPerformance8Impl_MusicToReferenceTime,
	IDirectMusicPerformance8Impl_ReferenceToMusicTime,
	IDirectMusicPerformance8Impl_IsPlaying,
	IDirectMusicPerformance8Impl_GetTime,
	IDirectMusicPerformance8Impl_AllocPMsg,
	IDirectMusicPerformance8Impl_FreePMsg,
	IDirectMusicPerformance8Impl_GetGraph,
	IDirectMusicPerformance8Impl_SetGraph,
	IDirectMusicPerformance8Impl_SetNotificationHandle,
	IDirectMusicPerformance8Impl_GetNotificationPMsg,
	IDirectMusicPerformance8Impl_AddNotificationType,
	IDirectMusicPerformance8Impl_RemoveNotificationType,
	IDirectMusicPerformance8Impl_AddPort,
	IDirectMusicPerformance8Impl_RemovePort,
	IDirectMusicPerformance8Impl_AssignPChannelBlock,
	IDirectMusicPerformance8Impl_AssignPChannel,
	IDirectMusicPerformance8Impl_PChannelInfo,
	IDirectMusicPerformance8Impl_DownloadInstrument,
	IDirectMusicPerformance8Impl_Invalidate,
	IDirectMusicPerformance8Impl_GetParam,
	IDirectMusicPerformance8Impl_SetParam,
	IDirectMusicPerformance8Impl_GetGlobalParam,
	IDirectMusicPerformance8Impl_SetGlobalParam,
	IDirectMusicPerformance8Impl_GetLatencyTime,
	IDirectMusicPerformance8Impl_GetQueueTime,
	IDirectMusicPerformance8Impl_AdjustTime,
	IDirectMusicPerformance8Impl_CloseDown,
	IDirectMusicPerformance8Impl_GetResolvedTime,
	IDirectMusicPerformance8Impl_MIDIToMusic,
	IDirectMusicPerformance8Impl_MusicToMIDI,
	IDirectMusicPerformance8Impl_TimeToRhythm,
	IDirectMusicPerformance8Impl_RhythmToTime,
1275 1276 1277 1278 1279 1280 1281 1282 1283
	IDirectMusicPerformance8Impl_InitAudio,
	IDirectMusicPerformance8Impl_PlaySegmentEx,
	IDirectMusicPerformance8Impl_StopEx,
	IDirectMusicPerformance8Impl_ClonePMsg,
	IDirectMusicPerformance8Impl_CreateAudioPath,
	IDirectMusicPerformance8Impl_CreateStandardAudioPath,
	IDirectMusicPerformance8Impl_SetDefaultAudioPath,
	IDirectMusicPerformance8Impl_GetDefaultAudioPath,
	IDirectMusicPerformance8Impl_GetParamEx
1284
};
1285 1286

/* for ClassFactory */
1287
HRESULT create_dmperformance(REFIID lpcGUID, void **ppobj)
1288
{
1289 1290
	IDirectMusicPerformance8Impl *obj;

1291
        TRACE("(%s, %p)\n", debugstr_guid(lpcGUID), ppobj);
1292

1293
	obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicPerformance8Impl));
1294
        if (NULL == obj) {
1295
		*ppobj = NULL;
1296
		return E_OUTOFMEMORY;
1297
	}
1298
        obj->IDirectMusicPerformance8_iface.lpVtbl = &DirectMusicPerformance8_Vtbl;
1299
	obj->ref = 0;  /* will be inited by QueryInterface */
1300
	obj->pDefaultPath = NULL;
1301
	InitializeCriticalSection(&obj->safe);
1302
	obj->safe.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IDirectMusicPerformance8Impl*->safe");
1303
        wine_rb_init(&obj->pchannels, pchannel_block_compare);
1304

1305 1306 1307
        obj->rtLatencyTime  = 100;  /* 100 ms TO FIX */
        obj->dwBumperLength =   50; /* 50 ms default */
        obj->dwPrepareTime  = 1000; /* 1000 ms default */
1308 1309
        return IDirectMusicPerformance8Impl_QueryInterface(&obj->IDirectMusicPerformance8_iface,
                                                           lpcGUID, ppobj);
1310
}