using UnityEngine;
using System.Collections.Generic;
public class SoundManager : MonoBehaviour
{
private const byte RFC_PlaySoundFromFile = 218;
private const string RockMLCommandTypePlaySound = "PlaySound";
private const string RockMLArgName = "Name";
private const string RockMLArgFile = "File";
private const string RockMLArgPosX = "PosX";
private const string RockMLArgPosY = "PosY";
private const string RockMLArgPosZ = "PosZ";
private const string RockMLArgMinDistance = "MinDistance";
private const string RockMLArgMaxDistance = "MaxDistance";
private const string RockMLArgVolume = "Volume";
private const string RockMLArgDopplerLevel = "DopplerLevel";
private const string RockMLArgLoopCount = "LoopCount";
private const string RockMLArgDistortionLvl = "DistortionLvl";
private const string RockMLArglowPassCutoff = "LowPassCutoff";
private const string RockMLArglowPassRes = "LowPassRes";
private const string RockMLArgHighPassCutoff = "HighPassCutoff";
private const string RockMLArgHighPassRes = "HighPassRes";
private const string RockMLArgHighPassFilterOn = "HPF";
private const string RockMLArgLowPassFilterOn = "LPF";
private const string RockMLArgReverbFilterOn = "Reverb";
private const string RockMLArgReverbType = "ReverbType";
private const string RockMLArgLog = "LogRollOff";
private const string RockMLArgDelaySound = "DelaySound";
private const string RockMLArgSpatialBlend = "2DSound";
private const string RockMLArgSpread = "Spread";
private const string RockMLArgPitch = "Pitch";
//AudioMixer
private const string RockMLArgUseAudioMixer = "CleanMixer";
private const string RockMLArgRadioEffect = "RadioMixer";
private const string RockMLArgAmbienceMixer = "AmbienceMixer";
public static bool VoiceCommsActive = true;
public static bool VoiceCommsCanSend = true;
public static bool VoiceCommsCanReceive = true;
public static bool VoiceReliableSend = false;
public static int VoiceSendChunkSize = 640;
public static float VoiceSendVolume = 1.0f;
public static float VoiceReceiveVolume = 1.0f;
public static Dictionary
<string, SpawnedSoundObject
> SoundObjects
= new Dictionary
<string, SpawnedSoundObject
>(4);
public static SoundManager Instance = null;
private TNObject tnObject = null;
private void Awake()
{
Instance = this;
tnObject = GetComponent<TNObject>();
if (SimManager.IsHost)
{
if (RMLComms.Instance == null) RMLComms.Initialise();
RMLComms.Instance.RegisterHandler(RockMLCommandTypePlaySound, PlaySoundCommand);
}
DebugManager.TimerStart();
VoiceCommsHandler(null, null, null);
Config.Instance.RegisterHandler(Config.ALLOWVOICECOMMS, VoiceCommsHandler);
Config.Instance.RegisterHandler(Config.VOICESENDVOLUME, VoiceCommsHandler);
Config.Instance.RegisterHandler(Config.VOICERECEIVEVOLUME, VoiceCommsHandler);
Config.Instance.RegisterHandler(Config.VOICERELIABLESEND, VoiceCommsHandler);
Config.Instance.RegisterHandler(Config.VOICESENDCHUNKSIZE, VoiceCommsHandler);
DebugManager.TimerEnd("Startup Timer: HUDManager Init: ");
}
private void VoiceCommsHandler(string key, string value, string oldValue)
{
Config.Instance.GetValueBool(Config.ALLOWVOICECOMMS, ref VoiceCommsActive);
Config.Instance.GetValueFloat(Config.VOICESENDVOLUME, ref VoiceSendVolume);
Config.Instance.GetValueFloat(Config.VOICERECEIVEVOLUME, ref VoiceReceiveVolume);
Config.Instance.GetValueBool(Config.VOICERELIABLESEND, ref VoiceReliableSend);
Config.Instance.GetValueInt(Config.VOICESENDCHUNKSIZE, ref VoiceSendChunkSize);
}
public static void PlaySoundCommand(RockML.Command command)
{
string soundName = null;
string file = null;
float posX = 0.0f;
float posY = 0.0f;
float posZ = 0.0f;
float minDistance = 0.5f;
float maxDistance = 80.0f;
float volume = 1.0f;
float dopplerLevel = 0.1f;
int loopCount = 0;
float distortionLvl = 0.0f; //Distortion
float lowPassCutoff = 0.0f; //Low Pass Filter
float lowPassRes = 0.0f; //Low Pass Resonance //High Pass Filter
float highPassCutoff = 0.0f; //High Pass Cuttoff
float highPassRes = 0.0f; //High Pass Res
int hpf = 0; //High Pass Filter On/Off
int lpf = 0; //Low Pass Filter On/Off
int reverb = 0; //Turn Reverb On/Off
int reverbType = 0;
int radio = 0;
int mixer = 0;
int rollOffLog = 0;
int ambienceMix = 0;
float delaySound = 0;
int spatialBlend = 0;
float spread = 0;
float pitch = 1.0f;
int numArguments = command.arguments.Count;
for (int i = 0; i < numArguments; i++)
{
string key = command.arguments[i].name;
switch (key)
{
case RockMLArgName:
soundName = command.arguments[i].value;
break;
case RockMLArgFile:
file = command.arguments[i].value;
if (!file.Contains(":/") && !file.Contains(":\\"))
{
file = RMLComms.CurrentScenarioPathWithFilePrefix + file.Replace('/', '\\');
}
break;
case RockMLArgPosX:
float.TryParse(command.arguments[i].value, out posX);
break;
case RockMLArgPosY:
float.TryParse(command.arguments[i].value, out posY);
break;
case RockMLArgPosZ:
float.TryParse(command.arguments[i].value, out posZ);
break;
case RockMLArgMinDistance:
float.TryParse(command.arguments[i].value, out minDistance);
break;
case RockMLArgMaxDistance:
float.TryParse(command.arguments[i].value, out maxDistance);
break;
case RockMLArgVolume:
float.TryParse(command.arguments[i].value, out volume);
break;
case RockMLArgDopplerLevel:
float.TryParse(command.arguments[i].value, out dopplerLevel);
break;
case RockMLArgLoopCount:
int.TryParse(command.arguments[i].value, out loopCount);
break;
case RockMLArgDistortionLvl:
float.TryParse(command.arguments[i].value, out distortionLvl);
break;
case RockMLArglowPassCutoff:
float.TryParse(command.arguments[i].value, out lowPassCutoff);
break;
case RockMLArglowPassRes:
float.TryParse(command.arguments[i].value, out lowPassRes);
break;
case RockMLArgHighPassCutoff:
float.TryParse(command.arguments[i].value, out highPassCutoff);
break;
case RockMLArgHighPassRes:
float.TryParse(command.arguments[i].value, out highPassRes);
break;
case RockMLArgHighPassFilterOn:
int.TryParse(command.arguments[i].value, out hpf);
break;
case RockMLArgLowPassFilterOn:
int.TryParse(command.arguments[i].value, out lpf);
break;
case RockMLArgReverbFilterOn:
int.TryParse(command.arguments[i].value, out reverb);
break;
case RockMLArgReverbType:
int.TryParse(command.arguments[i].value, out reverbType);
break;
case RockMLArgRadioEffect:
int.TryParse(command.arguments[i].value, out radio);
break;
case RockMLArgUseAudioMixer:
int.TryParse(command.arguments[i].value, out mixer);
break;
case RockMLArgLog:
int.TryParse(command.arguments[i].value, out rollOffLog);
break;
case RockMLArgAmbienceMixer:
int.TryParse(command.arguments[i].value, out ambienceMix);
break;
case RockMLArgDelaySound:
float.TryParse(command.arguments[i].value, out delaySound);
break;
case RockMLArgSpatialBlend:
int.TryParse(command.arguments[i].value, out spatialBlend);
break;
case RockMLArgSpread:
float.TryParse(command.arguments[i].value, out spread);
break;
case RockMLArgPitch:
float.TryParse(command.arguments[i].value, out pitch);
break;
default:
break;
}
}
if (file != null)
{
Vector3 pos
= new Vector3
(posX, posY, posZ
); if (file != null)
{
SpawnedSoundObject spawnedSoundOb = Instance.DoPlaySoundFromFile(file,
pos,
minDistance,
maxDistance,
volume,
dopplerLevel,
loopCount,
distortionLvl,
lowPassCutoff,
lowPassRes,
highPassCutoff,
highPassRes,
lpf,
hpf,
reverb,
reverbType,
radio,
mixer,
rollOffLog,
ambienceMix,
delaySound,
spatialBlend,
spread,
pitch);
if (TNManager.players.Count > 0) Instance.tnObject.Send(RFC_PlaySoundFromFile,
TNet.Target.Others,
file,
pos,
minDistance,
maxDistance,
volume,
dopplerLevel,
loopCount,
distortionLvl,
lowPassCutoff,
lowPassRes,
highPassCutoff,
highPassRes,
lpf,
hpf,
reverb,
reverbType,
radio,
mixer,
rollOffLog,
ambienceMix,
delaySound,
spatialBlend,
spread,
pitch);
if (soundName != null)
{
SoundObjects[soundName] = spawnedSoundOb;
}
}
}
else
{
Debug.LogWarning("No file given in PlaySound command");
}
}
[TNet.RFC(RFC_PlaySoundFromFile)]
public void PlaySoundFromFile(
string soundFile,
Vector3 pos,
float minDistance,
float maxDistance,
float volume,
float dopplerLevel,
int loopCount,
float distortionLvl,
float lowPassCutoff,
float lowPassRes,
float highPassCutoff,
float highPassRes,
int lpf,
int hpf,
int reverb,
int reverbType,
int radio,
int mixer,
int rollOffLog,
int ambienceMix,
float delaySound,
int spatialBlend,
float spread,
float pitch)
{
DoPlaySoundFromFile(
soundFile,
pos,
minDistance,
maxDistance,
volume,
dopplerLevel,
loopCount,
distortionLvl,
lowPassCutoff,
lowPassRes,
highPassCutoff,
highPassRes,
lpf,
hpf,
reverb,
reverbType,
radio,
mixer,
rollOffLog,
ambienceMix,
delaySound,
spatialBlend,
spread,
pitch);
}
public SpawnedSoundObject DoPlaySoundFromFile(
string soundFile,
Vector3 pos,
float minDistance,
float maxDistance,
float volume,
float dopplerLevel,
int loopCount,
float distortionLvl,
float lowPassCutoff,
float lowPassRes,
float highPassCutoff,
float highPassRes,
int lpf,
int hpf,
int reverb,
int reverbType,
int radio,
int mixer,
int rollOffLog,
int ambienceMix,
float delaySound,
int spatialBlend,
float spread,
float pitch)
{
GameObject soundOb
= new GameObject
(); SpawnedSoundObject spawnedSoundObject = soundOb.AddComponent<SpawnedSoundObject>();
spawnedSoundObject.transform.position = pos;
spawnedSoundObject.volume = volume;
spawnedSoundObject.minDistance = minDistance;
spawnedSoundObject.maxDistance = maxDistance;
spawnedSoundObject.dopplerLevel = dopplerLevel;
spawnedSoundObject.loopCount = loopCount;
spawnedSoundObject.distortionLevel = distortionLvl;
spawnedSoundObject.lowPassCutoff = lowPassCutoff;
spawnedSoundObject.lowPassRes = lowPassRes;
spawnedSoundObject.highPassCutoff = highPassCutoff;
spawnedSoundObject.highPassRes = highPassRes;
spawnedSoundObject.HighPassFilterOff = hpf;
spawnedSoundObject.LowPassFilterOff = lpf;
spawnedSoundObject.ReverbOff = reverb;
spawnedSoundObject.ReverbPreset = reverbType;
spawnedSoundObject.RadioMixer = radio;
spawnedSoundObject.CleanMixer = mixer;
spawnedSoundObject.RolloffMode = rollOffLog;
spawnedSoundObject.AmbienceMixer = ambienceMix;
spawnedSoundObject.DelaySound = delaySound;
spawnedSoundObject.SpatialBlend = spatialBlend;
spawnedSoundObject.spread = spread;
spawnedSoundObject.pitch = pitch;
spawnedSoundObject.LoadFromFile(soundFile);
return spawnedSoundObject;
}
}