C#实现麦克风採集与播放
在网络聊天系统中。採集麦克风的声音并将其播放出来。是最基础的模块之中的一个。本文我们就介绍怎样高速地实现这个基础模块。
一. 基础知识
有几个与声音採集和播放相关的专业术语必需要先了解一下,否则。后面的介绍将无法展开。语音採集指的是从麦克风採集音频数据。即声音样本转换成数字信号。
其涉及到几个重要的參数:採样率、採样位数、声道数。
简单的来说:
採样率:即採样频率,就是在1秒内进行採集动作的次数。
採样位数:又叫採样深度。就是每次採集动作得到的数据长度,即使用多少个bit来记录一个样本。
声道数:通常是单声道或双声道(立体声)。普通的麦克风採集差点儿都是单声道的。
这样。1秒钟採集得到的声音数据的大小为(单位byte):(採样频率×採样位数×声道数×时间)/8。
音频帧:通常一个音频帧的时长为10ms,即每10ms的数据构成一个音频帧。假设:採样率16k、採样位数16bit、声道数1,那么一个10ms的音频帧的大小为:(16000*16*1*0.01)/8 = 320 字节。计算式中的0.01为秒。即10ms
二. 怎样採集、播放?
假设直接基于底层的DirectX来进行麦克风的採集与播放。那将是十分繁琐的。
好在我们有现成的组件来完毕这个工作。MCapture 用于採集硬件设备(如麦克风、摄像头、声卡、屏幕等)。MPlayer 用于播放採集到的数据。
1.採集麦克风
MCapture提供了IMicrophoneCapturer。用于採集麦克风输入的声音。其每隔20ms触发一次AudioCaptured事件,通过事件的參数byte[]暴露这20ms採集得到的数据。
IMicrophoneCapturer 相关採集參数的值是这种:
採样频率:16000,採样位数:16bit。声道数:1。
所以,依照上面的公式进行计算,我们能够得到AudioCaptured事件的參数byte[]的长度为640。
2. 播放声音数据
MPlayer提供了IAudioPlayer,用于播放声音数据。在创建IAudioPlayer实例时,要正确的设置採样频率、採样位数、声道数这些參数的值。假设它们与即将要播放的声音数据的特征不一致,播放将出现错误。
我们在拿到MCapture採集的声音数据后,将其提交给IAudioPlayer的Play方法进行播放就可以。
三.Demo实现
在有了前面的介绍作为基础后,接下来实现麦克风的採集和播放就相当简单了。在接下来的demo中。不仅演示了播放从麦克风採集到的声音,并且多加了一个功能。就是直接播放wav声音文件,这些实现都是相当简单的。
public partial class Form1 : Form { private IAudioPlayer audioPlayer; private IMicrophoneCapturer microphoneCapturer; public Form1() { InitializeComponent(); } private void button_mic_Click(object sender, EventArgs e) { try { this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(int.Parse(this.textBox_mic.Text)); this.microphoneCapturer.AudioCaptured += new ESBasic.CbGeneric<byte[]>(microphoneCapturer_AudioCaptured); this.audioPlayer = PlayerFactory.CreateAudioPlayer(int.Parse(this.textBox_speaker.Text), 16000, 1, 16, 2); this.microphoneCapturer.Start(); this.label_msg.Text = "正在採集麦克风,并播放 . . ."; this.label_msg.Visible = true; this.button_wav.Enabled = false; this.button_mic.Enabled = false; this.button_stop.Enabled = true; } catch (Exception ee) { MessageBox.Show(ee.Message); } } void microphoneCapturer_AudioCaptured(byte[] audioData) { if (this.audioPlayer != null) { this.audioPlayer.Play(audioData); } } private void button_wav_Click(object sender, EventArgs e) { try { string path = ESBasic.Helpers.FileHelper.GetFileToOpen2("请选择要播放的wav文件", AppDomain.CurrentDomain.BaseDirectory, ".wav"); if (path == null) { return; } AudioInformation info = PlayerFactory.ParseWaveFile(path); if (info.FormatTag != (int)WaveFormats.Pcm) { MessageBox.Show("只支持PCM编码方式的语音数据!"); return; } int secs = info.GetTimeInMsecs() / 1000; //声音数据的播放时长 this.audioPlayer = PlayerFactory.CreateAudioPlayer(int.Parse(this.textBox_speaker.Text), info.SampleRate, info.ChannelCount, info.BitsNumber, secs + 1); this.audioPlayer.Play(info.AudioData); this.label_msg.Text = "正在播放wav文件 . . ."; this.label_msg.Visible = true; this.button_wav.Enabled = false; this.button_mic.Enabled = false; this.button_stop.Enabled = true; } catch (Exception ee) { MessageBox.Show(ee.Message); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (this.microphoneCapturer != null) { this.microphoneCapturer.Stop(); this.microphoneCapturer.Dispose(); this.microphoneCapturer = null; } if (this.audioPlayer != null) { this.audioPlayer.Dispose(); this.audioPlayer = null; } } private void button_stop_Click(object sender, EventArgs e) { if (this.audioPlayer == null) { return; } if (this.microphoneCapturer != null) { this.microphoneCapturer.Stop(); this.microphoneCapturer.Dispose(); this.microphoneCapturer = null; } this.audioPlayer.Clear(); this.audioPlayer.Dispose(); this.audioPlayer = null; this.label_msg.Visible = false; this.button_wav.Enabled = true; this.button_mic.Enabled = true; this.button_stop.Enabled = false; } }
看看demo执行的效果图:
posted on 2017-04-24 17:17 slgkaifa 阅读(10185) 评论(1) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决