Author Topic: Sound question  (Read 16580 times)

Defsine79

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Sound question
« on: October 27, 2016, 03:27:42 AM »
Hi this may sound like a bit of a nube question, but when Im playing as the host, I can here the audio I have put in, but when playing as a client there is no sound, can anyone help, Im using Tnet 2.

Defsine

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Sound question
« Reply #1 on: October 27, 2016, 05:06:01 PM »
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class SoundManager : MonoBehaviour
  5. {
  6.     private const byte RFC_PlaySoundFromFile = 218;
  7.  
  8.     private const string RockMLCommandTypePlaySound = "PlaySound";
  9.     private const string RockMLArgName = "Name";
  10.     private const string RockMLArgFile = "File";
  11.     private const string RockMLArgPosX = "PosX";
  12.     private const string RockMLArgPosY = "PosY";
  13.     private const string RockMLArgPosZ = "PosZ";
  14.     private const string RockMLArgMinDistance = "MinDistance";
  15.     private const string RockMLArgMaxDistance = "MaxDistance";
  16.     private const string RockMLArgVolume = "Volume";
  17.     private const string RockMLArgDopplerLevel = "DopplerLevel";
  18.     private const string RockMLArgLoopCount = "LoopCount";
  19.     private const string RockMLArgDistortionLvl = "DistortionLvl";
  20.     private const string RockMLArglowPassCutoff = "LowPassCutoff";
  21.     private const string RockMLArglowPassRes = "LowPassRes";
  22.     private const string RockMLArgHighPassCutoff = "HighPassCutoff";
  23.     private const string RockMLArgHighPassRes = "HighPassRes";
  24.     private const string RockMLArgHighPassFilterOn = "HPF";
  25.     private const string RockMLArgLowPassFilterOn = "LPF";
  26.     private const string RockMLArgReverbFilterOn = "Reverb";
  27.     private const string RockMLArgReverbType = "ReverbType";
  28.     private const string RockMLArgLog = "LogRollOff";
  29.     private const string RockMLArgDelaySound = "DelaySound";
  30.     private const string RockMLArgSpatialBlend = "2DSound";
  31.     private const string RockMLArgSpread = "Spread";
  32.     private const string RockMLArgPitch = "Pitch";
  33.  
  34.  
  35.     //AudioMixer
  36.     private const string RockMLArgUseAudioMixer = "CleanMixer";
  37.     private const string RockMLArgRadioEffect = "RadioMixer";
  38.     private const string RockMLArgAmbienceMixer = "AmbienceMixer";
  39.  
  40.  
  41.  
  42.     public static bool VoiceCommsActive = true;
  43.     public static bool VoiceCommsCanSend = true;
  44.     public static bool VoiceCommsCanReceive = true;
  45.     public static bool VoiceReliableSend = false;
  46.     public static int VoiceSendChunkSize = 640;
  47.     public static float VoiceSendVolume = 1.0f;
  48.     public static float VoiceReceiveVolume = 1.0f;
  49.  
  50.     public static Dictionary<string, SpawnedSoundObject> SoundObjects = new Dictionary<string, SpawnedSoundObject>(4);
  51.  
  52.     public static SoundManager Instance = null;
  53.  
  54.     private TNObject tnObject = null;
  55.  
  56.     private void Awake()
  57.     {
  58.         Instance = this;
  59.         tnObject = GetComponent<TNObject>();
  60.  
  61.         if (SimManager.IsHost)
  62.         {
  63.             if (RMLComms.Instance == null) RMLComms.Initialise();
  64.             RMLComms.Instance.RegisterHandler(RockMLCommandTypePlaySound, PlaySoundCommand);
  65.         }
  66.  
  67.         DebugManager.TimerStart();
  68.         VoiceCommsHandler(null, null, null);
  69.         Config.Instance.RegisterHandler(Config.ALLOWVOICECOMMS, VoiceCommsHandler);
  70.         Config.Instance.RegisterHandler(Config.VOICESENDVOLUME, VoiceCommsHandler);
  71.         Config.Instance.RegisterHandler(Config.VOICERECEIVEVOLUME, VoiceCommsHandler);
  72.         Config.Instance.RegisterHandler(Config.VOICERELIABLESEND, VoiceCommsHandler);
  73.         Config.Instance.RegisterHandler(Config.VOICESENDCHUNKSIZE, VoiceCommsHandler);
  74.         DebugManager.TimerEnd("Startup Timer: HUDManager Init: ");
  75.     }
  76.  
  77.     private void VoiceCommsHandler(string key, string value, string oldValue)
  78.     {
  79.         Config.Instance.GetValueBool(Config.ALLOWVOICECOMMS, ref VoiceCommsActive);
  80.         Config.Instance.GetValueFloat(Config.VOICESENDVOLUME, ref VoiceSendVolume);
  81.         Config.Instance.GetValueFloat(Config.VOICERECEIVEVOLUME, ref VoiceReceiveVolume);
  82.         Config.Instance.GetValueBool(Config.VOICERELIABLESEND, ref VoiceReliableSend);
  83.         Config.Instance.GetValueInt(Config.VOICESENDCHUNKSIZE, ref VoiceSendChunkSize);
  84.     }
  85.  
  86.     public static void PlaySoundCommand(RockML.Command command)
  87.     {
  88.         string soundName = null;
  89.         string file = null;
  90.         float posX = 0.0f;
  91.         float posY = 0.0f;
  92.         float posZ = 0.0f;
  93.         float minDistance = 0.5f;
  94.         float maxDistance = 80.0f;
  95.         float volume = 1.0f;
  96.         float dopplerLevel = 0.1f;
  97.         int loopCount = 0;
  98.         float distortionLvl = 0.0f;                         //Distortion
  99.         float lowPassCutoff = 0.0f;                         //Low Pass Filter
  100.         float lowPassRes = 0.0f;                            //Low Pass Resonance                                                          //High Pass Filter
  101.         float highPassCutoff = 0.0f;                        //High Pass Cuttoff                  
  102.         float highPassRes = 0.0f;                           //High Pass Res
  103.         int hpf = 0;                                        //High Pass Filter On/Off
  104.         int lpf = 0;                                        //Low Pass Filter On/Off
  105.         int reverb = 0;                                     //Turn Reverb On/Off
  106.         int reverbType = 0;
  107.         int radio = 0;
  108.         int mixer = 0;
  109.         int rollOffLog = 0;
  110.         int ambienceMix = 0;
  111.         float delaySound = 0;
  112.         int spatialBlend = 0;
  113.         float spread = 0;
  114.         float pitch = 1.0f;
  115.  
  116.  
  117.  
  118.     int numArguments = command.arguments.Count;
  119.         for (int i = 0; i < numArguments; i++)
  120.         {
  121.             string key = command.arguments[i].name;
  122.             switch (key)
  123.             {
  124.                 case RockMLArgName:
  125.                     soundName = command.arguments[i].value;
  126.                     break;
  127.  
  128.                 case RockMLArgFile:
  129.                     file = command.arguments[i].value;
  130.                     if (!file.Contains(":/") && !file.Contains(":\\"))
  131.                     {
  132.                         file = RMLComms.CurrentScenarioPathWithFilePrefix + file.Replace('/', '\\');
  133.                     }
  134.                     break;
  135.  
  136.                 case RockMLArgPosX:
  137.                     float.TryParse(command.arguments[i].value, out posX);
  138.                     break;
  139.  
  140.                 case RockMLArgPosY:
  141.                     float.TryParse(command.arguments[i].value, out posY);
  142.                     break;
  143.  
  144.                 case RockMLArgPosZ:
  145.                     float.TryParse(command.arguments[i].value, out posZ);
  146.                     break;
  147.  
  148.                 case RockMLArgMinDistance:
  149.                     float.TryParse(command.arguments[i].value, out minDistance);
  150.                     break;
  151.  
  152.                 case RockMLArgMaxDistance:
  153.                     float.TryParse(command.arguments[i].value, out maxDistance);
  154.                     break;
  155.  
  156.                 case RockMLArgVolume:
  157.                     float.TryParse(command.arguments[i].value, out volume);
  158.                     break;
  159.  
  160.                 case RockMLArgDopplerLevel:
  161.                     float.TryParse(command.arguments[i].value, out dopplerLevel);
  162.                     break;
  163.  
  164.                 case RockMLArgLoopCount:
  165.                     int.TryParse(command.arguments[i].value, out loopCount);
  166.                     break;
  167.  
  168.                 case RockMLArgDistortionLvl:  
  169.                     float.TryParse(command.arguments[i].value, out distortionLvl);
  170.                     break;
  171.  
  172.                 case RockMLArglowPassCutoff:  
  173.                     float.TryParse(command.arguments[i].value, out lowPassCutoff);
  174.                     break;
  175.  
  176.                 case RockMLArglowPassRes:
  177.                     float.TryParse(command.arguments[i].value, out lowPassRes);
  178.                     break;
  179.  
  180.                 case RockMLArgHighPassCutoff:
  181.                     float.TryParse(command.arguments[i].value, out highPassCutoff);
  182.                     break;
  183.  
  184.                 case RockMLArgHighPassRes:
  185.                     float.TryParse(command.arguments[i].value, out highPassRes);
  186.                     break;
  187.  
  188.                 case RockMLArgHighPassFilterOn:
  189.                     int.TryParse(command.arguments[i].value, out hpf);
  190.                     break;
  191.  
  192.                 case RockMLArgLowPassFilterOn:
  193.                     int.TryParse(command.arguments[i].value, out lpf);
  194.                     break;
  195.  
  196.                 case RockMLArgReverbFilterOn:
  197.                     int.TryParse(command.arguments[i].value, out reverb);
  198.                     break;
  199.  
  200.                 case RockMLArgReverbType:
  201.                     int.TryParse(command.arguments[i].value, out reverbType);
  202.                     break;
  203.  
  204.                 case RockMLArgRadioEffect:
  205.                     int.TryParse(command.arguments[i].value, out radio);
  206.                     break;
  207.  
  208.                 case RockMLArgUseAudioMixer:
  209.                     int.TryParse(command.arguments[i].value, out mixer);
  210.                     break;
  211.  
  212.                 case RockMLArgLog:
  213.                     int.TryParse(command.arguments[i].value, out rollOffLog);
  214.                     break;
  215.  
  216.                 case RockMLArgAmbienceMixer:
  217.                     int.TryParse(command.arguments[i].value, out ambienceMix);
  218.                     break;
  219.  
  220.                 case RockMLArgDelaySound:
  221.                     float.TryParse(command.arguments[i].value, out delaySound);
  222.                     break;
  223.  
  224.                 case RockMLArgSpatialBlend:
  225.                     int.TryParse(command.arguments[i].value, out spatialBlend);
  226.                     break;
  227.  
  228.                 case RockMLArgSpread:
  229.                     float.TryParse(command.arguments[i].value, out spread);
  230.                     break;
  231.  
  232.                 case RockMLArgPitch:
  233.                     float.TryParse(command.arguments[i].value, out pitch);
  234.                     break;
  235.  
  236.                 default:
  237.                     break;
  238.             }
  239.         }
  240.  
  241.         if (file != null)
  242.         {
  243.             Vector3 pos = new Vector3(posX, posY, posZ);
  244.             if (file != null)
  245.             {
  246.                 SpawnedSoundObject spawnedSoundOb = Instance.DoPlaySoundFromFile(file,
  247.                     pos,
  248.                     minDistance,
  249.                     maxDistance,
  250.                     volume,
  251.                     dopplerLevel,
  252.                     loopCount,
  253.                     distortionLvl,
  254.                     lowPassCutoff,
  255.                     lowPassRes,
  256.                     highPassCutoff,
  257.                     highPassRes,
  258.                     lpf,
  259.                     hpf,
  260.                     reverb,
  261.                     reverbType,
  262.                     radio,
  263.                     mixer,
  264.                     rollOffLog,
  265.                     ambienceMix,
  266.                     delaySound,
  267.                     spatialBlend,
  268.                     spread,
  269.                     pitch);
  270.  
  271.                 if (TNManager.players.Count > 0) Instance.tnObject.Send(RFC_PlaySoundFromFile,
  272.                     TNet.Target.Others,
  273.                     file,
  274.                     pos,
  275.                     minDistance,
  276.                     maxDistance,
  277.                     volume,
  278.                     dopplerLevel,
  279.                     loopCount,
  280.                     distortionLvl,
  281.                     lowPassCutoff,
  282.                     lowPassRes,
  283.                     highPassCutoff,
  284.                     highPassRes,
  285.                     lpf,
  286.                     hpf,
  287.                     reverb,
  288.                     reverbType,
  289.                     radio,
  290.                     mixer,
  291.                     rollOffLog,
  292.                     ambienceMix,
  293.                     delaySound,
  294.                     spatialBlend,
  295.                     spread,
  296.                     pitch);
  297.  
  298.                 if (soundName != null)
  299.                 {
  300.                     SoundObjects[soundName] = spawnedSoundOb;
  301.                 }
  302.             }
  303.         }
  304.         else
  305.         {
  306.             Debug.LogWarning("No file given in PlaySound command");
  307.         }
  308.     }
  309.  
  310.     [TNet.RFC(RFC_PlaySoundFromFile)]
  311.     public void PlaySoundFromFile(
  312.         string soundFile,
  313.         Vector3 pos,
  314.         float minDistance,
  315.         float maxDistance,
  316.         float volume,
  317.         float dopplerLevel,
  318.         int loopCount,
  319.         float distortionLvl,
  320.         float lowPassCutoff,
  321.         float lowPassRes,
  322.         float highPassCutoff,
  323.         float highPassRes,
  324.         int lpf,
  325.         int hpf,
  326.         int reverb,
  327.         int reverbType,
  328.         int radio,
  329.         int mixer,
  330.         int rollOffLog,
  331.         int ambienceMix,
  332.         float delaySound,
  333.         int spatialBlend,
  334.         float spread,
  335.         float pitch)
  336.     {
  337.         DoPlaySoundFromFile(
  338.             soundFile,
  339.             pos,
  340.             minDistance,
  341.             maxDistance,
  342.             volume,
  343.             dopplerLevel,
  344.             loopCount,
  345.             distortionLvl,
  346.             lowPassCutoff,
  347.             lowPassRes,
  348.             highPassCutoff,
  349.             highPassRes,
  350.             lpf,
  351.             hpf,
  352.             reverb,
  353.             reverbType,
  354.             radio,
  355.             mixer,
  356.             rollOffLog,
  357.             ambienceMix,
  358.             delaySound,
  359.             spatialBlend,
  360.             spread,
  361.             pitch);
  362.     }
  363.  
  364.     public SpawnedSoundObject DoPlaySoundFromFile(
  365.         string soundFile,
  366.         Vector3 pos,
  367.         float minDistance,
  368.         float maxDistance,
  369.         float volume,
  370.         float dopplerLevel,
  371.         int loopCount,
  372.         float distortionLvl,
  373.         float lowPassCutoff,
  374.         float lowPassRes,
  375.         float highPassCutoff,
  376.         float highPassRes,
  377.         int lpf,
  378.         int hpf,
  379.         int reverb,
  380.         int reverbType,
  381.         int radio,
  382.         int mixer,
  383.         int rollOffLog,
  384.         int ambienceMix,
  385.         float delaySound,
  386.         int spatialBlend,
  387.         float spread,
  388.         float pitch)
  389.     {
  390.         GameObject soundOb = new GameObject();
  391.         SpawnedSoundObject spawnedSoundObject = soundOb.AddComponent<SpawnedSoundObject>();
  392.         spawnedSoundObject.transform.position = pos;
  393.         spawnedSoundObject.volume = volume;
  394.         spawnedSoundObject.minDistance = minDistance;
  395.         spawnedSoundObject.maxDistance = maxDistance;
  396.         spawnedSoundObject.dopplerLevel = dopplerLevel;
  397.         spawnedSoundObject.loopCount = loopCount;
  398.         spawnedSoundObject.distortionLevel = distortionLvl;
  399.         spawnedSoundObject.lowPassCutoff = lowPassCutoff;
  400.         spawnedSoundObject.lowPassRes = lowPassRes;
  401.         spawnedSoundObject.highPassCutoff = highPassCutoff;
  402.         spawnedSoundObject.highPassRes = highPassRes;
  403.         spawnedSoundObject.HighPassFilterOff = hpf;
  404.         spawnedSoundObject.LowPassFilterOff = lpf;
  405.         spawnedSoundObject.ReverbOff = reverb;
  406.         spawnedSoundObject.ReverbPreset = reverbType;
  407.         spawnedSoundObject.RadioMixer = radio;
  408.         spawnedSoundObject.CleanMixer = mixer;
  409.         spawnedSoundObject.RolloffMode = rollOffLog;
  410.         spawnedSoundObject.AmbienceMixer = ambienceMix;
  411.         spawnedSoundObject.DelaySound = delaySound;
  412.         spawnedSoundObject.SpatialBlend = spatialBlend;
  413.         spawnedSoundObject.spread = spread;
  414.         spawnedSoundObject.pitch = pitch;
  415.         spawnedSoundObject.LoadFromFile(soundFile);
  416.  
  417.         return spawnedSoundObject;
  418.     }
  419. }
  420.  

