【问题标题】:How to play an array of [Int16] audio samples from memory in Swift如何在 Swift 中从内存中播放 [Int16] 音频样本数组
【发布时间】:2020-07-12 17:53:42
【问题描述】:

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

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

测试:

  1. AVAudioPlayerNode example(无法开始工作,例如找不到输入设备、没有声音等)
  2. Buffer to wav data example(工作,但慢)

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)
}

【问题讨论】:

    标签: swift audio wav pcm audiobuffer


    【解决方案1】:

    快速浏览了 SDL 库及其 audio capabilities。似乎你可以只提供你想要的任何缓冲区类型,它就可以工作:

    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!
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多