Firelight Technologies FMOD Ex |
术语 / 基本原理 导言 下文中的术语和原理将贯穿整个FMOD文档,本节对部分术语和原理进行解释,以免混淆。
Samples vs bytes vs milliseconds 在很多FMOD函数中都会提到PCM的samples、bytes和milliseconds。 要理解它们有什么区别,请看下图。下图描述了原始PCM采样数据在FMOD缓冲中的存储方式:
从图中可以看出立体声音频由左/右声道的数据交替组成。 .
一对左/右声道(双声道)数据称为一个sample。 .
因为是16bit的数据,所以1 sample = 4 bytes。 .
如果采样率是44.1khz或者或回放频率是44100 采样/秒,那么1个采样是1/44100秒,或者1/44毫秒。因此44100个采样就等于1秒钟的数据。
下面的公式可以用来在不同的术语间进行转换: .
ms = samples * 1000 / samplerate. .
samples = ms * samplerate / 1000. .
samplerate = samples * 1000 / ms. .
bytes = samples * bits * channels / 8. .
samples = bytes * 8 / bits / channels.
一些函数如MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_Sound_GetLength.html;">Sound::getLength
提供了以毫秒、字节和采样为单位的长度值,这样就不必再使用上述公式计算了。 声音——采样 vs 压缩采样 vs 流 加载声音有三种方式:一是将静态帧解压后以PCM格式装入内存;二是以其原始格式装入内存,并在运行时解压;三是将声音流化后并从外部媒介(如硬盘或CD)实时(in chunks)解码。 .
"Samples" 适合那些需要多次播放的小的声音片段,例如音效。它播放时占用很少的CPU,并且能使用硬件加速。参见MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_MODE.html;">FMOD_CREATESAMPLE。 .
"Streams" 适合那些因为太大而无法载如内存的声音,声音会被流化并装入到一小段由FMOD管理的环形缓冲中。这会占用少量CPU和硬盘带宽,具体视文件格式而定。举个例子,实时解码MP3会比播放一个PCM(未压缩WAV)文件占用更多的CPU。一个流式的声音只能播放一次,因为一个流只有一个文件句柄和一个环形缓冲。参见MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_MODE.html;">FMOD_CREATESTREAM. .
"Compressed samples" 是一个新的高级选项,它允许用户将一个确定格式的压缩文件装载到内存中,并且不需要对其解压。用CPU进行软件混合,没有stream的只能播放一次的限制。它比标准的PCM采样占用更多的CPU,但由于不需要访问硬盘且使用更少的缓冲,因此比stream对CPU的占用更少。参见MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_MODE.html;">FMOD_CREATECOMPRESSEDSAMPLE.
你可能注意到术语"Sample"和"Stream"都没有对应的类名,那是因为所有的FMOD APIs 都统一使用"MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/sound.html;">Sound"类型。 默认情况下,MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_System_CreateSound.html;">System::createSound
会将整个声音文件解码到内存中。 要使用流进行实时播放且节约内存的话,创建声音时使用MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_MODE.html;">FMOD_CREATESTREAM标记,或者使用helper函数MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_System_CreateStream.html;">System::createStream,其本质和MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_System_CreateSound.html;">System::createSound相同,只是自动使用了MSITStore:FMOD%20SoundSystemFMOD%20Programmers%20API%20Win32documentationfmodex.chm::/HTML/FMOD_MODE.html;">FMOD_CREATESTREAM标记。 要创建一个压缩采样则在System::createSound中使用FMOD_CREATECOMPRESSEDSAMPLE标记。 硬件 vs 软件 FMOD Ex支持通过DirectSound或者控制台硬件API对音频回放使用硬件加速,但也可以使用自己的软件混合机制。 由于基于硬件和软件的音频回放的某些特征,所以通常硬件和软件交替使用。 Hardware sounds(使用FMOD_HARDWARE创建,CPU占用率低,响应时间短,可以访问硬件混响),例如EAX4。 Hardware sounds在某些方式下也有其局限性,例如DirecSound就仅限于Windows,静态采样不支持任意位置的循环点(循环播放整个sound或者不循环), 不能播放非混响音效(例如chorus, distortion, lowpass etc)。
Software sounds (使用FMOD_SOFTWARE创建,虽然有时会占用更多的CPU,但它能做更多的事,如复杂循环、实时解析、音效和采样精确同步)。
Hardware vs Software.
Hardware 优点 .
Usually lower latency. (Although on consoles or ASIO output in windows, using FMOD_SOFTWARE can have extremely low latency as low as 2-5ms) .
Less CPU time. (Although on Windows software is a lot faster due to bad hardware sound card driver design, and inefficiencies in the DirectSound API). .
On Windows, access to EAX2, EAX3, EAX4, I3DL2 reverb per voice. (FMOD Ex has its own high quality I3DL2 reverb solution in software, but may not be as flexible or have the quality of EAX4 for example.). .
Free hardware obstruction / occlusion (this is usually equivalent to a lowpass filter or reverb attenuation which can also be performed in software at some expense to the CPU), but only on EAX compatible sound cards on Windows. FMOD_SOFTWARE is cross platform. .
On PS2, PSP, XBox, GameCube, Wii, hardware voices can play back ADPCM compressed sound data with no cpu hit. .
On a limited number of soundcards, hardware 3d sounds will be realtime encoded into an AC3 Dolby Digital stream via a digital / optical output on the card so an amplifier can play it in 3D surround sound. FMOD software mixing now supports 5.1 and 7.1 mixing at slightly higher CPU expense, and will work via analog outputs such as soundcards with 3 stereo jacks to run to a 5.1 speaker setup.
Hardware 缺点 .
在Win32平台上不支持点到点循环,XBox和GameCube平台支持点到点循环,而PS2上仅支持循环起点,所以不具有跨平台兼容性。 .
No access to hardware effects per voice. 大多数PC声卡和控制台都不支持如lowpass、distortion、flange、chorus etc.等硬件加速音效。
.
没有循环计数控制。一个声音要么被无限次地循环,要么就不循环。 .
特性支持不一致。例如PS2不支持EAX混响,且3D音效的执行通常听上去不同。
.
有时比FOMD在Windows上的软件混合速度慢许多。虚拟声音在换进/出时导致的大量状态变化会带来很大的硬件开销(明显的帧率下降),而软件却不会。
Software 优点 .
Consistent sound on every platform, there is no variation in playback. .
Sample accurate synchronization callbacks and events. .
Compressed sample playback support without using streams. .
Cross platform reverb. .
Complex looping and loop counts. .
Reverse sample playback. .
Spectrum analysis. .
Filters per channel or for the global mix, to perform effects such as lowpass, distortion, flange, chorus etc. .
Complex DSP network construction for realtime sound synthesis. .
Access to final mix buffer to allow analyzing, drawing to screen, or saving to file.
Software 缺点 .
某些音频设备的响应时间(如win32的waveout输出)可能会很长。
.
内存占用量更大(分配混合单元和混合缓冲,或将声音储存在主存而非声卡缓存中)。 Channels and sounds. 当你加载完你的声音后就须要播放,这时会用到System::playSound函数,它返回一个Channel / FMOD_CHANNEL句柄。 参数channelid通常设为FMOD_CHANNEL_FREE,表示FMOD将自动选择一个未使用的通道来播放。 2D vs 3D. A 3D sound source is a channel that has a position and a velocity. When a 3D channel is playing, its volume, speaker placement and pitch will be affected automatically based on the relation to the listener. A listener is the player, or the game camera. It has a position, velocity like a sound source, but it also has an orientation.
listener和source间的相对距离决定音量。 listener和source
间的相对速度决定音调(多普勒现象)。 listener相对于source
的方位决定左右声道平衡或扬声器位置。
2D sound不受3D sound中listener的影响 ,没有多普勒现象,不衰减,也不受扬声器位置影响。 创建2D sound可以调用Channel::setSpeakerMix、Channel::setSpeakerLevels或Channel::setPan。 创建3D sound可以调用所有函数名中含有3D的函数.
For a more detailed description of 3D sound, read the tutorial in the documentation on 3D sound. |