作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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;
}
}
最佳答案
关于c# - 如何用 C# 和 NAudio 覆盖两个 wav 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50900998/
我是一名优秀的程序员,十分优秀!