加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 带均衡器的声音

发布时间:2020-12-15 18:03:22 所属栏目:百科 来源:网络整理
导读:几天后,我试图用C#创建一个均衡器. 看过NAudio很多时间,但我找不到任何适用于naudio的均衡器. 现在几天之后,我终于在@stackoverflow,希望你知道一种使用c#创建均衡器的方法. PS:我也尝试过System.Media.SoundPlayer.但SoundPlayer甚至不支持任何与dsp有关的
几天后,我试图用C#创建一个均衡器.
看过NAudio很多时间,但我找不到任何适用于naudio的均衡器.
现在几天之后,我终于在@stackoverflow,希望你知道一种使用c#创建均衡器的方法.

PS:我也尝试过System.Media.SoundPlayer.但SoundPlayer甚至不支持任何与dsp有关的东西.那么还有另一个音频库可以在外面使用“纯”音频吗?

解决方法

So is there another audio library which works with “pure” audio outside?

是的,有一个:https://cscore.codeplex.com

根据EqualizerSample,你可以像这样使用均衡器:

using CSCore;
using CSCore.Codecs;
using CSCore.SoundOut;
using CSCore.Streams;
using System;
using System.Threading;

...

private static void Main(string[] args)
{
    const string filename = @"C:Temptest.mp3";
    EventWaitHandle waitHandle = new AutoResetEvent(false);

    try
    {
        //create a source which provides audio data
        using(var source = CodecFactory.Instance.GetCodec(filename))
        {
            //create the equalizer.
            //You can create a custom eq with any bands you want,or you can just use the default 10 band eq.
            Equalizer equalizer = Equalizer.Create10BandEqualizer(source);

            //create a soundout to play the source
            ISoundOut soundOut;
            if(WasapiOut.IsSupportedOnCurrentPlatform)
            {
                soundOut = new WasapiOut();
            }
            else
            {
                soundOut = new DirectSoundOut();
            }

            soundOut.Stopped += (s,e) => waitHandle.Set();

            IWaveSource finalSource = equalizer.ToWaveSource(16); //since the equalizer is a samplesource,you have to convert it to a raw wavesource
            soundOut.Initialize(finalSource); //initialize the soundOut with the previously created finalSource
            soundOut.Play();

            /*
             * You can change the filter configuration of the equalizer at any time.
             */
            equalizer.SampleFilters[0].SetGain(20); //eq set the gain of the first filter to 20dB (if needed,you can set the gain value for each channel of the source individually)

            //wait until the playback finished
            //of course that is optional
            waitHandle.WaitOne();

            //remember to dispose and the soundout and the source
            soundOut.Dispose();
        }
    }
    catch(NotSupportedException ex)
    {
        Console.WriteLine("Fileformat not supported: " + ex.Message);
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unexpected exception: " + ex.Message);
    }
}

您可以将均衡器配置为您想要的.由于它实时运行100%,因此所有更改都会立即应用.如果需要,还可以单独访问修改每个通道.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读