实例介绍
【实例简介】使用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); } } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)