跳轉到內容

BlitzMax/模組/音訊/音訊播放

來自Wikibooks,開放世界中的開放書籍

BlitzMax音訊模組包含載入和播放聲音的命令。

可以使用LoadSound(載入聲音檔案)和PlaySound(透過系統音訊系統播放聲音,如果可用)的組合在BlitzMax中播放聲音檔案。

BlitzMax 原生支援 .wav(未壓縮)和 .ogg(壓縮)兩種格式的聲音檔案。

可以使用各種音訊通道運算子控制聲音的播放,包括SetChannelVolumeSetChannelPanSetChannelDepthSetChannelRate

通道控制代碼可以從PlaySoundCueSound的返回值獲取,也可以透過AllocChannel預留通道獲取。

音訊聲音型別

方法
  • 播放
  • 提示
函式
  • 載入

TSound:方法

[編輯 | 編輯原始碼]
播放

方法 Play:TChannel( alloced_channel:TChannel=Null )

描述:播放聲音

返回值:一個音訊通道物件

資訊:開始透過音訊通道播放聲音。如果未指定通道,Play 會自動為您分配一個通道。

提示

方法 Cue:TChannel( alloced_channel:TChannel=Null )

描述:提示聲音進行播放

返回值:一個音訊通道物件

資訊:準備音訊通道以播放聲音。要實際開始播放聲音,必須使用通道的 SetPaused 方法。如果未指定通道,Cue 會自動為您分配一個通道。

Cue 允許您在聲音實際開始播放之前設定各種音訊通道狀態,例如音量、聲場、深度和速率。

TSound:函式

[編輯 | 編輯原始碼]
載入

函式 Load:TSound( url:Object,loop_flag )

描述:載入聲音

返回值:一個聲音物件

資訊url 可以是字串、流或音訊樣本物件。可以使用 PlayCue 播放返回的聲音物件。

音訊通道型別

方法
  • 停止
  • 設定暫停
  • 設定音量
  • 設定聲場
  • 設定深度
  • 設定速率
  • 播放中

TChannel:方法

[編輯 | 編輯原始碼]
停止

方法 Stop()

描述:停止音訊通道播放

資訊:關閉音訊通道。此音訊通道上的進一步命令將無效。

設定暫停

方法 SetPaused( paused )

描述:暫停或取消暫停音訊通道播放

資訊:如果paused為True,則暫停音訊通道。否則,取消暫停音訊通道。

設定音量

