iPhone开发音频资料

移动开发 iOS
对于简单的、无混音音频,AVAudio ToolBox框架提供了一个简单的C语言风格的音频服务。你可以使用AudioservicesPlaySystemSound函数来播放简单的声音。要遵守以下几个规则:1.音频长度小于30秒;2.格式只能是PCM或者IMA4;3.文件必须被存储为.caf、.aif、或者.wav格式;4.简单音频不能从内存播放,而只能是磁盘文件。

每个音频文件都是由两部分内容所构成:它的文件格式(或者音频容器)以及它的数据格式(或者音频编码)。

文件格式(或音频容器)是用于形容文件本身的格式。我们可以通过多种不同的方法为真正的音频数据编码。例如CAF文件便是一种文件格式,它能够包含MP3格式,线性PCM以及其它数据格式的音频。

数据格式(或音频编码)

我们将从音频编码开始阐述(而不是文件格式),因为编码是最重要的环节。

线性PCM:这是表示线性脉冲编码调制,主要是描写用于将模拟声音数据转换成数字格式的技术。简单地说也就是未压缩的数据。因为数据是未压缩的,所以我们便可以最快速地播放出音频,而如果空间不是问题的话这便是iPhone音频的优先代码选择。

文件格式(或音频容器)

iPhone支持许多文件格式,包括MPEG-1(.mp3),MPEG-2 ADTS(.aac),AIFF,CAF以及WAVE。但是通常情况下我们都会选择CAF,因为它能够同时包含所有iPhone所支持的编码,并且它也是 iPhone中的优先文件格式选择。

比特率:

比特率是指一个音频文件所占有的每秒字节数。像AAC或MP3等编码便能够指定字节数而压缩音频文件。当你降低每秒钟的字节数时,你同时也在降低音频的质量。

以下是一些较常见的比特率:

每秒32千比特率:调频广播质量

每秒48千比特率:较长的语音博客中常见的频率

每秒64千比特率:标准长度的语音博客中常见的频率

每秒96千比特率:调频收音机的质量

每秒128千比特率:MP3音乐最常见的比特率

每秒160千比特率:音乐家和敏感听众的优先选择

每秒192千比特率:数字无线电广播的质量

每秒320千比特率:和CD的质量没两样了

每秒500千至1411千比特率:无失真的音频编码,如线性PCM

采样频率

我们最后需要提到的一个术语便是:采样频率。

当我们将模拟信号转换成数字格式时,采样频率是指我们多长时间抽取一次声波去创造数字信号。

通常情况下44100赫兹便是最常用的采样频率,因为这与CD音频的频率相同。

转换和记录

这是iPhone开发者需要掌握的制作音频要素的第二部分。

在上文中我提到了文件格式与数据格式间的区别,以及iPhone所支持的各种格式。而接下来我将说说如何在这两种格式间进行转换。

Which format should I use?

The short answer: Use CAFF (uncompressed) for short sound effects and AIFF IMA4 (compressed) for music. This is also the recommendation from Apple.

  • CAFF (Core Audio File Format) is, in a nutshell, the iPhone's native audio file format for uncompressed sounds.
  • AIFF (Audio Interchange File Format) with IMA4 gets you about 4:1 compression on audio files. It is supported natively by the iPhone. One other advantage of this format is that it loops seamlessly - most other compressed audio files are problematic in this regard.
  1. # creates sound.caf (little endian, 16 bit) afconvert -f caff -d LEI16 sound.wav   
  2.  
  3. # creates sound.caf (little endian, 16 bit, 22kHz) afconvert -f caff -d LEI16@22050 sound.wav   
  4.  
  5. # creates sound.caf (little endian, 16 bit, MONO) afconvert -f caff -d LEI16 -c 1 sound.wav   
  6.  
  7. # creates sound.aifc (IMA4 compression) afconvert -f AIFC -d ima4 sound.wav   
  8.  
  9. # creates sound.aifc (IMA4 compression, MONO) afconvert -f AIFC -d ima4 -c 1 sound.wav 

