背景 - 我在Apple最近的WWDC发布的视频列表中看到了一个名为"AVAudioEngine in Practice"的视频,用于将音效应用于音频. https://developer.apple.com/videos/wwdc/2014/
之后,我成功地使用以下代码更改了音频的音高:
//Audio Engine is initialized in viewDidLoad()
audioEngine = AVAudioEngine()
//The following Action is called on clicking a button
@IBAction func chipmunkPlayback(sender: UIButton) {
var pitchPlayer = AVAudioPlayerNode()
var timePitch = AVAudioUnitTimePitch()
timePitch.pitch = 1000
audioEngine.attachNode(pitchPlayer)
audioEngine.attachNode(timePitch)
audioEngine.connect(pitchPlayer, to: timePitch, format: myAudioFile.processingFormat)
audioEngine.connect(timePitch, to: audioEngine.outputNode, format: myAudioFile.processingFormat)
pitchPlayer.scheduleFile(myAudioFile, atTime: nil, completionHandler: nil)
audioEngine.startAndReturnError(&er)
pitchPlayer.play()
}
Run Code Online (Sandbox Code Playgroud)
据我所知,我使用AudioEngine将AudioPlayerNode与AudioEffect连接,而AudioEffect又连接到Output.
我现在很想知道为音频添加多种音效.例如,音高变化和混响.我该如何为音频添加多种音效?
另外,在viewDidLoad中附加和连接节点是否有意义,而不是我在IBAction中如何完成它?
我刚看了WWDC视频(AVAudioEngine
实践中的会话502 )AVAudioEngine
,我非常高兴能够建立一个基于这种技术的应用程序.
我无法弄清楚如何对麦克风输入或调音台输出进行电平监控.
有人可以帮忙吗?为了清楚起见,我所说的是监控当前输入信号(并在UI中显示),而不是通道/轨道的输入/输出音量设置.
我知道你可以做到这一点AVAudioRecorder
,但是这不是AVAudioNode
它的AVAudioEngine
需要.
我使用了多个AVAudioPlayerNode
在AVAudioEngine
为混合播放的音频文件.完成所有设置(引擎准备,启动,音频文件段已安排)后,我play()
在每个播放器节点上调用方法开始播放.
因为遍历所有播放器节点需要花费时间,所以我拍摄了第一个节点lastRenderTime
值的快照,并使用它来计算节点play(at:)
方法的开始时间,以保持节点之间的播放同步:
let delay = 0.0
let startSampleTime = time.sampleTime // time is the snapshot value
let sampleRate = player.outputFormat(forBus: 0).sampleRate
let startTime = AVAudioTime(
sampleTime: startSampleTime + AVAudioFramePosition(delay * sampleRate),
atRate: sampleRate)
player.play(at: startTime)
Run Code Online (Sandbox Code Playgroud)
问题在于当前播放时间.
我使用这个计算得到值,在seekTime
我们寻找玩家的情况下,我跟踪的值是哪里.它0.0
开始时:
private var _currentTime: TimeInterval {
guard player.engine != nil,
let lastRenderTime = player.lastRenderTime,
lastRenderTime.isSampleTimeValid,
lastRenderTime.isHostTimeValid else {
return seekTime
}
let sampleRate = player.outputFormat(forBus: 0).sampleRate
let sampleTime = player.playerTime(forNodeTime: …
Run Code Online (Sandbox Code Playgroud) HEJ.我想用AVAudioEngine
Swift中的new实现一个实时音频应用程序.有人有新框架经验吗?实时应用程序如何工作?
我的第一个想法是将(已处理的)输入数据存储到一个AVAudioPCMBuffer
对象中,然后让它AVAudioPlayerNode
在我的演示类中看到:
import AVFoundation
class AudioIO {
var audioEngine: AVAudioEngine
var audioInputNode : AVAudioInputNode
var audioPlayerNode: AVAudioPlayerNode
var audioMixerNode: AVAudioMixerNode
var audioBuffer: AVAudioPCMBuffer
init(){
audioEngine = AVAudioEngine()
audioPlayerNode = AVAudioPlayerNode()
audioMixerNode = audioEngine.mainMixerNode
let frameLength = UInt32(256)
audioBuffer = AVAudioPCMBuffer(PCMFormat: audioPlayerNode.outputFormatForBus(0), frameCapacity: frameLength)
audioBuffer.frameLength = frameLength
audioInputNode = audioEngine.inputNode
audioInputNode.installTapOnBus(0, bufferSize:frameLength, format: audioInputNode.outputFormatForBus(0), block: {(buffer, time) in
let channels = UnsafeArray(start: buffer.floatChannelData, length: Int(buffer.format.channelCount))
let floats = UnsafeArray(start: channels[0], length: Int(buffer.frameLength))
for var i …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个iOS应用程序,它使用AVAudioEngine进行各种操作,包括将音频录制到文件,使用音频单元对音频应用效果,以及播放应用效果的音频.我使用tap也将输出写入文件.完成此操作后,它会在播放音频时实时写入文件.
是否可以设置AVAudioEngine图形,该图形从文件读取,使用音频单元处理声音,并输出到文件,但比实时更快(即,与硬件可以处理的速度一样快)?用于此的用例是输出几分钟的音频并应用效果,我当然不希望等待几分钟来处理它.
编辑:这是我用来设置AVAudioEngine图表的代码,并播放声音文件:
AVAudioEngine* engine = [[AVAudioEngine alloc] init];
AVAudioPlayerNode* player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];
self.player = player;
self.engine = engine;
if (!self.distortionEffect) {
self.distortionEffect = [[AVAudioUnitDistortion alloc] init];
[self.engine attachNode:self.distortionEffect];
[self.engine connect:self.player to:self.distortionEffect format:[self.distortionEffect outputFormatForBus:0]];
AVAudioMixerNode* mixer = [self.engine mainMixerNode];
[self.engine connect:self.distortionEffect to:mixer format:[mixer outputFormatForBus:0]];
}
[self.distortionEffect loadFactoryPreset:AVAudioUnitDistortionPresetDrumsBitBrush];
NSError* error;
if (![self.engine startAndReturnError:&error]) {
NSLog(@"error: %@", error);
} else {
NSURL* fileURL = [[NSBundle mainBundle] URLForResource:@"test2" withExtension:@"mp3"];
AVAudioFile* file = [[AVAudioFile alloc] initForReading:fileURL error:&error];
if (error) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个语音文本功能,我收到错误:
Initializer for conditional binding must have Optional type, not 'AVAudioInputNode'
guard let inputNode = audioEngine.inputNode else {
fatalError("Audio engine has no input node")
}
Run Code Online (Sandbox Code Playgroud) AudioKit,macOS:
当我执行 Mixer.addInput(myAudioPlayer) 时,程序输出以下消息:
2021-09-16 11:41:44.578038+0200 ShowTime[16140:1611137] 投掷-10878
...无数次。
你知道-10878是什么以及如何修复它吗?
我也有兴趣了解“ShowTime[16140:1611137]”的含义。我可以使用这些数字来跟踪我的程序失败的位置吗?
谢谢。
我有一个音频文件,我想使用一些效果(如音高效果)处理,然后将最终结果写入文件.
在我处理文件并将其保存到光盘之前,让用户播放音高效果并实时收听变化.
这就是我做实时的事情:
let audioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPlayback, error: nil)
audioSession.setActive(true, error: nil)
audioEngine = AVAudioEngine()
audioFile = AVAudioFile(forReading: audioUrl!, error: nil)
audioPlayerNode = AVAudioPlayerNode()
audioEngine.attachNode(audioPlayerNode)
changePitchEffect = AVAudioUnitTimePitch()
changePitchEffect.pitch = 1.0 // default
audioEngine.attachNode(changePitchEffect)
audioEngine.connect(audioPlayerNode, to: changePitchEffect, format: nil)
audioEngine.connect(changePitchEffect, to: audioEngine.outputNode, format: nil)
let frameCapacity = UInt32(audioFile.length)
let buffer = AVAudioPCMBuffer(PCMFormat: audioFile.processingFormat, frameCapacity: frameCapacity)
if audioFile.readIntoBuffer(buffer, error: nil) {
audioEngine.startAndReturnError(nil)
audioPlayerNode.scheduleBuffer(buffer, atTime: nil, options: .Loops, completionHandler: nil)
audioPlayerNode.play() // start playing in a loop
}
Run Code Online (Sandbox Code Playgroud)
然后使用UISlider
我让用户在循环中聆听音频的同时改变音高的值. …
我想录制一些音频AVAudioEngine
和用户麦克风.我已经有了一个工作样本,但是无法弄清楚如何指定我想要的输出格式...
我的要求是,我需要AVAudioPCMBuffer
我现在说的话......
我是否需要添加一个可以进行转码的单独节点?我找不到关于那个问题的文档/样本......
在Audio-Stuff方面,我也是一个菜鸟.我知道我想要NSData
包含PCM-16bit,最大采样率为16000(8000会更好)
这是我的工作样本:
private var audioEngine = AVAudioEngine()
func startRecording() {
let format = audioEngine.inputNode!.inputFormatForBus(bus)
audioEngine.inputNode!.installTapOnBus(bus, bufferSize: 1024, format: format) { (buffer: AVAudioPCMBuffer, time:AVAudioTime) -> Void in
let audioFormat = PCMBuffer.format
print("\(audioFormat)")
}
audioEngine.prepare()
do {
try audioEngine.start()
} catch { /* Imagine some super awesome error handling here */ }
}
Run Code Online (Sandbox Code Playgroud)
如果我改变格式让'说'
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatInt16, sampleRate: 8000.0, channels: 1, interleaved: false)
Run Code Online (Sandbox Code Playgroud)
然后如果会产生一个错误,说样本速率需要与hwInput相同...
很感谢任何形式的帮助!!!
编辑:我刚刚发现,AVAudioConverter
但我需要兼容iOS8 ...
我有以下代码(重新)开始AVAudioEngine
连接到AVAudioEngineConfigurationChangeNotification
:
do {
try self.engine.start()
} catch {
DDLogError("could not start sound engine")
self.soundEnabled = false
return
}
Run Code Online (Sandbox Code Playgroud)
self.engine定义为
private let engine = AVAudioEngine()
Run Code Online (Sandbox Code Playgroud)
但是,我经常通过Crashlytics收到崩溃报告
致命异常:com.apple.coreaudio.avfaudio错误561015905
在包含的线上try self.engine.start()
.
561015905是AVAudioSessionErrorCodeCannotStartPlaying
从我的理解,这应该是一个NSError
错误代码,而不是一个例外,应该由我catch
在上面的代码中的空白捕获.不过,该应用程序似乎只是崩溃了.我错过了什么?
我知道有些情况下应用程序会在可能发生此错误的后台唤醒,我会很好,只要我能够以某种方式发现它,就像我想的那样do/catch
.
avaudioengine ×10
ios ×8
avfoundation ×4
swift ×4
core-audio ×2
macos ×2
audiokit ×1
audiounit ×1
conditional ×1
objective-c ×1
optional ×1
real-time ×1
speech ×1
xcode ×1