cocos2d 音頻處理

http://www.raywenderlich.com/233/audio-101-for-iphone-developers-converting-and-recording

This article is the second in a three-part series covering audio topics of interest to the iPhone developer.

In the first article in the series, I covered the difference between file formats and data formats, and the various formats that were supported on the iPhone. Now let’s talk about how you can convert between different formats!

(Jump to the third article in the series, where I cover how to actually play audio on your phone.)

Afplay, AFConvert, and AFInfo

Converting audio files on the Mac is extremely easy due to three built in command-line utilities on the Mac: afplay, afconvert, and afinfo.

The easiest to use is afplay – just give it the name of your audio file from a Terminal and it will play away. This is quite convenient when compressing files to various bit rates to hear how they sound.

The next one is afinfo – just give it the name of your audio file, and it will display the file format, data format, bit rate, and other useful info like so:

afinfo pew-pew-lei.caf 
File:           pew-pew-lei.caf
File type ID:   caff
Data format:     1 ch,  44100 Hz, 'lpcm' (0x0000000C) 
    16-bit little-endian signed integer no channel layout.
estimated duration: 0.560 sec
audio bytes: 49408
audio packets: 24704
audio 24704 valid frames + 0 priming + 0 remainder = 24704
bit rate: 705600 bits per second
packet size upper bound: 2
audio data file offset: 4096
optimized
sound check:
    approximate duration in seconds          0.56
----

The above shows you that this file has a file type of CAF, a data format of 16-bit little-endian signed integer (LEI16), a sample rate of 44,100 Hz, and a bit rate of 705,600 bits per second.

Finally, let’s discuss the best utility of all: afconvert. Afconvert is extremely easy to use, just issue a command line like the following:

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

So to convert a file to the preferred uncompressed audio encoding for the iPhone (reminder: the little-endian integer 16-bit variant of linear PCM, a.k.a. LEI16) and the preferred file format for the iPhone (reminder: Core Audio File Format a.k.a. CAFF), you would issue a command like the following:

afconvert -d LEI16 -f 'caff' input_file.xxx output_file.caf

Note I didn’t specify the extension for the input file, because afconvert is smart enough to detect the type of audio file and convert appropriately, so it can be any audio data format with any audio file format.

One other note: You can add the -b option right before the input/output files to set the bit rate. So for example, here I save the file at 32kbit/sec, then 128kbit/sec:

afconvert -d aac -f 'caff' -b 131072 background-music-lei.caf test_128.caf
afconvert -d aac -f 'caff' -b 32768 background-music-lei.caf test_32.caf

Recording Audio on the Mac

I wanted to jot down a couple of notes about good ways to make music and sounds for your apps on the Mac.

First, there is GarageBand. GarageBand makes it really easy to put together some premade loops of drums, guitars, and other sound instruments and make a little song out of it. And if you’re musically inclined, you can record yourself playing along and make some much cooler stuff.

Garage Band Screenshot

So if you haven’t already, take a couple minutes to go through the GarageBand tutorials from Apple. Specifically, “Using Built-In Software Instruments” is the one I found the most useful.

Note that after you are happy with your song, you’ll have to export it to iTunes and then “Reveal in Finder” to grab your file for future use.

I found that GarageBand wasn’t the greatest for recording simple sound effects. For that, I turned to a great free audio program called Audacity. You can plug in your mike (I used my Rock Band mike and it worked just fine!) and record your effect, and save it out easily.

Audacity Screenshot

Don’t forget that when you make your own sounds like this, they will be most likely be saved as 16-bit big-endian signed integer, or BEI16. So don’t forget to convert to LEI16 before you include them in your app.

If you aren’t musically inclined, there are some sounds licensed under the Creative Commons license at The Freesound Project. Or you can always hire a professional!

What’s Next?

In the next and final article in the series I show how to play audio programmatically on the iPhone.

發佈了55 篇原創文章 · 獲贊 5 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章