我正在使用 NAudio 将 Wav 文件分成相等间隔的多个部分。我正在使用 Mark Heath 在 Sound Code 上提供的代码版本:
public static void TrimWavFile(string inPath, string outPath, TimeSpan cutFromStart, TimeSpan cutFromEnd)
{
using (WaveFileReader reader = new WaveFileReader(inPath))
{
using (WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat))
{
//int bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;
float bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000f;
int startPos = (int)cutFromStart.TotalMilliseconds * (int)bytesPerMillisecond;
startPos = startPos - startPos % reader.WaveFormat.BlockAlign;
int endBytes = (int)cutFromEnd.TotalMilliseconds * (int)bytesPerMillisecond;
endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
int endPos = (int)reader.Length …Run Code Online (Sandbox Code Playgroud)