iPhone開發(fā)音頻資料
每個音頻文件都是由兩部分內(nèi)容所構(gòu)成:它的文件格式(或者音頻容器)以及它的數(shù)據(jù)格式(或者音頻編碼)。
文件格式(或音頻容器)是用于形容文件本身的格式。我們可以通過多種不同的方法為真正的音頻數(shù)據(jù)編碼。例如CAF文件便是一種文件格式,它能夠包含MP3格式,線性PCM以及其它數(shù)據(jù)格式的音頻。
數(shù)據(jù)格式(或音頻編碼)
我們將從音頻編碼開始闡述(而不是文件格式),因為編碼是最重要的環(huán)節(jié)。
線性PCM:這是表示線性脈沖編碼調(diào)制,主要是描寫用于將模擬聲音數(shù)據(jù)轉(zhuǎn)換成數(shù)字格式的技術(shù)。簡單地說也就是未壓縮的數(shù)據(jù)。因為數(shù)據(jù)是未壓縮的,所以我們便可以最快速地播放出音頻,而如果空間不是問題的話這便是iPhone音頻的優(yōu)先代碼選擇。
文件格式(或音頻容器)
iPhone支持許多文件格式,包括MPEG-1(.mp3),MPEG-2 ADTS(.aac),AIFF,CAF以及WAVE。但是通常情況下我們都會選擇CAF,因為它能夠同時包含所有iPhone所支持的編碼,并且它也是 iPhone中的優(yōu)先文件格式選擇。
比特率:
比特率是指一個音頻文件所占有的每秒字節(jié)數(shù)。像AAC或MP3等編碼便能夠指定字節(jié)數(shù)而壓縮音頻文件。當你降低每秒鐘的字節(jié)數(shù)時,你同時也在降低音頻的質(zhì)量。
以下是一些較常見的比特率:
每秒32千比特率:調(diào)頻廣播質(zhì)量
每秒48千比特率:較長的語音博客中常見的頻率
每秒64千比特率:標準長度的語音博客中常見的頻率
每秒96千比特率:調(diào)頻收音機的質(zhì)量
每秒128千比特率:MP3音樂最常見的比特率
每秒160千比特率:音樂家和敏感聽眾的優(yōu)先選擇
每秒192千比特率:數(shù)字無線電廣播的質(zhì)量
每秒320千比特率:和CD的質(zhì)量沒兩樣了
每秒500千至1411千比特率:無失真的音頻編碼,如線性PCM
采樣頻率
我們最后需要提到的一個術(shù)語便是:采樣頻率。
當我們將模擬信號轉(zhuǎn)換成數(shù)字格式時,采樣頻率是指我們多長時間抽取一次聲波去創(chuàng)造數(shù)字信號。
通常情況下44100赫茲便是最常用的采樣頻率,因為這與CD音頻的頻率相同。
轉(zhuǎn)換和記錄
這是iPhone開發(fā)者需要掌握的制作音頻要素的第二部分。
在上文中我提到了文件格式與數(shù)據(jù)格式間的區(qū)別,以及iPhone所支持的各種格式。而接下來我將說說如何在這兩種格式間進行轉(zhuǎn)換。
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
若是要轉(zhuǎn)換一個文件夾內(nèi)的所有音頻文件,可創(chuàng)建如下的一個腳本:
- #!/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,拷貝到要轉(zhuǎn)換的音頻文件夾內(nèi),在終端執(zhí)行如下命令:
- chmod u+x convert_sounds.sh
- ./convert_sounds.sh
不在乎內(nèi)存空間大小的話,采用 linear PCM數(shù)據(jù)格式的音頻,因為該格式播放最快,而且也可以同時播放多個音頻且沒有cpu消耗資源問題;否則采用 AAC(背景音樂)和 IMA4(音效)
- 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
afplay, afinfo, afconvert-hf可打印出能轉(zhuǎn)換的所有音頻格式 )
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數(shù)據(jù)格式創(chuàng)建caf格式音頻,ios最常用)afconvert -d aac -f caff -b 32768 background-music-lei.caf test_32.caf (-b后跟比特率32768=32*1024,即32kbit)
NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"pew-pew-lei" ofType:@"caf"];NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];AudioServicesCreateSystemSoundID((CFURLRef)pewPewURL, &_pewPewSound);AudioServicesPlaySystemSound(_pewPewSound);播放背景音樂,開始播放前會有一定的延遲:a nice Sound Engine library using OpenAL
NSError *error;_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];[_backgroundMusicPlayer prepareToPlay];[_backgroundMusicPlayer play];OpenAL:有一個開源引擎:下載地址: