Represents a sound source.
Beispiel #1
0
 internal SoundHandleEx(double volume, double pitch, Sounds.SoundSource source)
 {
     base.MyVolume = volume;
     base.MyPitch  = pitch;
     base.MyValid  = true;
     this.Source   = source;
 }
Beispiel #2
0
 /// <summary>Called by the controls loop to stop playback of this horn</summary>
 internal void Stop()
 {
     //Reset loop control variable
     LoopStarted = false;
     if (!StartEndSounds & !Loop)
     {
         //Don't stop horns which are play-once single part sounds
         return;
     }
     if (!StartEndSounds & Loop)
     {
         //This sound is a toggle music horn sound
         return;
     }
     if (Sounds.IsPlaying(Source))
     {
         //Stop the loop sound playing
         Sounds.StopSound(Source);
     }
     if (StartEndSounds && !Sounds.IsPlaying(Source) && EndSound != null)
     {
         //If our end sound is defined and in use, play once
         Source = Sounds.PlaySound(EndSound, 1.0, 1.0, SoundPosition,
                                   TrainManager.PlayerTrain,
                                   TrainManager.PlayerTrain.DriverCar, false);
     }
 }
Beispiel #3
0
 internal Horn(Sounds.SoundBuffer startSound, Sounds.SoundBuffer loopSound, Sounds.SoundBuffer endSound, bool loop)
 {
     this.Source         = null;
     this.StartSound     = startSound;
     this.LoopSound      = loopSound;
     this.EndSound       = endSound;
     this.Loop           = loop;
     this.StartEndSounds = false;
     this.LoopStarted    = false;
     SoundPosition       = new Vector3();
 }
Beispiel #4
0
 /// <summary>Attempts to load a sound file into a car-sound</summary>
 /// <param name="FileName">The sound to load</param>
 /// <param name="Position">The position that the sound is emitted from within the car</param>
 /// <param name="Radius">The sound radius</param>
 /// <returns>The new car sound, or an empty car sound if load fails</returns>
 internal CarSound(string FileName, Vector3 Position, double Radius)
 {
     this          = TrainManager.CarSound.Empty;
     this.Position = Position;
     this.Source   = null;
     if (FileName != null)
     {
         if (System.IO.File.Exists(FileName))
         {
             this.Buffer = Sounds.RegisterBuffer(FileName, Radius);
         }
     }
 }
Beispiel #5
0
 internal SoundHandleEx PlaySound(int index, double volume, double pitch, bool looped)
 {
     if (index < 0 || index >= this.Train.Cars[this.Train.DriverCar].Sounds.Plugin.Length || this.Train.Cars[this.Train.DriverCar].Sounds.Plugin[index].Buffer == null)
     {
         return(null);
     }
     Sounds.SoundBuffer buffer         = this.Train.Cars[this.Train.DriverCar].Sounds.Plugin[index].Buffer;
     OpenBveApi.Math.Vector3D position = this.Train.Cars[this.Train.DriverCar].Sounds.Plugin[index].Position;
     Sounds.SoundSource source         = Sounds.PlaySound(buffer, pitch, volume, position, this.Train, this.Train.DriverCar, looped);
     if (this.SoundHandlesCount == this.SoundHandles.Length)
     {
         Array.Resize <SoundHandleEx>(ref this.SoundHandles, this.SoundHandles.Length << 1);
     }
     this.SoundHandles[this.SoundHandlesCount] = new SoundHandleEx(volume, pitch, source);
     this.SoundHandlesCount++;
     return(this.SoundHandles[this.SoundHandlesCount - 1]);
 }
