gpt4 book ai didi

iphone - 更改 "sounder"的音频音高

转载 作者:行者123 更新时间:2023-11-28 07:47:19 26 4
gpt4 key购买 nike

编辑:在底部添加了新代码。

我在编码方面还很陌生,所以我首先创建了一个音板应用程序。我已经能够使用“速率”命令来减慢或加快音频,但现在我也想改变音调。理想情况下,我想要两个开关。一种加速音频并提高音调,一种减慢和降低音调,如果它们都关闭,则播放正常。下面是我的代码,它适用于除改变音高之外的所有内容。非常感谢任何输入。作为引用,sass 很慢,chip 很快。

import UIKit
import AVFoundation

var Sounder1 = AVAudioPlayer()
var Sounder2 = AVAudioPlayer()

let sassFloat: Float = 0.5
let myInt = Int(sassFloat)

let chipFloat: Float = 3.0
let myInt2 = Int(chipFloat)

class ViewController: UIViewController {

@IBOutlet weak var sassSwitch: UISwitch!

@IBOutlet weak var chipSwitch: UISwitch!

override func viewDidLoad() {
super.viewDidLoad()

do {
Sounder1 = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Sound1", ofType: "mp3")!))
Sounder1.prepareToPlay()
Sounder1.enableRate = true
}catch{

}

do {
Sounder2 = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Sound2", ofType: "wav")!))
Sounder2.prepareToPlay()
Sounder2.enableRate = true
}catch{

}
// Do any additional setup after loading the view, typically from a nib.
}

@IBAction func sassAction(_ sender: UISwitch) {
chipSwitch.setOn(false, animated: true)
}

@IBAction func chipAction(_ sender: UISwitch) {
sassSwitch.setOn(false, animated: true)
}

@IBAction func play(_ sender: Any) {
if sassSwitch.isOn {
Sounder1.rate = sassFloat
Sounder1.play()
} else if chipSwitch.isOn {
Sounder1.rate = chipFloat
Sounder1.play()
} else {
Sounder1.rate = 1.0
Sounder1.play()
}

}

@IBAction func play2(_ sender: Any) {
if sassSwitch.isOn {
Sounder2.rate = sassFloat
Sounder2.play()
} else if chipSwitch.isOn {
Sounder2.rate = chipFloat
Sounder2.play()
} else {
Sounder2.rate = 1.0
Sounder2.play()
}
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

这是我根据@ericl 的建议编写的新代码。我仍然需要 reset() 音频引擎,但我也有几个问题。1) if else if else 语句在这种情况下真的有效吗?2) 我在哪里添加 reset() 语句。3) 是否需要在每次播放声音后分离节点?还是只是重置?

class ViewController: UIViewController {

@IBOutlet weak var sassSwitch: UISwitch!
@IBOutlet weak var chipSwitch: UISwitch!

@IBAction func sassAction(_ sender: UISwitch) {
chipSwitch.setOn(false, animated: true)
}
@IBAction func chipSwitch(_ sender: UISwitch) {
sassSwitch.setOn(false, animated: true)
}


///Playback Engine
private let audioEngine = AVAudioEngine()

///Player's Nodes
private let pitchPlayer = AVAudioPlayerNode()
private let timePitch = AVAudioUnitTimePitch()

///Audio Files to be played
private var audioFile1 = AVAudioFile()
private var audioFile2 = AVAudioFile()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

if let filePath = Bundle.main.path(forResource: "PeteNope", ofType:
"mp3") {
let filePathURL = URL(fileURLWithPath: filePath)

setPlayerFile(filePathURL)

}

if let filePath2 = Bundle.main.path(forResource: "Law_WOW", ofType:
"mp3") {
let filePath2URL = URL(fileURLWithPath: filePath2)

setPlayerFile2(filePath2URL)

}
}

private func setPlayerFile(_ fileURL: URL) {
do {
let file = try AVAudioFile(forReading: fileURL)

self.audioFile1 = file


} catch {
fatalError("Could not create AVAudioFile instance. error: \(error).")
}
}

private func setPlayerFile2(_ fileURL: URL) {
do {
let file = try AVAudioFile(forReading: fileURL)

self.audioFile2 = file


} catch {
fatalError("Could not create AVAudioFile instance. error: \(error).")
}
}


@IBAction func sound1Play(_ sender: UIButton) {
if sassSwitch.isOn {
timePitch.pitch = -300
timePitch.rate = 0.5
audioEngine.attach(pitchPlayer)
audioEngine.attach(timePitch)

audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile1.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile1.processingFormat)
pitchPlayer.scheduleFile(audioFile1, at: nil, completionHandler: nil)

// Start the engine.
do {
try audioEngine.start()
} catch {
fatalError("Could not start engine. error: \(error).")
}

pitchPlayer.play()

} else if chipSwitch.isOn {
timePitch.pitch = +500
timePitch.rate = 2.0
audioEngine.attach(pitchPlayer)
audioEngine.attach(timePitch)

audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile1.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile1.processingFormat)
pitchPlayer.scheduleFile(audioFile1, at: nil, completionHandler: nil)

// Start the engine.
do {
try audioEngine.start()
} catch {
fatalError("Could not start engine. error: \(error).")
}

pitchPlayer.play()

} else {
timePitch.pitch = +0
timePitch.rate = 1.0
audioEngine.attach(pitchPlayer)
audioEngine.attach(timePitch)

audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile1.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile1.processingFormat)
pitchPlayer.scheduleFile(audioFile1, at: nil, completionHandler: nil)

// Start the engine.
do {
try audioEngine.start()
} catch {
fatalError("Could not start engine. error: \(error).")
}
pitchPlayer.play()
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

您将使用 AVAudioEngineAVAudioPlayerNode 附加到 AVAudioUnitTimePitch 效果。我检查了苹果的 AudioUnitV3Example示例代码并从中拼凑出一个工作示例。你会想要实现更多的状态和错误处理,你可以在那里找到。无论如何,这里是:

在类的顶部定义一些私有(private)变量:

   /// Playback engine.
private let audioEngine = AVAudioEngine()

/// Engine's player node.
private let pitchPlayer = AVAudioPlayerNode()
private let timePitch = AVAudioUnitTimePitch()

/// File to play.
private var audioFile: AVAudioFile?

检查有效的音频文件:

     override func viewDidLoad() {
super.viewDidLoad()

if let filePath = Bundle.main.path(forResource: "Sound1", ofType:
"mp3") {
let filePathURL = URL(fileURLWithPath: filePath)

setPlayerFile(filePathURL)

}
}

private func setPlayerFile(_ fileURL: URL) {
do {
let file = try AVAudioFile(forReading: fileURL)

self.audioFile = file


} catch {
fatalError("Could not create AVAudioFile instance. error: \(error).")
}
}

@IBAction func verySlowPlayback(sender: UIButton) {


timePitch.pitch = -300
timePitch.rate = 0.5
audioEngine.attach(pitchPlayer)
audioEngine.attach(timePitch)

audioEngine.connect(pitchPlayer, to: timePitch, format: audioFile?.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: audioFile?.processingFormat)
pitchPlayer.scheduleFile(audioFile!, at: nil, completionHandler: nil)

// Start the engine.
do {
try audioEngine.start()
} catch {
fatalError("Could not start engine. error: \(error).")
}

pitchPlayer.play()

}

关于iphone - 更改 "sounder"的音频音高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50669495/

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