0

尝试使用GME库为 Mac 构建游戏音乐播放器(NSF、SPC 等)。

我花了好几个小时在 SO 上测试这么多的解决方案和技巧,但似乎没有一个解决方案能很好地工作。我尝试了该AVAudioEngine/AVAudioPlayerNode/scheduleBuffer路线的许多变体,但由于它们都不起作用,我只是切换到将样本转换为 Wav 数据并从内存中播放。但是,这确实有效,从[Int16]to转换[UInt8](为了为波阵列创建数据)非常慢。至少对于更高的采样率和超过几秒钟的歌曲。下面的“清洁”代码示例。非常欢迎反馈和建议。

测试:

  1. AVAudioPlayerNode 示例(无法开始工作,例如,找不到输入设备、没有声音等)
  2. 缓冲区到 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)
}
4

1 回答 1

0

快速浏览了 SDL 库及其音频功能。似乎您可以提供所需的任何缓冲区类型,并且它可以正常工作:

var desiredSpec = SDL_AudioSpec()
desiredSpec.freq = 48000
desiredSpec.format = SDL_AudioFormat(AUDIO_S16) // Specify Int16 as format
desiredSpec.samples = 1024

var obtainedSpec = SDL_AudioSpec()

SDL_OpenAudio(&desiredSpec, &obtainedSpec)
SDL_QueueAudio(1, samples, Uint32(sampleCount)) // Samples and count from original post
SDL_PauseAudio(0) // Starts playing, virtually no lag!

仍然会感谢对原始帖子/问题的任何反馈,但就解决方案而言,我认为这与任何解决方案一样好(或更好)。

于 2020-07-13T09:32:02.847 回答