Author Topic: Question  (Read 4653 times)

Ares

  • Posts: 3
Question
« on: 7 May '07 - 13:07 »
Does anyone knows how to read the Directshow PCM data with VB?

Thanks

Dreamy

  • Posts: 55
Re: Question
« Reply #1 on: 10 May '07 - 16:56 »
It's a stream/buffer, isn't it? What do you mean with 'read'?

Ares

  • Posts: 3
Re: Question
« Reply #2 on: 13 May '07 - 22:08 »
what i mean is read the beat when i play a song using Directshow.

i have this code for C++ but i want to do the same in VB6

here is the code:

#include <dshow.h>
#include <iostream.h>
#include <stdio.h>
#include <qedit.h>
#include <atlbase.h>



HRESULT GetUnconnectedPin(
    IBaseFilter *pFilter,   // Pointer to the filter.
    PIN_DIRECTION PinDir,   // Direction of the pin to find.
    IPin **ppPin);           // Receives a pointer to the pin.


 HRESULT ConnectFilters(
    IGraphBuilder *pGraph, // Filter Graph Manager.
    IPin *pOut,            // Output pin on the upstream filter.
    IBaseFilter *pDest);    // Downstream filter.

 HRESULT ConnectFilters(
    IGraphBuilder *pGraph,
    IBaseFilter *pSrc,
    IBaseFilter *pDest);

 void MyFreeMediaType(AM_MEDIA_TYPE& mt);

class CFakeCallback : public ISampleGrabberCB
{

public:

    STDMETHODIMP_(ULONG) AddRef()  { return 2; }
    STDMETHODIMP_(ULONG) Release() { return 1; }

    STDMETHODIMP QueryInterface(REFIID riid, void ** ppv)
    {
        if (riid == IID_ISampleGrabberCB || riid == IID_IUnknown)
        {
            *ppv = (void *) static_cast<ISampleGrabberCB *>(this);
            return NOERROR;
        }
        return E_NOINTERFACE;
    }

    STDMETHODIMP SampleCB( double SampleTime, IMediaSample * pSample )
    {
        static long counter = 0;



        return 0;
    }

    STDMETHODIMP BufferCB( double SampleTime, BYTE * pBuffer, long
BufferLen )
    {
  //HERE IS WHERE YOU GRAB THE AUDIO SAMPLES!!!
  printf("%d\n", (short)pBuffer[0]);
  for(int n=0; n<BufferLen; n++)
  {

  }
        return 0;
    }

};


void main(void)
{
    IGraphBuilder *pGraph = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;
 IBaseFilter *pGrabberF = NULL;






    // Initialize the COM library.
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return;
    }

    // Create the filter graph manager and query for interfaces.
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
                        IID_IGraphBuilder, (void **)&pGraph);
    if (FAILED(hr))
    {
        printf("ERROR - Could not create the Filter Graph Manager.");
        return;
    }


  hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
      IID_IBaseFilter, (void**)&pGrabberF);

 if (FAILED(hr))
 {
  printf("ERROR - Could not create the Sample Grabber.");
  return;
 }


 hr = pGraph->AddFilter(pGrabberF, L"Sample Grabber");

 if (FAILED(hr))
 {
  printf("ERROR - Could not add Sample Grabber filter to graph.");
  return;
 }

 ISampleGrabber *pGrabber;
 hr =pGrabberF->QueryInterface(IID_ISampleGrabber, (void**)&pGrabber);
 if (FAILED(hr))
 {
  printf("ERROR - Could not create ISampleGrabber interface.");
  return;
 }
    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
 if (FAILED(hr))
 {
  printf("ERROR - Could not crate IMediaControl interface.");
  return;
 }
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
 if (FAILED(hr))
 {
  printf("ERROR - Could not create IMediaEvent interface.");
  return;
 }

 AM_MEDIA_TYPE smt;
 ZeroMemory(&smt, sizeof(AM_MEDIA_TYPE));

 smt.majortype = MEDIATYPE_Audio;
 smt.formattype = FORMAT_WaveFormatEx;

 smt.subtype = MEDIASUBTYPE_PCM;
 hr = pGrabber->SetMediaType(&smt);
 if (FAILED(hr))
 {
  printf("ERROR - Could not set media type for pSampleGrabber.");
  return;
 }
 hr = pGrabber->SetOneShot(FALSE);
 if (FAILED(hr))
 {
  printf("ERROR - Could not set one shot for pSampleGrabber.");
  return;
 }
 hr = pGrabber->SetBufferSamples(FALSE);
 if (FAILED(hr))
 {
  printf("ERROR - Could not set buffer samples for pSampleGrabber.");
  return;
 }

 CFakeCallback pCallbackA;
 CComQIPtr< ISampleGrabberCB, &IID_ISampleGrabberCB > pCBa( &pCallbackA );


 hr = pGrabber->SetCallback(pCBa, 1);
 if (FAILED(hr))
 {
  printf("ERROR - Could not set callback for pSampleGrabber.");
  return;
 }

