C#使用NAudio錄音并導(dǎo)出錄音數(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 = "麥克風(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è)置錄音格式運行之后枚舉的效果如下:

二、獲取錄音數(shù)據(jù)
錄音數(shù)據(jù)主要是從waveIn.DataAvailable中獲取,我保存了兩種形式,一種是將錄音直接生成wav文件,另一種是將byte類型的錄音數(shù)據(jù)變換short數(shù)據(jù)導(dǎo)出到txt中。假如需要對錄音數(shù)據(jù)進(jìn)行實時處理就直接在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,導(dǎo)出的數(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ù)導(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)
{
// 錄音對象
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對象來保存錄音數(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 將兩個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錄音并導(dǎo)出錄音數(shù)據(jù)的文章就介紹到這了,更多相關(guān)C# NAudio錄音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity實現(xiàn)Flappy Bird游戲開發(fā)實戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了Unity實現(xiàn)Flappy Bird游戲開發(fā)實戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
C#獲取DataTable對象狀態(tài)DataRowState
這篇文章介紹了C#獲取DataTable對象狀態(tài)DataRowState的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
C#實現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法
這篇文章主要介紹了C#實現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法,可實現(xiàn)系統(tǒng)服務(wù)的啟動和停止功能,非常具有實用價值,需要的朋友可以參考下2015-04-04

