Exemple #1
0
        // --- registering buffers ---

        /// <summary>Registers a sound buffer and returns a handle to the buffer.</summary>
        /// <param name="path">The path to the sound.</param>
        /// <param name="radius">The default effective radius.</param>
        /// <returns>The handle to the sound buffer.</returns>
        public SoundBuffer RegisterBuffer(string path, double radius)
        {
            if (!File.Exists(path))
            {
                return(null);
            }
            for (int i = 0; i < BufferCount; i++)
            {
                if (!(Buffers[i].Origin is PathOrigin))
                {
                    continue;
                }

                if (((PathOrigin)Buffers[i].Origin).Path == path)
                {
                    return(Buffers[i]);
                }
            }
            if (Buffers.Length == BufferCount)
            {
                Array.Resize(ref Buffers, Buffers.Length << 1);
            }
            Buffers[BufferCount] = new SoundBuffer(CurrentHost, path, radius);
            BufferCount++;
            return(Buffers[BufferCount - 1]);
        }
Exemple #2
0
 // --- constructors ---
 /// <summary>Creates a new sound source.</summary>
 /// <param name="buffer">The sound buffer.</param>
 /// <param name="radius">The effective sound radius.</param>
 /// <param name="pitch">The pitch change factor.</param>
 /// <param name="volume">The volume change factor.</param>
 /// <param name="position">The position. If a train and car are specified, the position is relative to the car, otherwise absolute.</param>
 /// <param name="parent">The parent object this sound source is attached to, or a null reference.</param>
 /// <param name="looped">Whether this sound source plays in a loop.</param>
 internal SoundSource(SoundBuffer buffer, double radius, double pitch, double volume, Vector3 position, object parent, bool looped)
 {
     Buffer           = buffer;
     Radius           = radius;
     Pitch            = pitch;
     Volume           = volume;
     Position         = position;
     Parent           = parent;
     Looped           = looped;
     State            = SoundSourceState.PlayPending;
     OpenAlSourceName = 0;
     //Set the sound type to undefined to use Michelle's original processing
     if (parent is AbstractCar)
     {
         Type = SoundType.TrainCar;
     }
     else if (parent is WorldObject)
     {
         Type = SoundType.AnimatedObject;
     }
     else
     {
         Type = SoundType.Undefined;
     }
 }
Exemple #3
0
        // --- loading buffers ---

        /// <summary>Loads the specified sound buffer.</summary>
        /// <param name="buffer">The sound buffer.</param>
        /// <returns>Whether loading the buffer was successful.</returns>
        public void LoadBuffer(SoundBuffer buffer)
        {
            if (buffer.Loaded)
            {
                return;
            }
            if (buffer.Ignore)
            {
                return;
            }
            Sound sound;

            if (buffer.Origin.GetSound(out sound))
            {
                if (sound.BitsPerSample == 8 | sound.BitsPerSample == 16)
                {
                    byte[] bytes = sound.GetMonoMix();
                    AL.GenBuffers(1, out buffer.OpenAlBufferName);
                    ALFormat format = sound.BitsPerSample == 8 ? ALFormat.Mono8 : ALFormat.Mono16;
                    AL.BufferData(buffer.OpenAlBufferName, format, bytes, bytes.Length, sound.SampleRate);
                    buffer.Duration = sound.Duration;
                    buffer.Loaded   = true;
                    return;
                }
            }
            buffer.Ignore = true;
        }
Exemple #4
0
        public CarSound(HostInterface currentHost, string trainFolder, string configurationFile, int currentLine, string soundFile, double radius, Vector3 position)
        {
            if (soundFile.Length == 0 || Path.ContainsInvalidChars(soundFile))
            {
                currentHost.AddMessage(MessageType.Error, false, "FileName contains illegal characters or is empty at line " + (currentLine + 1) + " in file " + configurationFile);
                return;
            }

            string absolutePathTosoundFile = Path.CombineFile(trainFolder, soundFile);

            if (!System.IO.File.Exists(absolutePathTosoundFile))
            {
                if (configurationFile != string.Empty)
                {
                    //Only add missing file message for BVE4 / XML sound configs, not default
                    currentHost.AddMessage(MessageType.Error, false, "The SoundFile " + soundFile + " was not found at line " + (currentLine + 1) + " in file " + configurationFile);
                }
                return;
            }
            SoundHandle handle;

            currentHost.RegisterSound(absolutePathTosoundFile, radius, out handle);
            Buffer        = handle as SoundBuffer;
            this.Position = position;
        }
Exemple #5
0
        public CarSound(HostInterface currentHost, string soundFile, double radius, Vector3 position)
        {
            SoundHandle handle;

            currentHost.RegisterSound(soundFile, radius, out handle);
            Buffer        = handle as SoundBuffer;
            this.Position = position;
        }
Exemple #6
0
 /// <summary>Plays a sound.</summary>
 /// <param name="buffer">The sound buffer.</param>
 /// <param name="pitch">The pitch change factor.</param>
 /// <param name="volume">The volume change factor.</param>
 /// <param name="position">The position. If a train car is specified, the position is relative to the car, otherwise absolute.</param>
 /// <param name="parent">The parent object the sound is attached to, or a null reference.</param>
 /// <param name="looped">Whether to play the sound in a loop.</param>
 /// <returns>The sound source.</returns>
 public SoundSource PlaySound(SoundBuffer buffer, double pitch, double volume, OpenBveApi.Math.Vector3 position, object parent, bool looped)
 {
     if (Sources.Length == SourceCount)
     {
         Array.Resize(ref Sources, Sources.Length << 1);
     }
     Sources[SourceCount] = new SoundSource(buffer, buffer.Radius, pitch, volume, position, parent, looped);
     SourceCount++;
     return(Sources[SourceCount - 1]);
 }
