Naudio“TrimWavFile”功能没有正确修剪wav [英] Naudio “TrimWavFile” function is not trimming wav correctly
问题描述
我被困在试图修剪wav文件。我的问题是,当我将文件(32 kbps比特率)修剪为10秒剪辑时,它会将其修剪为00:01:18。完整的原始时间是1:37:13。
我已经尝试指定CutFromStart(00:0:0:0)的值并给出CutFromEnd(00: 1:37:03),但如上所述,产生的音频是01:18,因为我预计它将返回前10秒的wav剪辑...请帮助。
这是我正在使用的代码:
I am stuck in trying to trim a wav file. My problem is that when I trim the file (of 32 kbps bit rate) to a 10-second clip, it is trimming it into 00:01:18. The full, original time is 1:37:13.
I have tried specifying value of CutFromStart ( 00 : 0 : 0 : 0 ) and giving CutFromEnd (00:1:37:03), but as stated, the resulting audio is 01:18 where as i was expecting that it would return first 10 seconds of wav clip... Please help.
This is the code I am using:
public static void TrimWavFile(string input, string output, TimeSpan start, TimeSpan end)
{
using (WaveFileReader reader = new WaveFileReader(input))
{
using (WaveFileWriter writer = new WaveFileWriter(output, reader.WaveFormat))
{
int segement = reader.WaveFormat.AverageBytesPerSecond / 1000;
Console.WriteLine(""+segement);
int startPosition = (int)start.TotalMilliseconds * segement;
startPosition = startPosition - startPosition % reader.WaveFormat.BlockAlign;
int endBytes = (int)end.TotalMilliseconds * segement;
endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
int endPosition = (int)reader.Length - endBytes;
TrimWavFile(reader, writer, startPosition, endPosition);
}
}
}
private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPosition, int endPosition)
{
reader.Position = startPosition;
byte[] buffer = new byte[1024];
while (reader.Position < endPosition)
{
int segment = (int)(endPosition - reader.Position);
if (segment > 0)
{
int bytesToRead = Math.Min(segment, buffer.Length);
int bytesRead = reader.Read(buffer, 0, bytesToRead);
if (bytesRead > 0)
{
writer.WriteData(buffer, 0, bytesRead);
}
}
}
}
不必要代码块已删除
unnecesary code block removed
推荐答案
只需看看这一行:
Just look at this line:
int endPosition = (int)reader.Length - endBytes;
在此计算文件结束前 TimeSpan end
的位置 - 而不是在文件开头之后。
Here you calculate the position of TimeSpan end
before the end of the file - not after the beginning of the file.
这篇关于Naudio“TrimWavFile”功能没有正确修剪wav的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!