Skip to content

Wwise Sound Engine provider for the Collision Sounds framework #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ namespace NewtonVR
public class NVRCollisionSoundControllerEditor : Editor
{
private const string FMODDefine = "NVR_FMOD";
//---
// @ROGUESUN BEGIN: Ben - Wwise support
//---
private const string WwiseDefine = "NVR_WWISE";
//---
// @ROGUESUN END
//---

private static bool hasReloaded = false;
private static bool waitingForReload = false;
Expand All @@ -36,6 +43,8 @@ public class NVRCollisionSoundControllerEditor : Editor
private static bool hasFMODSDK = false;
//private static bool hasFMODDefine = false;

private static bool hasWwiseSDK = false;

private static string progressBarMessage = null;

[DidReloadScripts]
Expand All @@ -44,6 +53,8 @@ private static void DidReloadScripts()
hasReloaded = true;
hasFMODSDK = DoesTypeExist("FMODPlatform");

hasWwiseSDK = DoesTypeExist("AkSoundEngine");

//string scriptingDefine = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
//string[] scriptingDefines = scriptingDefine.Split(';');
//hasFMODDefine = scriptingDefines.Contains(FMODDefine);
Expand Down Expand Up @@ -133,9 +144,12 @@ public override void OnInspectorGUI()
HasWaitedLongEnough();

bool installFMOD = false;
bool installWwise = false;
bool isFMODEnabled = controller.SoundEngine == NVRCollisionSoundProviders.FMOD;
bool isWwiseEnabled = controller.SoundEngine == NVRCollisionSoundProviders.Wwise;
bool isUnityEnabled = controller.SoundEngine == NVRCollisionSoundProviders.Unity;
bool enableFMOD = controller.SoundEngine == NVRCollisionSoundProviders.FMOD;
bool enableWwise = controller.SoundEngine == NVRCollisionSoundProviders.Wwise;
bool enableUnity = controller.SoundEngine == NVRCollisionSoundProviders.Unity;


Expand All @@ -153,7 +167,22 @@ public override void OnInspectorGUI()
enableFMOD = EditorGUILayout.Toggle("Use FMOD", enableFMOD);
}
EditorGUILayout.EndHorizontal();


EditorGUILayout.BeginHorizontal();
if (hasWwiseSDK == false)
{
using (new EditorGUI.DisabledScope(hasWwiseSDK == false))
{
EditorGUILayout.Toggle("Use Wwise", false);
}
installWwise = GUILayout.Button("Install Wwise");
}
else
{
enableWwise = EditorGUILayout.Toggle("Use Wwise", enableWwise);
}
EditorGUILayout.EndHorizontal();

EditorGUILayout.BeginHorizontal();
enableUnity = EditorGUILayout.Toggle("Use Unity Sound", enableUnity);
EditorGUILayout.EndHorizontal();
Expand All @@ -174,15 +203,27 @@ public override void OnInspectorGUI()
controller.SoundEngine = NVRCollisionSoundProviders.FMOD;
}

if (enableWwise == false && isWwiseEnabled == true)
{
RemoveDefine(WwiseDefine);
controller.SoundEngine = NVRCollisionSoundProviders.None;
}
else if (enableWwise == true && isWwiseEnabled == false)
{
AddDefine(WwiseDefine);
controller.SoundEngine = NVRCollisionSoundProviders.Wwise;
}

if (enableUnity == false && isUnityEnabled == true)
{
RemoveDefine(FMODDefine);
RemoveDefine(WwiseDefine);
controller.SoundEngine = NVRCollisionSoundProviders.None;
}
else if (enableUnity == true && isUnityEnabled == false)
{
RemoveDefine(FMODDefine);
RemoveDefine(WwiseDefine);
controller.SoundEngine = NVRCollisionSoundProviders.Unity;
}

Expand All @@ -191,7 +232,10 @@ public override void OnInspectorGUI()
{
Application.OpenURL("http://www.fmod.org/download/");
}

if (installWwise == true)
{
Application.OpenURL("https://www.audiokinetic.com/download/");
}

DrawDefaultInspector();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ private void Awake()
{
Instance = this;

#if NVR_FMOD
#if NVR_FMOD
Provider = this.gameObject.AddComponent<NVRCollisionSoundProviderFMOD>();
#else
#elif NVR_WWISE
Provider = this.gameObject.AddComponent<NVRCollisionSoundProviderWwise>();
#else
Provider = this.gameObject.AddComponent<NVRCollisionSoundProviderUnity>();
#endif
}
Expand All @@ -48,5 +50,6 @@ public enum NVRCollisionSoundProviders
None,
Unity,
FMOD,
Wwise
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

#if NVR_WWISE

