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

什么是FMOD?
FMOD是一個(gè)非常簡(jiǎn)單通用的audio引擎,可以在windows,winCE,Linux,GameCube Xbox等平臺(tái)上很好運(yùn)行.
FMOD是一個(gè)共享軟件,如果不用于商業(yè)用途可以免費(fèi)使用,商業(yè)用途需要付費(fèi)100美金.詳細(xì)情況請(qǐng)關(guān)注www.fomod.org

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

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

所有的music如:mod,s3m,xm,it,mid,rmi,sgt,fsb 都通過(guò)FMUSIC 這個(gè)API來(lái)播放
FSOUND API是提供給壓縮格式使用的.文件一般如:wav,mp3,ogg,raw等
這些格式,你都可以通過(guò)別的軟件進(jìn)行互相轉(zhuǎn)換.
如果你要播放的單樂(lè)是像炮彈發(fā)射一樣的短小的聲音,那么你可以將這些聲間轉(zhuǎn)成sample
Samples將在播放前先解壓到內(nèi)存,而且可以多次播放.
如果你要播放的是象背景音樂(lè)一樣的較長(zhǎng)的音樂(lè),你可以得到這個(gè)音樂(lè)轉(zhuǎn)化為流,這將導(dǎo)致
使用一些CPU和內(nèi)存,因?yàn)槲募拇疟P(pán)讀取然后轉(zhuǎn)成流需要一個(gè)過(guò)程.同時(shí)需要注意一點(diǎn),在
同一時(shí)間不能多次播放

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

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

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

循環(huán)播放
FMUSIC_SetLooping(handle,true);

停止音樂(lè)
FMUSIC_StopSong(handle);

free內(nèi)存
FMUSIC_FreeSong(handle);

一個(gè)例子:
#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();
}


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

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

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

停止音樂(lè)
FSOUND_StopSound (handle);

清除內(nèi)存
FSOUND_Sample_Free(handle);

看一個(gè)例子:
#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);

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

一個(gè)例子:
#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();
}