尝试使用GME库为 Mac 构建游戏音乐播放器(NSF、SPC 等)。
我花了好几个小时在 SO 上测试这么多的解决方案和技巧,但似乎没有一个解决方案能很好地工作。我尝试了该AVAudioEngine/AVAudioPlayerNode/scheduleBuffer
路线的许多变体,但由于它们都不起作用,我只是切换到将样本转换为 Wav 数据并从内存中播放。但是,这确实有效,从[Int16]
to转换[UInt8]
(为了为波阵列创建数据)非常慢。至少对于更高的采样率和超过几秒钟的歌曲。下面的“清洁”代码示例。非常欢迎反馈和建议。
测试:
- AVAudioPlayerNode 示例(无法开始工作,例如,找不到输入设备、没有声音等)
- 缓冲区到 wav 数据示例(有效,但 sloooow)
override func viewDidLoad() {
super.viewDidLoad()
gme_type_list()
var emu = gme_new_emu( gme_nsf_type, 48000 ) // 48kHz
gme_open_file("path-to-file-on-disk.nsf", &emu, 48000) // 48kHz
let sampleCount: Int32 = 150 * 48000 * 2 // 150 = Lenght in sec, 48kHz
var output = Array<Int16>.init(repeating: 0, count: sampleCount)
gme_start_track(emu, 0)
gme_play(emu, sampleCount, &output) // Generates *sampleCount* samples in Int16 format
let samples = output.withUnsafeBufferPointer { buffer -> Array<Int16> in
var result = [Int16]()
for i in stride(from: buffer.startIndex, to: buffer.endIndex, by: 2) {
result.append(buffer[i])
}
return result
}
// Calls a slightly modified version of example 2 above
// (to support own samples, in Int16 rather than Float).
// Works! But "fillWave" method is soooo slow!
play(samples: samples)
}