C# BASS音频库 EQ设置效果
上篇简单的介绍了 BASS的播放 和频谱的使用,这篇文章主要讲 BASS插件的 音效设置。直接上代码。
效果图:
代码:
1 using System; 2 using System.Windows.Forms; 3 using Un4seen.Bass; 4 5 namespace WindowsFormsApp4 6 { 7 public partial class Form1 : Form 8 { 9 public Form1() 10 { 11 InitializeComponent(); 12 } 13 string file = ""; 14 int _stream = 0; 15 private void Form1_Load(object sender, EventArgs e) 16 { 17 bool r = Bass.BASS_Init(-1, 48000, BASSInit.BASS_DEVICE_CPSPEAKERS, this.Handle); 18 19 if (!r) 20 { 21 MessageBox.Show("出错了," + Bass.BASS_ErrorGetCode().ToString()); 22 return; 23 } 24 } 25 26 /// <summary> 27 /// 打开文件 28 /// </summary> 29 /// <param name="sender"></param> 30 /// <param name="e"></param> 31 private void btnOpen_Click(object sender, EventArgs e) 32 { 33 OpenFileDialog o = new OpenFileDialog(); 34 if (o.ShowDialog() == DialogResult.OK) 35 { 36 file = o.FileName; 37 38 if (_stream != 0) 39 Bass.BASS_ChannelStop(_stream); 40 41 //第一个参数是文件名, 42 //第二个参数是文件流开始位置, 43 //第三个是文件流长度 0为使用文件整个长度, 44 //最后一个是流的创建模式 45 _stream = Bass.BASS_StreamCreateFile(file, 0L, 0L, BASSFlag.BASS_SAMPLE_FLOAT); 46 47 LoadEQ_(_stream);//应用EQ设置 48 49 Bass.BASS_ChannelPlay(_stream, true);//播放 50 } 51 } 52 53 54 private int[] _fxEQ = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 55 private float[] _fxCenter = { 31, 63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000 }; 56 private float[] _fxGain = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };//增益效果 57 58 /// <summary> 59 /// 应用EQ设置 60 /// </summary> 61 /// <param name="_stream"></param> 62 private void LoadEQ_(int _stream) 63 { 64 BASS_DX8_PARAMEQ eq = new BASS_DX8_PARAMEQ(); 65 for (int i = 0; i < _fxCenter.Length; i++) 66 { 67 _fxEQ[i] = Bass.BASS_ChannelSetFX(_stream, BASSFXType.BASS_FX_DX8_PARAMEQ, 0); 68 eq.fBandwidth = 18f; 69 eq.fCenter = _fxCenter[i]; 70 eq.fGain = _fxGain[i]; 71 Bass.BASS_FXSetParameters(_fxEQ[i], eq); 72 } 73 } 74 75 /// <summary> 76 /// 设置EQ 77 /// </summary> 78 /// <param name="band"></param> 79 /// <param name="gain"></param> 80 private void UpdateEQ(int band, float gain) 81 { 82 BASS_DX8_PARAMEQ eq = new BASS_DX8_PARAMEQ(); 83 if (Bass.BASS_FXGetParameters(_fxEQ[band], eq)) 84 { 85 eq.fGain = _fxGain[band] = gain; 86 Bass.BASS_FXSetParameters(_fxEQ[band], eq); 87 } 88 } 89 90 private void trackBar_Scroll(object sender, EventArgs e) 91 { 92 TrackBar t = (TrackBar)sender; 93 int band = int.Parse(t.Tag.ToString()); 94 UpdateEQ(band, t.Value); 95 } 96 97 98 } 99 }