使用 NAudio 拆分 Wav 文件会从每个部分移除 1 秒
Splitting a Wav File using NAudio removes 1 second from each part
我正在使用 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 - endBytes;
TrimWavFile(reader, writer, startPos, endPos);
}
}
}
private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos)
{
reader.Position = startPos;
byte[] buffer = new byte[reader.WaveFormat.BlockAlign * 100];
while (reader.Position < endPos)
{
int bytesRequired = (int)(endPos - reader.Position);
if (bytesRequired > 0)
{
int bytesToRead = Math.Min(bytesRequired, buffer.Length);
int bytesRead = reader.Read(buffer, 0, bytesToRead);
if (bytesRead > 0)
{
writer.Write(buffer, 0, bytesRead);
}
}
}
}
这是分割Wav文件,但每一部分都比指定的间隔短1秒。我不知道这是代码问题还是库本身的问题。有没有人遇到过同样的问题?如果是这样,有人能解决吗?
编辑
这就是我发送给函数的内容:
TimeSpan cutFromStart = new TimeSpan(0, 0, 0);
TimeSpan cutFromEnd = new TimeSpan(0, 0, 0);
cutFromEnd = totalTime.Subtract(interval);
for (i = 0; i < numberOfParts; i++)
{
outPath = inpath + prefix + (i + 1).ToString() + ".wav";
TrimWavFile(inPath, outPath, cutFromStart, cutFromEnd);
cutFromStart = cutFromStart.Add(interval);
cutFromEnd = cutFromEnd.Subtract(interval);
}
我怀疑你的位置计算有问题。
但也许您会发现使用此技术更容易
using (var reader = new AudioFileReader(inPath))
{
reader.CurrentTime = startTime; // jump forward to the position we want to start from
WaveFileWriter.CreateWaveFile16(outPath, reader.Take(duration));
}
我正在使用 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 - endBytes;
TrimWavFile(reader, writer, startPos, endPos);
}
}
}
private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos)
{
reader.Position = startPos;
byte[] buffer = new byte[reader.WaveFormat.BlockAlign * 100];
while (reader.Position < endPos)
{
int bytesRequired = (int)(endPos - reader.Position);
if (bytesRequired > 0)
{
int bytesToRead = Math.Min(bytesRequired, buffer.Length);
int bytesRead = reader.Read(buffer, 0, bytesToRead);
if (bytesRead > 0)
{
writer.Write(buffer, 0, bytesRead);
}
}
}
}
这是分割Wav文件,但每一部分都比指定的间隔短1秒。我不知道这是代码问题还是库本身的问题。有没有人遇到过同样的问题?如果是这样,有人能解决吗?
编辑 这就是我发送给函数的内容:
TimeSpan cutFromStart = new TimeSpan(0, 0, 0);
TimeSpan cutFromEnd = new TimeSpan(0, 0, 0);
cutFromEnd = totalTime.Subtract(interval);
for (i = 0; i < numberOfParts; i++)
{
outPath = inpath + prefix + (i + 1).ToString() + ".wav";
TrimWavFile(inPath, outPath, cutFromStart, cutFromEnd);
cutFromStart = cutFromStart.Add(interval);
cutFromEnd = cutFromEnd.Subtract(interval);
}
我怀疑你的位置计算有问题。
但也许您会发现使用此技术更容易
using (var reader = new AudioFileReader(inPath))
{
reader.CurrentTime = startTime; // jump forward to the position we want to start from
WaveFileWriter.CreateWaveFile16(outPath, reader.Take(duration));
}