声音的时域、频域(FFT)波形实时可视化绘制

 

声音的时域、频域(FFT)波形实时可视化绘制

浏览:1378 评论:4 源代码附件:1
C#,.NET 1.0,.NET 2.0

文章包含源代码 1个,成功下载 206次( 请点击本文附件链接下载)
<script type=text/javascript> </script> <script src="声音的时域、频域(FFT)波形实时可视化绘制.files/show_ads.js" type=text/javascript> </script>

[翻译]

JeffMortonSound visualizer in C#


[简介]

本文演示快速傅立叶变换的使用,以及如何使用Windows GDI绘制一个近乎实时的时域、频域的可视化声音处理。

[背景]

当我在做一个项目的信号处理部分的时候,我发现很难找到一个用C#些的例子。这让我觉得应该自己写一个这样的类,希望对你有用。

声音的时域、频域(FFT)波形可视化绘制

[代码使用]

本文中的声音输入部分使用了Ianier Munoz开发的Wave类。
波形样本进一步地被AudioFrame类处理。

Code: 

    class AudioFrame
    {
        private Bitmap _canvasTimeDomain;
        private Bitmap _canvasFrequencyDomain;
        private double[] _waveLeft;
        private double[] _waveRight;
        private double[] _fftLeft;
        private double[] _fftRight;
        private SignalGenerator _signalGenerator;
        private bool _isTest = false;
        public AudioFrame(bool isTest)
        {
            _isTest = isTest;
        }

        /// 
        /// Process 16 bit sample
        /// 
        /// 
        public void Process(ref byte[] wave)
        {
            _waveLeft = new double[wave.Length / 4];
            _waveRight = new double[wave.Length / 4];
            if (_isTest == false)
            {
                // Split out channels from sample
                int h = 0;
                for (int i = 0; i < wave.Length; i += 4)
                {
                    _waveLeft[h] = (double)BitConverter.ToInt16(wave, i);
                    _waveRight[h] = (double)BitConverter.ToInt16(wave, i + 2);
                    h++;
                }
            }
            else
            {
                // Generate artificial sample for testing
                _signalGenerator = new SignalGenerator();
                _signalGenerator.SetWaveform("Sine");
                _signalGenerator.SetSamplingRate(44100);
                _signalGenerator.SetSamples(16384);
                _signalGenerator.SetFrequency(5000);
                _signalGenerator.SetAmplitude(32768);
                _waveLeft = _signalGenerator.GenerateSignal();
                _waveRight = _signalGenerator.GenerateSignal();
            }
            // Generate frequency domain data in decibels
            _fftLeft = FourierTransform.FFTDb(ref _waveLeft);
            _fftRight = FourierTransform.FFTDb(ref _waveRight);
        }

        /// 
        /// Render time domain to PictureBox
        /// 
        /// 
        public void RenderTimeDomain(ref PictureBox pictureBox)
        {
            // Set up for drawing
            _canvasTimeDomain = new Bitmap(pictureBox.Width, pictureBox.Height);
            Graphics offScreenDC = Graphics.FromImage(_canvasTimeDomain);
            SolidBrush brush = new System.Drawing.SolidBrush(Color.FromArgb(0, 0, 0));
            Pen pen = new System.Drawing.Pen(Color.WhiteSmoke);
            // Determine channnel boundries
            int width = _canvasTimeDomain.Width;
            int center = _canvasTimeDomain.Height / 2;
            int height = _canvasTimeDomain.Height;
            offScreenDC.DrawLine(pen, 0, center, width, center);
            int leftLeft = 0;
            int leftTop = 0;
            int leftRight = width;
            int leftBottom = center - 1;
            int rightLeft = 0;
            int rightTop = center + 1;
            int rightRight = width;
            int rightBottom = height;
            // Draw left channel
            double yCenterLeft = (leftBottom - leftTop) / 2;
            double yScaleLeft = 0.5 * (leftBottom - leftTop) / 32768;  // a 16 bit sample has values from -32768 to 32767
            int xPrevLeft = 0, yPrevLeft = 0;
            for (int xAxis = leftLeft; xAxis < leftRight; xAxis++)
            {
                int yAxis = (int)(yCenterLeft + (_waveLeft[_waveLeft.Length / (leftRight - leftLeft) * xAxis] * yScaleLeft));
                if (xAxis == 0)
                {
                    xPrevLeft = 0;
                    yPrevLeft = yAxis;
                }
                else
                {
                    pen.Color = Color.LimeGreen;
                    offScreenDC.DrawLine(pen, xPrevLeft, yPrevLeft, xAxis, yAxis);
                    xPrevLeft = xAxis;
                    yPrevLeft = yAxis;
                }
            }
            // Draw right channel
            int xCenterRight = rightTop + ((rightBottom - rightTop) / 2);
            double yScaleRight = 0.5 * (rightBottom - rightTop) / 32768;  // a 16 bit sample has values from -32768 to 32767
            int xPrevRight = 0, yPrevRight = 0;
            for (int xAxis = rightLeft; xAxis < rightRight; xAxis++)
            {
                int yAxis = (int)(xCenterRight + (_waveRight[_waveRight.Length / (rightRight - rightLeft) * xAxis] * yScaleRight));
                if (xAxis == 0)
                {
                    xPrevRight = 0;
                    yPrevRight = yAxis;
                }
                else
                {
                    pen.Color = Color.LimeGreen;
                    offScreenDC.DrawLine(pen, xPrevRight, yPrevRight, xAxis, yAxis);
                    xPrevRight = xAxis;
                    yPrevRight = yAxis;
                }
            }
            // Clean up
            pictureBox.Image = _canvasTimeDomain;
            offScreenDC.Dispose();
        }

        /// 
        /// Render frequency domain to PictureBox
        /// 
        /// 
        public void RenderFrequencyDomain(ref PictureBox pictureBox)
        {
            // Set up for drawing
            _canvasFrequencyDomain = new Bitmap(pictureBox.Width, pictureBox.Height);
            Graphics offScreenDC = Graphics.FromImage(_canvasFrequencyDomain);
            SolidBrush brush = new System.Drawing.SolidBrush(Color.FromArgb(0, 0, 0));
            Pen pen = new System.Drawing.Pen(Color.WhiteSmoke);
            // Determine channnel boundries
            int width = _canvasFrequencyDomain.Width;
            int center = _canvasFrequencyDomain.Height / 2;
            int height = _canvasFrequencyDomain.Height;
            offScreenDC.DrawLine(pen, 0, center, width, center);
            int leftLeft = 0;
            int leftTop = 0;
            int leftRight = width;
            int leftBottom = center - 1;
            int rightLeft = 0;
            int rightTop = center + 1;
            int rightRight = width;
            int rightBottom = height;
            // Draw left channel
            for (int xAxis = leftLeft; xAxis < leftRight; xAxis++)
            {
                double amplitude = (int)_fftLeft[(int)(((double)(_fftLeft.Length) / (double)(width)) * xAxis)];
                if (amplitude < 0) // Drop negative values
                    amplitude = 0;
                int yAxis = (int)(leftTop + ((leftBottom - leftTop) * amplitude) / 100);  // Arbitrary factor
                pen.Color = Color.FromArgb(0, 0, (int)amplitude % 255);
                offScreenDC.DrawLine(pen, xAxis, leftTop, xAxis, yAxis);
            }
            // Draw right channel
            for (int xAxis = rightLeft; xAxis < rightRight; xAxis++)
            {
                double amplitude = (int)_fftRight[(int)(((double)(_fftRight.Length) / (double)(width)) * xAxis)];
                if (amplitude < 0)
                    amplitude = 0;
                int yAxis = (int)(rightBottom - ((rightBottom - rightTop) * amplitude) / 100);
                pen.Color = Color.FromArgb(0, 0, (int)amplitude % 255);
                offScreenDC.DrawLine(pen, xAxis, rightBottom, xAxis, yAxis);
            }
            // Clean up
            pictureBox.Image = _canvasFrequencyDomain;
            offScreenDC.Dispose();
        }
    }
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值