如何在.Net Core中将两个wave文件合并为一个文件?

时间:2018-09-03 12:51:37

标签: c# .net-core opentk openal wave

我需要在.net核心中合并两个wave文件。所以我选择了OpenTK作为OpenAL的包装。

我试图合并每个样本和采样率相同的两个波形文件。

1)为此,我得到了这个example

2)设置2个字节的区域:

var sound_data1 = LoadWave(path1, FileMode.Open), out channels, out bits_per_sample, out sample_rate);

var sound_data2 = LoadWave(path2, FileMode.Open), out channels, out bits_per_sample, out sample_rate);

3)为每个字节求和并将其分配为2

for (int i = 0; i < sound_data1; i++)
{
result_sound_data[i] = (byte)((sound_data1[i] + sound_data2[i]) / 2);
}

4)然后:

AL.BufferData(buffer, GetSoundFormat(channels, bits_per_sample), result_sound_data, result_sound_data.Length, sample_rate);

AL.Source(source, ALSourcei.Buffer, buffer);
AL.SourcePlay(source);

最后,我的声音有些损坏,而不是混合信号。我该如何解决?

1 个答案:

答案 0 :(得分:1)

合并音频流显然基本上是在每个输入音频文件中取相应样本的总和。您可以研究this sample on codeproject的源代码。这段代码可能不是最干净的,但似乎可以完成工作(我测试过)。

除了处理WAV文件头之外,此处还描述了输入文件数组的实际合并逻辑:

// 8. Do the merging..
// The basic algorithm for doing the merging is as follows:
// while there is at least 1 sample remaining in any of the source files
//    sample = 0
//    for each source file
//       if the source file has any samples remaining
//          sample = sample + next available sample from the source file
//    sample = sample / # of source files
//    write the sample to the output file

对于8位样本,该代码样本如下实现:

while (SamplesRemain(scaledAudioFiles))
{
    byte sample = 0;
    for (var i = 0; i < scaledAudioFiles.GetLength(0); ++i)
    {
        if (scaledAudioFiles[i].NumSamplesRemaining > 0)
        {
            sample += scaledAudioFiles[i].GetNextSample_8bit();
        }
    }
    sample /= (byte)(scaledAudioFiles.GetLength(0));
    outputFile.AddSample_8bit(sample);
}

该示例中的代码与.Net Core完全兼容。

如果您只想获取该代码并合并一些.wav文件,则可以执行以下操作(仍然使用所涉及的示例):

    private static void Main(string[] args) => WAVFile.MergeAudioFiles(
            new[] { "file1.wav", "file2.wav", "file3.wav" },
            "result.wav",
            Path.GetTempPath()
        );