gpt4 book ai didi

c# - 如何用 C# 和 NAudio 覆盖两个 wav 文件?

转载 作者:行者123 更新时间:2023-12-02 23:03:57 25 4
gpt4 key购买 nike

我正在使用 NAudio 库来处理音频文件。我想要做的是将多个音频放在一起并从起点将它们组合起来。如果需要用图来说明;

A.wav
|-------------------------|
B.wav
|---------------|

我为这个过程写了下面的代码,但是这段代码以下面的方式组合了声音;
A.wav |--------------------| B.wav |--------------|

这是代码块;
public static void Concatenate(string outputFile, IEnumerable<string> sourceFiles)
{
outputFile = Path.Combine(@"C:\Users\umutg\OneDrive\Masaüstü\Muesyco\Combined\" + outputFile);

byte[] buffer = new byte[1024];
WaveFileWriter waveFileWriter = null;

try
{
foreach (string sourceFile in sourceFiles)
{
using (WaveFileReader reader = new WaveFileReader(sourceFile))
{
if (waveFileWriter == null)
{
// first time in create new Writer
waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
}
else
{
if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
{
throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
}
}

int read;
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
{
waveFileWriter.WriteData(buffer, 0, read);
}
}
}
}
finally
{
if (waveFileWriter != null)
{
waveFileWriter.Dispose();
}
}

}

编辑
public static string CreateMashup(List<string> files, string filename)
{
List<AudioFileReader> mixList = new List<AudioFileReader>();
// because there is no mash up with less than 2 files
if (files.Count() < 2)
{
throw new Exception("Not enough files selected!");
}

try
{
var mixer = new WaveMixerStream32
{
AutoStop = true
};

var outputFile = Path.Combine(@"C:\Users\umutg\OneDrive\Masaüstü\Muesyco\Combined\" + filename);

foreach (var file in files)
{
var filePath = Path.Combine(@"C:\Users\umutg\OneDrive\Masaüstü\Muesyco\Download\" + file);

if (File.Exists(filePath))
{
var reader = new AudioFileReader(filePath);
mixList.Add(reader);
}
}

var _mixer = new MixingSampleProvider(mixList);
WaveFileWriter.CreateWaveFile16(outputFile, _mixer);

return outputFile;
}
catch (Exception)
{
// TODO: handle exception
throw;
}
}

最佳答案

Found what you are looking for in their Documentation Here



更新:
你看看 this

关于c# - 如何用 C# 和 NAudio 覆盖两个 wav 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50900998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com