CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2010
    Posts
    121

    Cannot save a wav file after SampleGrabber grabs the audio sample?

    Code:
    DexterLib::_AMMediaType mt;
    	ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
    	//mt.majortype = MEDIATYPE_Video;
    	//mt.subtype = MEDIASUBTYPE_RGB24;
    	mt.majortype = MEDIATYPE_Audio;
    	mt.formattype = FORMAT_WaveFormatEx;
    	mt.subtype = MEDIASUBTYPE_PCM;
    	mt.bTemporalCompression = FALSE;
    
    	WAVEFORMATEXTENSIBLE *pwfx = 
    			(WAVEFORMATEXTENSIBLE *) new BYTE[sizeof(WAVEFORMATEXTENSIBLE)];
    
    	pwfx->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
    	pwfx->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
    	pwfx->Format.nChannels = 2;
    	pwfx->Format.nSamplesPerSec = 16000; // 16khz
    	pwfx->Format.wBitsPerSample = 16;
    	pwfx->Format.nAvgBytesPerSec = pwfx->Format.nSamplesPerSec * pwfx->Format.wBitsPerSample * pwfx->Format.nChannels / 8;
    	pwfx->Format.nBlockAlign = pwfx->Format.wBitsPerSample * pwfx->Format.nChannels / 8;
    	pwfx->dwChannelMask = (1 << pwfx->Format.nChannels) - 1;
    	pwfx->Samples.wValidBitsPerSample = pwfx->Format.wBitsPerSample;
    	pwfx->SubFormat = MEDIASUBTYPE_PCM;
    	//pMediaType->SetFormat((BYTE*)pwfx, sizeof(WAVEFORMATEXTENSIBLE));
    	mt.pbFormat = (BYTE*)pwfx;
    	mt.cbFormat = sizeof(WAVEFORMATEXTENSIBLE);
    	mt.lSampleSize = 2;
    
    if ((mt.formattype == FORMAT_WaveFormatEx))
            //(mt.cbFormat >= sizeof(VIDEOINFOHEADER)) &&
            //(mt.pbFormat != NULL)) 
        {
             
    		WAVEFORMATEX* pWav = (WAVEFORMATEX*)mt.pbFormat;
    		size_t hdr_size = sizeof(WAVEFORMATEX);
    		WriteWav(L"123.wav", pWav, hdr_size, pBuffer, cbBuffer);
    		 
        }
    I've specified the type, subtype and format. When the sample comes out, it is about 80k bytes in length, can't play back when saved.
    Thanks
    Jack
    Last edited by luckiejacky; January 1st, 2018 at 02:06 AM.

  2. #2
    Join Date
    Mar 2017
    Posts
    105

    Re: Cannot save a wav file after SampleGrabber grabs the audio sample?

    You can use this:
    Code:
    STDMETHODIMP CMpeg2Wav::Mpeg2WavConvertor(LPCWSTR file, HWND g_hwnd, LPCWSTR waveFile)
    {
        if(g_hwnd == NULL)
        {
            MessageBox(NULL, "FAILED TO CREATE THE HANDLE" 
                       " OF THE WINDOW", NULL, NULL);
            exit(0);
        }
        //creating capture graph
        hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, 
             CLSCTX_INPROC,  IID_ICaptureGraphBuilder2, (void **)&m_pCapture);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Unable to create capture graph", NULL, NULL);
        }
        //creating graph builder
        hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
             IID_IGraphBuilder, (void **)&pGraphBuilder);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to Create Graph Builder", NULL, NULL);
        }
     
        hr = m_pCapture->SetFiltergraph(pGraphBuilder);  
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed", NULL, NULL);
        }
        pGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&pMC);
        pGraphBuilder->QueryInterface(IID_IMediaEventEx, (void **)&pEvent);
        hr = CoCreateInstance(CLSID_MPEG1Splitter, NULL, CLSCTX_INPROC,
                              IID_IBaseFilter, (void**)&pSplitter);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to Create MpegSplitter Filter", NULL, NULL);
        }
        //creating Audio Decoder
        static const GUID CLSID_MPEG_Audio_Decoder = 
            { 0x4a2286e0, 0x7bef, 0x11ce, 
            { 0x9b, 0xd9, 0x0, 0x0, 0xe2, 0x02, 0x59, 0x9c } };
        hr = CoCreateInstance(CLSID_MPEG_Audio_Decoder, NULL, 
             CLSCTX_INPROC, IID_IBaseFilter, (void**)&pDecoder);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to Create MpegAudioDecoder Filter", 
                       NULL, NULL);
        }
        //creating Video Decoder
        static const GUID CLSID_MPEG_Video_Decoder = 
            {0xfeb50740 , 0x7bef, 0x11ce, 
            {0x9b , 0xd9, 0x0, 0x0, 0xe2, 0x2, 0x59,  0x9c} };
        hr = CoCreateInstance(CLSID_MPEG_Video_Decoder, 
             NULL, CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVideoDecoder);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to Create MpegVideoDecoder Filter", NULL, NULL);
        }
        //creating Video Renderer for windows XP
        static const GUID CLSID_MPEG_Video_Renderer = 
            {0x6BC1CFFA , 0x8FC1, 0x4261, 
            { 0xAC, 0x22,0xCF , 0xB4, 0xCC, 0x38, 0xDB,  0x50} };
        hr = CoCreateInstance(CLSID_MPEG_Video_Renderer, NULL, 
             CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVideoRenderer);
        if(FAILED(hr))
        {
            //creating Video Renderer for windows 2000
            static const GUID CLSID_Video_Renderer = 
                   {0x70E102B0 , 0x5556, 0x11CE, 
                   { 0x97, 0xC0, 0x00 , 0xAA, 0x00, 0x55, 0x59,  0x5A} };
            hr = CoCreateInstance(CLSID_Video_Renderer, NULL, 
                 CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVideoRenderer);
    
            if (FAILED(hr))
            {
                MessageBox(NULL, "Still Error!", "Error", MB_OK);
            }
         }
    
        //creating wavedest filter
        static const GUID CLSID_WavDest = 
               { 0x3c78b8e2, 0x6c4d, 0x11d1, 
               { 0xad, 0xe2, 0x0, 0x0, 0xf8, 0x75, 0x4b, 0x99 } };
        hr = CoCreateInstance(CLSID_WavDest, NULL, 
             CLSCTX_INPROC,  IID_IBaseFilter, (void **)&pWavDest);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to Create WaveDest Filter", NULL, NULL);
        }
        //Creating FileSink2
        hr = CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_INPROC,
                              IID_IFileSinkFilter2, (void **)&pFileSink);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to Create FileSink2 Filter", NULL, NULL);
        }
        // Get the file sink interface pointer from the File Writer
        hr = pFileSink->QueryInterface(IID_IBaseFilter, (void **)&pFileWriter);
        if(FAILED(hr))
        {
           MessageBox(NULL, "Unable to get the File Writer" 
                      " interface pointer from the File Sink", NULL, NULL);
        }
        hr = pFileSink->SetFileName(waveFile, NULL);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Couldnt create wav file", NULL, NULL);
        }
        //adding writer to the graph
        hr = pGraphBuilder->AddFilter((IBaseFilter *)pFileWriter, L"File Writer");
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to add Writer To Graph", NULL, NULL);
        }
        //adding wavdest to the graph
        hr = pGraphBuilder->AddFilter(pWavDest, L"WAV DEST");
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to add WavDest To Graph", NULL, NULL);
        }
        //adding audioDecoder to the graph
        hr = pGraphBuilder->AddFilter(pDecoder, NULL);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to add audio decoder To Graph", NULL, NULL);
        }
        //adding video renderer to the graph
        hr = pGraphBuilder->AddFilter(pVideoRenderer, NULL);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to add video renderer To Graph", NULL, NULL);
        }
        //adding video Decoder to the graph
        hr = pGraphBuilder->AddFilter(pVideoDecoder, NULL);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to add video decoder To Graph", NULL, NULL);
        }
        //adding Mpeg Stream Splitter to the graph
        hr = pGraphBuilder->AddFilter(pSplitter, NULL);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to add MpegSplitter to graph", NULL, NULL);
        }
        hr = pFileSink->SetMode(AM_FILE_OVERWRITE);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to OverWrite", NULL, NULL);
        }
        hr = pGraphBuilder->RenderFile(file, NULL);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to RenderFile", NULL, NULL);
        }
        //Removing Video Renderer From the Graph
        hr = pGraphBuilder->RemoveFilter(pVideoRenderer);
        if(FAILED(hr))
        {
            MessageBox(NULL, "Couldn't remove the video" 
                             " rendere filter", NULL, NULL);
        }
        //Removing Video Decoder From the Graph
        hr = pGraphBuilder->RemoveFilter(pVideoDecoder);
    
        if(FAILED(hr))
        {
            MessageBox(NULL, "Couldn't remove the video decoder filter", NULL, NULL);
        }
        //Instructing the graph manager to send a windows message 
        //to the window of the calling application 
        //whenever an event occurs on the graph
        pEvent->SetNotifyWindow((OAHWND)g_hwnd, WM_GRAPHNOTIFY, 0);
        // Execute the graph
        hr = pMC->Run();
        if(FAILED(hr))
        {
            MessageBox(NULL, "Failed to Run", NULL, NULL);
        }
        return S_OK;
    }
    If you like my answer, do rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured