iPhone开发音频资料

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

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

 

    文件格式(或音频容器)是用于形容文件本身的格式。我们可以通过多种不同的方法为真正的音频数据编码。例如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.

 
# creates sound.caf (little endian, 16 bit) afconvert -f caff -d LEI16 sound.wav  
# creates sound.caf (little endian, 16 bit, 22kHz) afconvert -f caff -d LEI16@22050 sound.wav  
# creates sound.caf (little endian, 16 bit, MONO) afconvert -f caff -d LEI16 -c 1 sound.wav  
# creates sound.aifc (IMA4 compression) afconvert -f AIFC -d ima4 sound.wav  
# creates sound.aifc (IMA4 compression, MONO) afconvert -f AIFC -d ima4 -c 1 sound.wav
若是要转换一个文件夹内的所有音频文件,可创建如下的一个脚本:
#!/bin/bash 
for i in *.wav; do   
afconvert -f caff -d LEI16 $i 
done  
# move new files to project directory 
mv *.caf ~/my_project_path/sounds/
存储该脚本文件名为 convert_sounds.sh,拷贝到要转换的音频文件夹内,在终端执行如下命令:
chmod u+x convert_sounds.sh    
./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(执行 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/s)

 
录制音效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];

责任编辑:冰凝儿
相关推荐

2013-04-08 09:46:23

iPhone开发音频资料

2011-08-08 18:19:09

iPhone音频播放

2012-12-24 09:04:04

iOSUnity3D

2011-07-27 09:50:31

iPhone AVAudioPla 音频

2011-08-02 16:58:15

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-08 10:10:14

iPhone开发 图片 方法

2011-08-01 18:27:58

iPhone开发 UISearchBa

2011-08-15 10:06:22

iPhone开发nib 文件

2011-08-09 17:29:29

iPhone文件屏幕

2011-08-15 10:35:43

iPhone开发Atomicnonatomic

2011-08-02 17:37:01

IPhone开发 环境搭建

2011-08-15 10:45:11

iPhone开发delegate

2011-08-08 16:56:44

iPhone 字符处理 视图

2011-08-05 10:48:11

iPhone开发 Objective- Cocoa Touc
点赞
收藏

51CTO技术栈公众号