FMOD.dll的介紹
翻譯(Robin)
Email:eway365@hotmail.com

什么是FMOD?
FMOD是一個非常簡單通用的audio引擎,可以在windows,winCE,Linux,GameCube Xbox等平臺上很好運行.
FMOD是一個共享軟件,如果不用于商業用途可以免費使用,商業用途需要付費100美金.詳細情況請關注www.fomod.org

下載后將fmod.dll和fmodvc.lib加入你的工程中.并引用頭文件fmod.h
在使用fmod播入音樂之前,首先要初始化,代碼如下:
FSOUND_Init(44100,32,0);
第一個參數為單樂輸出的rate,單位為赫茲,在這里我們設置為44100.
第二個參數為設置最大的通道數量
第三個參數,可以指定一些標識,如果我們想的話.這里暫時置它為0.

好了,現在我們準備開始播放單樂了,可FMOD道底支持什么樣的單樂格式呢?
歌曲,采樣或是文件流(song,sample and stream)?
FMOD將它細分在兩個API中.他們分別是FSOUND和FMUSIC

所有的music如:mod,s3m,xm,it,mid,rmi,sgt,fsb 都通過FMUSIC 這個API來播放
FSOUND API是提供給壓縮格式使用的.文件一般如:wav,mp3,ogg,raw等
這些格式,你都可以通過別的軟件進行互相轉換.
如果你要播放的單樂是像炮彈發射一樣的短小的聲音,那么你可以將這些聲間轉成sample
Samples將在播放前先解壓到內存,而且可以多次播放.
如果你要播放的是象背景音樂一樣的較長的音樂,你可以得到這個音樂轉化為流,這將導致
使用一些CPU和內存,因為文件從磁盤讀取然后轉成流需要一個過程.同時需要注意一點,在
同一時間不能多次播放

FMUSIC:
用FMUSIC播放需要一個handle,看如下代碼:
handle = FMUSIC_LoadSong("YourFileName");
FMUSIC_PlaySong(handle);
現在你可能注意到,幾乎每個function的第一個參數都需要一個handle;

設置單量
FMUSIC_SetMasterVolume(handle,256);
256是最高音,0表示靜音

暫定單樂
FMUSIC_SetPaused(handle,true);
FMUSIC_SetPaused(handle,false);

循環播放
FMUSIC_SetLooping(handle,true);

停止音樂
FMUSIC_StopSong(handle);

free內存
FMUSIC_FreeSong(handle);

一個例子:
#include <conio.h>
#include "inc/fmod.h"

FMUSIC_MODULE* handle;

int main ()
{
?// init FMOD sound system
?FSOUND_Init (44100, 32, 0);

?// load song
?handle=FMUSIC_LoadSong ("canyon.mid");

?// play song only once
?// when you want to play a midi file you have to disable looping
?// BEFORE playing the song else this command has no effect!
?FMUSIC_SetLooping (handle, false);

?// play song
?FMUSIC_PlaySong (handle);

?// wait until the users hits a key to end the app
?while (!_kbhit())
?{
?}

?//clean up
?FMUSIC_FreeSong (handle);
?FSOUND_Close();
}


下面開始介紹FSOUND
得到FSOUND的句柄:
handle = FSOUND_Sample_Load(0,"yourFileName",0,0,0);
FSOUND_PlaySound(0,handle);
這些音效在播放前將被載入內存,所以可能需要一點點時間.
第二行命令的第一個參數是播放時使用的通道.

設置音量
FSOUND_SetVolume(handle,255);
255為最大音量,0表示靜音

暫停音樂
FSOUND_SetPaused(handle,true);
FSOUND_SetPaused(handle,false);

停止音樂
FSOUND_StopSound (handle);

清除內存
FSOUND_Sample_Free(handle);

看一個例子:
#include <conio.h>
#include "inc/fmod.h"

FSOUND_SAMPLE* handle;

int main ()
{
?// init FMOD sound system
?FSOUND_Init (44100, 32, 0);

?// load and play sample
?handle=FSOUND_Sample_Load (0,"sample.mp3",0, 0, 0);
?FSOUND_PlaySound (0,handle);

?// wait until the users hits a key to end the app
?while (!_kbhit())
?{
?}

?// clean up
?FSOUND_Sample_Free (handle);
?FSOUND_Close();
}

Streams:
handle=FSOUND_Stream_Open("YourFileName",0, 0, 0);
FSOUND_Stream_Play (0,handle);

停止音樂
FSOUND_Stream_Stop(handle);
清除
FSOUND_Stream_Close(handle);

一個例子:
#include <conio.h>
#include "inc/fmod.h"

FSOUND_STREAM* handle;

void main ()
{
?//init FMOD sound system
?FSOUND_Init (44100, 32, 0);

?//load and play sample
?handle=FSOUND_Stream_Open("sample.mp3",0, 0, 0);
?FSOUND_Stream_Play (0,handle);

?//wait until the users hits a key to end the app
?while (!_kbhit())
?{
?}

?//clean up
?FSOUND_Stream_Close(handle);
?FSOUND_Close();
}