//by wgscd
//date:2022/11/7

UI:

 <Path Stroke="Red" Data="{Binding path}" RenderTransformOrigin="0.5,0.5">
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="180"/>
<TranslateTransform/>
</TransformGroup>
</Path.RenderTransform> </Path>

  

Code:

using NAudio.CoreAudioApi;
using NAudio.Wave.SampleProviders;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using NAudio.Wave.Compression;
using System.Runtime.Remoting.Channels;
using System.ComponentModel;
using System.Collections.ObjectModel;
using HandyControl.Collections;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows;
using System.CodeDom; namespace DouyuDanmu
{
//by wgscd
//date:2022/11/7
public class NAudioHelper
{ public static SampleData pathData = new SampleData();
public static void GetSampleData() {
// Redefine the capturer instance with a new instance of the LoopbackCapture class
WasapiLoopbackCapture _waveIn = new WasapiLoopbackCapture();
_waveIn.WaveFormat = new WaveFormat(16000, 16, 2); // Redefine the audio writer instance with the given configuration
//WaveFileWriter RecordedAudioWriter = new WaveFileWriter(outputFilePath, CaptureInstance.WaveFormat);
// When the capturer receives audio, start writing the buffer into the mentioned file
_waveIn.DataAvailable += (s, a) =>
{
// Write buffer into the file of the writer instance
AnalyzeVoice(a.Buffer); }; _waveIn.StartRecording(); } private static void SampleChannel_PreVolumeMeter(object sender, StreamVolumeEventArgs e)
{
Debug.Print(""+ e.MaxSampleValues[0]); } private static void waveIn_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
{
AnalyzeVoice(e.Buffer);
} static bool isbusy = false ;
/// <summary>
/// 语音分析
/// </summary>
/// <param name="buf"></param>
private async static void AnalyzeVoice(byte[] buffer)
{
if (isbusy) { return; } isbusy = true;
await Task.Delay(10); float[] sts = new float[buffer.Length / 2];
int outIndex = 0;
for (int n = 0; n < buffer.Length; n += 2)
{
sts[outIndex++] = BitConverter.ToInt16(buffer, n) / 32768f;
}
int channels = 20;//值越大提取的越稀疏
float value = 0;
List<Point> listPoint = new List<Point>(); float p=0;
float x = 0;
for (int n = 0; n < sts.Length; n += channels)
{ value = sts[n] > 0.0f ? sts[n] *400: 1f;//sts[n] * 400 是为了转化成有用的高度值
Debug.Print(""+value );//这个就是大概的波形图
p = value;
x += 2;
listPoint.Add(new Point(x,p));
}
UpdatePath(listPoint);//listPoint 更新一个类似贝塞尔曲线的效果
isbusy = false ;
} private static void UpdatePath(List<Point> points)
{ StringBuilder data = new StringBuilder("M");
data.AppendFormat("{0},{1} C", points[0].X, points[0].Y); for (int i = 1; i < points.Count; i++)
{
Point pre = new Point((points[i - 1].X + points[i].X) / 2, points[i - 1].Y); //控制点
Point next = new Point((points[i - 1].X + points[i].X) / 2, points[i].Y); //控制点
data.AppendFormat(" {0},{1} {2},{3} {4},{5}", pre.X, pre.Y, next.X, next.Y, points[i].X, points[i].Y);
}
// pathData.path = new Path { Stroke = Brushes.DodgerBlue, StrokeThickness = 1, Data = Geometry.Parse(data.ToString()) };
pathData.path = Geometry.Parse(data.ToString()); } } public class SampleData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
float _value = 0;
public float value
{
get
{ return _value;
}
set
{
_value = value;
NotifyPropertyChanged(nameof(value));
}
} Geometry _path ;
public Geometry path
{
get
{ return _path;
}
set
{
_path = value;
NotifyPropertyChanged(nameof(path));
}
} } }

  

