gpt4 book ai didi

ios - 录制带有附加效果的音频

转载 作者:行者123 更新时间:2023-11-28 06:35:21 28 4
gpt4 key购买 nike

我已经阅读了很多帖子并尝试了很多不同的方法并最终提出了实际可行的解决方案这是来自其他堆栈溢出帖子的休闲功能播放,添加效果并将其保存到文件中,它起到了额外的效果并保存它,但问题是保存的 caf.文件不可读。我看到它在主目录上创建了文件,但无法播放。任何想法导致问题的原因将不胜感激

func playAudio(pitch : Float, rate: Float, reverb: Float, echo: Float) {
// Initialize variables
audioEngine = AVAudioEngine()
audioPlayerNode = AVAudioPlayerNode()
audioEngine.attachNode(audioPlayerNode)

// Setting the pitch
let pitchEffect = AVAudioUnitTimePitch()
pitchEffect.pitch = pitch
audioEngine.attachNode(pitchEffect)

// Setting the platback-rate
let playbackRateEffect = AVAudioUnitVarispeed()
playbackRateEffect.rate = rate
audioEngine.attachNode(playbackRateEffect)

// Setting the reverb effect
let reverbEffect = AVAudioUnitReverb()
reverbEffect.loadFactoryPreset(AVAudioUnitReverbPreset.Cathedral)
reverbEffect.wetDryMix = reverb
audioEngine.attachNode(reverbEffect)

// Setting the echo effect on a specific interval
let echoEffect = AVAudioUnitDelay()
echoEffect.delayTime = NSTimeInterval(echo)
audioEngine.attachNode(echoEffect)

// Chain all these up, ending with the output
audioEngine.connect(audioPlayerNode, to: playbackRateEffect, format: nil)
audioEngine.connect(playbackRateEffect, to: pitchEffect, format: nil)
audioEngine.connect(pitchEffect, to: reverbEffect, format: nil)
audioEngine.connect(reverbEffect, to: echoEffect, format: nil)
audioEngine.connect(echoEffect, to: audioEngine.mainMixerNode, format: nil)


// Good practice to stop before starting
audioPlayerNode.stop()

// Play the audio file
if(audioEngine != nil){
audioEngine?.stop()
}

audioPlayerNode.scheduleFile(audioFile, atTime: nil, completionHandler: {
print("Complete")

})

try! audioEngine.start()


let dirPaths: AnyObject = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
let tmpFileUrl: NSURL = NSURL.fileURLWithPath(dirPaths.stringByAppendingPathComponent("dddeffefsdctedSoundf23f13.caf"))
filteredOutputURL = tmpFileUrl

do{
print(dirPaths)
print(tmpFileUrl)

self.newAudio = try! AVAudioFile(forWriting: tmpFileUrl, settings:[
AVFormatIDKey: NSNumber(unsignedInt:kAudioFormatAppleLossless),
AVEncoderAudioQualityKey : AVAudioQuality.Medium.rawValue,
AVEncoderBitRateKey : 12800,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0
])

audioEngine.mainMixerNode.installTapOnBus(0, bufferSize: 2048, format: audioEngine.mainMixerNode.inputFormatForBus(0)) {
(buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in

print(self.newAudio.length)
print("=====================")
print(self.audioFile.length)
print("**************************")
if (self.newAudio.length) < (self.audioFile.length){//Let us know when to stop saving the file, otherwise saving infinitely

do{
//print(buffer)
try self.newAudio.writeFromBuffer(buffer)
}catch _{
print("Problem Writing Buffer")
}
}else{
self.audioEngine.mainMixerNode.removeTapOnBus(0)//if we dont remove it, will keep on tapping infinitely

}

}
}catch _{
print("Problem")
}

audioPlayerNode.play()

最佳答案

您需要刷新并关闭文件音频文件,以便正确写出 caf 文件。

看到 AVAudioFile 没有明确的方法来做到这一点,你唯一的希望似乎是在你完成后将 newAudio 设置为 nil 并希望它完成在 AVAudioFiledealloc 期间:

self.audioEngine.mainMixerNode.removeTapOnBus(0)
print("finish?")
self.newAudio = nil // hopefully flush & close, if there are no other strong references

关于ios - 录制带有附加效果的音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39238180/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com