方法 SetVolume( volume# )

描述:設定音訊通道音量

資訊volume應在0(靜音)到1(最大音量)之間。

設定聲場

方法 SetPan( pan# )

描述:設定音訊通道立體聲聲場

資訊pan應在-1(最左)到1(最右)之間。

設定深度

方法 SetDepth( depth# )

描述:設定音訊通道深度

資訊depth應在-1(後)到1(前)之間。

設定速率

方法 SetRate( rate# )

描述:設定音訊通道播放速率

資訊rate是用於修改音訊通道頻率的乘數。例如,速率為0.5將導致音訊通道以半速播放(即:降低一個八度),而速率為2將導致音訊通道以雙倍速度播放(即:提高一個八度)。

播放中

方法 Playing()

描述:確定音訊通道是否正在播放

返回值:如果channel當前正在播放,則返回True

資訊:如果音訊通道已暫停或已使用Stop停止,則Playing將返回False。

LoadSound

[編輯 | 編輯原始碼]

函式 LoadSound:TSound( url:Object,flags=0 )

描述:載入聲音

返回值:一個聲音物件

資訊url可以是字串、流或TAudioSample物件。可以使用PlaySoundCueSound播放返回的聲音。

flags引數可以是以下任何組合

標誌值 效果
SOUND_LOOP 播放時聲音應迴圈。
SOUND_HARDWARE 如果可能,聲音應放置在板載音效卡記憶體中。

要組合標誌,請使用二進位制“或”運算子:“|”。

示例:

Rem
Load and Play a small example wav file.
End Rem

sound=LoadSound("shoot.wav")
PlaySound sound

Input "Press any key to continue"

PlaySound

[編輯 | 編輯原始碼]

函式 PlaySound:TChannel( sound:TSound,channel:TChannel=Null )

描述:播放聲音

返回值:一個音訊通道物件

資訊PlaySound開始透過音訊通道播放聲音。如果未指定channelPlaySound會自動為您分配一個通道。

示例:

Rem
Load and Play a small example wav file with looping.
End Rem

sound=LoadSound("shoot.wav",true)
PlaySound sound

Input "Press any key to continue"

函式 CueSound:TChannel( sound:TSound,channel:TChannel=Null )

描述:提示聲音

返回值:一個音訊通道物件

資訊:準備透過音訊通道播放聲音。要實際開始播放聲音,必須使用ResumeChannel。如果未指定channelCueSound會自動為您分配一個通道。

CueSound允許您在聲音實際開始播放之前設定各種音訊通道狀態,例如音量、聲場、深度和速率。

示例:

Rem
CueSound example
End Rem

sound=LoadSound("shoot.wav")
channel=CueSound(sound)

Input "Press return key to play cued sound"

ResumeChannel channel

Input "Press return key to quit"

AllocChannel

[編輯 | 編輯原始碼]

函式 AllocChannel:TChannel()

描述:分配音訊通道

返回值:一個音訊通道物件

資訊:分配音訊通道以用於PlaySoundCueSound。完成音訊通道後,應使用StopChannel

示例:

'AllocChannel.bmx

timer=CreateTimer(20)

sound=LoadSound ("shoot.wav")
channel=AllocChannel()

For i=1 To 20
	WaitTimer timer
	PlaySound sound,channel
Next

StopChannel

[編輯 | 編輯原始碼]

函式 StopChannel( channel:TChannel )

描述:停止一個音訊通道

資訊:關閉一個音訊通道。之後使用此通道的命令將無效。

示例:

Rem
StopChannel example
End Rem

sound=LoadSound("shoot.wav",true)
channel=PlaySound(sound)

print "channel="+channel

Input "Press return key to stop sound"

StopChannel channel

Input "Press return key to quit"

ChannelPlaying

[編輯 | 編輯原始碼]

函式 ChannelPlaying( channel:TChannel )

描述:確定一個音訊通道是否正在播放

返回值:如果channel當前正在播放,則返回True

資訊:如果通道使用PauseChannel暫停或使用StopChannel停止,則ChannelPlaying將返回False

示例:

' channelplaying.bmx

sound = LoadSound ("shoot.wav")

Input "Hit return to begin channelplaying test, use ctrl-C to exit"

channel=PlaySound (sound)
While True
	Print "ChannelPlaying(channel)="+ChannelPlaying(channel)
Wend

SetChannelVolume

[編輯 | 編輯原始碼]

函式 SetChannelVolume( channel:TChannel,volume# )

描述:設定音訊通道的播放音量

資訊volume應該在0(靜音)到1(最大音量)之間。

示例:

' setchannelvolume.bmx

timer=CreateTimer(20)

sound = LoadSound ("shoot.wav")

For volume#=.1 To 2 Step .05
	WaitTimer timer
	channel=CueSound(sound)
	SetChannelVolume channel,volume
	ResumeChannel channel
Next

SetChannelPan

[編輯 | 編輯原始碼]

函式 SetChannelPan( channel:TChannel,pan# )

描述:設定音訊通道的立體聲平衡

資訊pan應該在-1(左)到1(右)之間。

示例:

' setchannelpan.bmx

Graphics 640, 480

channel = AllocChannel ()
sound = LoadSound ("shoot.wav") ' Use a short sample...

Repeat
	If MouseHit(1) PlaySound sound,channel
	
	pan# = MouseX () / (GraphicsWidth () / 2.0) - 1
	vol# = 1 - MouseY () / 480.0
	SetChannelPan channel, pan
	SetChannelVolume channel, vol*2

	Cls
	DrawText "Click to play...", 240, 200
	DrawText "Pan   : " + pan, 240, 220
	DrawText "Volume: " + vol, 240, 240

	Flip
Until KeyHit (KEY_ESCAPE)

End

SetChannelDepth

[編輯 | 編輯原始碼]

函式 SetChannelDepth( channel:TChannel,depth# )

描述:設定音訊通道的環繞聲深度

資訊depth應該在-1(後)到1(前)之間。

示例:

' setchanneldepth.bmx

Graphics 640, 480

channel = AllocChannel ()
sound = LoadSound ("shoot.wav") ' Use a short sample...

Repeat
	If MouseHit(1) PlaySound sound,channel
	
	pan# = MouseX () / (640 / 2.0) - 1
	depth# = MouseY () / (480 /2.0) -1
	
	SetChannelPan channel,pan
	SetChannelDepth channel,depth

	Cls
	DrawText "Click to play...", 240, 200
	DrawText "Pan   : " + pan, 240, 220
	DrawText "Depth : " + depth, 240, 240

	Flip
Until KeyHit (KEY_ESCAPE)

End

SetChannelRate

[編輯 | 編輯原始碼]

函式 SetChannelRate( channel:TChannel,rate# )

描述:設定音訊通道的播放速度

資訊rate是用於修改音訊通道頻率的乘數。例如,速率為0.5將導致音訊通道以半速播放(即:降低一個八度),而速率為2將導致音訊通道以雙倍速度播放(即:提高一個八度)。

示例:

' setchannelrate.bmx

timer=CreateTimer(20)

sound = LoadSound ("shoot.wav",True)
channel=CueSound(sound)
ResumeChannel channel

For rate#=1.0 To 4 Step 0.01
	WaitTimer timer
	SetChannelRate channel,rate
Next

PauseChannel

[編輯 | 編輯原始碼]

函式 PauseChannel( channel:TChannel )

描述:暫停音訊通道播放

資訊:暫停音訊通道播放。

ResumeChannel

[編輯 | 編輯原始碼]

函式 ResumeChannel( channel:TChannel )

描述:恢復音訊通道播放

資訊:在使用CueSoundPauseChannel暫停後恢復音訊通道播放。

AudioDrivers

[編輯 | 編輯原始碼]

函式 AudioDrivers$[]()

描述:獲取音訊驅動程式

資訊:返回一個字串陣列,其中每個字串描述一個音訊驅動程式。

AudioDriverExists

[編輯 | 編輯原始碼]

函式 AudioDriverExists( name$ )

描述:確定音訊驅動程式是否存在

資訊:如果由driver指定的音訊驅動程式存在,則返回True。

SetAudioDriver

[編輯 | 編輯原始碼]

函式 SetAudioDriver( name$ )

描述:設定當前音訊驅動程式

資訊:如果音訊驅動程式成功設定,則返回true。

華夏公益教科書