How do I use SampleGrabber to build a spectrum/visualizer for audio?

£可爱£侵袭症+ 提交于 2019-12-23 01:51:11

问题


I am currently building an application that uses the DS sdk, and I need to figure out how to obtain the amplitude constantly from the audio source to draw a visualizer or spectrum of some sort. I've been trying to look on how sample grabber is implemented on audio, but all of the information I've found have been outdated, and unhelpful. After a few dozen attempts, this is what I have currently:

        ISampleGrabber pGrabber = (ISampleGrabber)pSampleGrabber;
        hr = pGraph.ConnectDirect(GetPin(pInfinitePinTeeFilterAudio, "Output3"), GetPin(pSampleGrabber, "Input"), null);
        checkHR(hr, "1040");
        if (hr < 0) return false;
        hr = pGraph.ConnectDirect(GetPin(pSampleGrabber, "Output"), GetPin(pNullRenderer, "In"), null);
        checkHR(hr, "1041");
        if (hr < 0) return false;

        AMMediaType media = new AMMediaType();
        media.formatType = FormatType.WaveEx;
        pGrabber.GetConnectedMediaType(media); //gets and sets media type into media

        pGrabber.SetBufferSamples(true);
        int cbbuffer = 0;
        hr = pGrabber.GetCurrentBuffer(ref cbbuffer, IntPtr.Zero);

How do I read what is on the current buffer, and continuously read what is on the buffer?


回答1:


You are on the right track, you need to implement a callback function that the sample grabber can use, that's what SampleCB is for, the C# equivalent would be something like this:

int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample pSample )
{
    //work with audio sample here
    return 0;
}

Also make sure that you have an audio decoder before your sample grabber in your graph, otherwise you will receive compressed samples.

There's also a relevant article here that might help you.



来源:https://stackoverflow.com/questions/4595165/how-do-i-use-samplegrabber-to-build-a-spectrum-visualizer-for-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!