namespace NewtonVR
{
//
//
// The Wwise provider for the Collidion Sound framework.
//
// The Wwise events for the collision sounds are Play_sfx_collision_ with the Collision Sound material added
// to the end, for example Play_sfx_collision_wood and Play_sfx_collision_metal.
// The RTPC impactVolume (0 to 100) is used to convey the impact volume to the sound engine.
//
// The WwiseCollisionSoundPrefab prefab should be on a layer that will only collider with
// any Wwise environments set up.
//
public class NVRCollisionSoundProviderWwise : NVRCollisionSoundProvider
{
private const string kSoundEventPrefix = "Play_sfx_collision_";
private const string kImpactVolumeControlName = "impactVolume";
private static string AudioSourcePrefabPath = "WwiseCollisionSoundPrefab";
private GameObject AudioSourcePrefab;

private AkGameObj[] AudioPool;
private int CurrentPoolIndex;
private uint mImpactVolumeControlId;

public bool showCollisions;

private bool mWwiseAvailable;

private static Dictionary<NVRCollisionSoundMaterials, string> eventStrings;
public static Dictionary<NVRCollisionSoundMaterials, string> EventStrings
{
get
{
if (eventStrings == null)
{
eventStrings = new Dictionary<NVRCollisionSoundMaterials, string>(new EnumEqualityComparer<NVRCollisionSoundMaterials>());

foreach (NVRCollisionSoundMaterials mat in NVRCollisionSoundMaterialsList.List)
{
if (mat == NVRCollisionSoundMaterials.EndNewtonVRMaterials)
{
continue;
}

string event_name = string.Format(kSoundEventPrefix + "{0}", mat.ToString());
eventStrings.Add(mat, event_name);
//Debug.Log("Generated event name for \"" + mat.ToString() + "\": \"" + event_name + "\"");
}
}
return eventStrings;
}
}

public override void Awake()
{
if (!AkSoundEngine.IsInitialized())
{
//
// Check to see if the AkInitializer is in the scene
//
AkInitializer initializer = GameObject.FindObjectOfType<AkInitializer>();

if (initializer == null)
{
mWwiseAvailable = false;
Debug.LogError("Trying to use Wwise NewtonVR collision sounds but no AkInitializer in scene");
return;
}

Debug.LogWarning("Wwise NewtonVR collision sounds framework awake but Wwise not yet initialised");
}
mWwiseAvailable = true;
AudioPool = new AkGameObj[NVRCollisionSoundController.Instance.SoundPoolSize];

AudioSourcePrefab = Resources.Load<GameObject>(AudioSourcePrefabPath);

for (int index = 0; index < AudioPool.Length; index++)
{
AudioPool[index] = GameObject.Instantiate<GameObject>(AudioSourcePrefab).GetComponent<AkGameObj>();
AudioPool[index].transform.parent = this.transform;

//
// Disable the AkGameObjs until they are actually in use
//
AudioPool[index].gameObject.SetActive(false);
}

mImpactVolumeControlId = AkSoundEngine.GetIDFromString(kImpactVolumeControlName);
}

public override void Play(NVRCollisionSoundMaterials material, Vector3 position, float impactVolume)
{
if (!mWwiseAvailable)
{
return;
}
if (material == NVRCollisionSoundMaterials.none)
return;

string event_name = EventStrings[material];

AkGameObj game_obj = AudioPool[CurrentPoolIndex];
CurrentPoolIndex++;
if (CurrentPoolIndex >= AudioPool.Length)
{
CurrentPoolIndex = 0;
}

game_obj.gameObject.SetActive(true);
Collider collider = game_obj.GetComponent<Collider>();
if (collider != null)
{
collider.enabled = true;
}

if (showCollisions)
{
MeshRenderer renderer = game_obj.GetComponent<MeshRenderer>();
if (renderer != null)
{
renderer.enabled = true;
}
}

//
// Position the object and post the event.
//
game_obj.transform.position = position;

//
// use the impactVolume to control the sound
//
float impact_value = impactVolume * 100.0f;
//Debug.Log("impactVolume = " + impactVolume);
AkSoundEngine.SetRTPCValue(mImpactVolumeControlId, impact_value, AudioPool[CurrentPoolIndex].gameObject);

//Debug.Log("Position in Unity: " + AudioPool[CurrentPoolIndex].transform.position +
// " position in Wise: " + AudioPool[CurrentPoolIndex].GetPosition());
uint res = AkSoundEngine.PostEvent(event_name, AudioPool[CurrentPoolIndex].gameObject,
(uint)AkCallbackType.AK_EndOfEvent, DisableGameObjectCallback, game_obj);

if (res == AkSoundEngine.AK_INVALID_PLAYING_ID)
{
//
// Failed to play the sound
//
DisableGameObject(game_obj);
}
}

void DisableGameObjectCallback(object in_cookie, AkCallbackType in_type, object in_info)
{
//Debug.Log("Hello from the callback");
if (in_type == AkCallbackType.AK_EndOfEvent)
{
//Debug.Log("Event has ended");
AkGameObj game_obj = in_cookie as AkGameObj;
DisableGameObject(game_obj);
}
}

void DisableGameObject(AkGameObj game_obj)
{
if (game_obj != null)
{
//
// Disable the collider and GameObject to save processing the object
// whilst it is not doing anything.
//
Collider collider = game_obj.GetComponent<Collider>();
if (collider != null)
{
collider.enabled = false;
}

MeshRenderer renderer = game_obj.GetComponent<MeshRenderer>();
if (renderer != null)
{
renderer.enabled = false;
}
game_obj.gameObject.SetActive(false);
}
}
}
}
#else

namespace NewtonVR
{
public class NVRCollisionSoundProviderWwise : NVRCollisionSoundProvider
{
public override void Awake()
{
}

public override void Play(NVRCollisionSoundMaterials material, Vector3 position, float impactVolume)
{
return;
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading