欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用NAudio錄音并導(dǎo)出錄音數(shù)據(jù)

 更新時(shí)間:2024年12月23日 15:11:26   作者:rbigbearr  
這篇文章主要為大家詳細(xì)介紹了C#如何使用NAudio實(shí)現(xiàn)錄音功能并導(dǎo)出錄音數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、枚舉電腦錄音設(shè)備,指定設(shè)備錄音

1、使用Vs2019的Nuget包管理器安裝NAudio包

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)文章

最新評(píng)論