从 AVAudioEngine 总线抽头生成的文件中的音频与设备输出上播放的速度不同
Audio from a file generated from an AVAudioEngine bus tap has different speed than the one played on device output
我正在使用 AVAudioEngine
生成音频。然后我在引擎的 mainMixerNode
输出上安装一个分路器,它提供 AVAudioPCMBuffer
然后写入 MPEG4ACC AVAudioFile
。我遇到的问题是,在生成的 .m4a 文件上播放的音频速度比您在设备输出本身(扬声器、耳机)上听到的速度慢。
这是我正在实施的代码:
// Audio Format
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 2)
// Engine
var engine = AVAudioEngine()
// Two AVAudioNodes are hooked up to the AVAudioEngine
engine.connect(myAVAudioNode0, to: engine.mainMixerNode, format: audioFormat)
engine.connect(myAVAudioNode1, to: engine.mainMixerNode, format: audioFormat)
// Function to Write Audio to a File
func writeAudioToFile() {
// File to write
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let audioURL = documentsDirectory.appendingPathComponent("share.m4a")
// Format parameters
let sampleRate = Int(audioFormat!.sampleRate)
let channels = Int(audioFormat!.channelCount)
// Audio File settings
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: Int(audioFormat!.sampleRate),
AVNumberOfChannelsKey: Int(audioFormat!.channelCount),
AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue
]
// Audio File
var audioFile = AVAudioFile()
do {
audioFile = try AVAudioFile(forWriting: audioURL, settings: settings, commonFormat: .pcmFormatFloat32, interleaved: false)
}
catch {
print ("Failed to open Audio File For Writing: \(error.localizedDescription)")
}
// Install Tap on mainMixer
// Write into buffer and then write buffer into AAC file
engine.mainMixerNode.installTap(onBus: 0, bufferSize: 8192, format: nil, block: { (pcmBuffer, when) in
do {
try audioFile.write(from: pcmBuffer)
}
catch {
print("Failed to write Audio File: \(error.localizedDescription)")
}
})
}
我打印了AVAudioPCMBuffer
格式,发现它默认的采样率为48000Hz。但情况并非总是如此。首先,确定音频会话采样率并将该采样率用于音频格式:
let audioSession = AVAudioSession.sharedInstance()
let sampleRate = audioSession.sampleRate
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: sampleRate, channels: 2)
我正在使用 AVAudioEngine
生成音频。然后我在引擎的 mainMixerNode
输出上安装一个分路器,它提供 AVAudioPCMBuffer
然后写入 MPEG4ACC AVAudioFile
。我遇到的问题是,在生成的 .m4a 文件上播放的音频速度比您在设备输出本身(扬声器、耳机)上听到的速度慢。
这是我正在实施的代码:
// Audio Format
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 2)
// Engine
var engine = AVAudioEngine()
// Two AVAudioNodes are hooked up to the AVAudioEngine
engine.connect(myAVAudioNode0, to: engine.mainMixerNode, format: audioFormat)
engine.connect(myAVAudioNode1, to: engine.mainMixerNode, format: audioFormat)
// Function to Write Audio to a File
func writeAudioToFile() {
// File to write
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let audioURL = documentsDirectory.appendingPathComponent("share.m4a")
// Format parameters
let sampleRate = Int(audioFormat!.sampleRate)
let channels = Int(audioFormat!.channelCount)
// Audio File settings
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: Int(audioFormat!.sampleRate),
AVNumberOfChannelsKey: Int(audioFormat!.channelCount),
AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue
]
// Audio File
var audioFile = AVAudioFile()
do {
audioFile = try AVAudioFile(forWriting: audioURL, settings: settings, commonFormat: .pcmFormatFloat32, interleaved: false)
}
catch {
print ("Failed to open Audio File For Writing: \(error.localizedDescription)")
}
// Install Tap on mainMixer
// Write into buffer and then write buffer into AAC file
engine.mainMixerNode.installTap(onBus: 0, bufferSize: 8192, format: nil, block: { (pcmBuffer, when) in
do {
try audioFile.write(from: pcmBuffer)
}
catch {
print("Failed to write Audio File: \(error.localizedDescription)")
}
})
}
我打印了AVAudioPCMBuffer
格式,发现它默认的采样率为48000Hz。但情况并非总是如此。首先,确定音频会话采样率并将该采样率用于音频格式:
let audioSession = AVAudioSession.sharedInstance()
let sampleRate = audioSession.sampleRate
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: sampleRate, channels: 2)