Author Topic: Sound question  (Read 13221 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));
  60.     }
  61.  
  62.     private IEnumerator LoadSoundFromFileRoutine(string file)
  63.     {
  64.         WWW fileLoad = new WWW(file);
  65.         yield return fileLoad;
  66.         if (!string.IsNullOrEmpty(fileLoad.error))
  67.         {
  68.             Debug.LogError("Failed to load Sound file " + file);
  69.             yield break;
  70.         }
  71.  
  72.         Debug.Log("Done Sound LoadFromFile " + file);
  73.  
  74.         audioClip = fileLoad.GetAudioClip(true);
  75.  
  76.         StartCoroutine(PlayClip());
  77.     }
  78.  
  79.     private IEnumerator PlayClip()
  80.     {
  81.         if (audioClip)
  82.         {
  83.  
  84.             //Here we get a refrence to our audio mixer
  85.             AudioMixer mixer = Resources.Load("UndergroundMixer") as AudioMixer;
  86.             string _outPutMixer = "UndergroundMixer";          
  87.             //Get our audio source
  88.             audioSource = gameObject.AddComponent<AudioSource>();
  89.             //We plugg out audio source into the Mixer Group
  90.             audioSource.outputAudioMixerGroup = mixer.FindMatchingGroups(_outPutMixer)[0];
  91.  
  92.             //Get a refrence to our audio mixer
  93.             AudioMixer mixer = Resources.Load("MainGen4Mixer") as AudioMixer;
  94.             string _outPutMixer = "MainGen4Group";
  95.             //Refrence to the radio mixer
  96.             AudioMixer radioMixer = Resources.Load("RadioMixer") as AudioMixer;
  97.             string _outPutRadioMixer = "RadioMixer";
  98.             //Refrence to the ambience mixer for ambience sounds
  99.             AudioMixer ambienceMixer = Resources.Load("AmbienceMixer") as AudioMixer;
  100.             string _outPutAmbienceMixer = "AmbienceMixer";
  101.  
  102.            
  103.             //we set our audiomixer update mode to normal
  104.             //mixer.updateMode = AudioMixerUpdateMode.Normal;
  105.                        
  106.  
  107.            
  108.             audioSource = gameObject.AddComponent<AudioSource>();          
  109.  
  110.  
  111.             audioSource = gameObject.AddComponent<AudioSource>();
  112.             //Plugg audio source into the Mixer Group
  113.             if (CleanMixer == 1)
  114.             {
  115.                 audioSource.outputAudioMixerGroup = mixer.FindMatchingGroups(_outPutMixer)[0];
  116.             }
  117.  
  118.             if (RadioMixer == 1)
  119.             {
  120.                 audioSource.outputAudioMixerGroup = radioMixer.FindMatchingGroups(_outPutRadioMixer)[0];
  121.             }
  122.  
  123.             if (AmbienceMixer == 1)
  124.             {
  125.                 Debug.Log("Ambience mixer is on");
  126.                 audioSource.outputAudioMixerGroup = ambienceMixer.FindMatchingGroups(_outPutAmbienceMixer)[0];
  127.             }
  128.  
  129.  
  130.             //Set our audiomixer update mode to normal
  131.             //mixer.updateMode = AudioMixerUpdateMode.Normal;
  132.  
  133.  
  134.             audioSource.volume = volume;
  135.             audioSource.rolloffMode = AudioRolloffMode.Linear;
  136.             audioSource.minDistance = minDistance;
  137.             audioSource.maxDistance = maxDistance;
  138.             audioSource.clip = audioClip;
  139.             audioSource.spatialBlend = 1.0f;            
  140.             audioSource.dopplerLevel = dopplerLevel;
  141.     audioSource.PlayDelayed(DelaySound);
  142.             audioSource.spread = spread;
  143.             audioSource.pitch = pitch;
  144.            
  145.  
  146.             if (RolloffMode == 1)
  147.             {
  148.                 audioSource.rolloffMode = AudioRolloffMode.Logarithmic;
  149.                
  150.             }
  151.  
  152.             if(SpatialBlend == 1)
  153.             {
  154.                 audioSource.spatialBlend = 0.0f;
  155.             }
  156.  
  157.            
  158.             //Distortion
  159.  
  160.             AudioDistortionFilter distortion = gameObject.AddComponent(typeof(AudioDistortionFilter)) as AudioDistortionFilter;
  161.             distortion.distortionLevel = distortionLevel;
  162.             //Low Pass Filter and High Pass Filter
  163.             AudioLowPassFilter lowPassFilter = gameObject.AddComponent(typeof(AudioLowPassFilter)) as AudioLowPassFilter;
  164.             AudioHighPassFilter highPassFilter = gameObject.AddComponent(typeof(AudioHighPassFilter)) as AudioHighPassFilter;
  165.             if (LowPassFilterOff == 0)
  166.             {
  167.                 Debug.Log("LowPassFilter is Off");
  168.                 lowPassFilter.enabled = false;                
  169.             }
  170.             else if (LowPassFilterOff == 1)
  171.             {
  172.                 Debug.Log("LowPassFilter is On");
  173.                 lowPassFilter.enabled = true;
  174.                 lowPassFilter.cutoffFrequency = lowPassCutoff;
  175.                 lowPassFilter.lowpassResonanceQ = lowPassRes;
  176.             }
  177.             if (HighPassFilterOff == 0)
  178.             {
  179.                 Debug.Log("HighPassFilter is Off");
  180.                 highPassFilter.enabled = false;                
  181.             }
  182.             else if (HighPassFilterOff == 1)
  183.             {
  184.                 Debug.Log("HighPassFilter is On");
  185.                 highPassFilter.enabled = true;
  186.                 highPassFilter.cutoffFrequency = highPassCutoff;
  187.                 highPassFilter.highpassResonanceQ = highPassRes;
  188.             }
  189.             //Reverb Filter
  190.             AudioReverbFilter reverFilter = gameObject.AddComponent(typeof(AudioReverbFilter)) as AudioReverbFilter;
  191.             if (ReverbOff == 0)
  192.             {
  193.                 Debug.Log("Reverb Filter is Off");
  194.                 reverFilter.enabled = false;
  195.             }
  196.             else if (ReverbOff == 1)
  197.             {
  198.                 Debug.Log("Reverb Filter is On");
  199.                 reverFilter.enabled = true;
  200.             }
  201.             //Reverb Presets
  202.             if (ReverbPreset == 1)
  203.             {
  204.                 reverFilter.reverbPreset = AudioReverbPreset.Cave;
  205.             }
  206.             if (ReverbPreset == 2)
  207.             {
  208.                 reverFilter.reverbPreset = AudioReverbPreset.Hangar;
  209.             }
  210.             if (ReverbPreset == 3)
  211.             {
  212.                 reverFilter.reverbPreset = AudioReverbPreset.Mountains;
  213.             }
  214.             if (ReverbPreset == 4)
  215.             {
  216.                 reverFilter.reverbPreset = AudioReverbPreset.Stoneroom;
  217.             }
  218.             if (ReverbPreset == 5)
  219.             {
  220.                 reverFilter.reverbPreset = AudioReverbPreset.Quarry;
  221.             }
  222.             if (ReverbPreset == 6)
  223.             {
  224.                 reverFilter.reverbPreset = AudioReverbPreset.Room;
  225.             }
  226.             if (ReverbPreset == 7)
  227.             {
  228.                 reverFilter.reverbPreset = AudioReverbPreset.StoneCorridor;
  229.             }
  230.             if (ReverbPreset == 8)
  231.             {
  232.                 reverFilter.reverbPreset = AudioReverbPreset.Generic;
  233.             }
  234.             //Audio MixerSnap Shots
  235.            // if (Radio == 1)
  236.             //{
  237.             //    AudioMixerSnapshot RadioSnapShot = mixer.FindSnapshot("Radio");
  238.             //    RadioSnapShot.TransitionTo(.01f);
  239.             //    Debug.Log("Radio Sound On");
  240.            // }
  241.  
  242.             if (loopCount < 0)
  243.             {
  244.                 audioSource.loop = true;
  245.                 audioSource.Play();
  246.                 yield break;
  247.             }
  248.             else if (loopCount == 0)
  249.             {
  250.                 audioSource.PlayOneShot(audioClip);
  251.  
  252.                 yield return null;
  253.                 while (audioSource.isPlaying) yield return null;
  254.             }
  255.             else
  256.             {
  257.                 audioSource.loop = true;
  258.                 audioSource.Play();
  259.  
  260.                 yield return new WaitForSeconds((audioClip.length * 0.5f) + (audioClip.length * (loopCount - 1)));
  261.                 audioSource.loop = false;
  262.                 while (audioSource.isPlaying) yield return null;
  263.             }
  264.  
  265.             Destroy(gameObject);
  266.         }
  267.     }
  268. }

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Sound question
« Reply #3 on: October 27, 2016, 11:02:32 PM »
Only briefly skimmed the code you posted.

