如何在WP8上拆分wav文件?

时间:2014-06-22 16:59:14

标签: c# windows-phone-8 split wav

有没有办法在C#WP8中拆分wav文件?我想保存一个wav文件样本,例如从00:30开始到00:40结束。

也许使用某种流或缓冲区,但是我必须知道何时开始/完成将流复制到另一个wav文件。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

在我的博客上我发布了这个帖子,从这篇文章(http://csharp-tricks-en.blogspot.de/2011/03/read-in-wave-files.html)开始,阅读一个wave文件,接下来的2个帖子正在建立起来...希望有所帮助! 最好 奥利弗

答案 1 :(得分:0)

由于wav文件的标题可以从44(标准)到100多字节不等,因此您需要首先确定标题的确切大小,并在开始切割wav文件之前读取重要的元信息(s )。

因此,您需要了解有关您的wav文件的以下元数据,

  • 采样率,
  • 比特深度,
  • 频道数,
  • 音频数据格式(样本是PCM还是浮点数)
  • 标题大小。

在继续之前,我建议您首先阅读format of a wav file header,以便更好地了解代码的作用。

首先,我们需要获取元信息,

private void ReadMetaData(FileStream stream, out bool isFloatinfPoint, out int channelCount, out int sampleRate, out int bitDepth, out int headerSize)
{
    var headerBytes = new byte[200];

    // Read header bytes.
    stream.Position = 0;
    stream.Read(headerBytes, 0, 200);

    headerSize = new string(Encoding.ASCII.GetChars(headerBytes)).IndexOf("data") + 8;
    isFloatinfPoint = BitConverter.ToUInt16(new byte[] { headerBytes[20], headerBytes[21] }, 0) == 3 ? true : false;
    channelCount = BitConverter.ToUInt16(new byte[] { headerBytes[22] , headerBytes[23] }, 0);
    sampleRate = (int)BitConverter.ToUInt32(new byte[] { headerBytes[24], headerBytes[25], headerBytes[26], headerBytes[27] }, 0);
    bitDepth = BitConverter.ToUInt16(new byte[] { headerBytes[34], headerBytes[35] }, 0);
}

一旦我们获得了这些数据,我们就可以计算出我们需要开始和停止阅读文件的位置。要计算我们的开始和结束指数,

var startIndex = (int)(start.TotalSeconds * sampleRate * byteDepth * channelCount);
var endIndex = (int)(end.TotalSeconds * sampleRate * byteDepth * channelCount);

start& end将是TimeSpan,表示何时开始和停止裁剪。

我们现在可以使用新计算的信息从我们的文件中读取字节,如果您使用的是FileStream,则可以执行以下操作,

var newBytes = new byte[endIndex - startIndex];

myStream.Position = headerSize + startIndex; // Add headerSize to position to make sure we don't read the header.
myStream.Read(newBytes, 0, newBytes.Length);

您所要做的就是将wav标头与新提取的音频一起写入目标文件。所以,总而言之,你应该最终得到这样的东西,

private void CropWavFile(string inputFilePath, string outputFilePath, TimeSpan start, TimeSpan end)
{
    var stream = new FileStream(inputFilePath, FileMode.Open);
    var newStream = new FileStream(outputFilePath, FileMode.OpenOrCreate);
    var isFloatingPoint = false;
    var sampleRate = 0;
    var bitDepth = 0;
    var channelCount = 0;
    var headerSize = 0;

    // Get meta info
    ReadMetaData(stream, out isFloatingPoint, out channelCount, out sampleRate, out bitDepth, out headerSize);

    // Calculate where we need to start and stop reading.
    var startIndex = (int)(start.TotalSeconds * sampleRate * (bitDepth / 8) * channelCount);
    var endIndex = (int)(end.TotalSeconds * sampleRate * (bitDepth / 8) * channelCount);
    var bytesCount = endIndex - startIndex;
    var newBytes = new byte[bytesCount];

    // Read audio data.
    stream.Position = startIndex + headerSize;
    stream.Read(newBytes, 0, bytesCount);

    // Write the wav header and our newly extracted audio to the new wav file.
    WriteMetaData(newStream, isFloatingPoint, (ushort)channelCount, (ushort)bitDepth, sampleRate, newBytes.Length / (bitDepth / 8));
    newStream.Write(newBytes, 0, newBytes.Length);

    stream.Dispose();
    newStream.Dispose();
}

private void WriteMetaData(FileStream stream, bool isFloatingPoint, ushort channels, ushort bitDepth, int sampleRate, int totalSampleCount)
{
    stream.Position = 0;

    // RIFF header.
    // Chunk ID.
    stream.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4);

    // Chunk size.
    stream.Write(BitConverter.GetBytes(((bitDepth / 8) * totalSampleCount) + 36), 0, 4);

    // Format.
    stream.Write(Encoding.ASCII.GetBytes("WAVE"), 0, 4);



    // Sub-chunk 1.
    // Sub-chunk 1 ID.
    stream.Write(Encoding.ASCII.GetBytes("fmt "), 0, 4);

    // Sub-chunk 1 size.
    stream.Write(BitConverter.GetBytes(16), 0, 4);

    // Audio format (floating point (3) or PCM (1)). Any other format indicates compression.
    stream.Write(BitConverter.GetBytes((ushort)(isFloatingPoint ? 3 : 1)), 0, 2);

    // Channels.
    stream.Write(BitConverter.GetBytes(channels), 0, 2);

    // Sample rate.
    stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);

    // Bytes rate.
    stream.Write(BitConverter.GetBytes(sampleRate * channels * (bitDepth / 8)), 0, 4);

    // Block align.
    stream.Write(BitConverter.GetBytes((ushort)channels * (bitDepth / 8)), 0, 2);

    // Bits per sample.
    stream.Write(BitConverter.GetBytes(bitDepth), 0, 2);



    // Sub-chunk 2.
    // Sub-chunk 2 ID.
    stream.Write(Encoding.ASCII.GetBytes("data"), 0, 4);

    // Sub-chunk 2 size.
    stream.Write(BitConverter.GetBytes((bitDepth / 8) * totalSampleCount), 0, 4);
}

private void ReadMetaData(FileStream stream, out bool isFloatinfPoint, out int channelCount, out int sampleRate, out int bitDepth, out int headerSize)
{
    var headerBytes = new byte[200];

    // Read header bytes.
    stream.Position = 0;
    stream.Read(headerBytes, 0, 200);

    headerSize = new string(Encoding.ASCII.GetChars(headerBytes)).IndexOf("data") + 8;
    isFloatinfPoint = BitConverter.ToUInt16(new byte[] { headerBytes[20], headerBytes[21] }, 0) == 3 ? true : false;
    channelCount = BitConverter.ToUInt16(new byte[] { headerBytes[22] , headerBytes[23] }, 0);
    sampleRate = (int)BitConverter.ToUInt32(new byte[] { headerBytes[24], headerBytes[25], headerBytes[26], headerBytes[27] }, 0);
    bitDepth = BitConverter.ToUInt16(new byte[] { headerBytes[34], headerBytes[35] }, 0);
}