filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.15k stars 451 forks source link

How to split SpectrumData into individual channels for stereo audio? #428

Closed ToxMox closed 3 years ago

ToxMox commented 3 years ago

Hi there.

I have a script that I'm trying to pull in spectrum data separately for each channel in a stereo audio source. I'm able to successfully pull in the full spectrum data but I'm unsure of how to call GetSpectrumData() in order to be able to assign the spectrum data from the left channel and right channel to their own variables. Any insights would be greatly appreciated! :)

Thanks!

filoe commented 3 years ago

If you take a look at this example: https://github.com/filoe/cscore/blob/master/Samples/WinformsVisualization/Form1.cs#L132 You can see that the left and right channel are added to the spectrumprovider and merged together. Create two spectrumProviders (one for left one for right). Add the left sample to the left spectrumProvider and the right sample to the right spectrumProvider.

ToxMox commented 3 years ago

I see. If I do that, am I right to assume that in order to get the left and right spectrum data separately it would open the audio device two times?

In my usage I have another script calling GetSpectrumData(). Currently that returns the merged together spectrum data. I'm trying to figure out how I could call GetSpectrumData just once and return both the left and right spectrum data into two variables.

So in BasicSpectrumProvider there is this section:

        public override void Add(float left, float right)
        {
            base.Add(left, right);
            _contexts.Clear();
        }

Is there some clever way to instead of adding the left and right channels there it would instead concatenate/append them so that the single spectrum data would be a single long spectrum containing the left first then the right?

If so would I then later be able to take the returned float spectrum from GetSpectrumData() mentioned above and then split the float in the middle somehow so that I can assign each half to a variable?

ToxMox commented 3 years ago

I had a friend who is very good with C# help me with this last night. He did a bunch of things but the basic gist was to create two spectrumProviders as you mentioned and modified some functions etc to send along both floats. Thank you.