Typically you don't network sound, you network events that would cause sound to play. For example, instead of networking both a bullet and the bullet sound effect, you would just network the "player X fired this weapon". Other players then receive this message, instantiate the bullet on their client, and play the sound effect.

Defsine79

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Sound question
« Reply #4 on: October 28, 2016, 05:17:22 PM »
Thanks for your reply, I understand what your try to say so how would you go about doing that and surely there must be a way to do it some how.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sound question
« Reply #5 on: October 30, 2016, 01:03:44 PM »
Yeah as cmifwdll mentioned, sound is something that should be local to your client, played as a result of some action happening. It's the actions you should sync with other players, not sounds. For example, if you want to play a jump sound when the player jumps, all you need to sync is the jump action -- telling other clients "jump now", and other clients will perform the actual jump and play the sound as a result.

Also note that I don't support TNet 2 anymore. Just TNet 3.

Defsine79

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Sound question
« Reply #6 on: November 03, 2016, 07:18:17 PM »
Thanks for the reply Aren,  is there a way you could stream the audio to the clients from the host ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sound question
« Reply #7 on: November 05, 2016, 09:33:55 AM »
Audio is just bytes of data. Send it via an RFC like any other data.

Defsine79

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Sound question
« Reply #8 on: November 06, 2016, 06:14:27 AM »
Awesome! is there a tutorial for this ?

