如何在 C# 中播放 .sf2 文件的声音?
我正在尝试用 C# 为我的最后一年项目编写一个音乐作曲家应用程序,但我找不到任何可以帮助我从 .sf2 文件播放声音的 API 或相关代码。
我能找到的最接近的是 MeltySynth。不过,作者并没有提供太多关于这个API的文档,所以我很难理解其中的命令。我只设法让这个 API 来播放 midi 文件,而不是播放我想要的 .sf2 文件中的相关音符。 GitHub 页面中有一个部分向用户展示了如何使用 API 通过 soundfont 文件中的音符来演奏旋律:
// Create the synthesizer.
var sampleRate = 44100;
var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);
// The length of a block is 0.1 sec.
var blockSize = sampleRate / 10;
// The entire output is 3 sec.
var blockCount = 30;
// Define the melody.
// A single row indicates the start timing, end timing, and pitch.
var data = new int[][]
{
new int[] { 5, 10, 60 },
new int[] { 10, 15, 64 },
new int[] { 15, 25, 67 }
};
// The output buffer.
var left = new float[blockSize * blockCount];
var right = new float[blockSize * blockCount];
for (var t = 0; t < blockCount; t++)
{
// Process the melody.
foreach (var row in data)
{
if (t == row[0]) synthesizer.NoteOn(0, row[2], 100);
if (t == row[1]) synthesizer.NoteOff(0, row[2]);
}
// Render the block.
var blockLeft = left.AsSpan(blockSize * t, blockSize);
var blockRight = right.AsSpan(blockSize * t, blockSize);
synthesizer.Render(blockLeft, blockRight);
}
我准确地复制了代码并尝试在简单的 C# CLI 类程序中运行它(并进行必要的更改,例如.sf2 文件目录当然)并且它不起作用,我不知道如何解决这个问题,程序运行并退出就好了。没有任何错误消息或抛出任何异常,我只是没有听到任何声音。
作者这里给出的示例代码在我的电脑上完美运行,并且设法用给定的 .sf2 文件的声音播放指定的 midi 文件,因此我怀疑我的音频驱动程序和硬件存在任何兼容性问题。
如果有人对如何访问 .sf2 文件内的声音文件有其他想法或对如何使用 MeltySynth 有清晰的了解,我将不胜感激。
I'm trying to code a music composer app for my final year project in C# and I can't find any APIs or relevant code that can help me play sound from .sf2 files.
The closest thing I can find is MeltySynth. However, the author didn't provide much documentation for this API, so it is really hard for me to understand the commands in it. I've only managed to get this API to play midi files, not play the relevant notes from the .sf2 file I want. There is a section in the GitHub page that shows users how to use the API to play melody with notes in the soundfont file:
// Create the synthesizer.
var sampleRate = 44100;
var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);
// The length of a block is 0.1 sec.
var blockSize = sampleRate / 10;
// The entire output is 3 sec.
var blockCount = 30;
// Define the melody.
// A single row indicates the start timing, end timing, and pitch.
var data = new int[][]
{
new int[] { 5, 10, 60 },
new int[] { 10, 15, 64 },
new int[] { 15, 25, 67 }
};
// The output buffer.
var left = new float[blockSize * blockCount];
var right = new float[blockSize * blockCount];
for (var t = 0; t < blockCount; t++)
{
// Process the melody.
foreach (var row in data)
{
if (t == row[0]) synthesizer.NoteOn(0, row[2], 100);
if (t == row[1]) synthesizer.NoteOff(0, row[2]);
}
// Render the block.
var blockLeft = left.AsSpan(blockSize * t, blockSize);
var blockRight = right.AsSpan(blockSize * t, blockSize);
synthesizer.Render(blockLeft, blockRight);
}
I copied the code exactly and tried running it in a simple C# CLI class program (and making the necessary changes like the .sf2 file directory of course) and it does not work, I have no clue on how to troubleshoot this problem, the program ran and exited just fine. There weren't any error messages or any exceptions thrown, I just don't hear any sound.
The sample code given by the author here ran perfectly on my computer and managed to play the specified midi file with the given .sf2 files' sounds, therefore I doubt there was any compatibility issues with my audio driver and hardware.
I'd appreciate it if someone has an alternate idea on how I can access the sound files inside of .sf2 files or a clear understanding on how to use MeltySynth.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我创建了一个像合成器的中型单行器样本中的MidiSample -provider:
为了演奏和弦,我使用了Naudio的Waveout:
I created a MidiSampleProvider like in the working MidiPlayer sample for the synthesizer:
In order to play a chord I used WaveOut of NAudio: