跳轉到內容

跨平臺遊戲程式設計:gameplay3d/音訊

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

Gameplay3d 支援 3D 音訊(使用 OpenAL,其規範可以在 此處 找到)。在 gameplay3d 中使用聲音的基本原理如下

  • 您的遊戲可以擁有一個或多個 AudioSource,如果需要,它們可以附加到 Node 並定位在 3D 空間中;
  • 非位置音訊,例如音樂,是透過建立和播放一個未附加到 NodeAudioSource 來實現的;
  • 您的遊戲有一個 AudioListener,它接收來自 AudioSources 的聲音。預設情況下,AudioListener 繫結到場景的活動攝像機,但可以根據需要附加到另一個攝像機或手動定位。

支援的音訊格式

[編輯 | 編輯原始碼]

Gameplay3d 目前支援以下格式

  • .ogg vorbis 音訊,這是一個免費的開源壓縮音訊格式規範;
  • .wav 檔案,這是一個 Microsoft 和 IBM 未壓縮的音訊格式。

建議在遊戲發行版中使用 .ogg 檔案,因為它們是壓縮的,因此使用更少的記憶體。

建立 AudioSource

[編輯 | 編輯原始碼]

AudioSources 可以從音訊樣本檔案建立,如下所示

AudioSource* wheelsSound = AudioSource::create("res/explosion.wav");
AudioSource* backgroundMusic = AudioSource::create("res/music.ogg");

AudioSources 也可以從 .audio 檔案建立和配置 - 以下內容將詳細介紹。

定位 AudioSource

[編輯 | 編輯原始碼]

可以使用 Node::setAudioSource()AudioSource 繫結到場景中的 Node。當節點變換時,音訊源的位置會自動更新。

例如,我們可以使用以下示例來定位一個發出鳥叫聲的 AudioSource


// Create and configure the AudioSource
AudioSource* audioSource = AudioSource::create("res/birdsound.ogg");
assert(audioSource);
audioSource->setLooped(true);

// Create a new node and set the AudioSource
Node* birdNode = _scene->addNode("bird");
birdNode->setAudioSource(audioSource);
SAFE_RELEASE(audioSource);

// Position the node (high up in the trees), add it to the scene and play the sound
birdNode->setTranslation(10.0f, 100.0f, 30.0f);
_scene->addNode(birdNode);
birdNode->getAudioSource()->play();
SAFE_RELEASE(birdNode);

播放和控制 AudioSource

[編輯 | 編輯原始碼]

要播放音訊源

audioSource->play();

其他控制播放的成員函式是 pause()resume()stop()rewind()

我們可以配置聲音,如下所示

audioSource->setLooped(true); // - Play the sound in a loop.

audioSource->setGain(0.5f);   // - Set the gain/volume of the audio source. 1.0 means that the sound is unattenuated.
                              //   Gain larger than one (i.e. amplification) is permitted for source and listener.
                              //   However, the implementation is free to clamp the total gain (effective gain per-source
                              //   multiplied by the listener gain) to one to prevent overflow.

audioSource->setPitch(2.0f);  // - 1.0 equals identity. Each reduction by 50 percent equals a pitch shift of -12
                              //   semitones (one octave reduction). Each doubling equals a pitch shift of 12
                              //   semitones (one octave increase). Zero is not a legal value.

audioSource->setVelocity(1.0f, 0.0f, 15.0f);   // - Velocity is taken into account by the OpenAL driver to synthesize the
                                               //   Doppler effect perceived by the listener for each source, based on the
                                               //   velocity of both source and listener (and the OpenAL Doppler-related
                                               //   parameters).

手動定位 AudioListener

[編輯 | 編輯原始碼]

AudioListener 可以手動定位(在這種情況下,位於場景的原點),如下所示

AudioListener::getInstance()->setCamera(NULL);
AudioListener::getInstance()->setPosition(0.0f, 0.0f, 0.0f);

AudioListener 可以附加到特定的攝像機(例如,當螢幕上同時有多個視角時),如下所示

AudioListener::getInstance()->setCamera(mainCamera);

.audio 檔案

[編輯 | 編輯原始碼]

以下示例說明了如何使用 .audio 檔案設定所有 AudioSource 屬性。

在您的 .cpp 檔案中新增以下內容

AudioSource* source = AudioSource::create("res/game.audio#fireball");

在您的 game.audio 檔案中新增以下內容

audio fireball
{
    path = res/audio/fireball.wav
    looped = false
    gain = 0.7
    pitch = 0.5
    velocity = 0.5 0.0 1.0
}
華夏公益教科書