Beispiel #6
0
 private CarSound(Sounds.SoundBuffer buffer, Sounds.SoundSource source, Vector3 position)
 {
     this.Buffer = buffer;
     this.Source = source;
     this.Position = position;
 }
            internal override void Update(double TimeElapsed, bool ForceUpdate)
            {
                const double extraRadius = 10.0;
                double       z           = Object.TranslateZFunction == null ? 0.0 : Object.TranslateZFunction.LastResult;
                double       pa          = TrackPosition + z - Radius - extraRadius;
                double       pb          = TrackPosition + z + Radius + extraRadius;
                double       ta          = World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z - World.BackgroundImageDistance - World.ExtraViewingDistance;
                double       tb          = World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z + World.BackgroundImageDistance + World.ExtraViewingDistance;
                bool         visible     = pb >= ta & pa <= tb;

                if (visible | ForceUpdate)
                {
                    if (Object.SecondsSinceLastUpdate >= Object.RefreshRate | ForceUpdate)
                    {
                        double timeDelta = Object.SecondsSinceLastUpdate + TimeElapsed;
                        Object.SecondsSinceLastUpdate = 0.0;
                        TrainManager.Train train         = null;
                        double             trainDistance = double.MaxValue;
                        for (int j = 0; j < TrainManager.Trains.Length; j++)
                        {
                            if (TrainManager.Trains[j].State == TrainManager.TrainState.Available)
                            {
                                double distance;
                                if (TrainManager.Trains[j].Cars[0].FrontAxle.Follower.TrackPosition < TrackPosition)
                                {
                                    distance = TrackPosition - TrainManager.Trains[j].Cars[0].FrontAxle.Follower.TrackPosition;
                                }
                                else if (TrainManager.Trains[j].Cars[TrainManager.Trains[j].Cars.Length - 1].RearAxle.Follower.TrackPosition > TrackPosition)
                                {
                                    distance = TrainManager.Trains[j].Cars[TrainManager.Trains[j].Cars.Length - 1].RearAxle.Follower.TrackPosition - TrackPosition;
                                }
                                else
                                {
                                    distance = 0;
                                }
                                if (distance < trainDistance)
                                {
                                    train         = TrainManager.Trains[j];
                                    trainDistance = distance;
                                }
                            }
                        }
                        Object.Update(false, train, train == null ? 0 : train.DriverCar, SectionIndex, TrackPosition, Position, Direction, Up, Side, false, true, true, timeDelta, true);
                        if (this.Object.CurrentState != this.lastState && Loading.SimulationSetup)
                        {
                            if (this.SingleBuffer && this.Buffers[0] != null)
                            {
                                switch (this.Object.CurrentState)
                                {
                                case -1:
                                    if (this.PlayOnHide)
                                    {
                                        this.Source = Sounds.PlaySound(this.Buffers[0], this.currentPitch, this.currentVolume, this.Position, false);
                                    }
                                    break;

                                case 0:
                                    if (this.PlayOnShow || this.lastState != -1)
                                    {
                                        this.Source = Sounds.PlaySound(this.Buffers[0], this.currentPitch, this.currentVolume, this.Position, false);
                                    }
                                    break;

                                default:
                                    this.Source = Sounds.PlaySound(this.Buffers[0], this.currentPitch, this.currentVolume, this.Position, false);
                                    break;
                                }
                            }
                            else
                            {
                                int bufferIndex = this.Object.CurrentState + 1;
                                if (bufferIndex < this.Buffers.Length && this.Buffers[bufferIndex] != null)
                                {
                                    switch (bufferIndex)
                                    {
                                    case 0:
                                        if (this.PlayOnHide)
                                        {
                                            this.Source = Sounds.PlaySound(this.Buffers[bufferIndex], this.currentPitch, this.currentVolume, this.Position, false);
                                        }
                                        break;

                                    case 1:
                                        if (this.PlayOnShow || this.lastState != -1)
                                        {
                                            this.Source = Sounds.PlaySound(this.Buffers[bufferIndex], this.currentPitch, this.currentVolume, this.Position, false);
                                        }
                                        break;

                                    default:
                                        this.Source = Sounds.PlaySound(this.Buffers[bufferIndex], this.currentPitch, this.currentVolume, this.Position, false);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Object.SecondsSinceLastUpdate += TimeElapsed;
                    }
                    if (!Visible)
                    {
                        Renderer.ShowObject(Object.ObjectIndex, ObjectType.Dynamic);
                        Visible = true;
                    }
                }
                else
                {
                    Object.SecondsSinceLastUpdate += TimeElapsed;
                    if (Visible)
                    {
                        Renderer.HideObject(Object.ObjectIndex);
                        Visible = false;
                    }
                }
                this.lastState = this.Object.CurrentState;
            }
Beispiel #8
0
 private CarSound(Sounds.SoundBuffer buffer, Sounds.SoundSource source, Vector3 position)
 {
     this.Buffer   = buffer;
     this.Source   = source;
     this.Position = position;
 }
Beispiel #9
0
            internal override void Update(double TimeElapsed, bool ForceUpdate)
            {
                const double extraRadius = 10.0;
                const double Radius      = 25.0;

                double pa      = currentTrackPosition + Radius - extraRadius;
                double pb      = currentTrackPosition + Radius + extraRadius;
                double ta      = World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z - World.BackgroundImageDistance - World.ExtraViewingDistance;
                double tb      = World.CameraTrackFollower.TrackPosition + World.CameraCurrentAlignment.Position.Z + World.BackgroundImageDistance + World.ExtraViewingDistance;
                bool   visible = pb >= ta & pa <= tb;

                if (visible | ForceUpdate)
                {
                    if (Game.MinimalisticSimulation || TimeElapsed > 0.05)
                    {
                        return;
                    }
                    TrainManager.Train train         = null;
                    double             trainDistance = double.MaxValue;
                    for (int j = 0; j < TrainManager.Trains.Length; j++)
                    {
                        if (TrainManager.Trains[j].State == TrainState.Available)
                        {
                            double distance;
                            if (TrainManager.Trains[j].Cars[0].FrontAxle.Follower.TrackPosition < this.Follower.TrackPosition)
                            {
                                distance = this.Follower.TrackPosition - TrainManager.Trains[j].Cars[0].FrontAxle.Follower.TrackPosition;
                            }
                            else if (TrainManager.Trains[j].Cars[TrainManager.Trains[j].Cars.Length - 1].RearAxle.Follower.TrackPosition > this.Follower.TrackPosition)
                            {
                                distance = TrainManager.Trains[j].Cars[TrainManager.Trains[j].Cars.Length - 1].RearAxle.Follower.TrackPosition - this.Follower.TrackPosition;
                            }
                            else
                            {
                                distance = 0;
                            }
                            if (distance < trainDistance)
                            {
                                train         = TrainManager.Trains[j];
                                trainDistance = distance;
                            }
                        }
                    }
                    if (this.TrackFollowerFunction != null)
                    {
                        double delta = this.TrackFollowerFunction.Perform(train, train == null ? 0 : train.DriverCar, this.Position, this.Follower.TrackPosition, 0, false, TimeElapsed, 0);
                        this.Follower.Update(this.currentTrackPosition + delta, true, true);
                        this.Follower.UpdateWorldCoordinates(false);
                    }
                    if (this.VolumeFunction != null)
                    {
                        this.currentVolume = this.VolumeFunction.Perform(train, train == null ? 0 : train.DriverCar, this.Position, this.Follower.TrackPosition, 0, false, TimeElapsed, 0);
                    }
                    if (this.PitchFunction != null)
                    {
                        this.currentPitch = this.PitchFunction.Perform(train, train == null ? 0 : train.DriverCar, this.Position, this.Follower.TrackPosition, 0, false, TimeElapsed, 0);
                    }
                    if (this.Source != null)
                    {
                        this.Source.Pitch  = this.currentPitch;
                        this.Source.Volume = this.currentVolume;
                    }
                    //Buffer should never be null, but check it anyways
                    if (!Sounds.IsPlaying(Source) && this.Buffer != null)
                    {
                        Source = Sounds.PlaySound(Buffer, 1.0, 1.0, Follower.WorldPosition + Position, this, true);
                    }
                }
                else
                {
                    if (Sounds.IsPlaying(Source))
                    {
                        Sounds.StopSound(Source);
                    }
                }
            }
Beispiel #10
0
            /// <summary>Called by the controls loop to start playback of this horn</summary>
            internal void Play()
            {
                if (StartEndSounds == true)
                {
                    //New style three-part sounds
                    if (LoopStarted == false)
                    {
                        if (!Sounds.IsPlaying(Source))
                        {
                            if (StartSound != null)
                            {
                                //The start sound is not currently playing, so start it
                                Source = Sounds.PlaySound(StartSound, 1.0, 1.0, SoundPosition,
                                                          TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, false);

                                //Set the loop control variable to started
                                LoopStarted = true;
                            }
                            else
                            {
                                Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition,
                                                          TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, true);
                            }
                        }
                    }
                    else
                    {
                        if (!Sounds.IsPlaying(Source))
                        {
                            //Start our loop sound playing if the start sound is finished
                            Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition,
                                                      TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, true);
                        }
                    }
                }
                else
                {
                    //Original single part sounds
                    if (LoopSound != null)
                    {
                        //Loop is ONLY true if this is a Music Horn
                        if (Loop)
                        {
                            if (!Sounds.IsPlaying(Source) && !LoopStarted)
                            {
                                //On the first keydown event, start the sound source playing and trigger the loop control variable
                                Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition,
                                                          TrainManager.PlayerTrain, TrainManager.PlayerTrain.DriverCar, true);
                                LoopStarted = true;
                            }
                            else
                            {
                                if (!LoopStarted)
                                {
                                    //Our loop control variable is reset by the keyup event so this code will only trigger on the
                                    //second keydown meaning our horn toggles
                                    Sounds.StopSound(Source);
                                    LoopStarted = true;
                                }
                            }
                        }
                        else
                        {
                            if (!LoopStarted)
                            {
                                Source = Sounds.PlaySound(LoopSound, 1.0, 1.0, SoundPosition, TrainManager.PlayerTrain,
                                                          TrainManager.PlayerTrain.DriverCar, false);
                            }
                            LoopStarted = true;
                        }
                    }
                }
            }
Beispiel #11
0
			internal SoundHandleEx(double volume, double pitch, Sounds.SoundSource source) {
				base.MyVolume = volume;
				base.MyPitch = pitch;
				base.MyValid = true;
				this.Source = source;
			}