menu
 
バージョン
2021.1.14.8108

2024.1.6.8842

2023.1.14.8770

2022.1.19.8584

2021.1.14.8108

2019.2.15.7667

2019.1.11.7296

2018.1.11.6987

2017.2.10.6745

2017.1.9.6501

2016.2.6.6153

2015.1.9.5624

menu

Wwise Unity Integration Documentation
サウンドエンジンの制御にC::コードを使う

Wwise SDKの大部分の機能を AkSoundEngine クラスからUnityで利用できます。C++ ネームスペースの AK::SoundEngineAK::MusicEngine などの代わりとしてとらえることができます。元のSDKと比較してAPIバインディングにどのような変更があったかは、 APIの変更と制約 を参照してください。 複雑な状況の場合は、コードでWwiseファンクションをコールする必要があります。APIで、全てのファンクションの GameObjectID がGameObjectのUnityフレーバーに置き換えられます。AkGameObj コンポーネントを既にマニュアル操作で追加してない限り、ランタイムにこのGameObjectに自動的に追加されます。

EventやBankの文字列の代わりに数値IDを使う

ネーティブWwise APIで、文字列やIDを使ってWwiseプロジェクト内のイベントやその他の名前の付いたオブジェクトをトリガーできます。C::の世界でも、 Wwise_IDs.h ファイルを Wwise_IDs.cs に変換することで、同様にできます。Assets > Wwise > Convert Wwise SoundBank IDsをクリックする。これが機能するには、Pythonをインストールしておく必要があります。

MIDIをWwiseに送る

MIDIをWwiseに送るには、 AkMIDIPostArray クラスの AkMIDIPost メンバーをfillして、以下の方式のどれかをコールします:

以下は、サウンドエンジンにMIDIメッセージを送るための基本的なスクリプトです。

public class MyMIDIBehaviour : UnityEngine.MonoBehaviour
{
public AK.Wwise.Event SynthEvent;
private void Start()
{
AkMIDIPostArray MIDIPostArrayBuffer = new AkMIDIPostArray(6);
AkMIDIPost midiEvent = new AkMIDIPost();
midiEvent.byType = AkMIDIEventTypes.NOTE_ON;
midiEvent.byChan = 0;
midiEvent.byOnOffNote = 56;
midiEvent.byVelocity = 127;
midiEvent.uOffset = 0;
MIDIPostArrayBuffer[0] = midiEvent;
midiEvent.byOnOffNote = 60;
MIDIPostArrayBuffer[1] = midiEvent;
midiEvent.byOnOffNote = 64;
MIDIPostArrayBuffer[2] = midiEvent;
midiEvent.byType = AkMIDIEventTypes.NOTE_OFF;
midiEvent.byOnOffNote = 56;
midiEvent.byVelocity = 0;
midiEvent.uOffset = 48000 * 8;
MIDIPostArrayBuffer[3] = midiEvent;
midiEvent.byOnOffNote = 60;
MIDIPostArrayBuffer[4] = midiEvent;
midiEvent.byOnOffNote = 64;
MIDIPostArrayBuffer[5] = midiEvent;
SynthEvent.PostMIDI(gameObject, MIDIPostArrayBuffer);
}
}

Unityのオフラインレンダリング

Unityインテグレーションで、 オフラインレンダリング機能が Wwise SDK内 で公開されるので、オーディオサンプルの取得方法が簡素化されます。

warning 注意: オフラインレンダリングを有効にする場合は、非同期の AkSoundEngine.LoadBankAkSoundEngine.UnloadBank のAPIだけがメインスレッドから使われるように、注意してください。詳細は Custom Scheduling of Audio Rendering を確認してください。

AkSoundEngine.StartDeviceCapture で、キャプチャする特定の出力オーディオデバイスをセットアップすることで、 AkSoundEngine.UpdateCaptureSampleCount をコールして利用可能なサンプル数を確認することができ、 AkSoundEngine.GetCaptureSamples をコールしてオーディオサンプルを取得できるようになります。

以下は、Unityインテグレーションを使い、Unityのスクリーンキャプチャ機能と合わせてオフラインオーディオレンダリングを実行する方法を示した例です。この機能を実装すれば、マルチプレックスのポストプロセスの処理を簡単に実行して、キャプチャしたオーディオサンプルと動画フレームを組み合わせてムービーにすることができます。