若是要转换一个文件夹内的所有音频文件,可创建如下的一个脚本:

  1. #!/bin/bash  
  2.  
  3. for i in *.wav; do    
  4.  
  5. afconvert -f caff -d LEI16 $i  
  6.  
  7. done   
  8.  
  9. # move new files to project directory  
  10.  
  11. mv *.caf ~/my_project_path/sounds/ 

存储该脚本文件名为 convert_sounds.sh,拷贝到要转换的音频文件夹内,在终端执行如下命令:

  1. chmod u+x convert_sounds.sh     
  2.  
  3. ./convert_sounds.sh 

不在乎内存空间大小的话,采用 linear PCM数据格式的音频,因为该格式播放最快,而且也可以同时播放多个音频且没有cpu消耗资源问题;否则采用 AAC(背景音乐)和 IMA4(音效)

linear PCM在iphone上的采用的是 little-endian integer 16-bit,简称 LEI16,而在mac上用的是 native-endian float 32-bit;
ios都采用caf格式音频文件
采样率使用 44,100Hz
比特率列表:
  • 32kbit/s: AM Radio quality
  • 48kbit/s: Common rate for long speech podcasts
  • 64kbit/s: Common rate for normal-length speech podcasts
  • 96kbit/s: FM Radio quality
  • 128kbit/s: Most common bit rate for MP3 music
  • 160kbit/s: Musicians or sensitive listeners prefer this from 128kbit/s
  • 192kbit/s: Digital radio broadcasting quality
  • 320kbit/s: Virtually indistinguishable from CDs
  • 500kbit/s-1,411kbit/s: Lossless audio encoding such as linear PCM
3个命令行指令:

afplay, afinfo,  afconvert-hf可打印出能转换的所有音频格式 )

afconvert -d [out data format] -f [out file format] [in file] [out file]

afconvert -d LEI16 -f caff input_file.xxx output_file.caf (使用linear PCM数据格式创建caf格式音频,ios最常用)
afconvert -d aac -f caff -b 32768 background-music-lei.caf test_32.caf (-b后跟比特率32768=32*1024,即32kbit)

录制音效Audacity软件: http://audacity.sourceforge.net/
该软件保存的是 16-bit big-endian signed integer格式,所以最后还要转换成 LEI16
 
播放音效,此种方式仅支持数据格式为linear PCM和IMA4,文件格式为caf,aif和wav,长度不超过30秒的音频文件:

NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"pew-pew-lei" ofType:@"caf"]
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath]
AudioServicesCreateSystemSoundID((CFURLRef)pewPewURL, &_pewPewSound)
AudioServicesPlaySystemSound(_pewPewSound);
播放背景音乐,开始播放前会有一定的延迟:
NSError *error; 
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error]
[_backgroundMusicPlayer prepareToPlay]
[_backgroundMusicPlayer play];
OpenAL:有一个开源引擎:下载地址:
a nice Sound Engine library using OpenAL

责任编辑:闫佳明 来源: oschina
相关推荐

2012-12-24 14:48:14

ios

2011-08-08 18:19:09

iPhone音频播放

2012-12-24 09:04:04

iOSUnity3D

2011-08-02 16:58:15

iPhone AVAudioPla 音频播放

2011-07-27 09:50:31

iPhone AVAudioPla 音频

2011-07-06 17:53:40

iPhone SDK Xcode

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-07-08 10:58:47

2011-07-06 17:34:47

iPhone

2011-08-10 15:48:10

iPhone网络

2012-03-28 22:38:20

iPhone

2011-08-12 09:52:35

iPhone开发TableviewUITextField

2011-07-19 09:58:36

2011-07-08 16:02:24

iphone

2011-07-19 09:46:38

2011-08-22 14:31:53

iPhone开发

2011-08-10 15:58:58

iPhone视频

2011-07-08 14:58:16

iPhone Xcode iOS

2011-08-10 18:24:22

iPhone 图形 绘图

2011-08-08 10:10:14

iPhone开发 图片 方法
点赞
收藏

51CTO技术栈公众号