Exemple #7
0
        // --- unloading buffers ---

        /// <summary>Unloads the specified sound buffer.</summary>
        /// <param name="buffer"></param>
        protected void UnloadBuffer(SoundBuffer buffer)
        {
            if (buffer.Loaded)
            {
                AL.DeleteBuffers(1, ref buffer.OpenAlBufferName);
                buffer.OpenAlBufferName = 0;
                buffer.Loaded           = false;
                buffer.Ignore           = false;
            }
        }
Exemple #8
0
 /// <summary>Registers a sound buffer and returns a handle to the buffer.</summary>
 /// <param name="data">The raw sound data.</param>
 /// <param name="radius">The default effective radius.</param>
 /// <returns>The handle to the sound buffer.</returns>
 public SoundBuffer RegisterBuffer(Sound data, double radius)
 {
     if (Buffers.Length == BufferCount)
     {
         Array.Resize(ref Buffers, Buffers.Length << 1);
     }
     Buffers[BufferCount] = new SoundBuffer(data, radius);
     BufferCount++;
     return(Buffers[BufferCount - 1]);
 }
Exemple #9
0
 /// <summary>Creates a new sound source.</summary>
 /// <param name="buffer">The sound buffer.</param>
 /// <param name="radius">The effective sound radius.</param>
 /// <param name="pitch">The pitch change factor.</param>
 /// <param name="volume">The volume change factor.</param>
 /// <param name="position">The position. If a train and car are specified, the position is relative to the car, otherwise absolute.</param>
 /// <param name="train">The train this sound source is attached to, or a null reference.</param>
 /// <param name="type">The type of sound</param>
 /// <param name="looped">Whether this sound source plays in a loop.</param>
 internal SoundSource(SoundBuffer buffer, double radius, double pitch, double volume, Vector3 position, AbstractTrain train, SoundType type, bool looped)
 {
     Buffer           = buffer;
     Radius           = radius;
     Pitch            = pitch;
     Volume           = volume;
     Position         = position;
     Parent           = train;
     Looped           = looped;
     State            = SoundSourceState.PlayPending;
     OpenAlSourceName = 0;
     //Set sound type manually
     Type = type;
 }
Exemple #10
0
 /// <summary>Creates a clone of the specified sound buffer</summary>
 /// <param name="b">The buffer to clone</param>
 /// <returns>The new buffer</returns>
 internal SoundBuffer Clone(SoundBuffer b)
 {
     return(new SoundBuffer(b.Origin)
     {
         Radius = b.Radius,
         Loaded = false,
         OpenAlBufferName = 0,
         Duration = b.Duration,
         InternalVolumeFactor = b.InternalVolumeFactor,
         Ignore = false,
         PitchFunction = b.PitchFunction,
         VolumeFunction = b.VolumeFunction
     });
 }
Exemple #11
0
 /// <summary>Plays a sound.</summary>
 /// <param name="buffer">The sound buffer.</param>
 /// <param name="pitch">The pitch change factor.</param>
 /// <param name="volume">The volume change factor.</param>
 /// <param name="position">The position. If a train car is specified, the position is relative to the car, otherwise absolute.</param>
 /// <param name="looped">Whether to play the sound in a loop.</param>
 /// <returns>The sound source.</returns>
 public SoundSource PlaySound(SoundHandle buffer, double pitch, double volume, OpenBveApi.Math.Vector3 position, bool looped)
 {
     if (buffer is SoundBuffer)
     {
         SoundBuffer b = (SoundBuffer)buffer;
         if (Sources.Length == SourceCount)
         {
             Array.Resize(ref Sources, Sources.Length << 1);
         }
         Sources[SourceCount] = new SoundSource(b, b.Radius, pitch, volume, position, null, looped);
         SourceCount++;
         return(Sources[SourceCount - 1]);
     }
     throw new NotSupportedException();
 }
Exemple #12
0
 /// <summary>Creates a new empty car sound</summary>
 public CarSound()
 {
     this.Position = Vector3.Zero;
     this.Source   = null;
     this.Buffer   = null;
 }
Exemple #13
0
 /// <summary>Creates a new car sound</summary>
 /// <param name="buffer">The sound buffer</param>
 /// <param name="Position">The position that the sound is emitted from within the car</param>
 /// <returns>The new car sound</returns>
 public CarSound(SoundBuffer buffer, Vector3 Position)
 {
     this.Position = Position;
     this.Source   = null;
     this.Buffer   = buffer;
 }
Exemple #14
0
 /// <summary>Creates a new car sound</summary>
 /// <param name="handle">The API handle to the sound buffer</param>
 /// <param name="Position">The position that the sound is emitted from within the car</param>
 /// <returns>The new car sound</returns>
 public CarSound(SoundHandle handle, Vector3 Position)
 {
     this.Buffer   = handle as SoundBuffer;
     this.Position = Position;
     this.Source   = null;
 }
Exemple #15
0
 /// <summary>Gets the duration of the specified sound buffer in seconds.</summary>
 /// <param name="buffer">The sound buffer.</param>
 /// <returns>The duration of the sound buffer in seconds, or zero if the buffer could not be loaded.</returns>
 public double GetDuration(SoundBuffer buffer)
 {
     LoadBuffer(buffer);
     return(buffer.Duration);
 }
Exemple #16
0
        // --- loading buffers ---

        /// <summary>Loads the specified sound buffer.</summary>
        /// <param name="buffer">The sound buffer.</param>
        /// <returns>Whether loading the buffer was successful.</returns>
        public void LoadBuffer(SoundBuffer buffer)
        {
            buffer.Load();
        }