public abstract class WwiseOfflineRenderer : UnityEngine.MonoBehaviour
{
public bool IsOfflineRendering { get; set; }
public bool StartWithOfflineRenderingEnabled = false;
private bool IsCurrentlyOfflineRendering = false;
public float FrameRate = 25.0f;
protected ulong OutputDeviceId = 0;
public abstract string GetUniqueScreenshotFileName(int frameCount);
public abstract void ProcessAudioSamples(float[] buffer);
protected void Start()
{
OutputDeviceId = AkSoundEngine.GetOutputID(AkSoundEngine.AK_INVALID_UNIQUE_ID, 0);
if (StartWithOfflineRenderingEnabled)
{
IsOfflineRendering = true;
Update();
}
}
private void LogAudioFormatInfo()
{
var sampleRate = AkSoundEngine.GetSampleRate();
var channelConfig = new AkChannelConfig();
var audioSinkCapabilities = new Ak3DAudioSinkCapabilities();
AkSoundEngine.GetOutputDeviceConfiguration(OutputDeviceId, channelConfig, audioSinkCapabilities);
UnityEngine.Debug.LogFormat("Sample Rate: {0}, Channels: {1}", sampleRate, channelConfig.uNumChannels);
}
protected void Update()
{
if (IsOfflineRendering != IsCurrentlyOfflineRendering)
{
IsCurrentlyOfflineRendering = IsOfflineRendering;
if (IsOfflineRendering)
{
#if UNITY_EDITOR
// エディタのアップデートで AkSoundEngine.RenderAudio() がコールされないように注意する。
AkSoundEngineController.Instance.DisableEditorLateUpdate();
#endif
LogAudioFormatInfo();
AkSoundEngine.ClearCaptureData();
AkSoundEngine.StartDeviceCapture(OutputDeviceId);
}
else
{
AkSoundEngine.StopDeviceCapture(OutputDeviceId);
#if UNITY_EDITOR
// エディタのアップデートコールを AkSoundEngine.RenderAudio() に戻す。
AkSoundEngineController.Instance.EnableEditorLateUpdate();
#endif
}
}
var frameTime = IsOfflineRendering && FrameRate != 0.0f ?1.0f / FrameRate : 0.0f;
UnityEngine.Time.captureDeltaTime = frameTime;
AkSoundEngine.SetOfflineRenderingFrameTime(frameTime);
AkSoundEngine.SetOfflineRendering(IsOfflineRendering);
if (!IsOfflineRendering)
return;
UnityEngine.ScreenCapture.CaptureScreenshot(GetUniqueScreenshotFileName(UnityEngine.Time.frameCount));
var sampleCount = AkSoundEngine.UpdateCaptureSampleCount(OutputDeviceId);
if (sampleCount <= 0)
return;
var buffer = new float[sampleCount];
var count = AkSoundEngine.GetCaptureSamples(OutputDeviceId, buffer, (uint)buffer.Length);
if (count <= 0)
return;
ProcessAudioSamples(buffer);
}
}

UnityでAudio Input Sourceプラグインを使う

Audio Input Sourceプラグインを、C::スクリプト経由で使えます。Wwise SDKドキュメンテーションの、オーディオ入力ソースプラグインを参照してください。

以下は、Audio Input Sourceプラグインにテストトーンを送るための基本的なスクリプトです。

public class MyAudioInputBehaviour : UnityEngine.MonoBehaviour
{
public AK.Wwise.Event AudioInputEvent;
public uint SampleRate = 48000;
public uint NumberOfChannels = 1;
public uint SampleIndex = 0;
public uint Frequency = 880;
private bool IsPlaying = true;
// Callback that fills audio samples - This function is called each frame for every channel.
bool AudioSamplesDelegate(uint playingID, uint channelIndex, float[] samples)
{
for (uint i = 0; i < samples.Length; ++i)
samples[i] = UnityEngine.Mathf.Sin(Frequency * 2 * UnityEngine.Mathf.PI * (i + SampleIndex) / SampleRate);
if (channelIndex == NumberOfChannels - 1)
SampleIndex = (uint)(SampleIndex + samples.Length) % SampleRate;
// これ以上提供するデータがないことを示すために、return falseとします。これで、関連するイベントも停止されます。
return IsPlaying;
}
// Callback that sets the audio format - This function is called once before samples are requested.
void AudioFormatDelegate(uint playingID, AkAudioFormat audioFormat)
{
// Channel configuration and sample rate are the main parameters that need to be set.
audioFormat.channelConfig.uNumChannels = NumberOfChannels;
audioFormat.uSampleRate = SampleRate;
}
private void Start()
{
// The AudioInputEvent event, that is setup within Wwise to use the Audio Input plug-in, is posted on gameObject.
// AudioFormatDelegate is called once, and AudioSamplesDelegate is called once per frame until it returns false.
AkAudioInputManager.PostAudioInputEvent(AudioInputEvent, gameObject, AudioSamplesDelegate, AudioFormatDelegate);
}
// コールバックを停止するために、このメソッドをほかのスクリプトからコールできます
public void StopSound()
{
IsPlaying = false;
}
private void OnDestroy()
{
AudioInputEvent.Stop(gameObject);
}
}