C# 获取系统声卡音频数据,并绘制波形的更多相关文章

  1. Android 获取系统的联系人

    本文主要介绍android中怎样获取系统的联系人数据 首先打开模拟器 点击联系人图标按钮 说明系统联系人数据库是空的,打开File explorer,找到data/data下面的文件夹: 将conta ...

  2. C# NAudio录音和播放音频文件-实时绘制音频波形图(从音频流数据获取,而非设备获取)

    NAudio的录音和播放录音都有对应的类,我在使用Wav格式进行录音和播放录音时使用的类时WaveIn和WaveOut,这两个类是对功能的回调和一些事件触发. 在WaveIn和WaveOut之外还有对 ...

  3. iOS 获取系统相册数据(不是调系统的相册)

    Framework:AssetsLibrary.framework 主要目的是获取到系统相册的数据,并把系统相册里的照片显示出来. 1.创建一个新的项目: 2.将AssetsLibrary.frame ...

  4. DirectSound播放PCM(可播放实时采集的音频数据)

    前言 该篇整理的原始来源为http://blog.csdn.net/leixiaohua1020/article/details/40540147.非常感谢该博主的无私奉献,写了不少关于不同多媒体库的 ...

  5. 使用AudioTrack播放PCM音频数据(android)

    众所周知,Android的MediaPlayer包含了Audio和video的播放功能,在Android的界面上,Music和Video两个应用程序都是调用MediaPlayer实现的.MediaPl ...

  6. Android音频系统之音频框架

    1.1 音频框架 转载请注明,From LXS, http://blog.csdn.net/uiop78uiop78/article/details/8796492 Android的音频系统在很长一段 ...

  7. Android 音视频开发(二):使用 AudioRecord 采集音频数据并保存到文件

    版权声明:转载请说明出处:http://www.cnblogs.com/renhui/p/7457321.html 一.AudioRecord API详解 AudioRecord是Android系统提 ...

  8. RTMPdump(libRTMP) 源代码分析 9: 接收消息(Message)(接收视音频数据)

    ===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...

  9. 获取系统中所有进程&线程信息

    读书笔记--[计算机病毒解密与对抗] 目录: 遍历进程&线程程序 终止进程 获取进程信息 获取进程内模块信息 获取进程命令行参数 代码运行环境:Win7 x64 VS2012 Update3 ...

  10. 如何用 ajax 连接mysql数据库,并且获取从中返回的数据。ajax获取从mysql返回的数据。responseXML分别输出不同数据的方法。

    开讲前,先说下网上,大部分的关于这方面的博文或者其他什么的,就我自己的感觉,第一说得不详细,第二语言不能很好的被初学者了解. 我这篇博文的标题之所以用了三句,是为了方便其他人好查找: 这里介绍的方法有 ...

随机推荐

  1. 批处理-- 查询进程,杀进程,启动pythond程序,任务计划程序

    @echo off wmic process where caption="python.exe" get processid,commandline | findstr &quo ...

  2. EVE-NG全面教程带下载资源以及链接-原创

    有关于EVE-NG的教程和资源 https://forum.eve-ng.cn/forum.php 1. 经验丰富的专业同行较多 2. 官方论坛,权威可靠 3. 有免费且可靠的镜像可以下载 并且简单好 ...

  3. 鸿蒙NEXT开发案例:年龄计算

    ​ [引言] 本案例的目标是开发一款年龄计算器应用,该应用能够根据用户输入的出生日期,计算出用户的实际年龄.虚岁.星座.生肖等信息.同时,应用还将提供距离下次公历和农历生日的天数及星期等信息.为了实现 ...

  4. 鸿蒙NEXT开发案例:数字转中文大小写

    [引言] 本应用的主要功能是将用户输入的数字转换为中文的小写.大写及大写金额形式.用户可以在输入框中输入任意数字,点击"示例"按钮可以快速填充预设的数字,点击"清空&qu ...

  5. python数据结构的性能分析

    2.python数据结构的性能分析 一.引言 - 现在大家对 大O 算法和不同函数之间的差异有了了解.本节的目标是告诉你 Python 列表和字典操作的 大O 性能.然后我们将做一些基于时间的实验来说 ...

  6. TreeMap源码分析——深入分析(基于JDK1.6)

    TreeMap有Values.EntrySet.KeySet.PrivateEntryIterator.EntryIterator.ValueIterator.KeyIterator.Descendi ...

  7. Redis可视化管理工具之Redislive

    RedisLive是一款用Python编写基于WEB的Redis图形监控工具,也是一款实时监控Redis数据的开源软件,以WEB的形式展现出redis中的key的情况,实例数据等信息. RedisLi ...

  8. Django admin实现图片上传到腾讯云

    官网参考:https://docs.djangoproject.com/zh-hans/3.2/howto/custom-file-storage/ 当前业务需要使用django的admin后台进行数 ...

  9. phpredis和predis

    一般我们常用的扩展是phpredis和predis两个 phpredis, 它是用c写的php的高效扩展:https://github.com/phpredis/phpredis, predis, 它 ...

  10. 关于IMultiValueConverter的使用

    在前端向后端传递数据的过程中,因为涉及多个属性的调用,将数据绑定到CommandParameter,采用了多值转换器进行数据传递. class MultiBindingConverter : IMul ...