如何修剪WAV文件?
我需要修剪wp7中的.wav文件。我有一些代码,但它不工作。有谁知道如何修剪Windows Phone 7中的WAV文件?如何修剪WAV文件?
下面是我没有用的代码。
public IsolatedStorageFileStream TrimWavFile(string FileName, TimeSpan cutFromStart, TimeSpan cutFromEnd)
{
long start = Convert.ToInt64((16/8) * 16000 * cutFromStart.TotalSeconds);
long end = Convert.ToInt64((16/8) * 16000 * cutFromEnd.TotalSeconds);
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = storage.OpenFile(FileName as string, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
return TrimWavFile(stream, start, end);
}
private IsolatedStorageFileStream TrimWavFile(IsolatedStorageFileStream reader, long startPos, long endPos)
{
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream writer = new IsolatedStorageFileStream("t", FileMode.OpenOrCreate,storage);
reader.Position = startPos;
byte[] buffer = new byte[1024];
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);
}
}
Makewav.WriteHeader(writer, (int)writer.Length - 44, 1, 16000);
}
return writer;
}
我有循环中的波头,我把它移出循环,删除了writer.length
中的-44。
我只是不得不寻找流回到开始,它的工作。
首先,当你跳到你的startPos时,你看起来并不像你正在考虑wav标题,所以你的位置将被关闭。
其次,我猜可能是块对齐问题。当你尝试播放它时,它会以静态方式出现吗?从n音讯展示如何修剪WAV文件(http://mark-dot-net.blogspot.com/2009/09/trimming-wav-file-using-naudio.html)一些示例代码显示了这一点:
startPos = startPos - startPos % reader.WaveFormat.BlockAlign;
所以你只需要弄清楚您的块对齐的大小和计算的话这样的,最有可能的。与您的最终位置相同。
我不得不移动Makewav.WriteHeader(作家,(int)writer.Length,1,16000); 在while循环之外并且回溯到流的开始。这是修复 –
为了找到头信息,如采样率,NUM样品...等等,你可能需要阅读WAV文件规范https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
这个开源项目http://code.google.com/p/musicg/可以给你一个参考。它是用Java编写的,但修剪逻辑是相同的。
恭喜修复。如果可以,请确保接受您的答案,以便其他人可以从您的解决方案中学习。干杯〜 –