ポジショニングのカスタム設定をUnityで適用する

デフォルトで、 AkGameObj コンポーネントは特定のUnity gameObject に添付されて、そのtransform(オフセットの設定も可)を使ってフルポジショニングを行います。ファーストパーソンシューティングゲームなど多くのゲームは、これで充分です。一方、オーディオリスナーを単純に1つのゲームオブジェクト、例えばUnityのメインカメラなどに添付するだけでは、多くのサードパーソンゲームなどにあるカスタム設定されたカメラ角度で、ポジショニングに対応するための2つの要素(距離減衰とスペーシャリゼーション)に対応するのが難しい場合があります。また、その他のカスタム設定ポジショニングをプレイヤーに提供するゲームも、考えられます。

このような使い方のために、 AkGameObj コンポーネントクラスで、Unityユーザーはポジショニングをオーバーライドできます。GetPosition()、 GetForward()、 GetUpward() という3つのバーチャル方式を使って、ユーザーは AkGameObj からサブクラスを取り、そのサブクラスコンポーネントでUnity gameObjects のポジショニングをいくつでもカスタマイズできます。

デフォルトの AkAudioListener 動作を、カスタムコンポーネントを使ってオーバーライドするシンプルな例を、ここに示します。Wwiseにサードパーソンのプロジェクトをインテグレートした状態で、既存の AkAudioListener と、それに関連付けられた AkGameObj を削除します。次に、以下のスクリプトをMainCameraオブジェクトに添付して、さらに AkAudioListener を添付して、最後にオーディオリスナーのポジションが追うターゲットとして、Unity gameObject (例えばプレイヤーのアバター)を指定します。これ以降、全てのエミッターの距離減衰を計算するのに、選択したターゲットのUnity gameObject のポジションをリスナーポジション(画面上の距離リスナー)としてとらえる一方、全てのエミッターのオリエンテーションを決めるには、引き続きメインカメラのオリエンテーションをリスナーのオリエンテーションとしてとらえます(画面外のオリエンテーションリスナー)。

#if !(UNITY_DASHBOARD_WIDGET || UNITY_WEBPLAYER || UNITY_WII || UNITY_WIIU || UNITY_NACL || UNITY_FLASH || UNITY_BLACKBERRY) // Disable under unsupported platforms.
/*******************************************************************************
The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
Technology released in source code form as part of the game integration package.
The content of this file may not be used without valid licenses to the
AUDIOKINETIC Wwise Technology.
Note that the use of the game engine is subject to the Unity(R) Terms of
Service at https://unity3d.com/legal/terms-of-service
License Usage
Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
this file in accordance with the end user license agreement provided with the
software or, alternatively, in accordance with the terms contained
in a written agreement between you and Audiokinetic Inc.
Copyright (c) 2023 Audiokinetic Inc.
*/
using UnityEngine;
using System;
using System.Collections.Generic;
[AddComponentMenu ("Wwise/AkGameObj3rdPersonCam")]
[ExecuteInEditMode] //ExecuteInEditMode necessary to maintain proper state of isStaticObject.
public class AkGameObj3rdPersonCam : AkGameObj
{
public Transform target; // The position that this camera will be following. ユーザーはこれを、InspectorでプレイヤーキャラクターのUnity gameObjectに対して指定できます。
// 距離減衰に対応するために、カメラのポジションをプレイヤーポジションに設定します。
public override Vector3 GetPosition ()
{
return target.GetComponent<AkGameObj> ().GetPosition ();
}
}
#endif // #if !(UNITY_DASHBOARD_WIDGET || UNITY_WEBPLAYER || UNITY_WII || UNITY_WIIU || UNITY_NACL || UNITY_FLASH || UNITY_BLACKBERRY) // Disable under unsupported platforms.
Definition: AkWwiseAcousticTexture.cs:20
This type can be used to post Events to the sound engine.
Definition: AkWwiseEvent.cs:28
This component represents a sound object in your scene tracking its position and other game syncs suc...
Definition: AkGameObj.cs:32
Definition: AkWwiseAcousticTexture.cs:20
virtual UnityEngine.Vector3 GetPosition()
Definition: AkGameObj.cs:221

このページはお役に立ちましたか?

サポートは必要ですか?

ご質問や問題、ご不明点はございますか?お気軽にお問い合わせください。

サポートページをご確認ください

あなたのプロジェクトについて教えてください。ご不明な点はありませんか。

プロジェクトを登録していただくことで、ご利用開始のサポートをいたします。

Wwiseからはじめよう