phoenix

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 3
  • Posts: 49
    • View Profile
Re: Sound question
« Reply #9 on: November 06, 2016, 04:55:07 PM »
I have the daikon forge package working with TNET2 but it is not working well enough for production. Also the asset is abandoned.
This open source code on github is very similar

https://github.com/DwayneBull/UnityVOIP

Defsine79

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Sound question
« Reply #10 on: November 07, 2016, 12:46:12 AM »
Thanks Phoenix, ill check that out. 
Im trying to use the same example as the RFC colored cubes to send my audioclip to everyone in the room and everyone that joins 
TNObject tno = GetComponent<TNObject>();
tno.Send("PlayClip", Target.AllSaved, audioClip);

Though I still cant here any audio on my client.
Heres my code :
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Audio;
  4. using TNet;
  5. [RequireComponent(typeof(TNObject))]
  6. public class SpawnedSoundObject : TNBehaviour
  7.  
  8. {
  9.     private AudioClip audioClip = null;
  10.     private AudioSource audioSource = null;
  11.                                                             //Audio Filters  
  12.     public float distortionLevel = 0.0f;                    //Distortion
  13.     public float lowPassCutoff = 0.0f;                      //Low Pass Filter
  14.     public float lowPassRes = 0.0f;                         //Low Pass Resonance                                                          
  15.     public float highPassCutoff = 0.0f;                     //High Pass Cuttoff                  
  16.     public float highPassRes = 0.0f;                        //High Pass Res
  17.     public int HighPassFilterOff;                           //HighPassFilter On/Off
  18.     public int LowPassFilterOff;                            //LowPassFilter On/Off
  19.     public int ReverbOff = 0;                               //Reverb On/Off
  20.     public int ReverbPreset = 0;                            //Reverb Preset
  21.     public int SpatialBlend = 0;
  22.     public float spread = 0.0f;
  23.     public float pitch = 1.0f;
  24.  
  25.     public float volume = 1.0f;
  26.     public float minDistance = 0.5f;
  27.     public float maxDistance = 80.0f;
  28.     public float dopplerLevel = 0.1f;
  29.     public int loopCount = 0;
  30.     public int RolloffMode = 0;
  31.     public float DelaySound = 0.0f;
  32.  
  33.  
  34.     //AudioMixer
  35.     public int CleanMixer = 0;    
  36.     public int RadioMixer = 0;
  37.     public int AmbienceMixer = 0;
  38.  
  39.     //AudioMixer SnapShots
  40.     //public int Radio = 0;
  41.     //public int Natural = 0;
  42.  
  43.    
  44.  
  45.     private void OnDestroy()
  46.     {
  47.         if (audioClip != null)
  48.         {
  49.             //Set the mixer to be with no effects
  50.             //AudioMixer mixer = Resources.Load("MainGen4Mixer") as AudioMixer;
  51.             //AudioMixerSnapshot DrySnapShot = mixer.FindSnapshot("Dry");
  52.             //DrySnapShot.TransitionTo(0.01f);
  53.             DestroyImmediate(audioClip);
  54.             audioClip = null;
  55.         }
  56.         else
  57.         {
  58.            
  59.         }
  60.     }
  61.    
  62.     public void LoadFromFile(string file)
  63.     {
  64.         Debug.Log("SpawnedSoundObject.LoadFromFile " + file);
  65.         StartCoroutine(LoadSoundFromFileRoutine(file));
  66.        
  67.     }
  68.  
  69.     private IEnumerator LoadSoundFromFileRoutine(string file)
  70.     {
  71.         WWW fileLoad = new WWW(file);
  72.         yield return fileLoad;
  73.         if (!string.IsNullOrEmpty(fileLoad.error))
  74.         {
  75.             Debug.LogError("Failed to load Sound file " + file);
  76.             yield break;
  77.         }
  78.  
  79.         Debug.Log("Done Sound LoadFromFile " + file);
  80.  
  81.         audioClip = fileLoad.GetAudioClip(true);
  82.  
  83.         StartCoroutine(PlayClip());
  84.        
  85.  
  86.     }
  87.  
  88.     void Awake()
  89.     {
  90.         TNObject tno = GetComponent<TNObject>();
  91.         tno.Send("PlayClip", Target.AllSaved, audioClip);
  92.     }
  93.    
  94.     [RFC]
  95.     private IEnumerator PlayClip()
  96.     {
  97.         if (audioClip)
  98.         {
  99.  
  100.  
  101.  
  102.            
  103.  
  104.             //Get a refrence to our audio mixer
  105.             AudioMixer mixer = Resources.Load("MainGen4Mixer") as AudioMixer;
  106.             string _outPutMixer = "MainGen4Group";
  107.             //Refrence to the radio mixer
  108.             AudioMixer radioMixer = Resources.Load("RadioMixer") as AudioMixer;
  109.             string _outPutRadioMixer = "RadioMixer";
  110.             //Refrence to the ambience mixer for ambience sounds
  111.             AudioMixer ambienceMixer = Resources.Load("AmbienceMixer") as AudioMixer;
  112.             string _outPutAmbienceMixer = "AmbienceMixer";
  113.  
  114.  
  115.             audioSource = gameObject.AddComponent<AudioSource>();
  116.             //Plugg audio source into the Mixer Group
  117.             if (CleanMixer == 1)
  118.             {
  119.                 audioSource.outputAudioMixerGroup = mixer.FindMatchingGroups(_outPutMixer)[0];
  120.             }
  121.  
  122.             if (RadioMixer == 1)
  123.             {
  124.                 audioSource.outputAudioMixerGroup = radioMixer.FindMatchingGroups(_outPutRadioMixer)[0];
  125.             }
  126.  
  127.             if (AmbienceMixer == 1)
  128.             {
  129.                 Debug.Log("Ambience mixer is on");
  130.                 audioSource.outputAudioMixerGroup = ambienceMixer.FindMatchingGroups(_outPutAmbienceMixer)[0];
  131.             }
  132.  
  133.  
  134.             //Set our audiomixer update mode to normal
  135.             //mixer.updateMode = AudioMixerUpdateMode.Normal;
  136.  
  137.             audioSource.volume = volume;
  138.             audioSource.rolloffMode = AudioRolloffMode.Linear;
  139.             audioSource.minDistance = minDistance;
  140.             audioSource.maxDistance = maxDistance;
  141.             audioSource.clip = audioClip;
  142.             audioSource.spatialBlend = 1.0f;            
  143.             audioSource.dopplerLevel = dopplerLevel;
  144.             audioSource.PlayDelayed(DelaySound);
  145.             audioSource.spread = spread;
  146.             audioSource.pitch = pitch;
  147.            
  148.  
  149.             if (RolloffMode == 1)
  150.             {
  151.                 audioSource.rolloffMode = AudioRolloffMode.Logarithmic;
  152.                
  153.             }
  154.  
  155.             if(SpatialBlend == 1)
  156.             {
  157.                 audioSource.spatialBlend = 0.0f;
  158.             }
  159.  
  160.            
  161.             //Distortion
  162.             AudioDistortionFilter distortion = gameObject.AddComponent(typeof(AudioDistortionFilter)) as AudioDistortionFilter;
  163.             distortion.distortionLevel = distortionLevel;
  164.             //Low Pass Filter and High Pass Filter
  165.             AudioLowPassFilter lowPassFilter = gameObject.AddComponent(typeof(AudioLowPassFilter)) as AudioLowPassFilter;
  166.             AudioHighPassFilter highPassFilter = gameObject.AddComponent(typeof(AudioHighPassFilter)) as AudioHighPassFilter;
  167.             if (LowPassFilterOff == 0)
  168.             {
  169.                 Debug.Log("LowPassFilter is Off");
  170.                 lowPassFilter.enabled = false;                
  171.             }
  172.             else if (LowPassFilterOff == 1)
  173.             {
  174.                 Debug.Log("LowPassFilter is On");
  175.                 lowPassFilter.enabled = true;
  176.                 lowPassFilter.cutoffFrequency = lowPassCutoff;
  177.                 lowPassFilter.lowpassResonanceQ = lowPassRes;
  178.             }
  179.             if (HighPassFilterOff == 0)
  180.             {
  181.                 Debug.Log("HighPassFilter is Off");
  182.                 highPassFilter.enabled = false;                
  183.             }
  184.             else if (HighPassFilterOff == 1)
  185.             {
  186.                 Debug.Log("HighPassFilter is On");
  187.                 highPassFilter.enabled = true;
  188.                 highPassFilter.cutoffFrequency = highPassCutoff;
  189.                 highPassFilter.highpassResonanceQ = highPassRes;
  190.             }
  191.             //Reverb Filter
  192.             AudioReverbFilter reverFilter = gameObject.AddComponent(typeof(AudioReverbFilter)) as AudioReverbFilter;
  193.             if (ReverbOff == 0)
  194.             {
  195.                 Debug.Log("Reverb Filter is Off");
  196.                 reverFilter.enabled = false;
  197.             }
  198.             else if (ReverbOff == 1)
  199.             {
  200.                 Debug.Log("Reverb Filter is On");
  201.                 reverFilter.enabled = true;
  202.             }
  203.             //Reverb Presets
  204.             if (ReverbPreset == 1)
  205.             {
  206.                 reverFilter.reverbPreset = AudioReverbPreset.Cave;
  207.             }
  208.             if (ReverbPreset == 2)
  209.             {
  210.                 reverFilter.reverbPreset = AudioReverbPreset.Hangar;
  211.             }
  212.             if (ReverbPreset == 3)
  213.             {
  214.                 reverFilter.reverbPreset = AudioReverbPreset.Mountains;
  215.             }
  216.             if (ReverbPreset == 4)
  217.             {
  218.                 reverFilter.reverbPreset = AudioReverbPreset.Stoneroom;
  219.             }
  220.             if (ReverbPreset == 5)
  221.             {
  222.                 reverFilter.reverbPreset = AudioReverbPreset.Quarry;
  223.             }
  224.             if (ReverbPreset == 6)
  225.             {
  226.                 reverFilter.reverbPreset = AudioReverbPreset.Room;
  227.             }
  228.             if (ReverbPreset == 7)
  229.             {
  230.                 reverFilter.reverbPreset = AudioReverbPreset.StoneCorridor;
  231.             }
  232.             if (ReverbPreset == 8)
  233.             {
  234.                 reverFilter.reverbPreset = AudioReverbPreset.Generic;
  235.             }
  236.  
  237.  
  238.  
  239.             //Audio MixerSnap Shots
  240.             // if (Radio == 1)
  241.             //{
  242.             //    AudioMixerSnapshot RadioSnapShot = mixer.FindSnapshot("Radio");
  243.             //    RadioSnapShot.TransitionTo(.01f);
  244.             //    Debug.Log("Radio Sound On");
  245.             // }
  246.  
  247.             if (loopCount < 0)
  248.             {
  249.                 audioSource.loop = true;
  250.                 audioSource.Play();
  251.                 yield break;
  252.             }
  253.             else if (loopCount == 0)
  254.             {
  255.                 audioSource.PlayOneShot(audioClip);
  256.  
  257.                 yield return null;
  258.                 while (audioSource.isPlaying) yield return null;
  259.             }
  260.             else
  261.             {
  262.                 audioSource.loop = true;
  263.                 audioSource.Play();
  264.  
  265.                 yield return new WaitForSeconds((audioClip.length * 0.5f) + (audioClip.length * (loopCount - 1)));
  266.                 audioSource.loop = false;
  267.                 while (audioSource.isPlaying) yield return null;
  268.             }
  269.  
  270.             Destroy(gameObject);
  271.         }
  272.     }
  273. }
   

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Sound question
« Reply #11 on: November 07, 2016, 03:17:54 AM »
Awake() is called first. At this time your audioclip is null. Load the audioclip before calling tno.Send(...).

