①
②
这两条每一条都是足够分量的优化点,而并不是某些眼里可大可小的部分。所以本文将会致力于提出一种游戏背景音乐无缝循环的解决方案。
音频格式的需求
LAME编码引擎
LAME是目前最好的MP3编码引擎。LAME编码出来的MP3音色纯厚、空间宽广、低音清晰、细节表现良好,它独创的心理音响模型技术保证了CD音频还原的真实性,配合VBR和ABR参数,音质几乎可以媲美CD音频,但文件体积却非常小。对于一个免费引擎,LAME的优势不言而喻。LAME本身是DOS下的文件,需要加外壳程序才比较容易使用,也可以在别的软件(比如EAC)中间调用。但目前很少有马甲能够适用于无缝循环的压缩。所以本文在此也会推出一些关于LAME的简单教程。
LAME 64bits version 3.99.3 (http://lame.sf.net)
usage: lame3.99.3-64lame.exe [options] <infile> [outfile]
<infile> and/or <outfile> can be "-", which means stdin/stdout.
RECOMMENDED:
lame -V2 input.wav output.mp3
OPTIONS:
Input options: 输入文件选项
--scale <arg> scale input (multiply PCM data) by <arg>
--scale-l <arg> scale channel 0 (left) input (multiply PCM data) by <arg>
--scale-r <arg> scale channel 1 (right) input (multiply PCM data) by <arg>
--mp1input input file is a MPEG Layer I file
--mp2input input file is a MPEG Layer II file
--mp3input input file is a MPEG Layer III file
--nogap <file1> <file2> <...> 指定连续输入一系列的文件,输出一个连续完整的文件,也就是合并功能
gapless encoding for a set of contiguous files
--nogapout <dir> 在上面的选项启用的情况下,指定输出文件目录
output dir for gapless encoding (must precede --nogap)
--nogaptags allow the use of VBR tags in gapless encoding
Input options for RAW PCM:
-r input is raw pcm
-x force byte-swapping of input
-s sfreq sampling frequency of input file (kHz) - default 44.1 kHz
--bitwidth w input bit width is w (default 16)
--signed input is signed (default)
--unsigned input is unsigned
--little-endian input is little-endian (default)
--big-endian input is big-endian
Operational options:
-a downmix from stereo to mono file for mono encoding
-m <mode> (j)oint, (s)imple, (f)orce, (d)ual-mono, (m)ono (l)eft (r)ight
default is (j) or (s) depending on bitrate
joint = joins the best possible of MS and LR stereo
simple = force LR stereo on all frames
force = force MS stereo on all frames.
--preset type type must be "medium", "standard", "extreme", "insane", 使用预置方案:"medium"=较差质量,"standard"="标准质量" (192), "extreme"=高质量 (256左右), "insane"=极限品质
or a value for an average desired bitrate and depending
on the value specified, appropriate quality settings will
be used.
"--preset help" gives more info on these
--comp <arg> choose bitrate to achieve a compression ratio of <arg> 指定压缩比,lame将使用能达到该压缩比的最佳比特率压缩。
--replaygain-fast compute RG fast but slightly inaccurately (default)
--replaygain-accurate compute RG more accurately and find the peak sample
--noreplaygain disable ReplayGain analysis
--clipdetect enable --replaygain-accurate and print a message whether 进行消波处理
clipping occurs and how far the waveform is from full scale
--flush flush output stream as soon as possible
--freeformat produce a free format bitstream
--decode input=mp3 file, output=wav 转入解码模式,解码生成wav文件
--swap-channel swap L/R channels
-t disable writing wav header when using --decode
Verbosity:
--disptime <arg>print progress report every arg seconds
-S don't print progress report, VBR histograms
--nohist disable VBR histogram display
--quiet don't print anything on screen
--silent don't print anything on screen, but fatal errors
--brief print more useful information
--verbose print a lot of useful information
Noise shaping & psycho acoustic algorithms: 信号噪音处理和听觉心理学算法
-q <arg> <arg> = 0...9. Default -q 5 噪音处理方式,<arg>取值为0~9,0最好,9最差,整数。
-q 0: Highest quality, very slow
-q 9: Poor quality, but fast
-h Same as -q 2. Recommended.
-f Same as -q 7. Fast, ok quality
CBR (constant bitrate, the default) options:
-b <bitrate> set the bitrate in kbps, default 128 kbps
--cbr enforce use of constant bitrate
ABR options:
--abr <bitrate> specify average bitrate desired (instead of quality)
VBR options:
-V n quality setting for VBR. default n=4 使用VBR压缩,并指定压缩质量, 0最好,9最差
0=high quality,bigger files. 9=smaller files
-v the same as -V 4
--vbr-old use old variable bitrate (VBR) routine
--vbr-new use new variable bitrate (VBR) routine (default)
-Y lets LAME ignore noise in sfb21, like in CBR
-b <bitrate> specify minimum allowed bitrate, default 32 kbps 指定动态比特率最低比特率,建议256或320,单位kbits/s
-B <bitrate> specify maximum allowed bitrate, default 320 kbps
-F strictly enforce the -b option, for use with players that 增强动态比特率文件的兼容性
do not support low bitrate mp3
-t disable writing LAME Tag
-T enable and force writing LAME Tag
MP3 header/stream options:
-e <emp> de-emphasis n/5/c (obsolete)
-c mark as copyright 标记为有版权的
-o mark as non-original
-p error protection. adds 16 bit checksum to every frame 生成校验信息,会导致文件增大,但也能提供更强劲纠错。
(the checksum is computed correctly)
--nores disable the bit reservoir
--strictly-enforce-ISO comply as much as possible to ISO MPEG spec
--buffer-constraint <constraint> available values for constraint:
default, strict, maximum
Filter options:
--lowpass <freq> frequency(kHz), lowpass filter cutoff above freq
--lowpass-width <freq> frequency(kHz) - default 15% of lowpass freq
--highpass <freq> frequency(kHz), highpass filter cutoff below freq
--highpass-width <freq> frequency(kHz) - default 15% of highpass freq
--resample <sfreq> sampling frequency of output file(kHz)- default=automatic
ID3 tag options:
--tt <title> audio/song title (max 30 chars for version 1 tag) 曲目标题
--ta <artist> audio/song artist (max 30 chars for version 1 tag) 表演者
--tl <album> audio/song album (max 30 chars for version 1 tag) 专辑
--ty <year> audio/song year of issue (1 to 9999) 年代
--tc <comment> user-defined text (max 30 chars for v1 tag, 28 for v1.1) 注视
--tn <track[/total]> audio/song track number and (optionally) the total 曲目编号/总曲目数
number of tracks on the original recording. (track
and total each 1 to 255. just the track number
creates v1.1 tag, providing a total forces v2.0).
--tg <genre> audio/song genre (name or number in list) 流派
--ti <file> audio/song albumArt (jpeg/png/gif file, v2.3 tag) 封面图片路径
--tv <id=value> user-defined frame specified by id and value (v2.3 tag) 自定义标签
--add-id3v2 force addition of version 2 tag
--id3v1-only add only a version 1 tag
--id3v2-only add only a version 2 tag
--id3v2-utf16 add following options in unicode text encoding 使用unicode字符集,推荐
--id3v2-latin1 add following options in latin-1 text encoding 使用latin-1字符集,慎用
--space-id3v1 pad version 1 tag with spaces instead of nulls
--pad-id3v2 same as '--pad-id3v2-size 128'
--pad-id3v2-size <value> adds version 2 tag, pad with extra <value> bytes
--genre-list print alphabetically sorted ID3 genre list and exit
--ignore-tag-errors ignore errors in values passed for tags
Note: A version 2 tag will NOT be added unless one of the input fields
won't fit in a version 1 tag (e.g. the title string is longer than 30
characters), or the '--add-id3v2' or '--id3v2-only' options are used,
or output is redirected to stdout.
MS-Windows-specific options:
--priority <type> sets the process priority:
0,1 = Low priority (IDLE_PRIORITY_CLASS)
2 = normal priority (NORMAL_PRIORITY_CLASS, default)
3,4 = High priority (HIGH_PRIORITY_CLASS))
Note: Calling '--priority' without a parameter will select priority 0.
Misc:
--license print License information
MPEG-1 layer III sample frequencies (kHz): 32 48 44.1
bitrates (kbps): 32 40 48 56 64 80 96 112 128 160 192 224 256 320
MP3可用的采样频率和比特率:
32 KHz, 44.1 KHz, 48 KHz
32 kbps, 8=40, 48, 56, 64; 16=80,96,112,128; 32=160,192,224,256; 64=320
代码上的优化
使用“Audio Queue Services”
l
l
l
由于Audio Queue的初始化配置,所有的MP3片段必须使用相同的解码器、属性以及参数,所以在一个会话期间没有办法重新配置解码器。
回调函数会帮你完成所有的繁琐事情,从文件中读取音频数据,然后再填入音频缓冲池里。默认情况下,如果没有数据可以读取,则回调函数停止。而我们制定的回调函数则会继续从下一个MP3文件中读取数据并填入缓冲池。
MP3无缝播放器程序
参阅文献:http://gamua.com/blog/2012/05/gapless-mp3-audio-on-ios/
感谢Daniel Sperl以及 的技术提供!
版权所有
附件下载:http://upload.gameres.com/201210/sf_17174854_2607.zip