C#使用NAudio錄音并導出錄音數(shù)據(jù)
一、枚舉電腦錄音設(shè)備,指定設(shè)備錄音
1、使用Vs2019的Nuget包管理器安裝NAudio包
如圖所示:

2、創(chuàng)建錄音對象并指定錄音格式
// 錄音對象 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 = "麥克風陣列"; //指定麥克風陣列錄制
//枚舉可用錄音設(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è)置錄音格式運行之后枚舉的效果如下:

二、獲取錄音數(shù)據(jù)
錄音數(shù)據(jù)主要是從waveIn.DataAvailable中獲取,我保存了兩種形式,一種是將錄音直接生成wav文件,另一種是將byte類型的錄音數(shù)據(jù)變換short數(shù)據(jù)導出到txt中。假如需要對錄音數(shù)據(jù)進行實時處理就直接在DataAvailable這個回調(diào)函數(shù)中處理即可。
// 創(chuàng)建WaveFileWriter對象來保存錄音數(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,導出的數(shù)據(jù)
//int sample = (int)((e.Buffer[i * bitIndex + 2] << 16) | (e.Buffer[i * bitIndex + 1] << 8) | e.Buffer[i * bitIndex]);
//16bit 將兩個byte數(shù)據(jù)組合成一個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($"錯誤信息:{ex.Message}");
}
Console.WriteLine("請按任意鍵結(jié)束錄音");
Console.ReadKey();
waveIn.StopRecording(); //停止錄音
writer.Close();
mStreamWriter.Close();
waveIn.Dispose();三、驗證錄音數(shù)據(jù)
可以將txt數(shù)據(jù)導入到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)
{
// 錄音對象
WaveInEvent waveIn = new WaveInEvent();
int sampleRate = 48000; //采樣率
int channels = 2; //錄音通道數(shù)
int bitsPerSample = 16; //位深
WaveFormat waveFormat = new WaveFormat(sampleRate, bitsPerSample, channels);
string RecordDeviceName = "麥克風陣列";
//枚舉可用錄音設(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對象來保存錄音數(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,導出的數(shù)據(jù)
//int sample = (int)((e.Buffer[i * bitIndex + 2] << 16) | (e.Buffer[i * bitIndex + 1] << 8) | e.Buffer[i * bitIndex]);
//16bit 將兩個byte數(shù)據(jù)組合成一個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($"錯誤信息:{ex.Message}");
}
Console.WriteLine("請按任意鍵結(jié)束錄音");
Console.ReadKey();
waveIn.StopRecording(); //停止錄音
writer.Close();
mStreamWriter.Close();
waveIn.Dispose();
}
}
}到此這篇關(guān)于C#使用NAudio錄音并導出錄音數(shù)據(jù)的文章就介紹到這了,更多相關(guān)C# NAudio錄音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中序列化實現(xiàn)深拷貝,實現(xiàn)DataGridView初始化刷新的方法
下面小編就為大家?guī)硪黄狢#中序列化實現(xiàn)深拷貝,實現(xiàn)DataGridView初始化刷新的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
C#實現(xiàn)利用泛型將DataSet轉(zhuǎn)為Model的方法
這篇文章主要介紹了C#實現(xiàn)利用泛型將DataSet轉(zhuǎn)為Model的方法,實例分析了C#泛型的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
C# TabControl手動觸發(fā)DrawItem的實現(xiàn)
本文主要介紹了C# TabControl手動觸發(fā)DrawItem的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
C#Process的OutputDataReceived事件不觸發(fā)問題及解決
這篇文章主要介紹了C#Process的OutputDataReceived事件不觸發(fā)問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼
這篇文章主要介紹了操作xml,將xml數(shù)據(jù)顯示到treeview的C#代碼,有需要的朋友可以參考一下2013-11-11

