Beispiel #1
0
        /// <summary>
        /// Tries to parse the SoundID from a string.
        /// </summary>
        /// <param name="parser">The Parser to use.</param>
        /// <param name="value">The string to parse.</param>
        /// <param name="outValue">If this method returns true, contains the parsed SoundID.</param>
        /// <returns>True if the parsing was successfully; otherwise false.</returns>
        public static bool TryParse(this Parser parser, string value, out SoundID outValue)
        {
            ushort tmp;
            var    ret = parser.TryParse(value, out tmp);

            outValue = new SoundID(tmp);
            return(ret);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the <see cref="SoundBuffer"/> for the given <see cref="SoundID"/>.
        /// </summary>
        /// <param name="id">The <see cref="SoundID"/> for the <see cref="SoundBuffer"/> to get.</param>
        /// <returns>The <see cref="SoundBuffer"/> for the <paramref name="id"/>, or null if the <see cref="SoundBuffer"/>
        /// failed to load or the <paramref name="id"/> is invalid.</returns>
        protected SoundBuffer GetSoundBuffer(SoundID id)
        {
            var i = (int)id;

            if (i < 0 || i >= _soundBuffers.Length)
            {
                return(null);
            }

            return(_soundBuffers[i]);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the <see cref="ISoundInfo"/> for a sound.
        /// </summary>
        /// <param name="id">The id of the <see cref="ISoundInfo"/> to get.</param>
        /// <returns>The <see cref="ISoundInfo"/> for the given <paramref name="id"/>, or null if the value
        /// was invalid.</returns>
        public ISoundInfo GetSoundInfo(SoundID id)
        {
            var i = (int)id;

            if (i < 0 || i >= _infos.Length)
            {
                return(null);
            }

            return(_infos[i]);
        }
Beispiel #4
0
        /// <summary>
        /// Stops all instances of a sound with the given <see cref="SoundID"/>.
        /// </summary>
        /// <param name="id">The ID of the sounds to stop.</param>
        public void Stop(SoundID id)
        {
            for (var i = 0; i < _soundInstances.Count; i++)
            {
                if (_soundInstances[i].SoundInfo.ID != id)
                {
                    continue;
                }

                _soundInstances.RemoveAt(i);
                i--;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Plays a sound by the given <see cref="SoundID"/>.
        /// </summary>
        /// <param name="id">The ID of the sound to play.</param>
        /// <param name="source">The <see cref="IAudioEmitter"/> that the sound is coming from.</param>
        /// <returns>
        /// True if the sound played successfully; otherwise false.
        /// </returns>
        public bool Play(SoundID id, IAudioEmitter source)
        {
            // Create the sound instance
            ISoundInfo info;
            var        snd = InternalCreateSound(id, true, out info);

            if (snd == null)
            {
                return(false);
            }

            // Set the sound up and start playing it
            SoundInstance si;

            try
            {
                snd.Position = new Vector3(source.Position, 0f);
                si           = new SoundInstance(info, snd, source);
                snd.Play();
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to play sound `{0}`: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, info, ex);
                }

                try
                {
                    snd.Dispose();
                }
                catch (Exception ex2)
                {
                    const string errmsg2 = "Failed to dispose sound that failed to play `{0}`: {1}";
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat(errmsg2, info, ex2);
                    }
                }

                return(false);
            }

            // Add to the list of active sounds
            _soundInstances.Add(si);

            return(true);
        }
Beispiel #6
0
        public SoundManager(IContentManager contentManager)
        {
            // Load the values from file
            var values = AudioManager.LoadValues(_fileName, _rootNodeName);

            // Create the _infos and _soundBuffers arrays large enough to hold all values
            var max = values.Max(x => x.Value);

            _infos        = new ISoundInfo[max + 1];
            _soundBuffers = new SoundBuffer[_infos.Length];

            // Populate both collections
            foreach (var value in values)
            {
                var id        = new SoundID(value.Value);
                var soundInfo = new SoundInfo(value.Key, id);

                // Ensure no duplicates
                if (_infos[(int)id] != null)
                {
                    throw new DuplicateKeyException(string.Format("Two or more SoundInfos found with the ID `{0}`!", soundInfo.ID));
                }

                if (_infosByName.ContainsKey(soundInfo.Name))
                {
                    throw new DuplicateKeyException(string.Format("Two or more SoundInfos found with the name `{0}`!",
                                                                  soundInfo.Name));
                }

                // Add
                _infosByName.Add(soundInfo.Name, soundInfo);
                _infos[(int)soundInfo.ID]        = soundInfo;
                _soundBuffers[(int)soundInfo.ID] = contentManager.LoadSoundBuffer(
                    ContentPaths.SoundsFolder + "/" + soundInfo.Name, ContentLevel.GameScreen);
            }
        }
Beispiel #7
0
 /// <summary>
 /// Attempts to create a <see cref="Sound"/> instance.
 /// </summary>
 /// <param name="id">The <see cref="SoundID"/>.</param>
 /// <param name="spatialized">True if this is for a spatialized sound; false for a static sound.</param>
 /// <param name="info">When this method returns a non-null object, contains the <see cref="ISoundInfo"/> for the
 /// <paramref name="id"/>.</param>
 /// <returns>
 /// The <see cref="Sound"/> instance, or null if it failed to be created.
 /// </returns>
 Sound InternalCreateSound(SoundID id, bool spatialized, out ISoundInfo info)
 {
     // Get the sound info
     info = GetSoundInfo(id);
     return(InternalCreateSound(info, spatialized));
 }
Beispiel #8
0
 /// <summary>
 /// Writes a SoundID to a IValueWriter.
 /// </summary>
 /// <param name="valueWriter">IValueWriter to write to.</param>
 /// <param name="name">Unique name of the SoundID that will be used to distinguish it
 /// from other values when reading.</param>
 /// <param name="value">SoundID to write.</param>
 public static void Write(this IValueWriter valueWriter, string name, SoundID value)
 {
     value.Write(valueWriter, name);
 }
Beispiel #9
0
 /// <summary>
 /// Writes a SoundID to a BitStream.
 /// </summary>
 /// <param name="bitStream">BitStream to write to.</param>
 /// <param name="value">SoundID to write.</param>
 public static void Write(this BitStream bitStream, SoundID value)
 {
     value.Write(bitStream);
 }
Beispiel #10
0
 /// <summary>
 /// Reads the SoundID from an IValueReader.
 /// </summary>
 /// <param name="valueReader">IValueReader to read the SoundID from.</param>
 /// <param name="name">The unique name of the value to read.</param>
 /// <returns>The SoundID read from the IValueReader.</returns>
 public static SoundID ReadSoundID(this IValueReader valueReader, string name)
 {
     return(SoundID.Read(valueReader, name));
 }
Beispiel #11
0
 /// <summary>
 /// Reads the SoundID from a BitStream.
 /// </summary>
 /// <param name="bitStream">BitStream to read the SoundID from.</param>
 /// <returns>The SoundID read from the BitStream.</returns>
 public static SoundID ReadSoundID(this BitStream bitStream)
 {
     return(SoundID.Read(bitStream));
 }
Beispiel #12
0
 /// <summary>
 /// Reads the SoundID from an <see cref="IDataRecord"/>.
 /// </summary>
 /// <param name="r"><see cref="IDataRecord"/> to read the SoundID from.</param>
 /// <param name="name">The name of the field to read the value from.</param>
 /// <returns>The SoundID read from the <see cref="IDataRecord"/>.</returns>
 public static SoundID GetSoundID(this IDataRecord r, string name)
 {
     return(SoundID.Read(r, name));
 }
Beispiel #13
0
 /// <summary>
 /// Reads the SoundID from an <see cref="IDataRecord"/>.
 /// </summary>
 /// <param name="r"><see cref="IDataRecord"/> to read the SoundID from.</param>
 /// <param name="i">The field index to read.</param>
 /// <returns>The SoundID read from the <see cref="IDataRecord"/>.</returns>
 public static SoundID GetSoundID(this IDataRecord r, int i)
 {
     return(SoundID.Read(r, i));
 }
Beispiel #14
0
        /// <summary>
        /// Tries to get the value in the <paramref name="dict"/> entry at the given <paramref name="key"/> as type SoundID.
        /// </summary>
        /// <typeparam name="T">The key Type.</typeparam>
        /// <param name="dict">The IDictionary.</param>
        /// <param name="key">The key for the value to get.</param>
        /// <param name="defaultValue">The value to use if the value at the <paramref name="key"/> could not be parsed.</param>
        /// <returns>The value at the given <paramref name="key"/> parsed as an int, or the
        /// <paramref name="defaultValue"/> if the <paramref name="key"/> did not exist in the <paramref name="dict"/>
        /// or the value at the given <paramref name="key"/> could not be parsed.</returns>
        public static SoundID AsSoundID <T>(this IDictionary <T, string> dict, T key, SoundID defaultValue)
        {
            string value;

            if (!dict.TryGetValue(key, out value))
            {
                return(defaultValue);
            }

            SoundID parsed;

            if (!Parser.Invariant.TryParse(value, out parsed))
            {
                return(defaultValue);
            }

            return(parsed);
        }
Beispiel #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SoundInfo"/> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="id">The id.</param>
 public SoundInfo(string name, SoundID id)
 {
     _name = name;
     _id   = id;
 }