C#实现麦克风采集与播放
2015-07-10 11:44
561 查看
在网络聊天系统中,采集麦克风的声音并将其播放出来,是最基础的模块之一。本文我们就介绍如何快速地实现这个基础模块。
看看demo运行的效果图:
麦克风采集与播放Demo源码下载
一. 基础知识
有几个与声音采集和播放相关的专业术语必须要先了解一下,否则,后面的介绍将无法展开。语音采集指的是从麦克风采集音频数据,即声音样本转换成数字信号。其涉及到几个重要的参数:采样率、采样位数、声道数。 简单的来说: 采样率:即采样频率,就是在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运行的效果图:
麦克风采集与播放Demo源码下载
相关文章推荐
- C#部署安装,将用户安装路径记录下写入注册表,并启动
- C# 中使用 Obsolete 标志 代码过期
- C#的常量和变量以及其作用域和命名规范
- 【C#】程序运行时间
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
- 【C#学习杂记】Directory获取文件路径
- C#中的基本数据类型
- 浅谈C#中Md5和Sha1两种加密方式
- C#生成唯一不重复订单号
- C#注册表操作,根据键取值
- C# 目录(文件夹)复制实现
- C#分页
- C#实现bitmap图像矫正
- C#三层中的分页
- C# 编写Windows服务并设置为“允许服务与桌面交互”
- C# Parallel用法
- C#实现根据银行卡卡号判断银行名
- c# 正则匹配程序
- 5个最优秀的Java和C#代码转换工具
- 开始学C#语言