Defsine

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: Sound question
« Reply #2 on: October 27, 2016, 05:09:19 PM »
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Audio;
  4.  
  5. public class SpawnedSoundObject : MonoBehaviour
  6. {
  7.     private AudioClip audioClip = null;
  8.     private AudioSource audioSource = null;
  9.                                                             //Audio Filters  
  10.     public float distortionLevel = 0.0f;                    //Distortion
  11.     public float lowPassCutoff = 0.0f;                      //Low Pass Filter
  12.     public float lowPassRes = 0.0f;                         //Low Pass Resonance                                                          
  13.     public float highPassCutoff = 0.0f;                     //High Pass Cuttoff                  
  14.     public float highPassRes = 0.0f;                        //High Pass Res
  15.     public int HighPassFilterOff;                           //HighPassFilter On/Off
  16.     public int LowPassFilterOff;                            //LowPassFilter On/Off
  17.     public int ReverbOff = 0;                               //Reverb On/Off
  18.     public int ReverbPreset = 0;                            //Reverb Preset
  19.     public int SpatialBlend = 0;
  20.     public float spread = 0.0f;
  21.     public float pitch = 1.0f;
  22.     public float volume = 1.0f;
  23.     public float minDistance = 0.5f;
  24.     public float maxDistance = 80.0f;
  25.     public float dopplerLevel = 0.1f;
  26.     public int   loopCount = 0;
  27.    
  28.     public int RolloffMode = 0;
  29.     public float DelaySound = 0.0f;
  30.  
  31.  
  32.     //AudioMixer
  33.     public int CleanMixer = 0;    
  34.     public int RadioMixer = 0;
  35.     public int AmbienceMixer = 0;
  36.  
  37.     //AudioMixer SnapShots
  38.     //public int Radio = 0;
  39.     //public int Natural = 0;
  40.  
  41.    
  42.  
  43.     private void OnDestroy()
  44.     {
  45.         if (audioClip != null)
  46.         {
  47.             //Set the mixer to be with no effects
  48.             //AudioMixer mixer = Resources.Load("MainGen4Mixer") as AudioMixer;
  49.             //AudioMixerSnapshot DrySnapShot = mixer.FindSnapshot("Dry");
  50.             //DrySnapShot.TransitionTo(0.01f);
  51.             DestroyImmediate(audioClip);
  52.             audioClip = null;
  53.         }
  54.     }
  55.    
  56.     public void LoadFromFile(string file)
  57.     {
  58.         Debug.Log("SpawnedSoundObject.LoadFromFile " + file);
  59.         StartCoroutine(LoadSoundFromFileRoutine(file));