//Sesler


#include "mostemp.h"
#include <windows.h>
#include <dshow.h>



#pragma comment (lib,"Strmiids.lib")
#pragma comment (lib,"winmm.lib")

IGraphBuilder *pGraph;             // filter graph için 
IMediaControl *pMediaControl;      //Data flow kontrolü için  filter graph tan geçen data
IMediaEvent   *pMediaEvent;        //Event handling 
IMediaSeeking *pMS   = NULL;

#include <dshow.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <tchar.h>
#include <atlbase.h>


CDXSound *Sound;
CDXSoundBuffer *Sounds[SOUND_MAX];

//SESLER
bool sesinit()
{

	Sound = new CDXSound();
	Sound->Create(g_hWnd);
	
	Sounds[SOUND_TOP] = new CDXSoundBuffer;
	Sounds[SOUND_TOP]->Load(Sound, "data\\snd\\ses\\top.WAV",7);
	
	Sounds[SOUND_YAN] = new CDXSoundBuffer;
	Sounds[SOUND_YAN]->Load(Sound, "data\\snd\\ses\\yan.WAV",2);

	Sounds[SOUND_UST] = new CDXSoundBuffer;
	Sounds[SOUND_UST]->Load(Sound, "data\\snd\\ses\\ykr.WAV",2);


	Sounds[SOUND_PATLA] = new CDXSoundBuffer;
	Sounds[SOUND_PATLA]->Load(Sound, "data\\snd\\ses\\patlama.WAV",4);
	
	Sounds[SOUND_SOPA] = new CDXSoundBuffer;
	Sounds[SOUND_SOPA]->Load(Sound, "data\\snd\\ses\\tops.WAV",1);

  return TRUE;
}
void SesKaldir(void)
{
	SAFEDELETE(Sounds[SOUND_TOP]);
	SAFEDELETE(Sounds[SOUND_YAN]);
	SAFEDELETE(Sounds[SOUND_UST]);
	SAFEDELETE(Sounds[SOUND_PATLA]);
	SAFEDELETE(Sounds[SOUND_SOPA]);

	SAFEDELETE(Sound);

}
BOOL Dsoundinit(void)
{
	HRESULT hr;
	long int EvCode;

	hr=CoInitialize(NULL);
	if(FAILED(hr))
		MessageBox(NULL,"Com init edilemedi!","Hata",MB_OK);

    hr=CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                        IID_IGraphBuilder, (void **)&pGraph);
    if(FAILED(hr))
	    MessageBox(NULL,"Filter acilamadi!","Hata",MB_OK);

	hr=pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);


	hr=pGraph->QueryInterface(IID_IMediaEvent, (void **)&pMediaEvent);

	hr=pGraph->RenderFile(L"data/snd/oyun.mp3", NULL);
	if(FAILED(hr))
		MessageBox(NULL,"Mp3 bulunamadi!","Hata",MB_OK);

   hr=pMediaControl->Run();


   hr=pMediaEvent->WaitForCompletion(0,&EvCode);

    hr=pGraph->QueryInterface(IID_IMediaSeeking, (void **)&pMS);


	sesinit();


	return TRUE;

}

/*-------------------------------------------------------------------------------------------
   FONKSIYON ISMI:KillDirectShowSystem
   YAPTIGI IS:DirectShow Sistemini Yok Eden Sistem 
-------------------------------------------------------------------------------------------*/
void Dsoundkaldir()
{

	SesKaldir();

	pMediaControl->Stop();

	pMediaControl->Release();
    pMediaEvent->Release();
	pMS->Release();
    pGraph->Release();
    CoUninitialize();
}

HRESULT HandleGraphEvent(void)
{
  
/*
	hr=pGraph-
   hr=pMediaControl-

   hr=pMediaEvent-
	*/
   LONG evCode, evParam1, evParam2;
    HRESULT hr=S_OK;

    // Make sure that we don't access the media event interface
    // after it has already been released.
    if (!pMediaEvent)
        return S_OK;

    // Process all queued events
    while(SUCCEEDED(pMediaEvent->GetEvent(&evCode, (LONG_PTR *) &evParam1,
                    (LONG_PTR *) &evParam2, 0)))
    {
        // Free memory associated with callback, since we're not using it
        hr = pMediaEvent->FreeEventParams(evCode, evParam1, evParam2);

        // If this is the end of the clip, reset to beginning
        if(EC_COMPLETE == evCode)
        {
	
            LONGLONG pos=0;
         
	  
            // Reset to first frame of movie
            hr = pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,
                                   NULL, AM_SEEKING_NoPositioning);	
		
		
            if (FAILED(hr))
            {
                // Some custom filters (like the Windows CE MIDI filter)
                // may not implement seeking interfaces (IMediaSeeking)
                // to allow seeking to the start.  In that case, just stop
                // and restart for the same effect.  This should not be
                // necessary in most cases.
                if (FAILED(hr = pMediaControl->Stop()))
                {
                   // Msg(TEXT("Failed(0x%08lx) to stop media clip!\r\n"), hr);
                    break;
                }

                if (FAILED(hr = pMediaControl->Run()))
                {
                   // Msg(TEXT("Failed(0x%08lx) to reset media clip!\r\n"), hr);
                    break;
                }
            }
        }
    }

    return hr;
}

void LoopSes()
{

/*
	hr=pGraph-
   hr=pMediaControl-

   hr=pMediaEvent-
	*/
  


	HandleGraphEvent();
   
	
}


