Exemplo n.º 1
0
        public static SoundEffectInstance PlaySound(string name, float volume = 1.0f, float pitch = 0.0f)
        {
            if (!HasAudioDevice)
            {
                return(null);
            }
            // TODO: Remove this block once the SoundManager is initialized in a better location.
            if (Content == null)
            {
                return(null);
            }

            SoundEffect effect = null;

            if (!EffectLibrary.ContainsKey(name))
            {
                effect = Content.Load <SoundEffect>(AssetManager.ResolveContentPath(name));
                EffectLibrary[name] = effect;
            }
            else
            {
                effect = EffectLibrary[name];
            }
            SFXMixer.Levels     levels   = Mixer.GetOrCreateLevels(name);
            SoundEffectInstance instance = effect.CreateInstance();

            instance.Volume = GameSettings.Default.MasterVolume * GameSettings.Default.SoundEffectVolume * volume * levels.Volume;
            instance.Pitch  = pitch;
            instance.Play();
            instance.Volume = GameSettings.Default.MasterVolume * GameSettings.Default.SoundEffectVolume * volume * levels.Volume;
            instance.Pitch  = pitch;

            ActiveSounds2D.Add(instance);
            return(instance);
        }
Exemplo n.º 2
0
        public static Sound3D PlaySound(string name, Vector3 location, bool randomPitch, float volume = 1.0f, float pitch = 0.0f)
        {
            if (!HasAudioDevice)
            {
                return(null);
            }
            if (Content == null)
            {
                return(null);
            }
            SoundEffect effect = null;

            if (!EffectLibrary.ContainsKey(name))
            {
                effect = Content.Load <SoundEffect>(name);
                EffectLibrary[name] = effect;
            }
            else
            {
                effect = EffectLibrary[name];
            }



            if (!SoundCounts.ContainsKey(name))
            {
                SoundCounts[name] = 0;
            }

            if (SoundCounts[name] < MaxSounds)
            {
                SoundCounts[name]++;

                Sound3D sound = new Sound3D
                {
                    Position       = location,
                    EffectInstance = effect.CreateInstance(),
                    HasStarted     = false,
                    Name           = name
                };
                SFXMixer.Levels levels = Mixer.GetOrCreateLevels(name);
                sound.EffectInstance.IsLooped = false;
                sound.VolumeMultiplier        = volume * levels.Volume;


                if (randomPitch)
                {
                    sound.EffectInstance.Pitch = MathFunctions.Clamp((float)(MathFunctions.Random.NextDouble() * 1.0f - 0.5f) * levels.RandomPitch + pitch, -1.0f, 1.0f);
                }
                ActiveSounds.Add(sound);

                return(sound);
            }


            return(null);
        }
Exemplo n.º 3
0
        public override void Construct()
        {
            var bottomBar = AddChild(new Widget()
            {
                AutoLayout  = AutoLayout.DockBottom,
                MinimumSize = new Point(Rect.Width - 128, 24)
            })
            ;

            bottomBar.AddChild(new Button()
            {
                Text        = "Back",
                OnClick     = (sender, args) => this.Close(),
                AutoLayout  = AutoLayout.DockLeft,
                MinimumSize = new Point(64, 24)
            });


            if (SoundManager.Mixer == null)
            {
                Text = "Error. Failed to load audio mixer :(";
                return;
            }

            bottomBar.AddChild(new Button()
            {
                Text    = "Save",
                OnClick = (sender, args) => {
                    try
                    {
                        FileUtils.SaveBasicJson(SoundManager.Mixer, AssetManager.ResolveContentPath(ContentPaths.mixer));
                    }
                    catch (Exception exception)
                    {
                        Console.Out.WriteLine(exception.ToString());
                        Root.ShowModalPopup(new Popup()
                        {
                            Text = "Failed to save audio mixer. This is a debug tool. Are you a dev?",
                        });
                    }
                },
                AutoLayout  = AutoLayout.DockRight,
                MinimumSize = new Point(64, 24)
            });


            var listView = AddChild(new WidgetListView()
            {
                AutoLayout  = AutoLayout.DockBottom,
                MinimumSize = new Point(Rect.Width - 128, 512),
                ItemHeight  = 24
            });

            foreach (var level in SoundManager.Mixer.Gains)
            {
                var row = listView.AddChild(new Widget()
                {
                    AutoLayout  = AutoLayout.DockTop,
                    MinimumSize = new Point(512, 24)
                });

                row.AddChild(new Widget()
                {
                    Text                = level.Key,
                    TextColor           = Color.Black.ToVector4(),
                    AutoLayout          = AutoLayout.DockLeft,
                    TextVerticalAlign   = VerticalAlign.Center,
                    TextHorizontalAlign = HorizontalAlign.Right,
                    MinimumSize         = new Point(256, 24)
                });

                KeyValuePair <string, SFXMixer.Levels> level1 = level;
                row.AddChild(new HorizontalFloatSlider()
                {
                    ScrollArea      = 1.0f,
                    ScrollPosition  = level.Value.Volume,
                    MinimumSize     = new Point(256, 24),
                    AutoLayout      = AutoLayout.DockLeft,
                    OnSliderChanged = (sender) =>
                    {
                        SFXMixer.Levels levels = SoundManager.Mixer.GetOrCreateLevels(level1.Key);
                        SoundManager.Mixer.SetLevels(level1.Key,
                                                     new SFXMixer.Levels()
                        {
                            RandomPitch = level1.Value.RandomPitch,
                            Volume      = (sender as HorizontalFloatSlider).ScrollPosition
                        });
                        if (MathFunctions.RandEvent(0.15f))
                        {
                            SoundManager.PlaySound(level1.Key);
                        }
                    }
                });
            }
            Layout();

            base.Construct();
        }