基本信息
源码名称:C#实时显示音频波形图
源码大小:0.43M
文件格式:.7z
开发语言:C#
更新时间:2019-05-31
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
使用wpf开发的的获取电脑音频,生成wav文件,并实时显示音频的波形图,参考Sound_Viewer编写

/* Copyright (C) 2007 Jeff Morton (jeffrey.raymond.morton@gmail.com)

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

using System;
using System.Drawing;
using System.Windows.Controls;
//using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Threading;

namespace AudioDemo
{
    class AudioFrame
    {
        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;
        }

        /// <summary>
        /// Process 16 bit sample
        /// </summary>
        /// <param name="wave"></param>
        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(2000);
                _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);
        }

        /// <summary>
        /// Render time domain to PictureBox
        /// 时域
        /// </summary>
        /// <param name="pictureBox"></param>
        public void RenderTimeDomain(ref Canvas canvas)
        {
            canvas.Children.Clear();
            // Determine channnel boundries
            int width = (int)canvas.Width;
            //单声道的时候center = (int)canvas.Height,双声道的时候center = (int)canvas.Height/2
            int center = (int)canvas.Height;
            int height = (int)canvas.Height;

            int leftLeft = 0;
            int leftTop = 0;
            int leftRight = width;
            int leftBottom = center - 1;

            // 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
                {
                    Line draw1 = new Line();
                    draw1.Stroke = Brushes.Red;//外宽颜色,在直线里为线颜色
                    draw1.StrokeThickness = 1;//线宽度
                    draw1.X1 = xPrevLeft;
                    draw1.Y1 = yPrevLeft;
                    draw1.X2 = xAxis;
                    draw1.Y2 = yAxis;
                    canvas.Children.Add(draw1);
                    xPrevLeft = xAxis;
                    yPrevLeft = yAxis;
                }
            }

            // Draw right channel

            //int rightLeft = 0;
            //int rightTop = center   1;
            //int rightRight = width;
            //int rightBottom = height;
            //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;

            //        Line draw2 = new Line();
            //        draw2.Stroke = Brushes.Red;//外宽颜色,在直线里为线颜色
            //        //mydrawline.Stroke = new SolidColorBrush(Color.FromArgb(0xFF, 0x5B, 0x9B, 0xD5));//自定义颜色则用这句
            //        draw2.StrokeThickness = 2;//线宽度
            //        draw2.X1 = xPrevLeft;
            //        draw2.Y1 = yPrevLeft;
            //        draw2.X2 = xAxis;
            //        draw2.Y2 = yAxis;
            //        //canvas.Children.Clear();
            //        canvas.Children.Add(draw2);
            //        //offScreenDC.DrawLine(pen, xPrevRight, yPrevRight, xAxis, yAxis);
            //        xPrevRight = xAxis;
            //        yPrevRight = yAxis;
            //    }
            //}

        }

        /// <summary>
        /// Render frequency domain to PictureBox
        /// 频域
        /// </summary>
        /// <param name="pictureBox"></param>
        public void RenderFrequencyDomain(ref Canvas canvas)
        {
            // Set up for drawing
            canvas.Children.Clear();
            // Determine channnel boundries
            int width = (int)canvas.Width;
            //单声道的时候center = (int)canvas.Height,双声道的时候center = (int)canvas.Height/2
            int center = (int)canvas.Height;
            int height = (int)canvas.Height;

            int leftLeft = 0;
            int leftTop = 0;
            int leftRight = width;
            int leftBottom = center - 1;

            // 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
                Line draw1 = new Line();
                draw1.Stroke = Brushes.Red;//外宽颜色,在直线里为线颜色
                draw1.StrokeThickness = 1;//线宽度
                //顶部显示
                //draw1.X1 = xAxis;
                //draw1.Y1 = leftTop;
                //draw1.X2 = xAxis;
                //draw1.Y2 = yAxis;

                //底部显示
                draw1.X1 = xAxis;
                draw1.Y1 = leftBottom;
                draw1.X2 = xAxis;
                draw1.Y2 = leftBottom - yAxis;
                canvas.Children.Add(draw1);
            }

            // Draw right channel
            //int rightLeft = 0;
            //int rightTop = center   1;
            //int rightRight = width;
            //int rightBottom = height;
            //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);
            //    Line draw2 = new Line();
            //    draw2.Stroke = Brushes.Red;//外宽颜色,在直线里为线颜色
            //    draw2.StrokeThickness = 1;//线宽度
            //    //底部显示
            //    draw2.X1 = xAxis;
            //    draw2.Y1 = rightBottom;
            //    draw2.X2 = xAxis;
            //    draw2.Y2 = yAxis;
            //    canvas.Children.Add(draw2);

            }

        }
    }
}