C#使用NAudio錄音并導(dǎo)出錄音數(shù)據(jù)
一、枚舉電腦錄音設(shè)備,指定設(shè)備錄音
1、使用Vs2019的Nuget包管理器安裝NAudio包
如圖所示:
2、創(chuàng)建錄音對(duì)象并指定錄音格式
// 錄音對(duì)象 WaveInEvent waveIn = new WaveInEvent(); int sampleRate = 48000; //采樣率 int channels = 2; //錄音通道數(shù) int bitsPerSample = 16; //位深 WaveFormat waveFormat = new WaveFormat(sampleRate, bitsPerSample, channels);
錄音格式最好和自己電腦一樣,我的電腦格式如下:
3、枚舉電腦的可用錄音設(shè)備,并指定
string RecordDeviceName = "麥克風(fēng)陣列"; //指定麥克風(fēng)陣列錄制 //枚舉可用錄音設(shè)備 for (int i = 0; i < WaveIn.DeviceCount; i++) { var capabilities = WaveIn.GetCapabilities(i); Console.WriteLine("DeviceIndex:{0},ProduceName:{1}", i, capabilities.ProductName); if (capabilities.ProductName.StartsWith(RecordDeviceName)) { Console.WriteLine("找到指定設(shè)備:{0}", RecordDeviceName); waveIn.DeviceNumber = i; //設(shè)置該設(shè)備為錄音設(shè)備 } } waveIn.WaveFormat = waveFormat; //設(shè)置錄音格式
運(yùn)行之后枚舉的效果如下:
二、獲取錄音數(shù)據(jù)
錄音數(shù)據(jù)主要是從waveIn.DataAvailable中獲取,我保存了兩種形式,一種是將錄音直接生成wav文件,另一種是將byte類型的錄音數(shù)據(jù)變換short數(shù)據(jù)導(dǎo)出到txt中。假如需要對(duì)錄音數(shù)據(jù)進(jìn)行實(shí)時(shí)處理就直接在DataAvailable這個(gè)回調(diào)函數(shù)中處理即可。
// 創(chuàng)建WaveFileWriter對(duì)象來(lái)保存錄音數(shù)據(jù) 路徑在bin文件下 WaveFileWriter writer = new WaveFileWriter("recorded.wav", waveFormat); //編寫器 StreamWriter mStreamWriter = new StreamWriter("Record.txt", false, new System.Text.UTF8Encoding(false)); // 設(shè)置錄音回調(diào)函數(shù) int bitIndex = bitsPerSample / 8; waveIn.DataAvailable += (sender, e) => { // 將錄音數(shù)據(jù)寫入文件 writer.Write(e.Buffer, 0, e.BytesRecorded); for (int i = 0; i < e.BytesRecorded/ bitIndex; i++) { //24bit,導(dǎo)出的數(shù)據(jù) //int sample = (int)((e.Buffer[i * bitIndex + 2] << 16) | (e.Buffer[i * bitIndex + 1] << 8) | e.Buffer[i * bitIndex]); //16bit 將兩個(gè)byte數(shù)據(jù)組合成一個(gè)short數(shù)據(jù) short sample = (short)((e.Buffer[i * bitIndex + 1] << 8) | e.Buffer[i * bitIndex]); mStreamWriter.Write("{0},", sample); } }; try { //嘗試打開錄音設(shè)備,如果設(shè)備支持設(shè)置的WaveFormat,則能夠成功打開 waveIn.StartRecording(); Console.WriteLine("開始錄音"); } catch (Exception ex) { Console.WriteLine($"錯(cuò)誤信息:{ex.Message}"); } Console.WriteLine("請(qǐng)按任意鍵結(jié)束錄音"); Console.ReadKey(); waveIn.StopRecording(); //停止錄音 writer.Close(); mStreamWriter.Close(); waveIn.Dispose();
三、驗(yàn)證錄音數(shù)據(jù)
可以將txt數(shù)據(jù)導(dǎo)入到matlab中生成wav文件,然后使用電腦播放器播放wav文件,聽一下是否錄制到聲音。
matlab代碼如下:
clear,clc,close all; %清除工作區(qū)變量 data=load("Record.txt"); %加載錄音數(shù)據(jù) left=data(1:2:end); %左聲道數(shù)據(jù) right=data(2:2:end); %右聲道數(shù)據(jù) doubleChannel=[left',right']; %組合成雙聲道 doubleChannel=[left',right']./max(abs(doubleChannel)); %錄音數(shù)據(jù)歸一化 audiowrite("test.wav",doubleChannel,48000); %以48000采樣率生成wav文件
四、完整代碼
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using NAudio.Wave; namespace record24位 { class Program { static void Main(string[] args) { // 錄音對(duì)象 WaveInEvent waveIn = new WaveInEvent(); int sampleRate = 48000; //采樣率 int channels = 2; //錄音通道數(shù) int bitsPerSample = 16; //位深 WaveFormat waveFormat = new WaveFormat(sampleRate, bitsPerSample, channels); string RecordDeviceName = "麥克風(fēng)陣列"; //枚舉可用錄音設(shè)備 for (int i = 0; i < WaveIn.DeviceCount; i++) { var capabilities = WaveIn.GetCapabilities(i); Console.WriteLine("DeviceIndex:{0},ProduceName:{1}", i, capabilities.ProductName); if (capabilities.ProductName.StartsWith(RecordDeviceName)) { Console.WriteLine("找到指定設(shè)備:{0}", RecordDeviceName); waveIn.DeviceNumber = i; //設(shè)置該設(shè)備為錄音設(shè)備 } } waveIn.WaveFormat = waveFormat; //設(shè)置錄音格式 // 創(chuàng)建WaveFileWriter對(duì)象來(lái)保存錄音數(shù)據(jù) 路徑在bin文件下 WaveFileWriter writer = new WaveFileWriter("recorded.wav", waveFormat); //編寫器 StreamWriter mStreamWriter = new StreamWriter("Record.txt", false, new System.Text.UTF8Encoding(false)); // 設(shè)置錄音回調(diào)函數(shù) int bitIndex = bitsPerSample / 8; waveIn.DataAvailable += (sender, e) => { // 將錄音數(shù)據(jù)寫入文件 writer.Write(e.Buffer, 0, e.BytesRecorded); for (int i = 0; i < e.BytesRecorded/ bitIndex; i++) { //24bit,導(dǎo)出的數(shù)據(jù) //int sample = (int)((e.Buffer[i * bitIndex + 2] << 16) | (e.Buffer[i * bitIndex + 1] << 8) | e.Buffer[i * bitIndex]); //16bit 將兩個(gè)byte數(shù)據(jù)組合成一個(gè)short數(shù)據(jù) short sample = (short)((e.Buffer[i * bitIndex + 1] << 8) | e.Buffer[i * bitIndex]); mStreamWriter.Write("{0},", sample); } }; try { //嘗試打開錄音設(shè)備,如果設(shè)備支持設(shè)置的WaveFormat,則能夠成功打開 waveIn.StartRecording(); Console.WriteLine("開始錄音"); } catch (Exception ex) { Console.WriteLine($"錯(cuò)誤信息:{ex.Message}"); } Console.WriteLine("請(qǐng)按任意鍵結(jié)束錄音"); Console.ReadKey(); waveIn.StopRecording(); //停止錄音 writer.Close(); mStreamWriter.Close(); waveIn.Dispose(); } } }
到此這篇關(guān)于C#使用NAudio錄音并導(dǎo)出錄音數(shù)據(jù)的文章就介紹到這了,更多相關(guān)C# NAudio錄音內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法
下面小編就為大家?guī)?lái)一篇C#中序列化實(shí)現(xiàn)深拷貝,實(shí)現(xiàn)DataGridView初始化刷新的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02C#實(shí)現(xiàn)利用泛型將DataSet轉(zhuǎn)為Model的方法
這篇文章主要介紹了C#實(shí)現(xiàn)利用泛型將DataSet轉(zhuǎn)為Model的方法,實(shí)例分析了C#泛型的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07C# TabControl手動(dòng)觸發(fā)DrawItem的實(shí)現(xiàn)
本文主要介紹了C# TabControl手動(dòng)觸發(fā)DrawItem的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#Process的OutputDataReceived事件不觸發(fā)問(wèn)題及解決
這篇文章主要介紹了C#Process的OutputDataReceived事件不觸發(fā)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼
這篇文章主要介紹了操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼,有需要的朋友可以參考一下2013-11-11