如何在 AudioKit 中渲染和导出音频?
Posted
技术标签:
【中文标题】如何在 AudioKit 中渲染和导出音频?【英文标题】:How to render and export audios in AudioKit? 【发布时间】:2018-08-24 21:51:55 【问题描述】:我正在构建一个关于音乐的项目,并且正在使用 AudioKit 框架。 AudioKit 非常有用,但我现在需要导出应用中创建的声音。
我正在使用 AKSequencer 来按顺序播放音符,并且我还在声音上应用了一些过滤器,例如混响。
我找到了一个示例代码here,它可以从另一个音频文件中导出音频,但这不是我需要的。实际上,我需要渲染笔记序列,包括过滤器,并将其导出给用户。
我的代码:
class Sequencer
let oscBank = AKOscillatorBank(waveform: AKTable(AKTableType.positiveReverseSawtooth))//1
let sequencer = AKSequencer()
init()
setup()
func setup()
// I instantiate a MIDI node with the oscillator bank to put it in the track output.
// It makes the oscillator's filters are recognized in the track output.
let midiNode = AKMIDINode(node: oscBank)
_ = sequencer.newTrack()
sequencer.tracks[0].setMIDIOutput(midiNode.midiIn)
generateSequence()
// Here I'm applying some filter, a reverb in this case.
let reverb = AKReverb(oscBank)
reverb.loadFactoryPreset(.plate)
// The Audiokit's output may be the last filter applyed. It is working to play.
AudioKit.output = AKMixer(reverb)
try? AudioKit.start()
func play()
sequencer.play()
func stop()
sequencer.stop()
/// Generates some melody (Sweet Child of Mine)
func generateSequence()
for _ in 0..<2
sequencer.tracks[0].add(noteNumber: 62, velocity: 127, position: AKDuration(beats: 0), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 74, velocity: 127, position: AKDuration(beats: 0.5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 1), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 67, velocity: 127, position: AKDuration(beats: 1.5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 79, velocity: 127, position: AKDuration(beats: 2), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 2.5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 78, velocity: 127, position: AKDuration(beats: 3), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 3.5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 62, velocity: 127, position: AKDuration(beats: 4), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 74, velocity: 127, position: AKDuration(beats: 4.5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 67, velocity: 127, position: AKDuration(beats: 5.5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 79, velocity: 127, position: AKDuration(beats: 6), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 6.5), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 78, velocity: 127, position: AKDuration(beats: 7), duration: AKDuration(beats: 0.5))
sequencer.tracks[0].add(noteNumber: 69, velocity: 127, position: AKDuration(beats: 7.5), duration: AKDuration(beats: 0.5))
// Here is my problem. I want to get the song and export it with the .wav extension exactly as it was played.
// This code is just one of my many attempts and it also does not works.
func saveFile()
guard let auxiliarPlayer = try? AKAudioPlayer(file: AKAudioFile(readFileName: "bass.wav")) else return
guard let outputURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("audio_file.m4a") else return
guard let output = try? AKAudioFile(forWriting: outputURL, settings: auxiliarPlayer.audioFile.fileFormat.settings) else return
try? AudioKit.renderToFile(output, seconds: sequencer.tracks[0].length, prerender:
self.sequencer.play()
)
有人可以帮忙吗?
【问题讨论】:
【参考方案1】:据我所知,由于时间问题,离线渲染不适用于 MIDI。基本上,只能渲染处理音频的东西,而不是依赖 MIDI 的东西。我相信这与 MIDI 时钟保持不变有关,并且无法虚拟地“加速”以匹配音频渲染速度。
【讨论】:
Aurelius,你建议我们怎么做?这个功能在我们的项目中是必不可少的。 您有什么建议吗? 有人必须创建一个基于音频样本而不是 midi 时间戳的音序器才能工作。 这个问题还没有解决吗?我们也需要在我们的项目中离线渲染 MIDI 序列。以上是关于如何在 AudioKit 中渲染和导出音频?的主要内容,如果未能解决你的问题,请参考以下文章