通过引用Bass.Net.dll这个声音处理类库就可以轻松的获取频谱图数据并绘制频谱图。
1 public void DrawFFT(Graphics g) 2 { 3 if (Bass.BASS_ChannelIsActive(SystemFont.Stream) == BASSActive.BASS_ACTIVE_PLAYING) 4 { 5 Bass.BASS_ChannelGetData(SystemFont.Stream, SystemFont.FFTDatas,(int)BASSData.BASS_DATA_FFT1024); 6 for (SystemFont.i = 0; SystemFont.i < SystemFont.FFTDatas.Length-1; SystemFont.i++) 7 { 8 SystemFont.Di = (int)(Math.Abs(SystemFont.FFTDatas[SystemFont.i]) * 1792); 9 if (SystemFont.Di > this.Height) 10 SystemFont.Di = this.Height; 11 if (SystemFont.Di >= SystemFont.FFTPeacks[SystemFont.i]) 12 SystemFont.FFTPeacks[SystemFont.i] = SystemFont.Di; 13 else 14 SystemFont.FFTPeacks[SystemFont.i] = SystemFont.FFTPeacks[SystemFont.i] - 1; 15 if (SystemFont.Di >= SystemFont.FFTFall[SystemFont.i]) 16 SystemFont.FFTFall[SystemFont.i] = SystemFont.Di; 17 else 18 SystemFont.FFTFall[SystemFont.i] = SystemFont.FFTFall[SystemFont.i]-1; 19 if ((this.Height - SystemFont.FFTPeacks[SystemFont.i]) > this.Height) 20 SystemFont.FFTPeacks[SystemFont.i] = this.Height; 21 if ((this.Height - SystemFont.FFTFall[SystemFont.i]) > this.Height) 22 SystemFont.FFTFall[SystemFont.i] = this.Height; 23 g.FillRectangle(solidBrush, SystemFont.i * (w+2), this.Height - SystemFont.FFTFall[SystemFont.i],w, this.Height); 24 } 25 } 26 else 27 { 28 return; 29 } 30 }
效果如下: