C#實現(xiàn)將音頻PCM數(shù)據(jù)封裝成wav文件
前言
之前實現(xiàn)了《C++ 將音頻PCM數(shù)據(jù)封裝成wav文件》,最近將其改成了C#版本。使用C#實現(xiàn)錄音功能時還是需要寫wav文件的,直接用C#實現(xiàn)也是比較簡單的,這樣可以免去了不必要的依賴。
一、如何實現(xiàn)
首先需要構(gòu)造wav頭部,wav文件音頻信息全部保存在頭部,我們要做的就是在PCM數(shù)據(jù)的前面加入wav頭,并且記錄PCM的相關(guān)參數(shù)。
1.定義頭結(jié)構(gòu)
只定義PCM格式的wav文件頭,包含3部分riff、format、data。需要使用[StructLayout(LayoutKind.Sequential)]描述結(jié)構(gòu)體,確保內(nèi)存連續(xù)。
WAV頭部
//WAV頭部結(jié)構(gòu)-PCM格式
[StructLayout(LayoutKind.Sequential)]
struct WavPCMFileHeader
{
RIFF riff=new RIFF();
Format format = new Format();
Data data = new Data();
public WavPCMFileHeader() { }
}RTTF部分
[StructLayout(LayoutKind.Sequential)]
struct RIFF
{
byte r = (byte)'R';
byte i = (byte)'I';
byte f = (byte)'F';
byte t = (byte)'F';
public uint fileLength = 0;
byte w = (byte)'W';
byte a = (byte)'A';
byte v = (byte)'V';
byte e = (byte)'E';
public RIFF() { }
}Format部分
[StructLayout(LayoutKind.Sequential)]
struct Format
{
byte f = (byte)'f';
byte m = (byte)'m';
byte t = (byte)'t';
byte s = (byte)' ';
public uint blockSize = 16;
public ushort formatTag=0;
public ushort channels = 0;
public uint samplesPerSec = 0;
public uint avgBytesPerSec = 0;
public ushort blockAlign = 0;
public ushort bitsPerSample = 0;
public Format() { }
}Data部分
[StructLayout(LayoutKind.Sequential)]
struct Data
{
byte d = (byte)'d';
byte a = (byte)'a';
byte t = (byte)'t';
byte a2 = (byte)'a';
public uint dataLength=0;
public Data() { }
}2.預留頭部空間
創(chuàng)建文件時預留頭部空間
_stream = File.Open(fileName, FileMode.Create); _stream!.Seek(Marshal.SizeOf<WavPCMFileHeader>(), SeekOrigin.Begin);
3.寫入PCM數(shù)據(jù)
寫入數(shù)據(jù)
_stream!.Write(data);
4.寫入頭部信息
關(guān)閉文件時,回到起始位置寫入頭部信息
//寫入頭部信息 _stream!.Seek(0, SeekOrigin.Begin); WavPCMFileHeader h = new WavPCMFileHeader(_channels, _sampleRate, _bitsPerSample, (uint)(_stream.Length - Marshal.SizeOf<WavPCMFileHeader>())); _stream!.Write(StructToBytes(h)); _stream!.Close(); _stream = null;
二、完整代碼
.net6.0WavWriter.cs
using System.Runtime.InteropServices;
/************************************************************************
* @Project: AC::WavWriter
* @Decription: wav文件寫入工具
* @Verision: v1.0.0.0
* @Author: Xin Nie
* @Create: 2023/10/8 09:27:00
* @LastUpdate: 2023/10/8 18:28:00
************************************************************************
* Copyright @ 2025. All rights reserved.
************************************************************************/
namespace AC
{
/// <summary>
/// wav寫入工具,目前只支持pcm格式
/// </summary>
public class WavWriter:IDisposable
{
ushort _channels;
uint _sampleRate;
ushort _bitsPerSample;
FileStream? _stream;
/// <summary>
/// 創(chuàng)建對象
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="channels">聲道數(shù)</param>
/// <param name="sampleRate">采樣率,單位hz</param>
/// <param name="bitsPerSample">位深</param>
public static WavWriter Create(string fileName, ushort channels, uint sampleRate, ushort bitsPerSample)
{
return new WavWriter(fileName, channels, sampleRate, bitsPerSample);
}
/// <summary>
/// 構(gòu)造方法
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="channels">聲道數(shù)</param>
/// <param name="sampleRate">采樣率,單位hz</param>
/// <param name="bitsPerSample">位深</param>
WavWriter(string fileName, ushort channels, uint sampleRate, ushort bitsPerSample)
{
_stream = File.Open(fileName, FileMode.Create);
_channels = channels;
_sampleRate = sampleRate;
_bitsPerSample = bitsPerSample;
_stream!.Seek(Marshal.SizeOf<WavPCMFileHeader>(), SeekOrigin.Begin);
}
/// <summary>
/// 寫入PCM數(shù)據(jù)
/// </summary>
/// <param name="data">PCM數(shù)據(jù)</param>
public void Write(byte[] data)
{
_stream!.Write(data);
}
/// <summary>
/// 寫入PCM數(shù)據(jù)
/// </summary>
/// <param name="stream">PCM數(shù)據(jù)</param>
public void Write(Stream stream)
{
stream.CopyTo(_stream!);
}
/// <summary>
/// 關(guān)閉文件
/// </summary>
public void Close()
{
//寫入頭部信息
_stream!.Seek(0, SeekOrigin.Begin);
WavPCMFileHeader h = new WavPCMFileHeader(_channels, _sampleRate, _bitsPerSample, (uint)(_stream.Length - Marshal.SizeOf<WavPCMFileHeader>()));
_stream!.Write(StructToBytes(h));
_stream!.Close();
_stream = null;
}
public void Dispose()
{
Close();
}
static byte[] StructToBytes<T>(T obj)
{
int size = Marshal.SizeOf(typeof(T));
IntPtr bufferPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(obj!, bufferPtr, false);
byte[] bytes = new byte[size];
Marshal.Copy(bufferPtr, bytes, 0, size);
return bytes;
}
catch (Exception ex)
{
throw new Exception("Error in StructToBytes ! " + ex.Message);
}
finally
{
Marshal.FreeHGlobal(bufferPtr);
}
}
}
//WAV頭部結(jié)構(gòu)-PCM格式
[StructLayout(LayoutKind.Sequential)]
struct WavPCMFileHeader
{
[StructLayout(LayoutKind.Sequential)]
struct RIFF
{
byte r = (byte)'R';
byte i = (byte)'I';
byte f = (byte)'F';
byte t = (byte)'F';
public uint fileLength = 0;
byte w = (byte)'W';
byte a = (byte)'A';
byte v = (byte)'V';
byte e = (byte)'E';
public RIFF() { }
}
[StructLayout(LayoutKind.Sequential)]
struct Format
{
byte f = (byte)'f';
byte m = (byte)'m';
byte t = (byte)'t';
byte s = (byte)' ';
public uint blockSize = 16;
public ushort formatTag=0;
public ushort channels = 0;
public uint samplesPerSec = 0;
public uint avgBytesPerSec = 0;
public ushort blockAlign = 0;
public ushort bitsPerSample = 0;
public Format() { }
}
[StructLayout(LayoutKind.Sequential)]
struct Data
{
byte d = (byte)'d';
byte a = (byte)'a';
byte t = (byte)'t';
byte a2 = (byte)'a';
public uint dataLength=0;
public Data() { }
}
RIFF riff=new RIFF();
Format format = new Format();
Data data = new Data();
public WavPCMFileHeader() { }
public WavPCMFileHeader(ushort nCh, uint nSampleRate, ushort bitsPerSample, uint dataSize)
{
riff.fileLength = (uint)(36 + dataSize);
format.formatTag = 1;
format.channels = nCh;
format.samplesPerSec = nSampleRate;
format.avgBytesPerSec = nSampleRate * nCh * bitsPerSample / 8;
format.blockAlign = (ushort)(nCh * bitsPerSample / 8);
format.bitsPerSample = bitsPerSample;
data.dataLength = dataSize;
}
};
}三、使用示例
using AC;
try
{
using (var ww = WavWriter.Create("test.wav", 2, 44100, 16))
{
byte[]data;
//獲取PCM數(shù)據(jù)
//略
//獲取PCM數(shù)據(jù)-end
//寫入PCM數(shù)據(jù)
ww.Write(data);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}總結(jié)
PCM封裝成wav還是相對較簡單的,只要了解wav頭結(jié)構(gòu),然后自定義其頭結(jié)構(gòu),然后再進行一定的測試,就可以實現(xiàn)這樣一個功能。
以上就是C#實現(xiàn)將音頻PCM數(shù)據(jù)封裝成wav文件的詳細內(nèi)容,更多關(guān)于C# PCM數(shù)據(jù)封裝成wav的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# InitializeComponent()方法案例詳解
這篇文章主要介紹了C# InitializeComponent()方法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
C#調(diào)用C++動態(tài)庫接口函數(shù)和回調(diào)函數(shù)方法
這篇文章主要介紹了C#調(diào)用C++動態(tài)庫接口函數(shù)和回調(diào)函數(shù)方法,通過C++端編寫接口展開內(nèi)容,文章介紹詳細具有一定的參考價值,需要的小伙伴可以參考一下2022-03-03

