// DUiWave.cpp: implementation of the CDUiWave class.
//
//////////////////////////////////////////////////////////////////////

#include "DUiMan.h"
#include "DirectXHelpers.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDUiWave::CDUiWave()
{
    SetObjectType(DUI_WAVE);
    m_wszClipName[0] = 0;
    m_pSoundBuffer = NULL;
    m_pDirectSound = NULL;
}

CDUiWave::~CDUiWave()
{
    if (m_pSoundBuffer)
        m_pSoundBuffer->Release();
}

DWORD CDUiWave::Initialize()
{
    DWORD dwRetVal = 0;
    WAVEFORMATEX WaveFormat;
    BYTE *btChannel1;
    DWORD dwChannel1Length;
    BYTE *btChannel2;
    DWORD dwChannel2Length;
    DSBUFFERDESC bufdesc;
    HRESULT hr;
    
    if (m_pSoundBuffer)
        return dwRetVal;

    g_pDUiManager->DisplayDebugErrorf(L"CDUiWave::Initialize - Enter");
    if (GetWaveData(m_wszClipName, &WaveFormat, &btChannel1, &dwChannel1Length, &btChannel2, &dwChannel2Length) == 0)
    {
        memset(&bufdesc, 0, sizeof(DSBUFFERDESC));
        bufdesc.dwSize = sizeof(DSBUFFERDESC);
        bufdesc.dwFlags = DSBCAPS_CTRLDEFAULT;
        bufdesc.dwBufferBytes = dwChannel1Length; //child.cksize;
        bufdesc.lpwfxFormat = &WaveFormat;
        hr = m_pDirectSound->CreateSoundBuffer(&bufdesc, &m_pSoundBuffer, NULL);
        if (DS_OK == hr)
        {
            void *write1 = 0, *write2 = 0; 	//Write the wave data to DirectSoundBuffer you just created
            unsigned long length1, length2;
            hr = m_pSoundBuffer->Lock(0, dwChannel1Length, &write1, &length1, &write2, &length2, 0);
            if ((write1 > 0) && dwChannel1Length)
            {
                memcpy(write1,btChannel1,length1);
            }
            if ((write2 > 0) && dwChannel1Length)
            {
                memcpy(write2,btChannel1,length1);
            }
            hr = m_pSoundBuffer->Unlock(write1, length1, write2, length2);
            hr = m_pSoundBuffer->SetCurrentPosition(0);
        }
        else
        {
            g_pDUiManager->DisplayDebugErrorf(L"Could not create sound buffer %08x",hr);
        }
        if (dwChannel1Length)
            free(btChannel1);
        if (dwChannel2Length)
            free(btChannel2);
    }
    else
    {
        g_pDUiManager->DisplayDebugErrorf(L"Could not get wave data");
    }

    return dwRetVal;
}

DWORD CDUiWave::Play()
{
    HRESULT hr;
    DWORD dwRes;

    if (m_pSoundBuffer)
    {
        hr = m_pSoundBuffer->GetStatus(&dwRes);
        if (!(dwRes & DSBSTATUS_PLAYING))
        {
            hr = m_pSoundBuffer->SetCurrentPosition(0);
            hr = m_pSoundBuffer->Play(0, 0, 0);
        }
    }
    return hr;
}

DWORD CDUiWave::Stop()
{
    HRESULT hr = S_OK;
    DWORD dwRes;

    if (m_pSoundBuffer)
    {
        hr = m_pSoundBuffer->GetStatus(&dwRes);
        if (dwRes & DSBSTATUS_PLAYING)
        {
            hr = m_pSoundBuffer->Stop();
        }
    }
    return hr;
}

BOOL CDUiWave::IsPlaying()
{
    BOOL bRetVal = FALSE;
    HRESULT hr = S_OK;
    DWORD dwRes;

    if (m_pSoundBuffer)
    {
        hr = m_pSoundBuffer->GetStatus(&dwRes);
        if (dwRes & DSBSTATUS_PLAYING)
        {
            bRetVal = TRUE;
        }
    }
    return bRetVal;
}
