如何使用AVAudioPlayer更快地播放音频*和*更高音调? [英] How can I use AVAudioPlayer to play audio faster *and* higher pitched?

查看:529
本文介绍了如何使用AVAudioPlayer更快地播放音频*和*更高音调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述:

我的应用中有一系列音效,存储为 .m4a 我希望以各种速度和音高播放的文件(AAC格式,48 KHz,16位),而不必将所有变体预先生成为单独的文件。

I have a collection of sound effects in my app stored as.m4a files (AAC format, 48 KHz, 16-bit) that I want to play at a variety of speeds and pitches, without having to pre-generate all the variants as separate files.

虽然 AVAudioPlayer 对象的 .rate 属性可以改变播放速度,但它始终保持原始音高,这不是我想要的。相反,我只是想要更快或更慢地播放声音样本并让音调上升或下降以匹配 - 就像加速或减慢老式卷轴到磁带录音机一样。换句话说,我需要一些方法来基本上改变音频采样率,如+2半音(快12%),-5半音(慢33%),+ 12半音(快2倍)等等。

Although the .rate property of an AVAudioPlayer object can alter playback speed, it always maintains the original pitch, which is not what I want. Instead, I simply want to play the sound sample faster or slower and have the pitch go up or down to match — just like speeding up or slowing down an old-fashioned reel-to-reel tape recorder. In other words, I need some way to essentially alter the audio sample rate by amounts like +2 semitones (12% faster), –5 semitones (33% slower), +12 semitones (2x faster), etc.

问题:

是否有某种方式从<$ c获取线性PCM音频数据$ c> AVAudioPlayer 对象,使用不同的iOS框架应用采样率转换,并将生成的音频数据填充到新的 AVAudioPlayer 对象中,该对象可以然后正常播放?

Is there some way fetch the Linear PCM audio data from an AVAudioPlayer object, apply sample rate conversion using a different iOS framework, and stuff the resulting audio data into a new AVAudioPlayer object, which can then be played normally?

可能的途径:

我正在阅读 AudioConverterConvertComplexBuffer 。特别是 kAudioConverterSampleRateConverterComplexity_Mastering kAudioConverterQuality_Max ,以及 AudioConverterFillComplexBuffer()吸引了我的注意。所以这个音频转换框架看起来很可能。这是我应该进一步探索的途径吗?

I was reading up on AudioConverterConvertComplexBuffer. In particular kAudioConverterSampleRateConverterComplexity_Mastering, and kAudioConverterQuality_Max, and AudioConverterFillComplexBuffer() caught my eye. So it looks possible with this audio conversion framework. Is this an avenue I should explore further?

要求:


  1. 我实际上不需要立即开始播放。如果采样率转换引起轻微延迟,那很好。我所有的样本都是4秒或更短的时间,所以我认为任何即时重新采样都会很快发生,大约为1/10秒或更短。 (但是超过1/2会是太多了。)

  1. I actually don't need playback to begin instantly. If sample rate conversion incurs a slight delay, that's fine. All of my samples are 4 seconds or less, so I would imagine that any on-the-fly resampling would occur quickly, on the order of 1/10 second or less. (More than 1/2 would be too much, though.)

如果有的话,我真的不想进入像OpenAL或Core Audio这样的重量级的东西使用iOS提供的转换框架更简单的方法。但是,如果使用OpenAL或Core Audio存在简单解决此问题的解决方案,我很乐意考虑这一点。 简单是指可以在50-100行代码中实现的内容,并且不需要启动其他线程来将数据提供给声音设备。我宁愿让所有事情都自动处理 - 这就是为什么我愿意在播放之前转换音频片段。

I'd really rather not get into heavyweight stuff like OpenAL or Core Audio if there is a simpler way to do this using a conversion framework provided by iOS. However, if there is a simple solution to this problem using OpenAL or Core Audio, I'd be happy to consider that. By "simple" I mean something that can be implemented in 50–100 lines of code and doesn't require starting up additional threads to feed data to the a sound device. I'd rather just have everything taken care of automatically — which is why I'm willing to convert the audio clip prior to playing.

我想避免这里有任何第三方库,因为这不是火箭科学,我知道必须以某种方式使用原生iOS框架。

I want to avoid any third-party libraries here, because this isn't rocket science and I know it must be possible with native iOS frameworks somehow.

同样,我需要一起调整音高和播放速率,而不是单独调整。因此,如果播放速度减慢2倍,人声会变得非常深沉且说话速度慢。如果播放速度加快2-3倍,那么人类的声音听起来就像一个快速说话的花栗鼠。换句话说,我绝对不希望在保持音频持续时间相同的同时改变音高,因为当将音调向上弯曲超过一对半音时,该操作导致不期望的细小声音。我只是想加快整个过程的速度,并将音调调高为一种自然的副作用,就像过去的老式录音机一样。

Again, I need to adjust the pitch and playback rate together, not separately. So if playback is slowed down 2x, a human voice would become very deep and slow-spoken. And if playback is sped up 2–3x, a human voice would sound like a fast-talking chipmunk. In other words, I absolutely do not want to alter the pitch while keeping the audio duration the same, because that operation results in an undesirably "tinny" sound when bending the pitch upward more than a couple semitones. I just want to speed the whole thing up and have the pitch go up as a natural side-effect, just like old-fashioned tape recorders used to do.

需要在iOS 6及更高版本中工作,虽然iOS 5支持将是一个不错的奖励。

Needs to work in iOS 6 and up, although iOS 5 support would be a nice bonus.


推荐答案

对于iOS 7,您需要查看AVPlayerItem的时间间距算法( audioTimePitchAlgorithm ),名为 AVAudioTimePitchAlgorithmVarispeed 。不幸的是,早期系统无法使用此功能。

For iOS 7, you'd want to look at AVPlayerItem's time-pitch algorithm (audioTimePitchAlgorithm) called AVAudioTimePitchAlgorithmVarispeed. Unfortunately this feature is not available on early systems.

这篇关于如何使用AVAudioPlayer更快地播放音频*和*更高音调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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