iOS:同时录制和播放的示例代码 [英] iOS: Sample code for simultaneous record and playback

查看:39
本文介绍了iOS:同时录制和播放的示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为多轨录音机设计一个简单的概念验证.

明显的起点是从文件 A.caf 播放到耳机,同时将麦克风输入记录到文件 B.caf

这个问题 -- 同时录制和播放音频 -- 指出我可以在三个级别上工作:

  • AVFoundation API(AVAudioPlayer + AVAudioRecorder)
  • 音频队列 API
  • 音频单元 API (RemoteIO)

最好的工作水平是什么?显然,通用的答案是在完成工作的最高级别工作,这将是 AVFoundation.

但是我从一个因延迟问题而放弃的人那里接手这项工作(他在文件之间有 0.3 秒的延迟),所以也许我需要在较低的级别上工作以避免这些问题?

此外,有哪些源代码可用于跳板?我一直在看 SpeakHere 示例( http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html).如果我找不到更简单的东西,我会用这个.

但是有人可以提出更简单/其他的建议吗?如果可以避免的话,我宁愿不使用 C++ 代码.

有没有人知道一些使用 AVFoundation 来做到这一点的公共代码?

此处的 AVFoundation 示例:http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1

编辑(2):这里看起来更好看:http://www.switchonthecode.com/tutorials/create-a-basic-iphone-audio-player-with-av-foundation-framework

EDIT(3): 我如何录制音频带 AVAudioRecorder 的 iPhone?

解决方案

正如 Viraj 所建议的,这就是答案.

是的,您可以使用 AVFoundation 获得非常好的结果.首先需要注意的是,对于播放器和记录器来说,激活它们是一个两步过程.

首先你准备好它.

然后你玩它.

所以,准备好一切.然后播放所有内容.

这将使您的延迟降低到大约 70 毫秒.我通过录制节拍器的滴答声进行测试,然后在将 iPhone 举到扬声器前并同时录音的同时通过扬声器播放它.

第二次录音有清晰的回声,我发现它大约为 70 毫秒.我本可以在 Audacity 中分析信号以获得准确的偏移量.

所以为了排列所有东西,我只是 performSelector:x withObject: y afterDelay: 70.0/1000.0

可能存在隐藏的障碍,例如延迟可能因设备而异.它甚至可能因设备活动而异.在启动播放器和启动记录器之间,线程甚至有可能被中断/重新安排.

但它有效,而且比处理音频队列/单元要整洁得多.

I'm designing a simple proof of concept for multitrack recorder.

Obvious starting point is to play from file A.caf to headphones while simultaneously recording microphone input into file B.caf

This question -- Record and play audio Simultaneously -- points out that there are three levels at which I can work:

  • AVFoundation API (AVAudioPlayer + AVAudioRecorder)
  • Audio Queue API
  • Audio Unit API (RemoteIO)

What is the best level to work at? Obviously the generic answer is to work at the highest level that gets the job done, which would be AVFoundation.

But I'm taking this job on from someone who gave up due to latency issues (he was getting a 0.3sec delay between the files), so maybe I need to work at a lower level to avoid these issues?

Furthermore, what source code is available to springboard from? I have been looking at SpeakHere sample ( http://developer.apple.com/library/ios/#samplecode/SpeakHere/Introduction/Intro.html ). if I can't find something simpler I will use this.

But can anyone suggest something simpler/else? I would rather not work with C++ code if I can avoid it.

Is anyone aware of some public code that uses AVFoundation to do this?

EDIT: AVFoundation example here: http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a-guide&more=1&c=1&tb=1&pb=1

EDIT(2): Much nicer looking one here: http://www.switchonthecode.com/tutorials/create-a-basic-iphone-audio-player-with-av-foundation-framework

EDIT(3): How do I record audio on iPhone with AVAudioRecorder?

解决方案

As suggested by Viraj, here is the answer.

Yes, you can achieve very good results using AVFoundation. Firstly you need to pay attention to the fact that for both the player and the recorder, activating them is a two step process.

First you prime it.

Then you play it.

So, prime everything. Then play everything.

This will get your latency down to about 70ms. I tested by recording a metronome tick, then playing it back through the speakers while holding the iPhone up to the speakers and simultaneously recording.

The second recording had a clear echo, which I found to be ~70ms. I could have analysed the signal in Audacity to get an exact offset.

So in order to line everything up I just performSelector:x withObject: y afterDelay: 70.0/1000.0

There may be hidden snags, for example the delay may differ from device to device. it may even differ depending on device activity. It is even possible the thread could get interrupted/rescheduled in between starting the player and starting the recorder.

But it works, and is a lot tidier than messing around with audio queues / units.

这篇关于iOS:同时录制和播放的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