首页 > 解决方案 > AppleSequencer 导出 m4a 格式文件错误 使用 Audiokit 时?

问题描述

我是 swift 开发人员的新手,我现在正在尝试制作一些音乐应用程序。我一直在使用 audiokit 框架来解决一些音频问题。Audiokit 对我很有帮助。我使用 AppleSequencer 来初始化音频并完成正确的播放。

但现在我有一些问题。具体来说,当我使用engine.renderToFile导出文件时,我得到一个噪音文件。以下是主要代码部分,我也将相关代码上传到了github地址:https ://github.com/devlong/sequenceExport.git

import AudioKit
import AVFoundation

class SequenceExport: NSObject {
    static let shared = SequenceExport.init()
    private override init(){}
    
    var mixer = Mixer();
    var sequencer : AppleSequencer?
    var engine  = AudioEngine()
    var currentMidiSample:MIDISampler?
    var track : MusicTrackManager?
    
    func setCurrentSequencer() {

        let instrumentName = "edm-kit"
        currentMidiSample = MIDISampler.init(name: instrumentName)
        engine.output = currentMidiSample;
        
        
        do {
            try engine.start()
        } catch {
            Log("AudioKit did not start \(error)")
        }

        let directory = "Sounds/\(instrumentName)"
        let resetPath = Bundle.main.path(forResource:instrumentName, ofType: "aupreset", inDirectory: directory)
        do{
            try currentMidiSample!.loadPath(resetPath!)
        }catch let error  {
            print("************load resetPath error!!!!!!:\(error)")
        }

        sequencer = AppleSequencer(filename: "tracks")

        track = sequencer!.newTrack()
        for index in 0...4 {
            track!.add(
                 noteNumber:MIDINoteNumber(5+index),
                 velocity: 100, //调节单个音符声音
                position: Duration.init(beats: Double(index)),
                duration: Duration.init(seconds: 0.5))
        }
        sequencer!.setGlobalMIDIOutput(currentMidiSample!.midiIn)
        sequencer!.setTempo(120)
        sequencer!.setLength(Duration.init(beats: 4))
        sequencer!.enableLooping()
        sequencer!.play()
    }
    
    
    
    func exportM4a() {

        guard let outputURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("audio_file_new.m4a") else { return }
        
        print("outputURL !!!!!!:\(outputURL)")
        
        guard let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 0, interleaved: true) else {
            fatalError("Failed to create format")
        }
        let file = try! AVAudioFile(forWriting: outputURL, settings: format.settings)
        do{
            try engine.renderToFile(file, maximumFrameCount: 1_096, duration: 5) {
                self.sequencer!.play()
            } progress: { progress in
//                print("progress !!!!!!:\(progress)")
            }
        }catch  let error  {
            print("export !!!!!!:\(error)")
        }
        
    }
}

我试图找到很多方法。Audiokit 有办法导出 mid 格式,但这不是我的要求。我需要了解如何从 AppleSequencer 导出 m4a、mp3 和其他格式文件。谁能帮我?

如果有人有办法,希望能帮我修改github代码,帮助更多遇到此类问题的人。我已经将代码上传到地址:https ://github.com/devlong/sequenceExport.git

标签: audiokit

解决方案


我认为 AppleSequencer 不会在实时上下文之外运行,因此当您尝试使用 renderToFile 时不会发生任何事情。您可以使用 NodeRecorder 在播放曲目时对其进行录制,但这将是实时的,而不是超快的。


推荐阅读