/*IBaseFilter *pSrc;
hr = pGraph->AddSourceFilter(L"C:\\Documents and
Settings\\gellis\\Desktop\\cloud number 9-(bryan adams)video.mpg",
L"Source", &pSrc);
if (FAILED(hr))
{
    // Return an error code.
}
hr = ConnectFilters(pGraph, pSrc, pGrabberF);


// Set one-shot mode and buffering.
hr = pGrabber->SetOneShot(TRUE);
hr = pGrabber->SetBufferSamples(TRUE);

long evCode;
pControl->Run(); // Run the graph.

// Find the required buffer size.
long cbBuffer = 0;
hr = pGrabber->GetCurrentBuffer(&cbBuffer, NULL);

char *pBuffer = new char[cbBuffer];
if (!pBuffer)
{
    // Out of memory. Return an error code.
}
hr = pGrabber->GetCurrentBuffer(&cbBuffer, (long*)pBuffer);

pEvent->WaitForCompletion(INFINITE, &evCode); // Wait till it's done.
*/


    // Build the graph. IMPORTANT: Change this string to a file on your
system.
    hr = pGraph->RenderFile(L"C:\\Documents and
Settings\\gellis\\Desktop\\cloud number 9-(bryan adams)video.mpg", NULL);

 if (SUCCEEDED(hr))
    {
        // Run the graph.
        hr = pControl->Run();
        if (SUCCEEDED(hr))
        {
            // Wait for completion.
            long evCode;
            pEvent->WaitForCompletion(INFINITE, &evCode);

            // Note: Do not use INFINITE in a real application, because it
            // can block indefinitely.
        }
    }
    pControl->Release();
    pEvent->Release();
    pGraph->Release();
    CoUninitialize();
}

HRESULT ConnectFilters(
    IGraphBuilder *pGraph,
    IBaseFilter *pSrc,
    IBaseFilter *pDest)
{
    if ((pGraph == NULL) || (pSrc == NULL) || (pDest == NULL))
    {
        return E_POINTER;
    }

    // Find an output pin on the first filter.
    IPin *pOut = 0;
    HRESULT hr = GetUnconnectedPin(pSrc, PINDIR_OUTPUT, &pOut);
    if (FAILED(hr))
    {
        return hr;
    }
    hr = ConnectFilters(pGraph, pOut, pDest);
    pOut->Release();
    return hr;
}

HRESULT ConnectFilters(
    IGraphBuilder *pGraph, // Filter Graph Manager.
    IPin *pOut,            // Output pin on the upstream filter.
    IBaseFilter *pDest)    // Downstream filter.
{
    if ((pGraph == NULL) || (pOut == NULL) || (pDest == NULL))
    {
        return E_POINTER;
    }
#ifdef debug
        PIN_DIRECTION PinDir;
        pOut->QueryDirection(&PinDir);
        _ASSERTE(PinDir == PINDIR_OUTPUT);
#endif

    // Find an input pin on the downstream filter.
    IPin *pIn = 0;
    HRESULT hr = GetUnconnectedPin(pDest, PINDIR_INPUT, &pIn);
    if (FAILED(hr))
    {
        return hr;
    }
    // Try to connect them.
    hr = pGraph->Connect(pOut, pIn);
    pIn->Release();
    return hr;
}

HRESULT GetUnconnectedPin(
    IBaseFilter *pFilter,   // Pointer to the filter.
    PIN_DIRECTION PinDir,   // Direction of the pin to find.
    IPin **ppPin)           // Receives a pointer to the pin.
{
    *ppPin = 0;
    IEnumPins *pEnum = 0;
    IPin *pPin = 0;
    HRESULT hr = pFilter->EnumPins(&pEnum);
    if (FAILED(hr))
    {
        return hr;
    }
    while (pEnum->Next(1, &pPin, NULL) == S_OK)
    {
        PIN_DIRECTION ThisPinDir;
        pPin->QueryDirection(&ThisPinDir);
        if (ThisPinDir == PinDir)
        {
            IPin *pTmp = 0;
            hr = pPin->ConnectedTo(&pTmp);
            if (SUCCEEDED(hr))  // Already connected, not the pin we want.
            {
                pTmp->Release();
            }
            else  // Unconnected, this is the pin we want.
            {
                pEnum->Release();
                *ppPin = pPin;
                return S_OK;
            }
        }
        pPin->Release();
    }
    pEnum->Release();
    // Did not find a matching pin.
    return E_FAIL;
}

void MyFreeMediaType(AM_MEDIA_TYPE& mt)
{
    if (mt.cbFormat != 0)
    {
        CoTaskMemFree((PVOID)mt.pbFormat);
        mt.cbFormat = 0;
        mt.pbFormat = NULL;
    }
    if (mt.pUnk != NULL)
    {
        // Unecessary because pUnk should not be used, but safest.
        mt.pUnk->Release();
        mt.pUnk = NULL;
    }
}
--------------------------------------------------------------------------------

any idea?

Dreamy

  • Posts: 55
Re: Question
« Reply #3 on: 15 May '07 - 13:34 »
I see your code is from CodeGuru forums... I recommend you to ask there.
:( I'm affraid this won't help you, that are functions for console app.
Try to find something about AM_MEDIA_TYPE in your Visual Studio help...

Ares

  • Posts: 3
Re: Question
« Reply #4 on: 22 May '07 - 11:22 »
Thanks for your reply... iŽll try to write the code in VB by my own slowly.