使用最新版本的AudioKit在Swift,XCode 9.4.1中将测试设备的麦克风的音频录制到.caf文件时遇到问题。在一个简单的测试中,我通过AKBooster将音频直接从麦克风发送到输出,它可以正常工作,并且我可以听到来自扬声器的麦克风输入。我或多或少地关注this example,尽管还是使用了增强节点而不是振荡器。
以下是我的代码:
class MicrophoneHandler
{
var microphone : AKMicrophone!
var booster : AKBooster!
var mixer : AKMixer!
var recorder : AKNodeRecorder!
var file : AKAudioFile!
var player : AKAudioPlayer!
init()
{
setupMicrophone()
microphone = AKMicrophone()
booster = AKBooster(microphone) // Stereo amplifier for microphone
mixer = AKMixer(booster)
file = try! AKAudioFile() // File to store recorder output
player = try? AKAudioPlayer(file: file) // Player to play back recorded audio file
//player.looping = true
recorder = try? AKNodeRecorder(node: mixer, file: file)
try? recorder.record()
sleep(5)
let dur = String(format: "%0.3f seconds", recorder.recordedDuration)
print("Stopped. (\(dur) recorded)")
recorder.stop()
//file.exportAsynchronously(name: "Test", baseDir: .documents, exportFormat: .caf){ [weak self] _, _ in
//}
//player.play()
//AudioKit.output = player!
//try? AudioKit.start()
}
func setupMicrophone()
{
// Function to initialise microphone settings
// Adapted from AudioKit example code found here:
// https://audiokit.io/examples/MicrophoneAnalysis
AKSettings.bufferLength = .medium
AKSettings.ioBufferDuration = 0.002 // TODO experiment with this to control latency
do
{
try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP) // Set session type & allow streaming to Bluetooth devices
} catch
{
AKLog("Could not set session category.")
}
AKSettings.defaultToSpeaker = true // Output to speaker when audio input is enabled
}
}
我已经注释了导出代码,因为问题似乎不在这里。控制台显示以下内容:
AKMicrophone.swift:init():45:Mixer inputs 8
AKAudioPlayer.swift:updatePCMBuffer():533:AKAudioPlayer Warning: "BF848EC0-94F8-4E39-A211-784B001CED72.caf" is an empty file
2018-11-16 17:49:16.936169+0000 VoxBox[2258:6984570] Audio files cannot be non-interleaved. Ignoring setting AVLinearPCMIsNonInterleaved YES.
AKNodeRecorder.swift:record():104:AKNodeRecorder: recording
Stopped. (0.000 seconds recorded)
如您所见,由于某种原因,记录器似乎未记录到文件中。在我看来,我的代码应该
但是由于某种原因,没有任何记录。显然,我在此方面缺少任何东西或误解了AKNodeRecorder的工作方式。我已经阅读了尽可能多的关于类似主题的StackOverflow问题,深入研究了AudioKit文档,并从AudioKit网站上阅读了一些示例,但是似乎没有什么可以解决我的特殊问题。
任何帮助将不胜感激。