That said though, this is not how you should be doing VoIP... you can find .NET wrappers for opus and speex, you should use those. It's complicated, so you should research how they work first. Seeing as how you seem pretty new to both programming and networking, there is a pre-made Unity solution for this as well - developed by github user fholm - called VoiceChat. This asset will take care of recording, encoding, and decoding with many easy to configure options. It doesn't support TNet, but it's coded in a way that you can easily add support for any networking solution.
You can find it here: https://github.com/fholm/unityassets/tree/master/VoiceChat
Again, this was made by fholm. You can see the other contributors on that github page as well.

Defsine79

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Sound question
« Reply #12 on: November 07, 2016, 04:01:45 AM »
Ok thanks, Ill check it out.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sound question
« Reply #13 on: November 09, 2016, 12:47:15 PM »
Awake() is where you cache local references and variables. You should never try to send anything in Awake() -- at the time the Awake() is called, the object has no owner as TNet haven't had a chance to set anything yet. Awake() is basically a constructor, called immediately.

Defsine79

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Sound question
« Reply #14 on: November 14, 2016, 07:25:17 AM »
OK so I guess once audioclip is loaded the best place would be in
 
private IEnumerator LoadSoundFromFileRoutine(string file)
    {
        WWW fileLoad = new WWW(file);
        yield return fileLoad;
        if (!string.IsNullOrEmpty(fileLoad.error))
        {
            Debug.LogError("Failed to load Sound file " + file);
            yield break;
        }
 
        Debug.Log("Done Sound LoadFromFile " + file);
 
        audioClip = fileLoad.GetAudioClip(true);
 
        StartCoroutine(PlayClip());
        tno.Send("PlayClip", Target.AllSaved, audioClip);


       
 
    }