Swift 语音变声播放

编程入门 行业动态 更新时间:2024-10-13 20:18:46

Swift <a href=https://www.elefans.com/category/jswz/34/1769309.html style=语音变声播放"/>

Swift 语音变声播放

QQ里面的变声功能是不是很搞笑?想要实现这样的功能?其实很简单,只需要使用音频引擎(AVAudioEngine)来改变节点的速率(AVAudioUnitTimePitch)、调整回声(AVAudioUnitDistortion)和混响(AVAudioUnitReverb)的值就可以达到你想要的很多效果。

[TOC]

音频录制

初始化录音器

//初始化录音器
func setUpRecord() -> Void {
   //创建录音文件保存路径
   let url = self.getSavePath()
   //创建录音格式设置
   let settingDic = self.getAudioSetting()
   //创建录音机
   do{
       try audioRecorder = AVAudioRecorder(url: url, settings: settingDic)
       audioRecorder.delegate = self
       audioRecorder.isMeteringEnabled = true
       audioRecorder.prepareToRecord()
       print("成功初始化")
   }
   catch{
       print("初始化失败")
   }
}
复制代码

获取录音权限

//获取录音权限. 返回YES为无拒绝,NO为拒绝录音.
func canRecord() -> Bool {
   var canR = false
   if (UIDevice.current.systemVersion as NSString).floatValue >= 7.0 {
       let audioSession = AVAudioSession.sharedInstance()
       if (audioSession.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {
           AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
               
               canR = granted
               if granted {
                   print("granted")
               } else{
                   print("not granted")
               }
           })
           
       }
   }else{
       canR = true
   }
   
   return canR
}
复制代码

长按开始录音

//长按响应函数
func longGesAction(longGes:UILongPressGestureRecognizer) -> Void {
   switch longGes.state {
   case .began:
       NSLog("录音开始")
       //判断下是否授权使用麦克风
       if self.canRecord() {
           audioRecorder.record()
       }
       .....
复制代码

音频播放

创建AudioFile

AVAudioFile的初始化函数 init(forReading fileURL: URL) throws 后面跟着 throws 关键词,所以在调用是需要使用 Do Try Catch 来处理错误。

do {
  try audioFile = AVAudioFile(forReading: url)
} catch {
  showAlert(Alerts.AudioFileError, message: String(describing: error))
}
复制代码

节点速率、节距、回声、混响的调整

在改变节点数据的时候,你需要先申请一个音频引擎,然后将修改的东西使用 attach() 函数加入到引擎中。

速率、节距

let changeRatePitchNode = AVAudioUnitTimePitch()
//速率
changeRatePitchNode.rate = rate!
//节距
changeRatePitchNode.pitch = pitch!
复制代码

回声

let echoNode = AVAudioUnitDistortion()
echoNode.loadFactoryPreset(.multiEcho1)
复制代码

混响

let revrebNode = AVAudioUnitReverb()
revrebNode.loadFactoryPreset(.cathedral)
revrebNode.wetDryMix = 50
复制代码

做完这些过后,你还需要将这些节点用音频引擎的 connect(_ node1: AVAudioNode, to node2: AVAudioNode, format: AVAudioFormat?) 方法拼接起来。

拼接所有节点

在这里我写了一个多参数的函数来拼接

func connectAudioNodes(_ nodes: AVAudioNode...) {
   for x in 0..<nodes.count-1 {
       audioEngine.connect(nodes[x], to: nodes[x+1], format: audioFile.processingFormat)
   }
}

调用:
connectAudioNodes(audioPlayerNode,changeRatePitchNode,audioEngine.outputNode) //记得这个 audioEngine.outputNode 是必须要的,相当于上下文的结尾
复制代码

启动播放引擎

在这里我加了一个计时器来停止播放

//启动引擎 schedule to play and start the engine!
audioPlayerNode.stop()
audioPlayerNode.scheduleFile(audioFile, at: nil) { 
  var delayInSeconds :Double = 0.0 //延迟秒数
  if let lastRenderTime = self.audioPlayerNode.lastRenderTime, let playerTime = self.audioPlayerNode.playerTime(forNodeTime: lastRenderTime) {
      if rate != nil {
          delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime)/Double(self.audioFile.processingFormat.sampleRate)/Double(rate!)
      }else {
          delayInSeconds = Double(self.audioFile.length - playerTime.sampleTime)/Double(self.audioFile.processingFormat.sampleRate)
      }
  }
  //组装一个定时器在播放完成时调用stopAudio schedule a stop timer for when audio finishes playing
  self.stopTimer = Timer(timeInterval: delayInSeconds, target: self, selector: #selector(VideoPlayHelper.stopAudio), userInfo: nil, repeats: false)
  RunLoop.main.add(self.stopTimer, forMode: RunLoopMode.defaultRunLoopMode)
}
do {
  try audioEngine.start()
} catch {
  showAlert(Alerts.AudioEngineError, message: String(describing: error))
  return
}
audioPlayerNode.play()
复制代码

当然在停止的时候记得将 播放引擎、定时器、播放器都停止掉哟。

func stopAudio() {
   if audioPlayerNode != nil {
       audioPlayerNode.stop()
   }
   
   if stopTimer != nil {
       stopTimer.invalidate()
   }
   
   if audioEngine != nil {
       audioEngine.stop()
       audioEngine.reset()
   }
}
复制代码

Demo地址

GitHub地址 你如果觉得喜欢帮忙给个Star!谢谢!

转载于:

更多推荐

Swift 语音变声播放

本文发布于:2024-03-10 06:04:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语音   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!